1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2014 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 Content-Type);
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} && lc $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 Attachment 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,
136 # We'll create a new ticket, and fall through to set fields that
137 # can't be set in the call to Create().
138 my (%v, $text, @atts);
140 foreach my $k (keys %data) {
141 # flexibly parse any dates
143 my $time = RT::Date->new($session{CurrentUser});
144 $time->Set(Format => 'unknown', Value => $data{$k});
145 $data{$k} = $time->ISO;
148 if (exists $create{lc $k}) {
149 $v{$create{lc $k}} = delete $data{$k};
152 elsif ($k =~ /^$cf_spec/) {
155 my $cf = RT::CustomField->new( $session{CurrentUser} );
156 $cf->LoadByName( Name => $key, Queue => $data{Queue} || $v{Queue} );
158 $cf->LoadByName( Name => $key, Queue => 0 );
162 push @comments, "# Invalid custom field name ($key)";
166 $v{"CustomField-".$cf->Id()} = delete $data{$k};
168 elsif (lc $k eq 'text') {
169 $text = delete $data{$k};
171 elsif (lc $k eq 'attachment') {
172 push @atts, @{ vsplit(delete $data{$k}) };
174 elsif ( $k !~ /^(?:id|requestors)$/i ) {
177 push(@comments, "# $k: Unknown field");
182 unshift @comments, "# Could not create ticket.";
187 # people fields allow multiple values
188 $v{$_} = vsplit($v{$_}) foreach ( grep $create{lc $_}, @people );
190 if ($text || @atts) {
193 Type => "multipart/mixed",
194 From => $session{CurrentUser}->EmailAddress,
195 Subject => $v{Subject},
196 'X-RT-Interface' => 'REST',
200 'Content-Type' => $v{'Content-Type'} || 'text/plain',
202 my ($status, $msg) = process_attachments($v{'MIMEObj'}, @atts);
204 push(@comments, "# $msg");
207 $v{MIMEObj}->make_singlepart;
210 my($tid,$trid,$terr) = $ticket->Create(%v);
212 push(@comments, "# Could not create ticket.");
213 push(@comments, "# " . $terr);
219 push(@comments, "# Ticket $id created.");
220 # see if the hash is empty
221 goto DONE if ! keys(%data);
225 # Now we know we're dealing with an existing ticket.
227 my ($time, $key, $val, @data);
229 push @data, [ id => "ticket/".$ticket->Id ];
230 push @data, [ Queue => $ticket->QueueObj->Name ]
231 if (!%$fields || exists $fields->{lc 'Queue'});
232 push @data, [ Owner => $ticket->OwnerObj->Name ]
233 if (!%$fields || exists $fields->{lc 'Owner'});
234 push @data, [ Creator => $ticket->CreatorObj->Name ]
235 if (!%$fields || exists $fields->{lc 'Creator'});
237 foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
238 next unless (!%$fields || (exists $fields->{lc $_}));
239 push @data, [$_ => $ticket->$_ ];
242 foreach $key (@people) {
243 next unless (!%$fields || (exists $fields->{lc $key}));
244 push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
247 $time = RT::Date->new ($session{CurrentUser});
248 foreach $key (@dates) {
249 next unless (!%$fields || (exists $fields->{lc $key}));
250 $time->Set(Format => 'sql', Value => $ticket->$key);
251 push @data, [ $key => $time->AsString ];
254 $time = RT::Date->new ($session{CurrentUser});
255 foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
256 next unless (!%$fields || (exists $fields->{lc $key}));
257 $val = $ticket->$key || 0;
258 $val = "$val minutes" if $val;
259 push @data, [ $key => $val ];
262 # Display custom fields
263 my $CustomFields = $ticket->CustomFields;
264 while (my $cf = $CustomFields->Next()) {
265 next unless !%$fields
266 || exists $fields->{"cf.{".lc($cf->Name)."}"}
267 || exists $fields->{"cf-".lc $cf->Name};
269 my $vals = $ticket->CustomFieldValues($cf->Id());
271 if ( $cf->SingleValue ) {
273 push @out, $v->Content if $v;
276 while (my $v = $vals->Next()) {
277 my $content = $v->Content;
278 $content =~ s/'/\\'/g;
279 if ( $v->Content =~ /,/ ) {
280 push @out, q{'} . $content . q{'};
287 push @data, [ ('CF.{' . $cf->Name . '}') => join ',', @out ];
290 my %k = map {@$_} @data;
291 $o = [ map {$_->[0]} @data ];
295 my ($get, $set, $key, $val, $n, $s);
298 foreach $key (keys %data) {
303 if (ref $val eq 'ARRAY') {
304 unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
306 $s = "$key may have only one value.";
311 if ($key =~ /^queue$/i) {
312 next if $val eq $ticket->QueueObj->Name;
313 ($n, $s) = $ticket->SetQueue($val);
315 elsif ($key =~ /^owner$/i) {
316 next if $val eq $ticket->OwnerObj->Name;
317 ($n, $s) = $ticket->SetOwner($val);
319 elsif (exists $simple{$key}) {
320 $key = $simple{$key};
322 my $current = $ticket->$key;
323 $current = '' unless defined $current;
325 next if ($val eq $current) or ($current =~ /^\d+$/ && $val =~ /^\d+$/ && $val == $current);
326 ($n, $s) = $ticket->$set("$val");
328 elsif (exists $dates{$key}) {
331 # We try to detect whether it should update a field by checking
332 # whether its current value equals the entered value. Since the
333 # LastUpdated field is automatically updated as other columns are
334 # changed, it is not properly skipped. Users cannot update this
336 next if $key eq 'LastUpdated';
340 my $time = RT::Date->new($session{CurrentUser});
341 $time->Set(Format => 'sql', Value => $ticket->$key);
342 next if ($val =~ /^not set$/i || $val eq $time->AsString);
344 $time->Set(Format => 'unknown', Value => $val);
345 ($n, $s) = $ticket->$set($time->ISO);
347 elsif (exists $people{$key}) {
348 $key = $people{$key};
351 my %new = map {$_=>1} @{ vsplit($val) };
352 my %old = map {$_=>1} $ticket->$key->MemberEmailAddresses;
353 my $type = $key eq 'Requestors' ? 'Requestor' : $key;
355 foreach $p (keys %old) {
356 unless (exists $new{$p}) {
357 ($s, $n) = $ticket->DeleteWatcher(Type => $type,
359 push @msgs, [ $s, $n ];
362 foreach $p (keys %new) {
363 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
364 ($s, $n) = $ticket->AddWatcher(Type => $type,
366 push @msgs, [ $s, $n ];
371 if (@msgs = grep {$_->[0] == 0} @msgs) {
373 $s = join "\n", map {"# ".$_->[1]} @msgs;
378 elsif ($key =~ /^$cf_spec/) {
381 my $cf = RT::CustomField->new( $session{CurrentUser} );
382 $cf->LoadByName( Name => $key, Queue => $ticket->Queue );
384 $cf->LoadByName( Name => $key, Queue => 0 );
389 $s = "Unknown custom field.";
392 my $vals = $ticket->CustomFieldValues($cf->id);
394 if ( !defined $val || !length $val ) {
395 while ( my $val = $vals->Next ) {
396 ($n, $s) = $ticket->DeleteCustomFieldValue(
397 Field => $cf, ValueId => $val->id,
399 $s =~ s/^# // if defined $s;
402 elsif ( $cf->SingleValue ) {
403 my $old = $vals->Next;
405 if ( $val ne $old->Content ) {
406 ($n, $s) = $ticket->AddCustomFieldValue(
407 Field => $cf, Value => $val );
408 $s =~ s/^# // if defined $s;
412 ($n, $s) = $ticket->AddCustomFieldValue(
413 Field => $cf, Value => $val );
414 $s =~ s/^# // if defined $s;
419 my ( $a, $b ) = split /\s*,\s*/, $val, 2;
421 no warnings 'uninitialized';
424 while ( $a !~ /'$/ || ( $a !~ /(\\\\)+'$/
425 && $a =~ /(\\)+'$/ ) ) {
426 ( $a, $b ) = split /\s*,\s*/, $b, 2;
434 elsif ( $a =~ /^q\{/ ) {
436 while ( $a !~ /\}$/ ) {
437 ( $a, $b ) = split /\s*,\s*/, $b, 2;
447 ( $a, $b ) = split /\s*,\s*/, $b, 2;
453 while (my $v = $vals->Next()) {
459 $ticket->DeleteCustomFieldValue( Field => $cf, ValueId => $v->id );
463 while ( $new{$_} && $new{$_}-- ) {
464 ($n, $s) = $ticket->AddCustomFieldValue(
465 Field => $cf, Value => $_ );
466 $s =~ s/^# // if defined $s;
472 elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator' && $key ne 'content-type' ) {
474 $s = "Unknown field.";
480 push @comments, "# $key: $s";
483 @$o = ("id", grep { $_ ne 'id' } keys %$changes);
491 push(@comments, "# Ticket ".$ticket->id." updated.") if $updated;
495 $c ||= join("\n", @comments) if @comments;
496 return [$c, $o, $k, $e];