Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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-2015 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 <%INIT>
178 my ($title, @results, $added_cfv);
179
180 my $CustomFieldObj = RT::CustomField->new( $session{'CurrentUser'} );
181
182 $m->callback(CallbackName => 'Initial', Pattern => \$Pattern, ARGSRef => \%ARGS);
183
184 unless ( $id ) {
185     $title = loc("Create a CustomField");
186     $id    = 'new';
187 }
188 else {
189     if ( $id eq 'new' ) {
190         my ( $val, $msg ) = $CustomFieldObj->Create(
191             Name          => $Name,
192             TypeComposite => $TypeComposite,
193             LookupType    => $LookupType,
194             Description   => $Description,
195             Pattern       => $Pattern,
196             LinkValueTo   => $LinkValueTo,
197             IncludeContentForValue => $IncludeContentForValue,
198             BasedOn       => $BasedOn,
199             Disabled      => ($Enabled ? 0 : 1),
200             NoClone       => !$YesClone,
201         );
202         if (!$val) {
203             push @results, loc("Could not create CustomField: [_1]", $msg);
204             $title = loc( 'Create a CustomField');
205         }
206         else {
207             push @results, loc("Object created");
208             $title = loc( 'Created CustomField [_1]', $CustomFieldObj->Name );
209         }
210     } else {
211         push @results, loc('No CustomField')
212             unless  $CustomFieldObj->Load( $id );
213
214         $title = loc( 'Editing CustomField [_1]', $CustomFieldObj->Name );
215     }
216 }
217
218 if ( $ARGS{'Update'} && $id ne 'new' ) {
219     #we're asking about enabled on the web page but really care about disabled.
220     $ARGS{'Disabled'} = $Enabled? 0 : 1;
221     #  likewise
222     $ARGS{'NoClone'} = $YesClone ? 0 : 1;
223    
224     $ARGS{'Required'} ||= 0;
225
226     my @attribs = qw(Disabled Required Pattern Name TypeComposite LookupType Description LinkValueTo IncludeContentForValue NoClone);
227     push @results, UpdateRecordObject(
228         AttributesRef => \@attribs,
229         Object        => $CustomFieldObj,
230         ARGSRef       => \%ARGS
231     );
232     if ( ($ValuesClass||'RT::CustomFieldValues') ne $CustomFieldObj->ValuesClass ) {
233         my $original = $CustomFieldObj->ValuesClass;
234         my ($good, $msg) = $CustomFieldObj->SetValuesClass( $ValuesClass );
235         if ( $good ) {
236             $msg = loc("[_1] changed from '[_2]' to '[_3]'",
237                         loc("Field values source"), $original, $ValuesClass );
238         }
239         push @results, $msg;
240     }
241
242     # Set the render type if we have it, but unset it if the new type doesn't
243     # support render types
244     if ( $CustomFieldObj->HasRenderTypes($TypeComposite) ) {
245         my $original = $CustomFieldObj->RenderType;
246
247         if ( defined $RenderType and $RenderType ne $original ) {
248             # It's changed!  Let's update it.
249             my ($good, $msg) = $CustomFieldObj->SetRenderType( $RenderType );
250
251             if ( $good ) {
252                 $msg = loc("[_1] changed from '[_2]' to '[_3]'",
253                             loc("Render Type"), $original, $RenderType );
254             }
255
256             push @results, $msg;
257         }
258     }
259     else {
260         # Delete it if we no longer support render types
261         $CustomFieldObj->SetRenderType( undef );
262     }
263
264     if (($CustomFieldObj->BasedOn||'') ne ($BasedOn||'')) {
265         my ($good, $msg) = $CustomFieldObj->SetBasedOn( $BasedOn );
266         push @results, $msg;
267     }
268
269     $CustomFieldObj->SetUILocation( $UILocation );
270
271     my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
272     # Delete any fields that want to be deleted
273     foreach my $key ( keys %ARGS ) {
274         next unless $key =~ /^Delete-$paramtag-(\d+)$/;
275         my ($val, $msg) = $CustomFieldObj->DeleteValue( $1 );
276         push (@results, $msg);
277     }
278
279     # Clean up values
280     foreach my $param (grep /^$paramtag-/, keys(%ARGS)) {
281       for ($ARGS{$param}) {
282         s/\r+\n/\n/g;
283         s/^\s+//;
284         s/\s+$//;
285       }
286     }
287
288     # Update any existing values
289     my $values = $CustomFieldObj->ValuesObj;
290     while ( my $value = $values->Next ) {
291         foreach my $attr (qw(Name Description SortOrder Category)) {
292             my $param = join("-", $paramtag, $value->Id, $attr);
293             next unless exists $ARGS{$param};
294             $ARGS{$param} =~ s/^\s+//;
295             $ARGS{$param} =~ s/\s+$//;
296             next if ($value->$attr()||'') eq ($ARGS{$param}||'');
297
298             my $mutator = "Set$attr";
299             my ($id, $msg) = $value->$mutator( $ARGS{$param} );
300             push (@results, $msg);
301         }
302         $m->callback(CallbackName => 'AfterUpdateCustomFieldValue', CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $value, ARGSRef => \%ARGS );
303     }
304
305     # Add any new values
306     if ( defined $ARGS{ $paramtag ."-new-Name" } && length $ARGS{ $paramtag ."-new-Name" } ) {
307         my ($id, $msg) = $CustomFieldObj->AddValue(
308             map { 
309                 $ARGS{$paramtag."-new-$_"} =~ s/^\s+//;
310                 $ARGS{$paramtag."-new-$_"} =~ s/\s+$//;
311                 $_ => $ARGS{ $paramtag ."-new-$_" } } qw/ Name Description SortOrder Category/
312         );
313         push (@results, $msg);
314         $added_cfv = 1 if $id;
315
316         my $cfv = RT::CustomFieldValue->new( $session{CurrentUser} );
317         $cfv->Load($id);
318         $m->callback(CallbackName => 'AfterCreateCustomFieldValue',
319 CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $cfv, ARGSRef => \%ARGS );
320     }
321 }
322
323 if ( $CustomFieldObj->id && $CustomFieldObj->IsOnlyGlobal ) {
324     my ( $ret, $msg );
325     my $object = $CustomFieldObj->RecordClassFromLookupType->new( $session{'CurrentUser'} );
326
327     if ( $CustomFieldObj->Disabled && $CustomFieldObj->IsGlobal ) {
328         ( $ret, $msg ) = $CustomFieldObj->RemoveFromObject($object);
329     }
330     elsif ( !$CustomFieldObj->Disabled && !$CustomFieldObj->IsGlobal ) {
331         ( $ret, $msg ) = $CustomFieldObj->AddToObject($object);
332     }
333
334     # successful msg("object created" or "object deleted ) is useless here
335     push @results, $msg unless $ret;
336 }
337
338 $id = $CustomFieldObj->id if $CustomFieldObj->id;
339
340 # This code does automatic redirection if any updates happen.
341 MaybeRedirectForResults(
342     Actions     => \@results,
343     Arguments   => { id => $id, FocusAddValue => ($added_cfv ? 1 : 0) },
344 ) if $CustomFieldObj->id;
345
346
347 my $EnabledChecked = qq[checked="checked"];
348 $EnabledChecked = '' if $CustomFieldObj->Disabled;
349
350 my $RequiredChecked = '';
351 $RequiredChecked = qq[checked="checked"] if $CustomFieldObj->Required;
352
353 my $YesCloneChecked = qq[checked="checked"];
354 $YesCloneChecked = '' if $CustomFieldObj->NoClone;
355
356
357 my @CFvalidations = (
358     '(?#Mandatory).',
359     '(?#Digits)^[\d.]+$',
360     '(?#Year)^[12]\d{3}$',
361 );
362
363 $m->callback(CallbackName => 'ValidationPatterns', Values => \@CFvalidations);
364
365 </%INIT>
366 <%ARGS>
367 $id => undef
368 $TypeComposite => undef
369 $LookupType => RT::Ticket->CustomFieldLookupType
370 $MaxValues => undef
371 $SortOrder => undef
372 $Description => undef
373 $Pattern => undef
374 $Name => undef
375 $SetEnabled => undef
376 $Enabled => 0
377 $ValuesClass => 'RT::CustomFieldValues'
378 $RenderType => undef
379 $LinkValueTo => undef
380 $IncludeContentForValue => undef
381 $BasedOn => undef
382 $UILocation => undef
383 $YesClone => undef
384 </%ARGS>