initial release
[RTx-MandatoryCustomFields.git] / html / Admin / CustomFields / Modify.html
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2010 Best Practical Solutions, LLC
6 %#                                          <jesse@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 || '' %>" size="20" /></td></tr>
60
61 <tr><td class="label"><&|/l&>Description</&></td>
62 <td><input name="Description" value="<% $CustomFieldObj->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, &>
93 </td></tr>
94
95 <tr class="edit_validation"><td class="label"><&|/l&>Validation</&></td>
96 <td><& /Widgets/ComboBox,
97     Name    => 'Pattern',
98     Default => $CustomFieldObj->Pattern,
99     Size    => 20,
100     Values  => \@CFvalidations,
101 &></td></tr>
102
103 <tr><td class="label"><&|/l&>Link values to</&></td><td>
104 <input size="60" name="LinkValueTo"  value="<% $CustomFieldObj->LinkValueTo || '' %>" />
105 <div class="hints">
106 <&|/l&>RT can make this custom field's values into hyperlinks to another service.</&>
107 <&|/l&>Fill in this field with a URL.</&>
108 <&|/l&>RT will replace <tt>__id__</tt> and <tt>__CustomField__</tt> with the record id and custom field value, respectively</&>
109 </div></td></tr>
110
111 <tr><td class="label"><&|/l&>Include page</&></td><td>
112 <input size="60" name="IncludeContentForValue" value="<% $CustomFieldObj->IncludeContentForValue || '' %>" />
113 <div class="hints">
114 <&|/l&>RT can include content from another web service when showing this custom field.</&>
115 <&|/l&>Fill in this field with a URL.</&>
116 <&|/l&>RT will replace <tt>__id__</tt> and <tt>__CustomField__</tt> with the record id and custom field value, respectively</&>
117 <i><&|/l&>Some browsers may only load content from the same domain as your RT server.</&></i>
118 </div></td></tr>
119
120 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
121 <tr class="categoriesbasedon"><td class="label"><&|/l&>Categories are based on</&></td><td>
122 <& /Admin/Elements/SelectCustomField,
123     Name => "BasedOn",
124     LookupType => $CustomFieldObj->LookupType,
125     Default => $CustomFieldObj->BasedOnObj,
126     Not => $CustomFieldObj->id,
127 &>
128 </td></tr>
129 % }
130
131 % $m->callback(CallbackName => 'BeforeEnabled', CustomField => $CustomFieldObj, CFvalidations => \@CFvalidations);
132 %# MandatoryCustomFields
133 <tr><td class="label">&nbsp;</td><td>
134 <input type="checkbox" class="checkbox" name="Required" value="1" <% $RequiredChecked |n%>/>
135 <&|/l&>Required for ticket resolution</&>
136 </td></tr>
137 %# MandatoryCustomFields
138 <tr><td class="label">&nbsp;</td><td>
139 <input type="hidden" class="hidden" name="SetEnabled" value="1" />
140 <input type="checkbox" class="checkbox" name="Enabled" value="1" <% $EnabledChecked |n%> />
141 <&|/l&>Enabled (Unchecking this box disables this custom field)</&>
142 </td></tr>
143
144 % $m->callback(CallbackName => 'EndOfTable', CustomField => $CustomFieldObj, CFvalidations => \@CFvalidations);
145
146 </table>
147
148 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
149 <h2><&|/l&>Values</&></h2>
150 <div>
151 <& /Admin/Elements/EditCustomFieldValues, CustomField => $CustomFieldObj &>
152 <& /Admin/Elements/AddCustomFieldValue, CustomField => $CustomFieldObj &>
153 </div>
154 % }
155
156 <& /Elements/Submit, Name => 'Update', Label => $id eq 'new'? loc('Create'): loc('Save Changes') &>
157
158 </form>
159 <%INIT>
160 my ($title, @results, $Disabled);
161
162 my $CustomFieldObj = RT::CustomField->new( $session{'CurrentUser'} );
163
164 $m->callback(CallbackName => 'Initial', Pattern => \$Pattern, ARGSRef => \%ARGS);
165
166 unless ( $id ) {
167     $title = loc("Create a CustomField");
168     $id    = 'new';
169 }
170 else {
171     if ( $id eq 'new' ) {
172         my ( $val, $msg ) = $CustomFieldObj->Create(
173             Name          => $Name,
174             TypeComposite => $TypeComposite,
175             LookupType    => $LookupType,
176             Description   => $Description,
177             Pattern       => $Pattern,
178             LinkValueTo   => $LinkValueTo,
179             IncludeContentForValue => $IncludeContentForValue,
180             BasedOn       => $BasedOn,
181         );
182         $m->comp( "/Elements/Error", Why => loc( "Could not create CustomField", $msg ) ) unless $val;
183         push @results, $msg;
184         $title = loc( 'Created CustomField [_1]', $CustomFieldObj->Name );
185     } else {
186         $CustomFieldObj->Load( $id ) || $m->comp("/Elements/Error", Why => loc('No CustomField') );
187         $title = loc( 'Editing CustomField [_1]', $CustomFieldObj->Name );
188     }
189 }
190
191 if ( $ARGS{'Update'} && $id ne 'new' ) {
192
193     #we're asking about enabled on the web page but really care about disabled.
194     $ARGS{'Disabled'} = $Disabled = $Enabled? 0 : 1;
195 # MandatoryCustomFields
196     $ARGS{'Required'} ||= 0;
197
198     my @attribs = qw(Disabled Required Pattern Name TypeComposite LookupType Description LinkValueTo IncludeContentForValue);
199 # MandatoryCustomFields
200     push @results, UpdateRecordObject(
201         AttributesRef => \@attribs,
202         Object        => $CustomFieldObj,
203         ARGSRef       => \%ARGS
204     );
205     $CustomFieldObj->SetValuesClass( $ValuesClass );
206
207     # Set the render type if we have it, but unset it if the new type doesn't
208     # support render types
209     if ( $CustomFieldObj->HasRenderTypes($TypeComposite) ) {
210         my $original = $CustomFieldObj->RenderType;
211
212         if ( defined $RenderType and $RenderType ne $original ) {
213             # It's changed!  Let's update it.
214             my ($good, $msg) = $CustomFieldObj->SetRenderType( $RenderType );
215
216             if ( $good ) {
217                 $msg = loc("[_1] changed from '[_2]' to '[_3]'",
218                             loc("Render Type"), $original, $RenderType );
219             }
220
221             push @results, $msg;
222         }
223     }
224     else {
225         # Delete it if we no longer support render types
226         $CustomFieldObj->SetRenderType( undef );
227     }
228
229     my ($good, $msg) = $CustomFieldObj->SetBasedOn( $BasedOn );
230     push @results, $msg unless $good or $msg =~ /No entry found/;
231
232     my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
233     # Delete any fields that want to be deleted
234     foreach my $key ( keys %ARGS ) {
235         next unless $key =~ /^Delete-$paramtag-(\d+)$/o;
236         my ($val, $msg) = $CustomFieldObj->DeleteValue( $1 );
237         push (@results, $msg);
238     }
239
240     # Update any existing values
241     my $values = $CustomFieldObj->ValuesObj;
242     while ( my $value = $values->Next ) {
243         foreach my $attr (qw(Name Description SortOrder Category)) {
244             my $param = join("-", $paramtag, $value->Id, $attr);
245             next unless exists $ARGS{$param};
246             $ARGS{$param} =~ s/^\s+//;
247             $ARGS{$param} =~ s/\s+$//;
248             next if ($value->$attr()||'') eq ($ARGS{$param}||'');
249
250             my $mutator = "Set$attr";
251             my ($id, $msg) = $value->$mutator( $ARGS{$param} );
252             push (@results, $msg);
253         }
254         $m->callback(CallbackName => 'AfterUpdateCustomFieldValue', CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $value, ARGSRef => \%ARGS );
255     }
256
257     # Add any new values
258     if ( defined $ARGS{ $paramtag ."-new-Name" } && length $ARGS{ $paramtag ."-new-Name" } ) {
259         my ($id, $msg) = $CustomFieldObj->AddValue(
260             map { 
261                 $ARGS{$paramtag."-new-$_"} =~ s/^\s+//;
262                 $ARGS{$paramtag."-new-$_"} =~ s/\s+$//;
263                 $_ => $ARGS{ $paramtag ."-new-$_" } } qw/ Name Description SortOrder Category/
264         );
265         push (@results, $msg);
266
267         my $cfv = RT::CustomFieldValue->new( $session{CurrentUser} );
268         $cfv->Load($id);
269         $m->callback(CallbackName => 'AfterCreateCustomFieldValue',
270 CustomFieldObj => $CustomFieldObj, CustomFieldValueObj => $cfv, ARGSRef => \%ARGS );
271     }
272 }
273
274
275
276
277 $id = $CustomFieldObj->id if $CustomFieldObj->id;
278
279
280 # This code does automatic redirection if any updates happen.
281 $m->comp(
282     '/Elements/MaybeRedirectForResults',
283     Actions     => \@results,
284     Path        => '/Admin/CustomFields/Modify.html',
285     id              => $id,
286     ARGSRef     => \%ARGS
287 );
288
289
290
291 my $EnabledChecked = qq[checked="checked"];
292 $EnabledChecked = '' if $CustomFieldObj->Disabled;
293
294 my $RequiredChecked = '';
295 $RequiredChecked = 'checked="checked"' if $CustomFieldObj->Required;
296
297 my @CFvalidations = (
298     '(?#Mandatory).',
299     '(?#Digits)^[\d.]+$',
300     '(?#Year)^[12]\d{3}$',
301 );
302
303 $m->callback(CallbackName => 'ValidationPatterns', Values => \@CFvalidations);
304
305 </%INIT>
306 <%ARGS>
307 $id => undef
308 $TypeComposite => undef
309 $LookupType => undef
310 $MaxValues => undef
311 $SortOrder => undef
312 $Description => undef
313 $Pattern => undef
314 $Name => undef
315 $SetEnabled => undef
316 $Enabled => 0
317 $ValuesClass => 'RT::CustomFieldValues'
318 $RenderType => undef
319 $LinkValueTo => undef
320 $IncludeContentForValue => undef
321 $BasedOn => undef
322 </%ARGS>