Master to 4.2.8
[usit-rt.git] / share / html / REST / 1.0 / Forms / ticket / default
CommitLineData
84fb5b46
MKG
1%# BEGIN BPS TAGGED BLOCK {{{
2%#
3%# COPYRIGHT:
4%#
320f0092 5%# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
84fb5b46
MKG
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
01e3b242 70 TimeLeft Starts Started Due Resolved Content-Type);
84fb5b46
MKG
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 ) {
01e3b242 85 if ( $data{status} && lc $data{status} eq 'deleted' && ! grep { $_ ne 'id' && $_ ne 'status' } keys %data ) {
84fb5b46
MKG
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
01e3b242 113 InitialPriority FinalPriority TimeEstimated Starts Due Attachment Text) ],
84fb5b46
MKG
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,
01e3b242 129 Attachment => '',
84fb5b46
MKG
130 Text => "",
131 },
132 0
133 ];
134 }
135 else {
136 # We'll create a new ticket, and fall through to set fields that
137 # can't be set in the call to Create().
01e3b242 138 my (%v, $text, @atts);
84fb5b46
MKG
139
140 foreach my $k (keys %data) {
141 # flexibly parse any dates
142 if ($dates{lc $k}) {
143 my $time = RT::Date->new($session{CurrentUser});
144 $time->Set(Format => 'unknown', Value => $data{$k});
145 $data{$k} = $time->ISO;
146 }
147
148 if (exists $create{lc $k}) {
149 $v{$create{lc $k}} = delete $data{$k};
150 }
151 # Set custom field
152 elsif ($k =~ /^$cf_spec/) {
b5747ff2
MKG
153 my $key = $1 || $2;
154
155 my $cf = RT::CustomField->new( $session{CurrentUser} );
c33a4027
MKG
156 $cf->LoadByName(
157 Name => $key,
158 LookupType => RT::Ticket->CustomFieldLookupType,
159 ObjectId => $data{Queue} || $v{Queue},
160 IncludeGlobal => 1,
161 );
b5747ff2
MKG
162
163 if (not $cf->id) {
164 push @comments, "# Invalid custom field name ($key)";
84fb5b46
MKG
165 delete $data{$k};
166 next;
167 }
168 $v{"CustomField-".$cf->Id()} = delete $data{$k};
169 }
170 elsif (lc $k eq 'text') {
171 $text = delete $data{$k};
172 }
01e3b242
MKG
173 elsif (lc $k eq 'attachment') {
174 push @atts, @{ vsplit(delete $data{$k}) };
175 }
403d7b0b 176 elsif ( $k !~ /^(?:id|requestors)$/i ) {
dab09ea8
MKG
177 $e = 1;
178 push @$o, $k;
179 push(@comments, "# $k: Unknown field");
180 }
181 }
182
183 if ( $e ) {
184 unshift @comments, "# Could not create ticket.";
185 $k = \%data;
186 goto DONE;
84fb5b46
MKG
187 }
188
189 # people fields allow multiple values
190 $v{$_} = vsplit($v{$_}) foreach ( grep $create{lc $_}, @people );
191
01e3b242 192 if ($text || @atts) {
84fb5b46
MKG
193 $v{MIMEObj} =
194 MIME::Entity->build(
01e3b242 195 Type => "multipart/mixed",
c33a4027
MKG
196 From => Encode::encode( "UTF-8", $session{CurrentUser}->EmailAddress ),
197 Subject => Encode::encode( "UTF-8", $v{Subject}),
403d7b0b 198 'X-RT-Interface' => 'REST',
84fb5b46 199 );
01e3b242 200 $v{MIMEObj}->attach(
c33a4027
MKG
201 Type => $v{'Content-Type'} || 'text/plain',
202 Charset => "UTF-8",
203 Data => Encode::encode( "UTF-8", $text ),
01e3b242
MKG
204 ) if $text;
205 my ($status, $msg) = process_attachments($v{'MIMEObj'}, @atts);
206 unless ($status) {
207 push(@comments, "# $msg");
208 goto DONE;
209 }
210 $v{MIMEObj}->make_singlepart;
84fb5b46
MKG
211 }
212
213 my($tid,$trid,$terr) = $ticket->Create(%v);
214 unless ($tid) {
215 push(@comments, "# Could not create ticket.");
216 push(@comments, "# " . $terr);
217 goto DONE;
218 }
219
220 delete $data{id};
221 $id = $ticket->Id;
222 push(@comments, "# Ticket $id created.");
223 # see if the hash is empty
224 goto DONE if ! keys(%data);
225 }
226}
227
228# Now we know we're dealing with an existing ticket.
229if (!keys(%data)) {
230 my ($time, $key, $val, @data);
231
232 push @data, [ id => "ticket/".$ticket->Id ];
af59614d
MKG
233 push @data, [ Queue => $ticket->QueueObj->Name ]
234 if (!%$fields || exists $fields->{lc 'Queue'});
84fb5b46 235 push @data, [ Owner => $ticket->OwnerObj->Name ]
af59614d 236 if (!%$fields || exists $fields->{lc 'Owner'});
84fb5b46 237 push @data, [ Creator => $ticket->CreatorObj->Name ]
af59614d 238 if (!%$fields || exists $fields->{lc 'Creator'});
84fb5b46
MKG
239
240 foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
af59614d 241 next unless (!%$fields || (exists $fields->{lc $_}));
84fb5b46
MKG
242 push @data, [$_ => $ticket->$_ ];
243 }
244
245 foreach $key (@people) {
246 next unless (!%$fields || (exists $fields->{lc $key}));
247 push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
248 }
249
250 $time = RT::Date->new ($session{CurrentUser});
251 foreach $key (@dates) {
af59614d 252 next unless (!%$fields || (exists $fields->{lc $key}));
84fb5b46
MKG
253 $time->Set(Format => 'sql', Value => $ticket->$key);
254 push @data, [ $key => $time->AsString ];
255 }
256
257 $time = RT::Date->new ($session{CurrentUser});
258 foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
af59614d 259 next unless (!%$fields || (exists $fields->{lc $key}));
84fb5b46
MKG
260 $val = $ticket->$key || 0;
261 $val = "$val minutes" if $val;
262 push @data, [ $key => $val ];
263 }
264
265 # Display custom fields
266 my $CustomFields = $ticket->CustomFields;
267 while (my $cf = $CustomFields->Next()) {
268 next unless !%$fields
269 || exists $fields->{"cf.{".lc($cf->Name)."}"}
270 || exists $fields->{"cf-".lc $cf->Name};
271
272 my $vals = $ticket->CustomFieldValues($cf->Id());
273 my @out = ();
274 if ( $cf->SingleValue ) {
275 my $v = $vals->Next;
276 push @out, $v->Content if $v;
277 }
278 else {
279 while (my $v = $vals->Next()) {
280 my $content = $v->Content;
281 $content =~ s/'/\\'/g;
282 if ( $v->Content =~ /,/ ) {
283 push @out, q{'} . $content . q{'};
284 }
285 else {
286 push @out, $content;
287 }
288 }
289 }
290 push @data, [ ('CF.{' . $cf->Name . '}') => join ',', @out ];
291 }
292
293 my %k = map {@$_} @data;
294 $o = [ map {$_->[0]} @data ];
295 $k = \%k;
296}
297else {
298 my ($get, $set, $key, $val, $n, $s);
01e3b242 299 my $updated;
84fb5b46
MKG
300
301 foreach $key (keys %data) {
302 $val = $data{$key};
303 $key = lc $key;
304 $n = 1;
305
306 if (ref $val eq 'ARRAY') {
307 unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
308 $n = 0;
309 $s = "$key may have only one value.";
310 goto SET;
311 }
312 }
313
314 if ($key =~ /^queue$/i) {
315 next if $val eq $ticket->QueueObj->Name;
316 ($n, $s) = $ticket->SetQueue($val);
317 }
318 elsif ($key =~ /^owner$/i) {
319 next if $val eq $ticket->OwnerObj->Name;
320 ($n, $s) = $ticket->SetOwner($val);
321 }
322 elsif (exists $simple{$key}) {
323 $key = $simple{$key};
324 $set = "Set$key";
dab09ea8
MKG
325 my $current = $ticket->$key;
326 $current = '' unless defined $current;
84fb5b46 327
dab09ea8 328 next if ($val eq $current) or ($current =~ /^\d+$/ && $val =~ /^\d+$/ && $val == $current);
84fb5b46
MKG
329 ($n, $s) = $ticket->$set("$val");
330 }
331 elsif (exists $dates{$key}) {
332 $key = $dates{$key};
333
334 # We try to detect whether it should update a field by checking
335 # whether its current value equals the entered value. Since the
336 # LastUpdated field is automatically updated as other columns are
337 # changed, it is not properly skipped. Users cannot update this
338 # field anyway.
339 next if $key eq 'LastUpdated';
340
341 $set = "Set$key";
342
343 my $time = RT::Date->new($session{CurrentUser});
344 $time->Set(Format => 'sql', Value => $ticket->$key);
345 next if ($val =~ /^not set$/i || $val eq $time->AsString);
346
347 $time->Set(Format => 'unknown', Value => $val);
348 ($n, $s) = $ticket->$set($time->ISO);
349 }
350 elsif (exists $people{$key}) {
351 $key = $people{$key};
352 my ($p, @msgs);
353
354 my %new = map {$_=>1} @{ vsplit($val) };
355 my %old = map {$_=>1} $ticket->$key->MemberEmailAddresses;
356 my $type = $key eq 'Requestors' ? 'Requestor' : $key;
357
358 foreach $p (keys %old) {
359 unless (exists $new{$p}) {
360 ($s, $n) = $ticket->DeleteWatcher(Type => $type,
361 Email => $p);
362 push @msgs, [ $s, $n ];
363 }
364 }
365 foreach $p (keys %new) {
84fb5b46
MKG
366 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
367 ($s, $n) = $ticket->AddWatcher(Type => $type,
368 Email => $p);
369 push @msgs, [ $s, $n ];
370 }
371 }
372
373 $n = 1;
374 if (@msgs = grep {$_->[0] == 0} @msgs) {
375 $n = 0;
376 $s = join "\n", map {"# ".$_->[1]} @msgs;
377 $s =~ s/^# //;
378 }
379 }
380 # Set custom field
381 elsif ($key =~ /^$cf_spec/) {
84fb5b46 382 $key = $1 || $2;
b5747ff2
MKG
383
384 my $cf = RT::CustomField->new( $session{CurrentUser} );
c33a4027
MKG
385 $cf->ContextObject( $ticket );
386 $cf->LoadByName(
387 Name => $key,
388 LookupType => RT::Ticket->CustomFieldLookupType,
389 ObjectId => $ticket->Queue,
390 IncludeGlobal => 1,
391 );
b5747ff2
MKG
392
393 if (not $cf->id) {
84fb5b46
MKG
394 $n = 0;
395 $s = "Unknown custom field.";
396 }
397 else {
398 my $vals = $ticket->CustomFieldValues($cf->id);
399
403d7b0b
MKG
400 if ( !defined $val || !length $val ) {
401 while ( my $val = $vals->Next ) {
402 ($n, $s) = $ticket->DeleteCustomFieldValue(
403 Field => $cf, ValueId => $val->id,
404 );
405 $s =~ s/^# // if defined $s;
406 }
407 }
408 elsif ( $cf->SingleValue ) {
af59614d
MKG
409 ($n, $s) = $ticket->AddCustomFieldValue(
410 Field => $cf, Value => $val );
411 $s =~ s/^# // if defined $s;
84fb5b46
MKG
412 }
413 else {
414 my @new;
415 my ( $a, $b ) = split /\s*,\s*/, $val, 2;
416 while ($a) {
417 no warnings 'uninitialized';
418 if ( $a =~ /^'/ ) {
419 my $s = $a;
420 while ( $a !~ /'$/ || ( $a !~ /(\\\\)+'$/
421 && $a =~ /(\\)+'$/ ) ) {
422 ( $a, $b ) = split /\s*,\s*/, $b, 2;
423 $s .= ',' . $a;
424 }
425 $s =~ s/^'//;
426 $s =~ s/'$//;
427 $s =~ s/\\'/'/g;
428 push @new, $s;
429 }
01e3b242 430 elsif ( $a =~ /^q\{/ ) {
84fb5b46 431 my $s = $a;
01e3b242 432 while ( $a !~ /\}$/ ) {
84fb5b46
MKG
433 ( $a, $b ) = split /\s*,\s*/, $b, 2;
434 $s .= ',' . $a;
435 }
01e3b242
MKG
436 $s =~ s/^q\{//;
437 $s =~ s/\}//;
84fb5b46
MKG
438 push @new, $s;
439 }
440 else {
441 push @new, $a;
442 }
443 ( $a, $b ) = split /\s*,\s*/, $b, 2;
444 }
445
446 my %new;
447 $new{$_}++ for @new;
448
449 while (my $v = $vals->Next()) {
450 my $c = $v->Content;
451 if ( $new{$c} ) {
452 $new{$c}--;
453 }
454 else {
403d7b0b 455 $ticket->DeleteCustomFieldValue( Field => $cf, ValueId => $v->id );
84fb5b46
MKG
456 }
457 }
458 for ( @new ) {
459 while ( $new{$_} && $new{$_}-- ) {
460 ($n, $s) = $ticket->AddCustomFieldValue(
461 Field => $cf, Value => $_ );
462 $s =~ s/^# // if defined $s;
463 }
464 }
465 }
466 }
467 }
01e3b242 468 elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator' && $key ne 'content-type' ) {
84fb5b46
MKG
469 $n = 0;
470 $s = "Unknown field.";
471 }
472
473 SET:
474 if ($n == 0) {
475 $e = 1;
476 push @comments, "# $key: $s";
477 unless (@$o) {
478 # move id forward
479 @$o = ("id", grep { $_ ne 'id' } keys %$changes);
480 $k = $changes;
481 }
482 }
01e3b242
MKG
483 else {
484 $updated ||= 1;
485 }
84fb5b46 486 }
01e3b242 487 push(@comments, "# Ticket ".$ticket->id." updated.") if $updated;
84fb5b46
MKG
488}
489
490DONE:
491$c ||= join("\n", @comments) if @comments;
492return [$c, $o, $k, $e];
493
494</%INIT>