1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2011 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 = new RT::Ticket $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 ];
84 elsif (!$ticket->CurrentUserHasRight('ShowTicket') ||
85 (%data && !$ticket->CurrentUserHasRight('ModifyTicket')))
87 my $act = %data ? "modify" : "display";
88 return [ "# You are not allowed to $act ticket $id.", [], {}, 1 ];
93 # GET ticket/new: Return a suitable default form.
94 # We get defaults from queue/1 (XXX: What if it isn't there?).
95 my $due = new RT::Date $session{CurrentUser};
96 my $queue = new RT::Queue $session{CurrentUser};
97 my $starts = new RT::Date $session{CurrentUser};
100 $due->AddDays($queue->DefaultDueIn) if $queue->DefaultDueIn;
104 "# Required: id, Queue",
105 [ qw(id Queue Requestor Subject Cc AdminCc Owner Status Priority
106 InitialPriority FinalPriority TimeEstimated Starts Due Text) ],
109 Queue => $queue->Name,
110 Requestor => $session{CurrentUser}->Name,
116 Priority => $queue->InitialPriority,
117 InitialPriority => $queue->InitialPriority,
118 FinalPriority => $queue->FinalPriority,
120 Starts => $starts->ISO,
128 # We'll create a new ticket, and fall through to set fields that
129 # can't be set in the call to Create().
132 foreach my $k (keys %data) {
133 # flexibly parse any dates
135 my $time = new RT::Date $session{CurrentUser};
136 $time->Set(Format => 'unknown', Value => $data{$k});
137 $data{$k} = $time->ISO;
140 if (exists $create{lc $k}) {
141 $v{$create{lc $k}} = delete $data{$k};
144 elsif ($k =~ /^$cf_spec/) {
145 my $cf = RT::CustomField->new( $RT::SystemUser );
147 unless($cf->LoadByName( Name => $cfk )) {
148 push @comments, "# Invalid custom field name ($cfk)";
152 $v{"CustomField-".$cf->Id()} = delete $data{$k};
154 elsif (lc $k eq 'text') {
155 $text = delete $data{$k};
159 # people fields allow multiple values
160 $v{$_} = vsplit($v{$_}) foreach ( grep $create{lc $_}, @people );
165 From => $session{CurrentUser}->EmailAddress,
166 Subject => $v{Subject},
171 my($tid,$trid,$terr) = $ticket->Create(%v);
173 push(@comments, "# Could not create ticket.");
174 push(@comments, "# " . $terr);
180 push(@comments, "# Ticket $id created.");
181 # see if the hash is empty
182 goto DONE if ! keys(%data);
186 # Now we know we're dealing with an existing ticket.
188 my ($time, $key, $val, @data);
190 push @data, [ id => "ticket/".$ticket->Id ];
191 push @data, [ Queue => $ticket->QueueObj->Name ]
192 if (!%$fields || exists $fields->{lc 'Queue'});
193 push @data, [ Owner => $ticket->OwnerObj->Name ]
194 if (!%$fields || exists $fields->{lc 'Owner'});
195 push @data, [ Creator => $ticket->CreatorObj->Name ]
196 if (!%$fields || exists $fields->{lc 'Creator'});
198 foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
199 next unless (!%$fields || (exists $fields->{lc $_}));
200 push @data, [$_ => $ticket->$_ ];
203 foreach $key (@people) {
204 next unless (!%$fields || (exists $fields->{lc $key}));
205 push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
208 $time = new RT::Date ($session{CurrentUser});
209 foreach $key (@dates) {
210 next unless (!%$fields || (exists $fields->{lc $key}));
211 $time->Set(Format => 'sql', Value => $ticket->$key);
212 push @data, [ $key => $time->AsString ];
215 $time = new RT::Date ($session{CurrentUser});
216 foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
217 next unless (!%$fields || (exists $fields->{lc $key}));
218 $val = $ticket->$key || 0;
219 $val = "$val minutes" if $val;
220 push @data, [ $key => $val ];
223 # Display custom fields
224 my $CustomFields = $ticket->CustomFields;
225 while (my $cf = $CustomFields->Next()) {
226 next unless !%$fields
227 || exists $fields->{"cf.{".lc($cf->Name)."}"}
228 || exists $fields->{"cf-".lc $cf->Name};
230 my $vals = $ticket->CustomFieldValues($cf->Id());
232 if ( $cf->SingleValue ) {
234 push @out, $v->Content if $v;
237 while (my $v = $vals->Next()) {
238 my $content = $v->Content;
239 $content =~ s/'/\\'/g;
240 if ( $v->Content =~ /,/ ) {
241 push @out, q{'} . $content . q{'};
248 push @data, [ ('CF.{' . $cf->Name . '}') => join ',', @out ];
251 my %k = map {@$_} @data;
252 $o = [ map {$_->[0]} @data ];
256 my ($get, $set, $key, $val, $n, $s);
258 foreach $key (keys %data) {
263 if (ref $val eq 'ARRAY') {
264 unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
266 $s = "$key may have only one value.";
271 if ($key =~ /^queue$/i) {
272 next if $val eq $ticket->QueueObj->Name;
273 ($n, $s) = $ticket->SetQueue($val);
275 elsif ($key =~ /^owner$/i) {
276 next if $val eq $ticket->OwnerObj->Name;
277 ($n, $s) = $ticket->SetOwner($val);
279 elsif (exists $simple{$key}) {
280 $key = $simple{$key};
283 next if (($val eq $ticket->$key)|| ($ticket->$key =~ /^\d+$/ && $val == $ticket->$key));
284 ($n, $s) = $ticket->$set("$val");
286 elsif (exists $dates{$key}) {
289 # We try to detect whether it should update a field by checking
290 # whether its current value equals the entered value. Since the
291 # LastUpdated field is automatically updated as other columns are
292 # changed, it is not properly skipped. Users cannot update this
294 next if $key eq 'LastUpdated';
298 my $time = new RT::Date $session{CurrentUser};
299 $time->Set(Format => 'sql', Value => $ticket->$key);
300 next if ($val =~ /^not set$/i || $val eq $time->AsString);
302 $time->Set(Format => 'unknown', Value => $val);
303 ($n, $s) = $ticket->$set($time->ISO);
305 elsif (exists $people{$key}) {
306 $key = $people{$key};
309 my %new = map {$_=>1} @{ vsplit($val) };
310 my %old = map {$_=>1} $ticket->$key->MemberEmailAddresses;
311 my $type = $key eq 'Requestors' ? 'Requestor' : $key;
313 foreach $p (keys %old) {
314 unless (exists $new{$p}) {
315 ($s, $n) = $ticket->DeleteWatcher(Type => $type,
317 push @msgs, [ $s, $n ];
320 foreach $p (keys %new) {
321 # XXX: This is a stupid test.
322 unless ($p =~ /^[\w.+-]+\@([\w.-]+\.)*\w+.?$/) {
324 $n = "$p is not a valid email address.";
325 push @msgs, [ $s, $n ];
328 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
329 ($s, $n) = $ticket->AddWatcher(Type => $type,
331 push @msgs, [ $s, $n ];
336 if (@msgs = grep {$_->[0] == 0} @msgs) {
338 $s = join "\n", map {"# ".$_->[1]} @msgs;
343 elsif ($key =~ /^$cf_spec/) {
344 my $cf = RT::CustomField->new( $RT::SystemUser );
346 if (not $cf->LoadByName( Name => $key )) {
348 $s = "Unknown custom field.";
351 my $vals = $ticket->CustomFieldValues($cf->id);
353 if ( $cf->SingleValue ) {
354 my $old = $vals->Next;
356 if ( $val ne $old->Content ) {
358 ($n, $s) = $ticket->AddCustomFieldValue(
359 Field => $cf, Value => $val );
360 $s =~ s/^# // if defined $s;
364 ($n, $s) = $ticket->AddCustomFieldValue(
365 Field => $cf, Value => $val );
366 $s =~ s/^# // if defined $s;
371 my ( $a, $b ) = split /\s*,\s*/, $val, 2;
373 no warnings 'uninitialized';
376 while ( $a !~ /'$/ || ( $a !~ /(\\\\)+'$/
377 && $a =~ /(\\)+'$/ ) ) {
378 ( $a, $b ) = split /\s*,\s*/, $b, 2;
386 elsif ( $a =~ /^q{/ ) {
388 while ( $a !~ /}$/ ) {
389 ( $a, $b ) = split /\s*,\s*/, $b, 2;
399 ( $a, $b ) = split /\s*,\s*/, $b, 2;
405 while (my $v = $vals->Next()) {
415 while ( $new{$_} && $new{$_}-- ) {
416 ($n, $s) = $ticket->AddCustomFieldValue(
417 Field => $cf, Value => $_ );
418 $s =~ s/^# // if defined $s;
424 elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator') {
426 $s = "Unknown field.";
432 push @comments, "# $key: $s";
435 @$o = ("id", grep { $_ ne 'id' } keys %$changes);
440 push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
444 $c ||= join("\n", @comments) if @comments;
445 return [$c, $o, $k, $e];