import rt 3.2.2
[freeside.git] / rt / html / REST / 1.0 / Forms / ticket / default
1 %# {{{ BEGIN BPS TAGGED BLOCK
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC 
6 %#                                          <jesse@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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 %# 
27 %# 
28 %# CONTRIBUTION SUBMISSION POLICY:
29 %# 
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.)
35 %# 
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.
44 %# 
45 %# }}} END BPS TAGGED BLOCK
46 %# REST/1.0/Forms/ticket/default
47 %#
48 <%ARGS>
49 $id
50 $changes => {}
51 $fields => undef
52 </%ARGS>
53 <%perl>
54 use MIME::Entity;
55
56 my @comments;
57 my ($c, $o, $k, $e) = ("", [], {}, 0);
58 my %data   = %$changes;
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;
71
72 # Are we dealing with an existing ticket?
73 if ($id ne 'new') {
74     $ticket->Load($id);
75     if (!$ticket->Id) {
76         return [ "# Ticket $id does not exist.", [], {}, 1 ];
77     }
78     elsif (!$ticket->CurrentUserHasRight('ShowTicket') ||
79            (%data && !$ticket->CurrentUserHasRight('ModifyTicket')))
80     {
81         my $act = %data ? "modify" : "display";
82         return [ "# You are not allowed to $act ticket $id.", [], {}, 1 ];
83     }
84 }
85 else {
86     if (%data == 0) {
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};
92         $queue->Load(1);
93         $due->SetToNow;
94         $due->AddDays($queue->DefaultDueIn) if $queue->DefaultDueIn;
95         $starts->SetToNow;
96
97         return [
98             "# Required: Queue, Requestor, Subject",
99             [ qw(id Queue Requestor Subject Cc AdminCc Owner Status Priority
100                  InitialPriority FinalPriority TimeEstimated Starts Due Text) ],
101             {
102                 id               => "ticket/new",
103                 Queue            => $queue->Name,
104                 Requestor        => $session{CurrentUser}->Name,
105                 Subject          => "",
106                 Cc               => [],
107                 AdminCc          => [],
108                 Owner            => "",
109                 Status           => "new",
110                 Priority         => $queue->InitialPriority,
111                 InitialPriority  => $queue->InitialPriority,
112                 FinalPriority    => $queue->FinalPriority,
113                 TimeEstimated    => 0,
114                 Starts           => $starts->ISO,
115                 Due              => $due->ISO,
116                 Text             => "",
117             },
118             0
119         ];
120     }
121     else {
122         # We'll create a new ticket, and fall through to set fields that
123         # can't be set in the call to Create().
124         my (%v, $text);
125
126         foreach my $k (keys %data) {
127             if (exists $create{lc $k}) {
128                 $v{$create{lc $k}} = delete $data{$k};
129             }
130             elsif (lc $k eq 'text') {
131                 $text = delete $data{$k};
132             }
133         }
134
135         if ($text) {
136             $v{MIMEObj} =
137                 MIME::Entity->build(
138                     From => $session{CurrentUser}->EmailAddress,
139                     Subject => $v{Subject},
140                     Data => $text
141                 );
142         }
143
144         $ticket->Create(%v);
145         unless ($ticket->Id) {
146             return [ "# Could not create ticket.", [], {}, 1 ];
147         }
148
149         delete $data{id};
150         $id = $ticket->Id;
151         push(@comments, "# Ticket $id created.");
152         goto DONE if %data == 0;
153     }
154 }
155
156 # Now we know we're dealing with an existing ticket.
157 if (%data == 0) {
158     my ($time, $key, $val, @data);
159
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'});
167
168     foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
169         next unless (!%$fields || (exists $fields->{lc $_}));
170         push @data, [$_ => $ticket->$_ ];
171     }
172
173     foreach $key (@people) {
174         next unless (!%$fields || (exists $fields->{lc $key}));
175         push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
176     }
177
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 ];
183     }
184
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 ];
191     }
192
193     my %k = map {@$_} @data;
194     $o = [ map {$_->[0]} @data ];
195     $k = \%k;
196 }
197 else {
198     my ($get, $set, $key, $val, $n, $s);
199
200     foreach $key (keys %data) {
201         $val = $data{$key};
202         $key = lc $key;
203         $n = 1;
204
205         if (ref $val eq 'ARRAY') {
206             unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
207                 $n = 0;
208                 $s = "$key may have only one value.";
209                 goto SET;
210             }
211         }
212
213         if ($key =~ /^queue$/i) {
214             next if $val eq $ticket->QueueObj->Name;
215             ($n, $s) = $ticket->SetQueue($val);
216         }
217         elsif ($key =~ /^owner$/i) {
218             next if $val eq $ticket->OwnerObj->Name;
219             ($n, $s) = $ticket->SetOwner($val);
220         }
221         elsif (exists $simple{$key}) {
222             $key = $simple{$key};
223             $set = "Set$key";
224
225             next if (($val eq $ticket->$key)|| ($ticket->$key =~ /^\d+$/ && $val == $ticket->$key));
226             ($n, $s) = $ticket->$set("$val");
227         }
228         elsif (exists $dates{$key}) {
229             $key = $dates{$key};
230             $set = "Set$key";
231
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);
236         }
237         elsif (exists $people{$key}) {
238             $key = $people{$key};
239             my ($p, @msgs);
240
241             my %new  = map {$_=>1} @{ vsplit($val) };
242             my %old  = map {$_=>1} $ticket->$key->MemberEmailAddresses;
243             my $type = $key eq 'Requestors' ? 'Requestor' : $key;
244
245             foreach $p (keys %old) {
246                 unless (exists $new{$p}) {
247                     ($s, $n) = $ticket->DeleteWatcher(Type => $type,
248                                                       Email => $p);
249                     push @msgs, [ $s, $n ];
250                 }
251             }
252             foreach $p (keys %new) {
253                 # XXX: This is a stupid test.
254                 unless ($p =~ /^[\w.+-]+\@([\w.-]+\.)*\w+.?$/) {
255                     $s = 0;
256                     $n = "$p is not a valid email address.";
257                     push @msgs, [ $s, $n ];
258                     next;
259                 }
260                 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
261                     ($s, $n) = $ticket->AddWatcher(Type => $type,
262                                                    Email => $p);
263                     push @msgs, [ $s, $n ];
264                 }
265             }
266
267             $n = 1;
268             if (@msgs = grep {$_->[0] == 0} @msgs) {
269                 $n = 0;
270                 $s = join "\n", map {"# ".$_->[1]} @msgs;
271                 $s =~ s/^# //;
272             }
273         }
274         elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator') {
275             $n = 0;
276             $s = "Unknown field.";
277         }
278
279     SET:
280         if ($n == 0) {
281             $e = 1;
282             push @comments, "# $key: $s";
283             unless (@$o) {
284                 my %o = keys %$changes;
285                 delete $o{id};
286                 @$o = ("id", keys %o);
287                 $k = $changes;
288             }
289         }
290     }
291     push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
292 }
293
294 DONE:
295 $c ||= join("\n", @comments) if @comments;
296 return [$c, $o, $k, $e];
297
298 </%perl>