rt 4.0.6
[freeside.git] / rt / share / html / Admin / CustomFields / Modify.html
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 <& /Admin/Elements/Header, Title => $title &>
49 <& /Elements/Tabs &>
50 <& /Elements/ListActions, actions => \@results &>
51
52
53 <form method="post" action="Modify.html" name="ModifyCustomField" id="ModifyCustomField">
54 <input type="hidden" class="hidden" name="id" value="<% $id %>" />
55
56 <table>
57
58 <tr><td class="label"><&|/l&>Name</&></td>
59 <td><input name="Name" value="<% $CustomFieldObj->Name || $Name || '' %>" size="20" /></td></tr>
60
61 <tr><td class="label"><&|/l&>Description</&></td>
62 <td><input name="Description" value="<% $CustomFieldObj->Description || $Description || '' %>" size="80" /></td></tr>
63
64 <tr><td class="label"><&|/l&>Type</&></td>
65 <td><& /Admin/Elements/SelectCustomFieldType, 
66         Name => "TypeComposite", 
67         Default => $CustomFieldObj->TypeComposite, &>
68 </td></tr>
69
70 % if ( $CustomFieldObj->Id and $CustomFieldObj->HasRenderTypes ) {
71 <tr>
72   <td class="label"><&|/l&>Render Type</&></td>
73   <td>
74     <& /Admin/Elements/SelectCustomFieldRenderType,
75         Name            => "RenderType",
76         TypeComposite   => $CustomFieldObj->TypeComposite,
77         Default         => $CustomFieldObj->RenderType, 
78         BasedOn         => $CustomFieldObj->BasedOnObj->id, &>
79   </td>
80 </tr>
81 % }
82
83 % if ( $CustomFieldObj->Id and $CustomFieldObj->IsSelectionType and RT->Config->Get('CustomFieldValuesSources') and ( scalar(@{RT->Config->Get('CustomFieldValuesSources')}) > 0 ) ) {
84 <tr><td class="label"><&|/l&>Field values source:</&></td><td>
85 <& /Admin/Elements/EditCustomFieldValuesSource, CustomField => $CustomFieldObj &>
86 </td></tr>
87 % }
88
89 <tr><td class="label"><&|/l&>Applies to</&></td>
90 <td><& /Admin/Elements/SelectCustomFieldLookupType, 
91         Name => "LookupType", 
92         Default => $CustomFieldObj->LookupType || $LookupType, &>
93 </td></tr>
94
95 % if ( $CustomFieldObj->Id 
96 %     and $CustomFieldObj->LookupType =~ /RT::Transaction/ ) {
97 <tr><td class="label"><&|/l&>Display&nbsp;with</&></td>
98 <td><& /Admin/Elements/EditCustomFieldUILocation,
99         CustomField => $CustomFieldObj &>
100 </td></tr>
101 % }
102
103 <tr class="edit_validation"><td class="label"><&|/l&>Validation</&></td>
104 <td><& /Widgets/ComboBox,
105     Name    => 'Pattern',
106     Default => $CustomFieldObj->Pattern || $Pattern,
107     Size    => 20,
108     Values  => \@CFvalidations,
109 &></td></tr>
110
111 <tr><td class="label"><&|/l&>Link values to</&></td><td>
112 <input size="60" name="LinkValueTo"  value="<% $CustomFieldObj->LinkValueTo || $LinkValueTo || '' %>" />
113 <div class="hints">
114 <&|/l&>RT can make this custom field's values into hyperlinks to another service.</&>
115 <&|/l&>Fill in this field with a URL.</&>
116 <&|/l_unsafe, '<tt>__id__</tt>', '<tt>__CustomField__</tt>' &>RT will replace [_1] and [_2] with the record's id and the custom field's value, respectively.</&>
117 </div></td></tr>
118
119 <tr><td class="label"><&|/l&>Include page</&></td><td>
120 <input size="60" name="IncludeContentForValue" value="<% $CustomFieldObj->IncludeContentForValue || $IncludeContentForValue || '' %>" />
121 <div class="hints">
122 <&|/l&>RT can include content from another web service when showing this custom field.</&>
123 <&|/l&>Fill in this field with a URL.</&>
124 <&|/l_unsafe, '<tt>__id__</tt>', '<tt>__CustomField__</tt>' &>RT will replace [_1] and [_2] with the record's id and the custom field's value, respectively.</&>
125 <i><&|/l&>Some browsers may only load content from the same domain as your RT server.</&></i>
126 </div></td></tr>
127
128 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
129 <tr class="categoriesbasedon"><td class="label"><&|/l&>Categories are based on</&></td><td>
130 <& /Admin/Elements/SelectCustomField,
131     Name => "BasedOn",
132     LookupType => $CustomFieldObj->LookupType,
133     Default => $CustomFieldObj->BasedOnObj || $BasedOn,
134     Not => $CustomFieldObj->id,
135 &>
136 </td></tr>
137 % }
138
139 % $m->callback(CallbackName => 'BeforeEnabled', CustomField => $CustomFieldObj, CFvalidations => \@CFvalidations);
140
141 <tr><td class="label">&nbsp;</td><td>
142 <input type="checkbox" class="checkbox" name="Required" value="1" <% $RequiredChecked |n%> />
143 <&|/l&>Required for ticket resolution</&>
144 </td></tr>
145
146 <tr><td class="label">&nbsp;</td><td>
147 <input type="hidden" class="hidden" name="SetEnabled" value="1" />
148 <input type="checkbox" class="checkbox" name="Enabled" value="1" <% $EnabledChecked |n%> />
149 <&|/l&>Enabled (Unchecking this box disables this custom field)</&>
150 </td></tr>
151
152 % $m->callback(CallbackName => 'EndOfTable', CustomField => $CustomFieldObj, CFvalidations => \@CFvalidations);
153
154 </table>
155
156 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
157 <h2><&|/l&>Values</&></h2>
158 <div>
159 <& /Admin/Elements/EditCustomFieldValues, CustomField => $CustomFieldObj &>
160 <& /Admin/Elements/AddCustomFieldValue, CustomField => $CustomFieldObj &>
161 </div>
162 % }
163
164 <& /Elements/Submit, Name => 'Update', Label => $id eq 'new'? loc('Create'): loc('Save Changes') &>
165
166 </form>
167 <%INIT>
168 my ($title, @results);
169
170 my $CustomFieldObj = RT::CustomField->new( $session{'CurrentUser'} );
171
172 $m->callback(CallbackName => 'Initial', Pattern => \$Pattern, ARGSRef => \%ARGS);
173
174 unless ( $id ) {
175     $title = loc("Create a CustomField");
176     $id    = 'new';
177 }
178 else {
179     if ( $id eq 'new' ) {
180         my ( $val, $msg ) = $CustomFieldObj->Create(
181             Name          => $Name,
182             TypeComposite => $TypeComposite,
183             LookupType    => $LookupType,
184             Description   => $Description,
185             Pattern       => $Pattern,
186             LinkValueTo   => $LinkValueTo,
187             IncludeContentForValue => $IncludeContentForValue,
188             BasedOn       => $BasedOn,
189             Disabled      => !$Enabled,
190         );
191         if (!$val) {
192             push @results, loc("Could not create CustomField: [_1]", $msg);
193             $title = loc( 'Create a CustomField');
194         }
195         else {
196             push @results, loc("Object created");
197             $title = loc( 'Created CustomField [_1]', $CustomFieldObj->Name );
198         }
199     } else {
200         push @results, loc('No CustomField')
201             unless  $CustomFieldObj->Load( $id );
202
203         $title = loc( 'Editing CustomField [_1]', $CustomFieldObj->Name );
204     }
205 }
206
207 if ( $ARGS{'Update'} && $id ne 'new' ) {
208     #we're asking about enabled on the web page but really care about disabled.
209     $ARGS{'Disabled'} = $Enabled? 0 : 1;
210    
211     $ARGS{'Required'} ||= 0;
212
213     my @attribs = qw(Disabled Required Pattern Name TypeComposite LookupType Description LinkValueTo IncludeContentForValue);
214     push @results, UpdateRecordObject(
215         AttributesRef => \@attribs,
216         Object        => $CustomFieldObj,
217         ARGSRef       => \%ARGS
218     );
219     $CustomFieldObj->SetValuesClass( $ValuesClass );
220
221     # Set the render type if we have it, but unset it if the new type doesn't
222     # support render types
223     if ( $CustomFieldObj->HasRenderTypes($TypeComposite) ) {
224         my $original = $CustomFieldObj->RenderType;
225
226         if ( defined $RenderType and $RenderType ne $original ) {
227             # It's changed!  Let's update it.
228             my ($good, $msg) = $CustomFieldObj->SetRenderType( $RenderType );
229
230             if ( $good ) {
231                 $msg = loc("[_1] changed from '[_2]' to '[_3]'",
232                             loc("Render Type"), $original, $RenderType );
233             }
234
235             push @results, $msg;
236         }
237     }
238     else {
239         # Delete it if we no longer support render types
240         $CustomFieldObj->SetRenderType( undef );
241     }
242
243     if (($CustomFieldObj->BasedOn||'') ne ($BasedOn||'')) {
244         my ($good, $msg) = $CustomFieldObj->SetBasedOn( $BasedOn );
245         push @results, $msg;
246     }
247
248     $CustomFieldObj->SetUILocation( $UILocation );
249
250     my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
251     # Delete any fields that want to be deleted
252     foreach my $key ( keys %ARGS ) {
253         next unless $key =~ /^Delete-$paramtag-(\d+)$/o;
254         my ($val, $msg) = $CustomFieldObj->DeleteValue( $1 );
255         push (@results, $msg);
256     }
257
258     # Clean up values
259     foreach my $param (grep /^$paramtag-/, keys(%ARGS)) {
260       for ($ARGS{$param}) {
261         s/\r+\n/\n/g;
262         s/^\s+//;
263         s/\s+$//;
264       }
265     }
266
267     # Update any existing values
268     my $values = $CustomFieldObj->ValuesObj;
269     while ( my $value = $values->Next ) {
270         foreach my $attr (qw(Name Description SortOrder Category)) {
271             my $param = join("-", $paramtag, $value->Id, $attr);
272             next unless exists $ARGS{$param};
273             $ARGS{$param} =~ s/^\s+//;
274             $ARGS{$param} =~ s/\s+$//;
275             next if ($value->$attr()||'') eq ($ARGS{$param}||'');
276
277             my $mutator = "Set$attr";
278             my ($id, $msg) = $value->$mutator( $ARGS{$param} );
279             push (@results, $msg);
280         }
281         $m->callback(CallbackName => 'AfterUpdateCustomFieldValue', CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $value, ARGSRef => \%ARGS );
282     }
283
284     # Add any new values
285     if ( defined $ARGS{ $paramtag ."-new-Name" } && length $ARGS{ $paramtag ."-new-Name" } ) {
286         my ($id, $msg) = $CustomFieldObj->AddValue(
287             map { 
288                 $ARGS{$paramtag."-new-$_"} =~ s/^\s+//;
289                 $ARGS{$paramtag."-new-$_"} =~ s/\s+$//;
290                 $_ => $ARGS{ $paramtag ."-new-$_" } } qw/ Name Description SortOrder Category/
291         );
292         push (@results, $msg);
293
294         my $cfv = RT::CustomFieldValue->new( $session{CurrentUser} );
295         $cfv->Load($id);
296         $m->callback(CallbackName => 'AfterCreateCustomFieldValue',
297 CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $cfv, ARGSRef => \%ARGS );
298     }
299 }
300
301 $id = $CustomFieldObj->id if $CustomFieldObj->id;
302
303 # This code does automatic redirection if any updates happen.
304 MaybeRedirectForResults(
305     Actions     => \@results,
306     Arguments   => { id => $id },
307 ) if $CustomFieldObj->id;
308
309
310 my $EnabledChecked = qq[checked="checked"];
311 $EnabledChecked = '' if $CustomFieldObj->Disabled;
312
313 my $RequiredChecked = '';
314 $RequiredChecked = qq[checked="checked"] if $CustomFieldObj->Required;
315
316 my @CFvalidations = (
317     '(?#Mandatory).',
318     '(?#Digits)^[\d.]+$',
319     '(?#Year)^[12]\d{3}$',
320 );
321
322 $m->callback(CallbackName => 'ValidationPatterns', Values => \@CFvalidations);
323
324 </%INIT>
325 <%ARGS>
326 $id => undef
327 $TypeComposite => undef
328 $LookupType => undef
329 $MaxValues => undef
330 $SortOrder => undef
331 $Description => undef
332 $Pattern => undef
333 $Name => undef
334 $SetEnabled => undef
335 $Enabled => 0
336 $ValuesClass => 'RT::CustomFieldValues'
337 $RenderType => undef
338 $LinkValueTo => undef
339 $IncludeContentForValue => undef
340 $BasedOn => undef
341 $UILocation => undef
342 </%ARGS>