1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 %# <sales@bestpractical.com>
8 %# (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 %# CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 %# END BPS TAGGED BLOCK }}}
48 %# REST/1.0/Forms/ticket/default
58 use RT::Interface::REST;
60 my $cf_spec = RT::Interface::REST->custom_field_spec(1);
63 my ($c, $o, $k, $e) = ("", [], {}, 0);
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;
78 # Are we dealing with an existing ticket?
82 return [ "# Ticket $id does not exist.", [], {}, 1 ];
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 ];
90 elsif ( !$ticket->CurrentUserHasRight('ModifyTicket') ) {
91 return [ "# You are not allowed to modify ticket $id.", [], {}, 1 ];
94 elsif (!$ticket->CurrentUserHasRight('ShowTicket')) {
95 return [ "# You are not allowed to display ticket $id.", [], {}, 1 ];
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});
107 $due->AddDays($queue->DefaultDueIn) if $queue->DefaultDueIn;
111 "# Required: id, Queue",
112 [ qw(id Queue Requestor Subject Cc AdminCc Owner Status Priority
113 InitialPriority FinalPriority TimeEstimated Starts Due Text) ],
116 Queue => $queue->Name,
117 Requestor => $session{CurrentUser}->Name,
123 Priority => $queue->InitialPriority,
124 InitialPriority => $queue->InitialPriority,
125 FinalPriority => $queue->FinalPriority,
127 Starts => $starts->ISO,
135 # We'll create a new ticket, and fall through to set fields that
136 # can't be set in the call to Create().
139 foreach my $k (keys %data) {
140 # flexibly parse any dates
142 my $time = RT::Date->new($session{CurrentUser});
143 $time->Set(Format => 'unknown', Value => $data{$k});
144 $data{$k} = $time->ISO;
147 if (exists $create{lc $k}) {
148 $v{$create{lc $k}} = delete $data{$k};
151 elsif ($k =~ /^$cf_spec/) {
154 my $cf = RT::CustomField->new( $session{CurrentUser} );
155 $cf->LoadByName( Name => $key, Queue => $data{Queue} || $v{Queue} );
157 $cf->LoadByName( Name => $key, Queue => 0 );
161 push @comments, "# Invalid custom field name ($key)";
165 $v{"CustomField-".$cf->Id()} = delete $data{$k};
167 elsif (lc $k eq 'text') {
168 $text = delete $data{$k};
170 elsif ( $k !~ /^(?:id|requestors)$/i ) {
173 push(@comments, "# $k: Unknown field");
178 unshift @comments, "# Could not create ticket.";
183 # people fields allow multiple values
184 $v{$_} = vsplit($v{$_}) foreach ( grep $create{lc $_}, @people );
189 From => $session{CurrentUser}->EmailAddress,
190 Subject => $v{Subject},
192 'X-RT-Interface' => 'REST',
196 my($tid,$trid,$terr) = $ticket->Create(%v);
198 push(@comments, "# Could not create ticket.");
199 push(@comments, "# " . $terr);
205 push(@comments, "# Ticket $id created.");
206 # see if the hash is empty
207 goto DONE if ! keys(%data);
211 # Now we know we're dealing with an existing ticket.
213 my ($time, $key, $val, @data);
215 push @data, [ id => "ticket/".$ticket->Id ];
216 push @data, [ Queue => $ticket->QueueObj->Name ]
217 if (!%$fields || exists $fields->{lc 'Queue'});
218 push @data, [ Owner => $ticket->OwnerObj->Name ]
219 if (!%$fields || exists $fields->{lc 'Owner'});
220 push @data, [ Creator => $ticket->CreatorObj->Name ]
221 if (!%$fields || exists $fields->{lc 'Creator'});
223 foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
224 next unless (!%$fields || (exists $fields->{lc $_}));
225 push @data, [$_ => $ticket->$_ ];
228 foreach $key (@people) {
229 next unless (!%$fields || (exists $fields->{lc $key}));
230 push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
233 $time = RT::Date->new ($session{CurrentUser});
234 foreach $key (@dates) {
235 next unless (!%$fields || (exists $fields->{lc $key}));
236 $time->Set(Format => 'sql', Value => $ticket->$key);
237 push @data, [ $key => $time->AsString ];
240 $time = RT::Date->new ($session{CurrentUser});
241 foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
242 next unless (!%$fields || (exists $fields->{lc $key}));
243 $val = $ticket->$key || 0;
244 $val = "$val minutes" if $val;
245 push @data, [ $key => $val ];
248 # Display custom fields
249 my $CustomFields = $ticket->CustomFields;
250 while (my $cf = $CustomFields->Next()) {
251 next unless !%$fields
252 || exists $fields->{"cf.{".lc($cf->Name)."}"}
253 || exists $fields->{"cf-".lc $cf->Name};
255 my $vals = $ticket->CustomFieldValues($cf->Id());
257 if ( $cf->SingleValue ) {
259 push @out, $v->Content if $v;
262 while (my $v = $vals->Next()) {
263 my $content = $v->Content;
264 $content =~ s/'/\\'/g;
265 if ( $v->Content =~ /,/ ) {
266 push @out, q{'} . $content . q{'};
273 push @data, [ ('CF.{' . $cf->Name . '}') => join ',', @out ];
276 my %k = map {@$_} @data;
277 $o = [ map {$_->[0]} @data ];
281 my ($get, $set, $key, $val, $n, $s);
283 foreach $key (keys %data) {
288 if (ref $val eq 'ARRAY') {
289 unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
291 $s = "$key may have only one value.";
296 if ($key =~ /^queue$/i) {
297 next if $val eq $ticket->QueueObj->Name;
298 ($n, $s) = $ticket->SetQueue($val);
300 elsif ($key =~ /^owner$/i) {
301 next if $val eq $ticket->OwnerObj->Name;
302 ($n, $s) = $ticket->SetOwner($val);
304 elsif (exists $simple{$key}) {
305 $key = $simple{$key};
307 my $current = $ticket->$key;
308 $current = '' unless defined $current;
310 next if ($val eq $current) or ($current =~ /^\d+$/ && $val =~ /^\d+$/ && $val == $current);
311 ($n, $s) = $ticket->$set("$val");
313 elsif (exists $dates{$key}) {
316 # We try to detect whether it should update a field by checking
317 # whether its current value equals the entered value. Since the
318 # LastUpdated field is automatically updated as other columns are
319 # changed, it is not properly skipped. Users cannot update this
321 next if $key eq 'LastUpdated';
325 my $time = RT::Date->new($session{CurrentUser});
326 $time->Set(Format => 'sql', Value => $ticket->$key);
327 next if ($val =~ /^not set$/i || $val eq $time->AsString);
329 $time->Set(Format => 'unknown', Value => $val);
330 ($n, $s) = $ticket->$set($time->ISO);
332 elsif (exists $people{$key}) {
333 $key = $people{$key};
336 my %new = map {$_=>1} @{ vsplit($val) };
337 my %old = map {$_=>1} $ticket->$key->MemberEmailAddresses;
338 my $type = $key eq 'Requestors' ? 'Requestor' : $key;
340 foreach $p (keys %old) {
341 unless (exists $new{$p}) {
342 ($s, $n) = $ticket->DeleteWatcher(Type => $type,
344 push @msgs, [ $s, $n ];
347 foreach $p (keys %new) {
348 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
349 ($s, $n) = $ticket->AddWatcher(Type => $type,
351 push @msgs, [ $s, $n ];
356 if (@msgs = grep {$_->[0] == 0} @msgs) {
358 $s = join "\n", map {"# ".$_->[1]} @msgs;
363 elsif ($key =~ /^$cf_spec/) {
366 my $cf = RT::CustomField->new( $session{CurrentUser} );
367 $cf->LoadByName( Name => $key, Queue => $ticket->Queue );
369 $cf->LoadByName( Name => $key, Queue => 0 );
374 $s = "Unknown custom field.";
377 my $vals = $ticket->CustomFieldValues($cf->id);
379 if ( !defined $val || !length $val ) {
380 while ( my $val = $vals->Next ) {
381 ($n, $s) = $ticket->DeleteCustomFieldValue(
382 Field => $cf, ValueId => $val->id,
384 $s =~ s/^# // if defined $s;
387 elsif ( $cf->SingleValue ) {
388 my $old = $vals->Next;
390 if ( $val ne $old->Content ) {
391 ($n, $s) = $ticket->AddCustomFieldValue(
392 Field => $cf, Value => $val );
393 $s =~ s/^# // if defined $s;
397 ($n, $s) = $ticket->AddCustomFieldValue(
398 Field => $cf, Value => $val );
399 $s =~ s/^# // if defined $s;
404 my ( $a, $b ) = split /\s*,\s*/, $val, 2;
406 no warnings 'uninitialized';
409 while ( $a !~ /'$/ || ( $a !~ /(\\\\)+'$/
410 && $a =~ /(\\)+'$/ ) ) {
411 ( $a, $b ) = split /\s*,\s*/, $b, 2;
419 elsif ( $a =~ /^q{/ ) {
421 while ( $a !~ /}$/ ) {
422 ( $a, $b ) = split /\s*,\s*/, $b, 2;
432 ( $a, $b ) = split /\s*,\s*/, $b, 2;
438 while (my $v = $vals->Next()) {
444 $ticket->DeleteCustomFieldValue( Field => $cf, ValueId => $v->id );
448 while ( $new{$_} && $new{$_}-- ) {
449 ($n, $s) = $ticket->AddCustomFieldValue(
450 Field => $cf, Value => $_ );
451 $s =~ s/^# // if defined $s;
457 elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator') {
459 $s = "Unknown field.";
465 push @comments, "# $key: $s";
468 @$o = ("id", grep { $_ ne 'id' } keys %$changes);
473 push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
477 $c ||= join("\n", @comments) if @comments;
478 return [$c, $o, $k, $e];