]> git.uio.no Git - usit-rt.git/blob - share/html/REST/1.0/Forms/ticket/default
Initial commit 4.0.5-3
[usit-rt.git] / share / html / REST / 1.0 / Forms / ticket / default
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>
57 use MIME::Entity;
58 use RT::Interface::REST;
59
60 my $cf_spec = RT::Interface::REST->custom_field_spec(1);
61
62 my @comments;
63 my ($c, $o, $k, $e) = ("", [], {}, 0);
64 my %data   = %$changes;
65 my $ticket = RT::Ticket->new($session{CurrentUser});
66 my @dates  = qw(Created Starts Started Due Resolved Told LastUpdated);
67 my @people = qw(Requestors Cc AdminCc);
68 my @create = qw(Queue Requestor Subject Cc AdminCc Owner Status Priority
69                 InitialPriority FinalPriority TimeEstimated TimeWorked
70                 TimeLeft Starts Started Due Resolved);
71 my @simple = qw(Subject Status Priority Disabled TimeEstimated TimeWorked
72                 TimeLeft InitialPriority FinalPriority);
73 my %dates  = map {lc $_ => $_} @dates;
74 my %people = map {lc $_ => $_} @people;
75 my %create = map {lc $_ => $_} @create;
76 my %simple = map {lc $_ => $_} @simple;
77
78 # Are we dealing with an existing ticket?
79 if ($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 }
98 else {
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/) {
152                 my $cf = RT::CustomField->new( RT->SystemUser );
153                 my $cfk = $1 || $2;
154                 unless($cf->LoadByName( Name => $cfk )) {
155                     push @comments, "# Invalid custom field name ($cfk)";
156                     delete $data{$k};
157                     next;
158                 }
159                 $v{"CustomField-".$cf->Id()} = delete $data{$k};
160             }
161             elsif (lc $k eq 'text') {
162                 $text = delete $data{$k};
163             }
164         }
165
166         # people fields allow multiple values
167         $v{$_} = vsplit($v{$_}) foreach ( grep $create{lc $_}, @people );
168
169         if ($text) {
170             $v{MIMEObj} =
171                 MIME::Entity->build(
172                     From => $session{CurrentUser}->EmailAddress,
173                     Subject => $v{Subject},
174                     Data => $text
175                 );
176         }
177
178         my($tid,$trid,$terr) = $ticket->Create(%v);    
179         unless ($tid) {
180             push(@comments, "# Could not create ticket.");
181             push(@comments, "# " . $terr);
182             goto DONE;
183         }
184
185         delete $data{id};
186         $id = $ticket->Id;
187         push(@comments, "# Ticket $id created.");
188         # see if the hash is empty
189         goto DONE if ! keys(%data);
190     }
191 }
192
193 # Now we know we're dealing with an existing ticket.
194 if (!keys(%data)) {
195     my ($time, $key, $val, @data);
196
197     push @data, [ id    => "ticket/".$ticket->Id   ];
198     push @data, [ Queue => $ticket->QueueObj->Name ] 
199         if (!%$fields || exists $fields->{lc 'Queue'});
200     push @data, [ Owner => $ticket->OwnerObj->Name ]
201         if (!%$fields || exists $fields->{lc 'Owner'});
202     push @data, [ Creator => $ticket->CreatorObj->Name ]
203         if (!%$fields || exists $fields->{lc 'Creator'});
204
205     foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
206         next unless (!%$fields || (exists $fields->{lc $_}));
207         push @data, [$_ => $ticket->$_ ];
208     }
209
210     foreach $key (@people) {
211         next unless (!%$fields || (exists $fields->{lc $key}));
212         push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
213     }
214
215     $time = RT::Date->new ($session{CurrentUser});
216     foreach $key (@dates) {
217         next unless (!%$fields || (exists $fields->{lc $key}));
218         $time->Set(Format => 'sql', Value => $ticket->$key);
219         push @data, [ $key => $time->AsString ];
220     }
221
222     $time = RT::Date->new ($session{CurrentUser});
223     foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
224         next unless (!%$fields || (exists $fields->{lc $key}));
225         $val = $ticket->$key || 0;
226         $val = "$val minutes" if $val;
227         push @data, [ $key => $val ];
228     }
229
230     # Display custom fields
231     my $CustomFields = $ticket->CustomFields;
232     while (my $cf = $CustomFields->Next()) {
233         next unless !%$fields
234                  || exists $fields->{"cf.{".lc($cf->Name)."}"}
235                  || exists $fields->{"cf-".lc $cf->Name};
236
237         my $vals = $ticket->CustomFieldValues($cf->Id());
238         my @out = ();
239         if ( $cf->SingleValue ) {
240             my $v = $vals->Next;
241             push @out, $v->Content if $v;
242         }
243         else {
244             while (my $v = $vals->Next()) {
245                 my $content = $v->Content;
246                 $content =~ s/'/\\'/g;
247                 if ( $v->Content =~ /,/ ) {
248                     push @out, q{'} . $content . q{'};
249                 }
250                 else {
251                     push @out, $content;
252                 }
253             }
254         }
255         push @data, [ ('CF.{' . $cf->Name . '}') => join ',', @out ];
256     }
257
258     my %k = map {@$_} @data;
259     $o = [ map {$_->[0]} @data ];
260     $k = \%k;
261 }
262 else {
263     my ($get, $set, $key, $val, $n, $s);
264
265     foreach $key (keys %data) {
266         $val = $data{$key};
267         $key = lc $key;
268         $n = 1;
269
270         if (ref $val eq 'ARRAY') {
271             unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
272                 $n = 0;
273                 $s = "$key may have only one value.";
274                 goto SET;
275             }
276         }
277
278         if ($key =~ /^queue$/i) {
279             next if $val eq $ticket->QueueObj->Name;
280             ($n, $s) = $ticket->SetQueue($val);
281         }
282         elsif ($key =~ /^owner$/i) {
283             next if $val eq $ticket->OwnerObj->Name;
284             ($n, $s) = $ticket->SetOwner($val);
285         }
286         elsif (exists $simple{$key}) {
287             $key = $simple{$key};
288             $set = "Set$key";
289
290             next if (($val eq ($ticket->$key||''))|| ($ticket->$key =~ /^\d+$/ && $val =~ /^\d+$/ && $val == $ticket->$key));
291             ($n, $s) = $ticket->$set("$val");
292         }
293         elsif (exists $dates{$key}) {
294             $key = $dates{$key};
295
296             # We try to detect whether it should update a field by checking
297             # whether its current value equals the entered value. Since the
298             # LastUpdated field is automatically updated as other columns are
299             # changed, it is not properly skipped. Users cannot update this
300             # field anyway.
301             next if $key eq 'LastUpdated';
302
303             $set = "Set$key";
304
305             my $time = RT::Date->new($session{CurrentUser});
306             $time->Set(Format => 'sql', Value => $ticket->$key);
307             next if ($val =~ /^not set$/i || $val eq $time->AsString);
308
309             $time->Set(Format => 'unknown', Value => $val);
310             ($n, $s) = $ticket->$set($time->ISO);
311         }
312         elsif (exists $people{$key}) {
313             $key = $people{$key};
314             my ($p, @msgs);
315
316             my %new  = map {$_=>1} @{ vsplit($val) };
317             my %old  = map {$_=>1} $ticket->$key->MemberEmailAddresses;
318             my $type = $key eq 'Requestors' ? 'Requestor' : $key;
319
320             foreach $p (keys %old) {
321                 unless (exists $new{$p}) {
322                     ($s, $n) = $ticket->DeleteWatcher(Type => $type,
323                                                       Email => $p);
324                     push @msgs, [ $s, $n ];
325                 }
326             }
327             foreach $p (keys %new) {
328                 # XXX: This is a stupid test.
329                 unless ($p =~ /^[\w.+-]+\@([\w.-]+\.)*\w+.?$/) {
330                     $s = 0;
331                     $n = "$p is not a valid email address.";
332                     push @msgs, [ $s, $n ];
333                     next;
334                 }
335                 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
336                     ($s, $n) = $ticket->AddWatcher(Type => $type,
337                                                    Email => $p);
338                     push @msgs, [ $s, $n ];
339                 }
340             }
341
342             $n = 1;
343             if (@msgs = grep {$_->[0] == 0} @msgs) {
344                 $n = 0;
345                 $s = join "\n", map {"# ".$_->[1]} @msgs;
346                 $s =~ s/^# //;
347             }
348         }
349         # Set custom field
350         elsif ($key =~ /^$cf_spec/) {
351             my $cf = RT::CustomField->new( RT->SystemUser );
352             $key = $1 || $2;
353             if (not $cf->LoadByName( Name => $key )) {
354                 $n = 0;
355                 $s = "Unknown custom field.";
356             }
357             else {
358                 my $vals = $ticket->CustomFieldValues($cf->id);
359
360                 if ( $cf->SingleValue ) {
361                     my $old = $vals->Next;
362                     if ( $old ) {
363                         if ( $val ne $old->Content ) {
364                             $old->Delete;
365                             ($n, $s) = $ticket->AddCustomFieldValue(
366                                  Field => $cf, Value => $val );
367                             $s =~ s/^# // if defined $s;
368                         }
369                     }
370                     else {
371                         ($n, $s) = $ticket->AddCustomFieldValue(
372                              Field => $cf, Value => $val );
373                         $s =~ s/^# // if defined $s;
374                     }
375                 }
376                 else {
377                     my @new;
378                     my ( $a, $b ) = split /\s*,\s*/, $val, 2;
379                     while ($a) {
380                         no warnings 'uninitialized';
381                         if ( $a =~ /^'/ ) {
382                             my $s = $a;
383                             while ( $a !~ /'$/ || ( $a !~ /(\\\\)+'$/
384                                             && $a =~ /(\\)+'$/ ) ) {
385                                 ( $a, $b ) = split /\s*,\s*/, $b, 2;
386                                 $s .= ',' . $a;
387                             }
388                             $s =~ s/^'//;
389                             $s =~ s/'$//;
390                             $s =~ s/\\'/'/g;
391                             push @new, $s;
392                         }
393                         elsif ( $a =~ /^q{/ ) {
394                             my $s = $a;
395                             while ( $a !~ /}$/ ) {
396                                 ( $a, $b ) = split /\s*,\s*/, $b, 2;
397                                 $s .= ',' . $a;
398                             }
399                             $s =~ s/^q{//;
400                             $s =~ s/}//;
401                             push @new, $s;
402                         }
403                         else {
404                             push @new, $a;
405                         }
406                         ( $a, $b ) = split /\s*,\s*/, $b, 2;
407                     }
408
409                     my %new;
410                     $new{$_}++ for @new;
411
412                     while (my $v = $vals->Next()) {
413                         my $c = $v->Content;
414                         if ( $new{$c} ) {
415                             $new{$c}--;
416                         }
417                         else {
418                             $v->Delete();
419                         }
420                     }
421                     for ( @new ) {
422                         while ( $new{$_} && $new{$_}-- ) {
423                             ($n, $s) = $ticket->AddCustomFieldValue(
424                                 Field => $cf, Value => $_ );
425                             $s =~ s/^# // if defined $s;
426                         }
427                     }
428                 }
429             }
430         }
431         elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator') {
432             $n = 0;
433             $s = "Unknown field.";
434         }
435
436     SET:
437         if ($n == 0) {
438             $e = 1;
439             push @comments, "# $key: $s";
440             unless (@$o) {
441                 # move id forward
442                 @$o = ("id", grep { $_ ne 'id' } keys %$changes);
443                 $k = $changes;
444             }
445         }
446     }
447     push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
448 }
449
450 DONE:
451 $c ||= join("\n", @comments) if @comments;
452 return [$c, $o, $k, $e];
453
454 </%INIT>