Upgrade to 4.0.8 with mod of ExternalAuth + absolute paths to ticket-menu.
[usit-rt.git] / share / html / REST / 1.0 / Forms / ticket / default
CommitLineData
84fb5b46
MKG
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%# REST/1.0/Forms/ticket/default
49%#
50<%ARGS>
51$id
52$changes => {}
53$fields => undef
54$args => undef
55</%ARGS>
56<%INIT>
57use MIME::Entity;
58use RT::Interface::REST;
59
60my $cf_spec = RT::Interface::REST->custom_field_spec(1);
61
62my @comments;
63my ($c, $o, $k, $e) = ("", [], {}, 0);
64my %data = %$changes;
65my $ticket = RT::Ticket->new($session{CurrentUser});
66my @dates = qw(Created Starts Started Due Resolved Told LastUpdated);
67my @people = qw(Requestors Cc AdminCc);
68my @create = qw(Queue Requestor Subject Cc AdminCc Owner Status Priority
69 InitialPriority FinalPriority TimeEstimated TimeWorked
70 TimeLeft Starts Started Due Resolved);
71my @simple = qw(Subject Status Priority Disabled TimeEstimated TimeWorked
72 TimeLeft InitialPriority FinalPriority);
73my %dates = map {lc $_ => $_} @dates;
74my %people = map {lc $_ => $_} @people;
75my %create = map {lc $_ => $_} @create;
76my %simple = map {lc $_ => $_} @simple;
77
78# Are we dealing with an existing ticket?
79if ($id ne 'new') {
80 $ticket->Load($id);
81 if (!$ticket->Id) {
82 return [ "# Ticket $id does not exist.", [], {}, 1 ];
83 }
84 elsif ( %data ) {
85 if ( $data{status} && $data{status} eq 'deleted' && ! grep { $_ ne 'id' && $_ ne 'status' } keys %data ) {
86 if ( !$ticket->CurrentUserHasRight('DeleteTicket') ) {
87 return [ "# You are not allowed to delete ticket $id.", [], {}, 1 ];
88 }
89 }
90 elsif ( !$ticket->CurrentUserHasRight('ModifyTicket') ) {
91 return [ "# You are not allowed to modify ticket $id.", [], {}, 1 ];
92 }
93 }
94 elsif (!$ticket->CurrentUserHasRight('ShowTicket')) {
95 return [ "# You are not allowed to display ticket $id.", [], {}, 1 ];
96 }
97}
98else {
99 if (!keys(%data)) {
100 # GET ticket/new: Return a suitable default form.
101 # We get defaults from queue/1 (XXX: What if it isn't there?).
102 my $due = RT::Date->new($session{CurrentUser});
103 my $queue = RT::Queue->new($session{CurrentUser});
104 my $starts = RT::Date->new($session{CurrentUser});
105 $queue->Load(1);
106 $due->SetToNow;
107 $due->AddDays($queue->DefaultDueIn) if $queue->DefaultDueIn;
108 $starts->SetToNow;
109
110 return [
111 "# Required: id, Queue",
112 [ qw(id Queue Requestor Subject Cc AdminCc Owner Status Priority
113 InitialPriority FinalPriority TimeEstimated Starts Due Text) ],
114 {
115 id => "ticket/new",
116 Queue => $queue->Name,
117 Requestor => $session{CurrentUser}->Name,
118 Subject => "",
119 Cc => [],
120 AdminCc => [],
121 Owner => "",
122 Status => "new",
123 Priority => $queue->InitialPriority,
124 InitialPriority => $queue->InitialPriority,
125 FinalPriority => $queue->FinalPriority,
126 TimeEstimated => 0,
127 Starts => $starts->ISO,
128 Due => $due->ISO,
129 Text => "",
130 },
131 0
132 ];
133 }
134 else {
135 # We'll create a new ticket, and fall through to set fields that
136 # can't be set in the call to Create().
137 my (%v, $text);
138
139 foreach my $k (keys %data) {
140 # flexibly parse any dates
141 if ($dates{lc $k}) {
142 my $time = RT::Date->new($session{CurrentUser});
143 $time->Set(Format => 'unknown', Value => $data{$k});
144 $data{$k} = $time->ISO;
145 }
146
147 if (exists $create{lc $k}) {
148 $v{$create{lc $k}} = delete $data{$k};
149 }
150 # Set custom field
151 elsif ($k =~ /^$cf_spec/) {
b5747ff2
MKG
152 my $key = $1 || $2;
153
154 my $cf = RT::CustomField->new( $session{CurrentUser} );
155 $cf->LoadByName( Name => $key, Queue => $data{Queue} || $v{Queue} );
156 unless ( $cf->id ) {
157 $cf->LoadByName( Name => $key, Queue => 0 );
158 }
159
160 if (not $cf->id) {
161 push @comments, "# Invalid custom field name ($key)";
84fb5b46
MKG
162 delete $data{$k};
163 next;
164 }
165 $v{"CustomField-".$cf->Id()} = delete $data{$k};
166 }
167 elsif (lc $k eq 'text') {
168 $text = delete $data{$k};
169 }
dab09ea8
MKG
170 elsif ( lc $k ne 'id' ) {
171 $e = 1;
172 push @$o, $k;
173 push(@comments, "# $k: Unknown field");
174 }
175 }
176
177 if ( $e ) {
178 unshift @comments, "# Could not create ticket.";
179 $k = \%data;
180 goto DONE;
84fb5b46
MKG
181 }
182
183 # people fields allow multiple values
184 $v{$_} = vsplit($v{$_}) foreach ( grep $create{lc $_}, @people );
185
186 if ($text) {
187 $v{MIMEObj} =
188 MIME::Entity->build(
189 From => $session{CurrentUser}->EmailAddress,
190 Subject => $v{Subject},
191 Data => $text
192 );
193 }
194
195 my($tid,$trid,$terr) = $ticket->Create(%v);
196 unless ($tid) {
197 push(@comments, "# Could not create ticket.");
198 push(@comments, "# " . $terr);
199 goto DONE;
200 }
201
202 delete $data{id};
203 $id = $ticket->Id;
204 push(@comments, "# Ticket $id created.");
205 # see if the hash is empty
206 goto DONE if ! keys(%data);
207 }
208}
209
210# Now we know we're dealing with an existing ticket.
211if (!keys(%data)) {
212 my ($time, $key, $val, @data);
213
214 push @data, [ id => "ticket/".$ticket->Id ];
215 push @data, [ Queue => $ticket->QueueObj->Name ]
216 if (!%$fields || exists $fields->{lc 'Queue'});
217 push @data, [ Owner => $ticket->OwnerObj->Name ]
218 if (!%$fields || exists $fields->{lc 'Owner'});
219 push @data, [ Creator => $ticket->CreatorObj->Name ]
220 if (!%$fields || exists $fields->{lc 'Creator'});
221
222 foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
223 next unless (!%$fields || (exists $fields->{lc $_}));
224 push @data, [$_ => $ticket->$_ ];
225 }
226
227 foreach $key (@people) {
228 next unless (!%$fields || (exists $fields->{lc $key}));
229 push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
230 }
231
232 $time = RT::Date->new ($session{CurrentUser});
233 foreach $key (@dates) {
234 next unless (!%$fields || (exists $fields->{lc $key}));
235 $time->Set(Format => 'sql', Value => $ticket->$key);
236 push @data, [ $key => $time->AsString ];
237 }
238
239 $time = RT::Date->new ($session{CurrentUser});
240 foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
241 next unless (!%$fields || (exists $fields->{lc $key}));
242 $val = $ticket->$key || 0;
243 $val = "$val minutes" if $val;
244 push @data, [ $key => $val ];
245 }
246
247 # Display custom fields
248 my $CustomFields = $ticket->CustomFields;
249 while (my $cf = $CustomFields->Next()) {
250 next unless !%$fields
251 || exists $fields->{"cf.{".lc($cf->Name)."}"}
252 || exists $fields->{"cf-".lc $cf->Name};
253
254 my $vals = $ticket->CustomFieldValues($cf->Id());
255 my @out = ();
256 if ( $cf->SingleValue ) {
257 my $v = $vals->Next;
258 push @out, $v->Content if $v;
259 }
260 else {
261 while (my $v = $vals->Next()) {
262 my $content = $v->Content;
263 $content =~ s/'/\\'/g;
264 if ( $v->Content =~ /,/ ) {
265 push @out, q{'} . $content . q{'};
266 }
267 else {
268 push @out, $content;
269 }
270 }
271 }
272 push @data, [ ('CF.{' . $cf->Name . '}') => join ',', @out ];
273 }
274
275 my %k = map {@$_} @data;
276 $o = [ map {$_->[0]} @data ];
277 $k = \%k;
278}
279else {
280 my ($get, $set, $key, $val, $n, $s);
281
282 foreach $key (keys %data) {
283 $val = $data{$key};
284 $key = lc $key;
285 $n = 1;
286
287 if (ref $val eq 'ARRAY') {
288 unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
289 $n = 0;
290 $s = "$key may have only one value.";
291 goto SET;
292 }
293 }
294
295 if ($key =~ /^queue$/i) {
296 next if $val eq $ticket->QueueObj->Name;
297 ($n, $s) = $ticket->SetQueue($val);
298 }
299 elsif ($key =~ /^owner$/i) {
300 next if $val eq $ticket->OwnerObj->Name;
301 ($n, $s) = $ticket->SetOwner($val);
302 }
303 elsif (exists $simple{$key}) {
304 $key = $simple{$key};
305 $set = "Set$key";
dab09ea8
MKG
306 my $current = $ticket->$key;
307 $current = '' unless defined $current;
84fb5b46 308
dab09ea8 309 next if ($val eq $current) or ($current =~ /^\d+$/ && $val =~ /^\d+$/ && $val == $current);
84fb5b46
MKG
310 ($n, $s) = $ticket->$set("$val");
311 }
312 elsif (exists $dates{$key}) {
313 $key = $dates{$key};
314
315 # We try to detect whether it should update a field by checking
316 # whether its current value equals the entered value. Since the
317 # LastUpdated field is automatically updated as other columns are
318 # changed, it is not properly skipped. Users cannot update this
319 # field anyway.
320 next if $key eq 'LastUpdated';
321
322 $set = "Set$key";
323
324 my $time = RT::Date->new($session{CurrentUser});
325 $time->Set(Format => 'sql', Value => $ticket->$key);
326 next if ($val =~ /^not set$/i || $val eq $time->AsString);
327
328 $time->Set(Format => 'unknown', Value => $val);
329 ($n, $s) = $ticket->$set($time->ISO);
330 }
331 elsif (exists $people{$key}) {
332 $key = $people{$key};
333 my ($p, @msgs);
334
335 my %new = map {$_=>1} @{ vsplit($val) };
336 my %old = map {$_=>1} $ticket->$key->MemberEmailAddresses;
337 my $type = $key eq 'Requestors' ? 'Requestor' : $key;
338
339 foreach $p (keys %old) {
340 unless (exists $new{$p}) {
341 ($s, $n) = $ticket->DeleteWatcher(Type => $type,
342 Email => $p);
343 push @msgs, [ $s, $n ];
344 }
345 }
346 foreach $p (keys %new) {
84fb5b46
MKG
347 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
348 ($s, $n) = $ticket->AddWatcher(Type => $type,
349 Email => $p);
350 push @msgs, [ $s, $n ];
351 }
352 }
353
354 $n = 1;
355 if (@msgs = grep {$_->[0] == 0} @msgs) {
356 $n = 0;
357 $s = join "\n", map {"# ".$_->[1]} @msgs;
358 $s =~ s/^# //;
359 }
360 }
361 # Set custom field
362 elsif ($key =~ /^$cf_spec/) {
84fb5b46 363 $key = $1 || $2;
b5747ff2
MKG
364
365 my $cf = RT::CustomField->new( $session{CurrentUser} );
366 $cf->LoadByName( Name => $key, Queue => $ticket->Queue );
367 unless ( $cf->id ) {
368 $cf->LoadByName( Name => $key, Queue => 0 );
369 }
370
371 if (not $cf->id) {
84fb5b46
MKG
372 $n = 0;
373 $s = "Unknown custom field.";
374 }
375 else {
376 my $vals = $ticket->CustomFieldValues($cf->id);
377
378 if ( $cf->SingleValue ) {
379 my $old = $vals->Next;
380 if ( $old ) {
381 if ( $val ne $old->Content ) {
382 $old->Delete;
383 ($n, $s) = $ticket->AddCustomFieldValue(
384 Field => $cf, Value => $val );
385 $s =~ s/^# // if defined $s;
386 }
387 }
388 else {
389 ($n, $s) = $ticket->AddCustomFieldValue(
390 Field => $cf, Value => $val );
391 $s =~ s/^# // if defined $s;
392 }
393 }
394 else {
395 my @new;
396 my ( $a, $b ) = split /\s*,\s*/, $val, 2;
397 while ($a) {
398 no warnings 'uninitialized';
399 if ( $a =~ /^'/ ) {
400 my $s = $a;
401 while ( $a !~ /'$/ || ( $a !~ /(\\\\)+'$/
402 && $a =~ /(\\)+'$/ ) ) {
403 ( $a, $b ) = split /\s*,\s*/, $b, 2;
404 $s .= ',' . $a;
405 }
406 $s =~ s/^'//;
407 $s =~ s/'$//;
408 $s =~ s/\\'/'/g;
409 push @new, $s;
410 }
411 elsif ( $a =~ /^q{/ ) {
412 my $s = $a;
413 while ( $a !~ /}$/ ) {
414 ( $a, $b ) = split /\s*,\s*/, $b, 2;
415 $s .= ',' . $a;
416 }
417 $s =~ s/^q{//;
418 $s =~ s/}//;
419 push @new, $s;
420 }
421 else {
422 push @new, $a;
423 }
424 ( $a, $b ) = split /\s*,\s*/, $b, 2;
425 }
426
427 my %new;
428 $new{$_}++ for @new;
429
430 while (my $v = $vals->Next()) {
431 my $c = $v->Content;
432 if ( $new{$c} ) {
433 $new{$c}--;
434 }
435 else {
436 $v->Delete();
437 }
438 }
439 for ( @new ) {
440 while ( $new{$_} && $new{$_}-- ) {
441 ($n, $s) = $ticket->AddCustomFieldValue(
442 Field => $cf, Value => $_ );
443 $s =~ s/^# // if defined $s;
444 }
445 }
446 }
447 }
448 }
449 elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator') {
450 $n = 0;
451 $s = "Unknown field.";
452 }
453
454 SET:
455 if ($n == 0) {
456 $e = 1;
457 push @comments, "# $key: $s";
458 unless (@$o) {
459 # move id forward
460 @$o = ("id", grep { $_ ne 'id' } keys %$changes);
461 $k = $changes;
462 }
463 }
464 }
465 push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
466}
467
468DONE:
469$c ||= join("\n", @comments) if @comments;
470return [$c, $o, $k, $e];
471
472</%INIT>