TimeWorked-like custom fields, RT#11168
[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-2011 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 <& /Admin/Elements/CustomFieldTabs,
50     id => $CustomFieldObj->Id ,
51     current_tab => $current_tab,
52     Title => $title &>
53 <& /Elements/ListActions, actions => \@results &>
54
55
56 <form method="post" action="Modify.html" name="ModifyCustomField">
57 <input type="hidden" class="hidden" name="id" value="<% $id %>" />
58
59 <table>
60
61 <tr><td class="label"><&|/l&>Name</&></td>
62 <td><input name="Name" value="<% $CustomFieldObj->Name || '' %>" size="20" /></td></tr>
63
64 <tr><td class="label"><&|/l&>Description</&></td>
65 <td><input name="Description" value="<% $CustomFieldObj->Description || '' %>" size="80" /></td></tr>
66
67 <tr><td class="label"><&|/l&>Type</&></td>
68 <td><& /Admin/Elements/SelectCustomFieldType, 
69         Name => "TypeComposite", 
70         Default => $CustomFieldObj->TypeComposite, &>
71 </td></tr>
72
73 % if ( $CustomFieldObj->Id and $CustomFieldObj->IsSelectionType and RT->Config->Get('CustomFieldValuesSources') and ( scalar(@{RT->Config->Get('CustomFieldValuesSources')}) > 0 ) ) {
74 <tr><td class="label"><&|/l&>Field values source:</&></td><td>
75 <& /Admin/Elements/EditCustomFieldValuesSource, CustomField => $CustomFieldObj &>
76 </td></tr>
77 % }
78
79 <tr><td class="label"><&|/l&>Applies to</&></td>
80 <td><& /Admin/Elements/SelectCustomFieldLookupType, 
81         Name => "LookupType", 
82         Default => $CustomFieldObj->LookupType, &>
83 </td></tr>
84
85 % if ( $CustomFieldObj->Id 
86 %     and $CustomFieldObj->LookupType =~ /RT::Transaction/ ) {
87 <tr><td class="label"><&|/l&>Display&nbsp;with</&></td>
88 <td><& /Admin/Elements/EditCustomFieldUILocation,
89         CustomField => $CustomFieldObj &>
90 </td></tr>
91 % }
92
93 <tr><td class="label"><&|/l&>Validation</&></td>
94 <td><& /Widgets/ComboBox,
95     Name    => 'Pattern',
96     Default => $CustomFieldObj->Pattern,
97     Size    => 20,
98     Values  => \@CFvalidations,
99 &></td></tr>
100
101 <tr><td class="label"><&|/l&>Link values to</&></td><td>
102 <input size="60" name="LinkValueTo"  value="<% $CustomFieldObj->LinkValueTo || '' %>" />
103 <div class="hints">
104 <&|/l&>RT can make this custom field's values into hyperlinks to another service.</&>
105 <&|/l&>Fill in this field with a URL.</&>
106 <&|/l&>RT will replace <tt>__id__</tt> and <tt>__CustomField__</tt> with the record id and custom field value, respectively</&>
107 </div></td></tr>
108
109 <tr><td class="label"><&|/l&>Include page</&></td><td>
110 <input size="60" name="IncludeContentForValue" value="<% $CustomFieldObj->IncludeContentForValue || '' %>" />
111 <div class="hints">
112 <&|/l&>RT can include content from another web service when showing this custom field.</&>
113 <&|/l&>Fill in this field with a URL.</&>
114 <&|/l&>RT will replace <tt>__id__</tt> and <tt>__CustomField__</tt> with the record id and custom field value, respectively</&>
115 <i><&|/l&>Some browsers may only load content from the same domain as your RT server.</&></i>
116 </div></td></tr>
117
118 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
119 <tr><td class="label"><&|/l&>Categories are based on</&></td><td>
120 <& /Admin/Elements/SelectCustomField,
121     Name => "BasedOn",
122     LookupType => $CustomFieldObj->LookupType,
123     Default => $CustomFieldObj->BasedOnObj,
124     Not => $CustomFieldObj->id,
125 &>
126 </td></tr>
127 % }
128
129 <tr><td class="label">&nbsp;</td><td>
130 <input type="checkbox" class="checkbox" name="Required" value="1" <% $RequiredChecked |n%> />
131 <&|/l&>Required for ticket resolution</&>
132 </td></tr>
133
134 <tr><td class="label">&nbsp;</td><td>
135 <input type="hidden" class="hidden" name="SetEnabled" value="1" />
136 <input type="checkbox" class="checkbox" name="Enabled" value="1" <% $EnabledChecked |n%> />
137 <&|/l&>Enabled (Unchecking this box disables this custom field)</&>
138 </td></tr>
139
140 </table>
141
142 % if ( $CustomFieldObj->Id && $CustomFieldObj->IsSelectionType && !$CustomFieldObj->IsExternalValues ) {
143 <h2><&|/l&>Values</&></h2>
144 <div>
145 <& /Admin/Elements/EditCustomFieldValues, CustomField => $CustomFieldObj &>
146 <& /Admin/Elements/AddCustomFieldValue, CustomField => $CustomFieldObj &>
147 </div>
148 % }
149
150 <& /Elements/Submit, Name => 'Update', Label => $id eq 'new'? loc('Create'): loc('Save Changes') &>
151
152 </form>
153 <%INIT>
154 my ($title, @results, $Disabled);
155
156 my $CustomFieldObj = RT::CustomField->new( $session{'CurrentUser'} );
157 my $current_tab = 'Admin/CustomFields/Modify.html';
158
159 unless ( $id ) {
160     $title = loc("Create a CustomField");
161     $id    = 'new';
162     $current_tab .= '?Create=1';
163 }
164 else {
165     if ( $id eq 'new' ) {
166         my ( $val, $msg ) = $CustomFieldObj->Create(
167             Name          => $Name,
168             TypeComposite => $TypeComposite,
169             LookupType    => $LookupType,
170             Description   => $Description,
171             Pattern       => $Pattern,
172             LinkValueTo   => $LinkValueTo,
173             IncludeContentForValue => $IncludeContentForValue,
174             BasedOn       => $BasedOn,
175         );
176         $m->comp( "/Elements/Error", Why => loc( "Could not create CustomField", $msg ) ) unless $val;
177         push @results, $msg;
178         $title = loc( 'Created CustomField [_1]', $CustomFieldObj->Name );
179     } else {
180         $CustomFieldObj->Load( $id ) || $m->comp("/Elements/Error", Why => loc('No CustomField') );
181         $title = loc( 'Editing CustomField [_1]', $CustomFieldObj->Name );
182     }
183     $current_tab .= "?id=$id";
184 }
185
186 if ( $ARGS{'Update'} && $id ne 'new' ) {
187     #we're asking about enabled on the web page but really care about disabled.
188     $ARGS{'Disabled'} = $Disabled = $Enabled? 0 : 1;
189    
190     $ARGS{'Required'} ||= 0;
191
192     my @attribs = qw(Disabled Required Pattern Name TypeComposite LookupType Description LinkValueTo IncludeContentForValue);
193     push @results, UpdateRecordObject(
194         AttributesRef => \@attribs,
195         Object        => $CustomFieldObj,
196         ARGSRef       => \%ARGS
197     );
198     $CustomFieldObj->SetValuesClass( $ValuesClass );
199
200     $CustomFieldObj->SetBasedOn( $BasedOn );
201
202     $CustomFieldObj->SetUILocation( $UILocation );
203
204     my $paramtag = "CustomField-". $CustomFieldObj->Id ."-Value";
205     # Delete any fields that want to be deleted
206     foreach my $key ( keys %ARGS ) {
207         next unless $key =~ /^Delete-$paramtag-(\d+)$/o;
208         my ($val, $msg) = $CustomFieldObj->DeleteValue( $1 );
209         push (@results, $msg);
210     }
211
212     # Clean up values
213     foreach my $param (grep /^$paramtag-/, keys(%ARGS)) {
214       for ($ARGS{$param}) {
215         s/\r+\n/\n/g;
216         s/^\s+//;
217         s/\s+$//;
218       }
219     }
220
221     # Update any existing values
222     my $values = $CustomFieldObj->ValuesObj;
223     while ( my $value = $values->Next ) {
224         foreach my $attr qw(Name Description SortOrder Category) {
225             my $param = join("-", $paramtag, $value->Id, $attr);
226             next unless exists $ARGS{$param};
227             $ARGS{$param} =~ s/^\s+//;
228             $ARGS{$param} =~ s/\s+$//;
229             next if ($value->$attr()||'') eq ($ARGS{$param}||'');
230             my $mutator = "Set$attr";
231             my ($id, $msg) = $value->$mutator( $ARGS{$param} );
232             push (@results, $msg);
233         }
234     }
235
236     # Add any new values
237     if ( defined $ARGS{ $paramtag ."-new-Name" } && length $ARGS{ $paramtag ."-new-Name" } ) {
238         my ($id, $msg) = $CustomFieldObj->AddValue(
239             map { 
240                 $ARGS{$paramtag."-new-$_"} =~ s/^\s+//;
241                 $ARGS{$paramtag."-new-$_"} =~ s/\s+$//;
242                 $_ => $ARGS{ $paramtag ."-new-$_" } } qw/ Name Description SortOrder Category/
243         );
244         push (@results, $msg);
245     }
246 }
247
248 $id = $CustomFieldObj->id if $CustomFieldObj->id;
249
250 my $EnabledChecked = qq[checked="checked"];
251 $EnabledChecked = '' if $CustomFieldObj->Disabled;
252
253 my $RequiredChecked = '';
254 $RequiredChecked = qq[checked="checked"] if $CustomFieldObj->Required;
255
256 my @CFvalidations = (
257     '(?#Mandatory).',
258     '(?#Digits)^[\d.]+$',
259     '(?#Year)^[12]\d{3}$',
260 );
261
262 $m->callback(CallbackName => 'ValidationPatterns', Values => \@CFvalidations);
263
264 </%INIT>
265 <%ARGS>
266 $id => undef
267 $TypeComposite => undef
268 $LookupType => undef
269 $MaxValues => undef
270 $SortOrder => undef
271 $Description => undef
272 $Pattern => undef
273 $Name => undef
274 $SetEnabled => undef
275 $Enabled => 0
276 $ValuesClass => 'RT::CustomFieldValues'
277 $LinkValueTo => undef
278 $IncludeContentForValue => undef
279 $BasedOn => undef
280 $UILocation => undef
281 </%ARGS>