1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 %# <jesse@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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 %# CONTRIBUTION SUBMISSION POLICY:
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
45 %# END BPS TAGGED BLOCK }}}
46 %# REST/1.0/Forms/ticket/default
57 my ($c, $o, $k, $e) = ("", [], {}, 0);
59 my $ticket = new RT::Ticket $session{CurrentUser};
60 my @dates = qw(Created Starts Started Due Resolved Told);
61 my @people = qw(Requestors Cc AdminCc);
62 my @create = qw(Queue Requestor Subject Cc AdminCc Owner Status Priority
63 InitialPriority FinalPriority TimeEstimated TimeWorked
64 TimeLeft Starts Started Due Resolved);
65 my @simple = qw(Subject Status Priority Disabled TimeEstimated TimeWorked
66 TimeLeft InitialPriority FinalPriority);
67 my %dates = map {lc $_ => $_} @dates;
68 my %people = map {lc $_ => $_} @people;
69 my %create = map {lc $_ => $_} @create;
70 my %simple = map {lc $_ => $_} @simple;
72 # Are we dealing with an existing ticket?
76 return [ "# Ticket $id does not exist.", [], {}, 1 ];
78 elsif (!$ticket->CurrentUserHasRight('ShowTicket') ||
79 (%data && !$ticket->CurrentUserHasRight('ModifyTicket')))
81 my $act = %data ? "modify" : "display";
82 return [ "# You are not allowed to $act ticket $id.", [], {}, 1 ];
87 # GET ticket/new: Return a suitable default form.
88 # We get defaults from queue/1 (XXX: What if it isn't there?).
89 my $due = new RT::Date $session{CurrentUser};
90 my $queue = new RT::Queue $session{CurrentUser};
91 my $starts = new RT::Date $session{CurrentUser};
94 $due->AddDays($queue->DefaultDueIn) if $queue->DefaultDueIn;
98 "# Required: Queue, Requestor, Subject",
99 [ qw(id Queue Requestor Subject Cc AdminCc Owner Status Priority
100 InitialPriority FinalPriority TimeEstimated Starts Due Text) ],
103 Queue => $queue->Name,
104 Requestor => $session{CurrentUser}->Name,
110 Priority => $queue->InitialPriority,
111 InitialPriority => $queue->InitialPriority,
112 FinalPriority => $queue->FinalPriority,
114 Starts => $starts->ISO,
122 # We'll create a new ticket, and fall through to set fields that
123 # can't be set in the call to Create().
126 foreach my $k (keys %data) {
127 if (exists $create{lc $k}) {
128 $v{$create{lc $k}} = delete $data{$k};
130 elsif (lc $k eq 'text') {
131 $text = delete $data{$k};
138 From => $session{CurrentUser}->EmailAddress,
139 Subject => $v{Subject},
145 unless ($ticket->Id) {
146 return [ "# Could not create ticket.", [], {}, 1 ];
151 push(@comments, "# Ticket $id created.");
152 goto DONE if %data == 0;
156 # Now we know we're dealing with an existing ticket.
158 my ($time, $key, $val, @data);
160 push @data, [ id => "ticket/".$ticket->Id ];
161 push @data, [ Queue => $ticket->QueueObj->Name ]
162 if (!%$fields || exists $fields->{lc 'Queue'});
163 push @data, [ Owner => $ticket->OwnerObj->Name ]
164 if (!%$fields || exists $fields->{lc 'Owner'});
165 push @data, [ Creator => $ticket->CreatorObj->Name ]
166 if (!%$fields || exists $fields->{lc 'Creator'});
168 foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
169 next unless (!%$fields || (exists $fields->{lc $_}));
170 push @data, [$_ => $ticket->$_ ];
173 foreach $key (@people) {
174 next unless (!%$fields || (exists $fields->{lc $key}));
175 push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
178 $time = new RT::Date ($session{CurrentUser});
179 foreach $key (@dates) {
180 next unless (!%$fields || (exists $fields->{lc $key}));
181 $time->Set(Format => 'sql', Value => $ticket->$key);
182 push @data, [ $key => $time->AsString ];
185 $time = new RT::Date ($session{CurrentUser});
186 foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
187 next unless (!%$fields || (exists $fields->{lc $key}));
188 $val = $ticket->$key || 0;
189 $val = $time->DurationAsString($val*60) if $val;
190 push @data, [ $key => $val ];
193 my %k = map {@$_} @data;
194 $o = [ map {$_->[0]} @data ];
198 my ($get, $set, $key, $val, $n, $s);
200 foreach $key (keys %data) {
205 if (ref $val eq 'ARRAY') {
206 unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
208 $s = "$key may have only one value.";
213 if ($key =~ /^queue$/i) {
214 next if $val eq $ticket->QueueObj->Name;
215 ($n, $s) = $ticket->SetQueue($val);
217 elsif ($key =~ /^owner$/i) {
218 next if $val eq $ticket->OwnerObj->Name;
219 ($n, $s) = $ticket->SetOwner($val);
221 elsif (exists $simple{$key}) {
222 $key = $simple{$key};
225 next if (($val eq $ticket->$key)|| ($ticket->$key =~ /^\d+$/ && $val == $ticket->$key));
226 ($n, $s) = $ticket->$set("$val");
228 elsif (exists $dates{$key}) {
232 my $time = new RT::Date $session{CurrentUser};
233 $time->Set(Format => 'sql', Value => $ticket->$key);
234 next if ($val =~ /^not set$/i || $val eq $time->AsString);
235 ($n, $s) = $ticket->$set($val);
237 elsif (exists $people{$key}) {
238 $key = $people{$key};
241 my %new = map {$_=>1} @{ vsplit($val) };
242 my %old = map {$_=>1} $ticket->$key->MemberEmailAddresses;
243 my $type = $key eq 'Requestors' ? 'Requestor' : $key;
245 foreach $p (keys %old) {
246 unless (exists $new{$p}) {
247 ($s, $n) = $ticket->DeleteWatcher(Type => $type,
249 push @msgs, [ $s, $n ];
252 foreach $p (keys %new) {
253 # XXX: This is a stupid test.
254 unless ($p =~ /^[\w.+-]+\@([\w.-]+\.)*\w+.?$/) {
256 $n = "$p is not a valid email address.";
257 push @msgs, [ $s, $n ];
260 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
261 ($s, $n) = $ticket->AddWatcher(Type => $type,
263 push @msgs, [ $s, $n ];
268 if (@msgs = grep {$_->[0] == 0} @msgs) {
270 $s = join "\n", map {"# ".$_->[1]} @msgs;
274 elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator') {
276 $s = "Unknown field.";
282 push @comments, "# $key: $s";
284 my %o = keys %$changes;
286 @$o = ("id", keys %o);
291 push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
295 $c ||= join("\n", @comments) if @comments;
296 return [$c, $o, $k, $e];