rt 4.2.13 ticket#13852
[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-2016 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,
49     Title => $title,
50     Focus => (($added_cfv or $ARGS{FocusAddValue})
51         ? "input[name=CustomField-$id-Value-new-Name]"
52         : undef),
53     &>
54 <& /Elements/Tabs &>
55 <& /Elements/ListActions, actions => \@results &>
56
57
58 <form method="post" action="Modify.html" name="ModifyCustomField" id="ModifyCustomField">
59 <input type="hidden" class="hidden" name="id" value="<% $id %>" />
60
61 <table>
62
63 <tr><td class="label"><&|/l&>Name</&></td>
64 <td><input name="Name" value="<% $CustomFieldObj->Name || $Name || '' %>" size="20" /></td></tr>
65
66 <tr><td class="label"><&|/l&>Description</&></td>
67 <td><input name="Description" value="<% $CustomFieldObj->Description || $Description || '' %>" size="80" /></td></tr>
68
69 <tr><td class="label"><&|/l&>Type</&></td>
70 <td><& /Admin/Elements/SelectCustomFieldType, 
71         Name => "TypeComposite", 
72         Default => $CustomFieldObj->TypeComposite, &>
73 </td></tr>
74
75 % if ( $CustomFieldObj->Id and $CustomFieldObj->HasRenderTypes ) {
76 <tr>
77   <td class="label"><&|/l&>Render Type</&></td>
78   <td>
79     <& /Admin/Elements/SelectCustomFieldRenderType,
80         Name            => "RenderType",
81         TypeComposite   => $CustomFieldObj->TypeComposite,
82         Default         => $CustomFieldObj->RenderType, 
83         BasedOn         => $CustomFieldObj->BasedOnObj->id, &>
84   </td>
85 </tr>
86 % }
87
88 % if ( $CustomFieldObj->Id and $CustomFieldObj->IsSelectionType and RT->Config->Get('CustomFieldValuesSources') and ( scalar(@{RT->Config->Get('CustomFieldValuesSources')}) > 0 ) ) {
89 <tr><td class="label"><&|/l&>Field values source:</&></td><td>
90 <& /Admin/Elements/EditCustomFieldValuesSource, CustomField => $CustomFieldObj &>
91 </td></tr>
92 % }
93
94 <tr><td class="label"><&|/l&>Applies to</&></td>
95 <td><& /Admin/Elements/SelectCustomFieldLookupType, 
96         Name => "LookupType", 
97         Default => $CustomFieldObj->LookupType || $LookupType, &>
98 </td></tr>
99
100 % if ( $CustomFieldObj->Id 
101 %     and $CustomFieldObj->LookupType =~ /RT::Transaction/ ) {
102 <tr><td class="label"><&|/l&>Display&nbsp;with</&></td>
103 <td><& /Admin/Elements/EditCustomFieldUILocation,
104         CustomField => $CustomFieldObj &>
105 </td></tr>
106 % }
107
108 <tr class="edit_validation"><td class="label"><&|/l&>Validation</&></td>
109 <td><& /Widgets/ComboBox,
110     Name    => 'Pattern',
111     Default => $CustomFieldObj->Pattern || $Pattern,
112     Size    => 20,
113     Values  => \@CFvalidations,
114 &></td></tr>
115
116 <tr><td class="label"><&|/l&>Link values to</&></td><td>
117 <input size="60" name="LinkValueTo"  value="<% $CustomFieldObj->LinkValueTo || $LinkValueTo || '' %>" />
118 <div class="hints">
119 <&|/l&>RT can make this custom field's values into hyperlinks to another service.</&>
120 <&|/l&>Fill in this field with a URL.</&>
121 <&|/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.</&>
122 </div></td></tr>
123
124 <tr><td class="label"><&|/l&>Include page</&></td><td>
125 <input size="60" name="IncludeContentForValue" value="<% $CustomFieldObj->IncludeContentForValue || $IncludeContentForValue || '' %>" />
126 <div class="hints">
127 <&|/l&>RT can include content from another web service when showing this custom field.</&>
128 <&|/l&>Fill in this field with a URL.</&>
129 <&|/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.</&>
130 <i><&|/l&>Some browsers may only load content from the same domain as your RT server.</&></i>
131 </div></td></tr>
132
133 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType ) {
134 <tr class="categoriesbasedon"><td class="label"><&|/l&>Categories are based on</&></td><td>
135 <& /Admin/Elements/SelectCustomField,
136     Name => "BasedOn",
137     LookupType => $CustomFieldObj->LookupType,
138     Default => $CustomFieldObj->BasedOnObj || $BasedOn,
139     Not => $CustomFieldObj->id,
140 &>
141 </td></tr>
142 % }
143
144 % $m->callback(CallbackName => 'BeforeEnabled', CustomField => $CustomFieldObj, CFvalidations => \@CFvalidations);
145
146 <tr><td class="label">&nbsp;</td><td>
147 <input type="checkbox" class="checkbox" name="Required" value="1" <% $RequiredChecked |n%> />
148 <&|/l&>Required for ticket resolution</&>
149 </td></tr>
150
151 <tr><td class="label">&nbsp;</td><td>
152 <input type="checkbox" class="checkbox" name="YesClone" value="1" <% $YesCloneChecked |n%> />
153 <&|/l&>Copy this field to new tickets</&>
154 </td></tr>
155
156 <tr><td class="label">&nbsp;</td><td>
157 <input type="hidden" class="hidden" name="SetEnabled" value="1" />
158 <input type="checkbox" class="checkbox" id="Enabled" name="Enabled" value="1" <% $EnabledChecked |n %> />
159 <label for="Enabled"><&|/l&>Enabled (Unchecking this box disables this custom field)</&></label>
160 </td></tr>
161
162 % $m->callback(CallbackName => 'EndOfTable', CustomField => $CustomFieldObj, CFvalidations => \@CFvalidations);
163
164 </table>
165
166 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
167 <h2><&|/l&>Values</&></h2>
168 <div>
169 <& /Admin/Elements/EditCustomFieldValues, CustomField => $CustomFieldObj &>
170 <& /Admin/Elements/AddCustomFieldValue, CustomField => $CustomFieldObj &>
171 </div>
172 % }
173
174 <& /Elements/Submit, Name => 'Update', Label => $id eq 'new'? loc('Create'): loc('Save Changes') &>
175
176 </form>
177 % $m->callback(%ARGS, CallbackName => 'EndOfPage', CustomFieldObj => $CustomFieldObj);
178 <%INIT>
179 my ($title, @results, $added_cfv);
180
181 my $CustomFieldObj = RT::CustomField->new( $session{'CurrentUser'} );
182
183 $m->callback(CallbackName => 'Initial', Pattern => \$Pattern, Results => \@results, ARGSRef => \%ARGS);
184
185 unless ( $id ) {
186     $title = loc("Create a CustomField");
187     $id    = 'new';
188 }
189 else {
190     if ( $id eq 'new' ) {
191         my ( $val, $msg ) = $CustomFieldObj->Create(
192             Name          => $Name,
193             TypeComposite => $TypeComposite,
194             LookupType    => $LookupType,
195             Description   => $Description,
196             Pattern       => $Pattern,
197             LinkValueTo   => $LinkValueTo,
198             IncludeContentForValue => $IncludeContentForValue,
199             BasedOn       => $BasedOn,
200             Disabled      => ($Enabled ? 0 : 1),
201             NoClone       => !$YesClone,
202         );
203         if (!$val) {
204             push @results, loc("Could not create CustomField: [_1]", $msg);
205             $title = loc( 'Create a CustomField');
206         }
207         else {
208             push @results, loc("Object created");
209             $title = loc( 'Created CustomField [_1]', $CustomFieldObj->Name );
210         }
211     } else {
212         push @results, loc('No CustomField')
213             unless  $CustomFieldObj->Load( $id );
214
215         $title = loc( 'Editing CustomField [_1]', $CustomFieldObj->Name );
216     }
217 }
218
219 if ( $ARGS{'Update'} && $id ne 'new' ) {
220     #we're asking about enabled on the web page but really care about disabled.
221     $ARGS{'Disabled'} = $Enabled? 0 : 1;
222     #  likewise
223     $ARGS{'NoClone'} = $YesClone ? 0 : 1;
224    
225     $ARGS{'Required'} ||= 0;
226
227     my @attribs = qw(Disabled Required Pattern Name TypeComposite LookupType Description LinkValueTo IncludeContentForValue NoClone);
228     push @results, UpdateRecordObject(
229         AttributesRef => \@attribs,
230         Object        => $CustomFieldObj,
231         ARGSRef       => \%ARGS
232     );
233     if ( ($ValuesClass||'RT::CustomFieldValues') ne $CustomFieldObj->ValuesClass ) {
234         my $original = $CustomFieldObj->ValuesClass;
235         my ($good, $msg) = $CustomFieldObj->SetValuesClass( $ValuesClass );
236         if ( $good ) {
237             $msg = loc("[_1] changed from '[_2]' to '[_3]'",
238                         loc("Field values source"), $original, $ValuesClass );
239         }
240         push @results, $msg;
241     }
242
243     # Set the render type if we have it, but unset it if the new type doesn't
244     # support render types
245     if ( $CustomFieldObj->HasRenderTypes($TypeComposite) ) {
246         my $original = $CustomFieldObj->RenderType;
247
248         if ( defined $RenderType and $RenderType ne $original ) {
249             # It's changed!  Let's update it.
250             my ($good, $msg) = $CustomFieldObj->SetRenderType( $RenderType );
251
252             if ( $good ) {
253                 $msg = loc("[_1] changed from '[_2]' to '[_3]'",
254                             loc("Render Type"), $original, $RenderType );
255             }
256
257             push @results, $msg;
258         }
259     }
260     else {
261         # Delete it if we no longer support render types
262         $CustomFieldObj->SetRenderType( undef );
263     }
264
265     if (($CustomFieldObj->BasedOn||'') ne ($BasedOn||'')) {
266         my ($good, $msg) = $CustomFieldObj->SetBasedOn( $BasedOn );
267         push @results, $msg;
268     }
269
270     $CustomFieldObj->SetUILocation( $UILocation );
271
272     my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
273     # Delete any fields that want to be deleted
274     foreach my $key ( keys %ARGS ) {
275         next unless $key =~ /^Delete-$paramtag-(\d+)$/;
276         my ($val, $msg) = $CustomFieldObj->DeleteValue( $1 );
277         push (@results, $msg);
278     }
279
280     # Clean up values
281     foreach my $param (grep /^$paramtag-/, keys(%ARGS)) {
282       for ($ARGS{$param}) {
283         s/\r+\n/\n/g;
284         s/^\s+//;
285         s/\s+$//;
286       }
287     }
288
289     # Update any existing values
290     my $values = $CustomFieldObj->ValuesObj;
291     while ( my $value = $values->Next ) {
292         foreach my $attr (qw(Name Description SortOrder Category)) {
293             my $param = join("-", $paramtag, $value->Id, $attr);
294             next unless exists $ARGS{$param};
295             $ARGS{$param} =~ s/^\s+//;
296             $ARGS{$param} =~ s/\s+$//;
297             next if ($value->$attr()||'') eq ($ARGS{$param}||'');
298
299             my $mutator = "Set$attr";
300             my ($id, $msg) = $value->$mutator( $ARGS{$param} );
301             push (@results, $msg);
302         }
303         $m->callback(CallbackName => 'AfterUpdateCustomFieldValue', CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $value, ARGSRef => \%ARGS );
304     }
305
306     # Add any new values
307     if ( defined $ARGS{ $paramtag ."-new-Name" } && length $ARGS{ $paramtag ."-new-Name" } ) {
308         my ($id, $msg) = $CustomFieldObj->AddValue(
309             map { 
310                 $ARGS{$paramtag."-new-$_"} =~ s/^\s+//;
311                 $ARGS{$paramtag."-new-$_"} =~ s/\s+$//;
312                 $_ => $ARGS{ $paramtag ."-new-$_" } } qw/ Name Description SortOrder Category/
313         );
314         push (@results, $msg);
315         $added_cfv = 1 if $id;
316
317         my $cfv = RT::CustomFieldValue->new( $session{CurrentUser} );
318         $cfv->Load($id);
319         $m->callback(CallbackName => 'AfterCreateCustomFieldValue',
320 CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $cfv, ARGSRef => \%ARGS );
321     }
322 }
323
324 if ( $CustomFieldObj->id && $CustomFieldObj->IsOnlyGlobal ) {
325     my ( $ret, $msg );
326     my $object = $CustomFieldObj->RecordClassFromLookupType->new( $session{'CurrentUser'} );
327
328     if ( $CustomFieldObj->Disabled && $CustomFieldObj->IsGlobal ) {
329         ( $ret, $msg ) = $CustomFieldObj->RemoveFromObject($object);
330     }
331     elsif ( !$CustomFieldObj->Disabled && !$CustomFieldObj->IsGlobal ) {
332         ( $ret, $msg ) = $CustomFieldObj->AddToObject($object);
333     }
334
335     # successful msg("object created" or "object deleted ) is useless here
336     push @results, $msg unless $ret;
337 }
338
339 $id = $CustomFieldObj->id if $CustomFieldObj->id;
340
341 # This code does automatic redirection if any updates happen.
342 MaybeRedirectForResults(
343     Actions     => \@results,
344     Arguments   => { id => $id, FocusAddValue => ($added_cfv ? 1 : 0) },
345 ) if $CustomFieldObj->id;
346
347
348 my $EnabledChecked = qq[checked="checked"];
349 $EnabledChecked = '' if $CustomFieldObj->Disabled;
350
351 my $RequiredChecked = '';
352 $RequiredChecked = qq[checked="checked"] if $CustomFieldObj->Required;
353
354 my $YesCloneChecked = qq[checked="checked"];
355 $YesCloneChecked = '' if $CustomFieldObj->NoClone;
356
357
358 my @CFvalidations = (
359     '(?#Mandatory).',
360     '(?#Digits)^[\d.]+$',
361     '(?#Year)^[12]\d{3}$',
362 );
363
364 $m->callback(CallbackName => 'ValidationPatterns', Values => \@CFvalidations);
365
366 </%INIT>
367 <%ARGS>
368 $id => undef
369 $TypeComposite => undef
370 $LookupType => RT::Ticket->CustomFieldLookupType
371 $MaxValues => undef
372 $SortOrder => undef
373 $Description => undef
374 $Pattern => undef
375 $Name => undef
376 $SetEnabled => undef
377 $Enabled => 0
378 $ValuesClass => 'RT::CustomFieldValues'
379 $RenderType => undef
380 $LinkValueTo => undef
381 $IncludeContentForValue => undef
382 $BasedOn => undef
383 $UILocation => undef
384 $YesClone => undef
385 </%ARGS>