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