import of rt 3.0.9
[freeside.git] / rt / html / REST / 1.0 / Forms / ticket / default
1 %# REST/1.0/Forms/ticket/default
2 %#
3 <%ARGS>
4 $id
5 $changes => {}
6 $fields => undef
7 </%ARGS>
8 <%perl>
9 use MIME::Entity;
10
11 my @comments;
12 my ($c, $o, $k, $e) = ("", [], {}, 0);
13 my %data   = %$changes;
14 my $ticket = new RT::Ticket $session{CurrentUser};
15 my @dates  = qw(Created Starts Started Due Resolved Told);
16 my @people = qw(Requestors Cc AdminCc);
17 my @create = qw(Queue Requestor Subject Cc AdminCc Owner Status Priority
18                 InitialPriority FinalPriority TimeEstimated TimeWorked
19                 TimeLeft Starts Started Due Resolved);
20 my @simple = qw(Subject Status Priority Disabled TimeEstimated TimeWorked
21                 TimeLeft InitialPriority FinalPriority);
22 my %dates  = map {lc $_ => $_} @dates;
23 my %people = map {lc $_ => $_} @people;
24 my %create = map {lc $_ => $_} @create;
25 my %simple = map {lc $_ => $_} @simple;
26
27 # Are we dealing with an existing ticket?
28 if ($id ne 'new') {
29     $ticket->Load($id);
30     if (!$ticket->Id) {
31         return [ "# Ticket $id does not exist.", [], {}, 1 ];
32     }
33     elsif (!$ticket->CurrentUserHasRight('ShowTicket') ||
34            (%data && !$ticket->CurrentUserHasRight('ModifyTicket')))
35     {
36         my $act = %data ? "modify" : "display";
37         return [ "# You are not allowed to $act ticket $id.", [], {}, 1 ];
38     }
39 }
40 else {
41     if (%data == 0) {
42         # GET ticket/new: Return a suitable default form.
43         # We get defaults from queue/1 (XXX: What if it isn't there?).
44         my $due = new RT::Date $session{CurrentUser};
45         my $queue = new RT::Queue $session{CurrentUser};
46         my $starts = new RT::Date $session{CurrentUser};
47         $queue->Load(1);
48         $due->SetToNow;
49         $due->AddDays($queue->DefaultDueIn) if $queue->DefaultDueIn;
50         $starts->SetToNow;
51
52         return [
53             "# Required: Queue, Requestor, Subject",
54             [ qw(id Queue Requestor Subject Cc AdminCc Owner Status Priority
55                  InitialPriority FinalPriority TimeEstimated Starts Due Text) ],
56             {
57                 id               => "ticket/new",
58                 Queue            => $queue->Name,
59                 Requestor        => $session{CurrentUser}->Name,
60                 Subject          => "",
61                 Cc               => [],
62                 AdminCc          => [],
63                 Owner            => "",
64                 Status           => "new",
65                 Priority         => $queue->InitialPriority,
66                 InitialPriority  => $queue->InitialPriority,
67                 FinalPriority    => $queue->FinalPriority,
68                 TimeEstimated    => 0,
69                 Starts           => $starts->ISO,
70                 Due              => $due->ISO,
71                 Text             => "",
72             },
73             0
74         ];
75     }
76     else {
77         # We'll create a new ticket, and fall through to set fields that
78         # can't be set in the call to Create().
79         my (%v, $text);
80
81         foreach my $k (keys %data) {
82             if (exists $create{lc $k}) {
83                 $v{$create{lc $k}} = delete $data{$k};
84             }
85             elsif (lc $k eq 'text') {
86                 $text = delete $data{$k};
87             }
88         }
89
90         if ($text) {
91             $v{MIMEObj} =
92                 MIME::Entity->build(
93                     From => $session{CurrentUser}->EmailAddress,
94                     Subject => $v{Subject},
95                     Data => $text
96                 );
97         }
98
99         $ticket->Create(%v);
100         unless ($ticket->Id) {
101             return [ "# Could not create ticket.", [], {}, 1 ];
102         }
103
104         delete $data{id};
105         $id = $ticket->Id;
106         push(@comments, "# Ticket $id created.");
107         goto DONE if %data == 0;
108     }
109 }
110
111 # Now we know we're dealing with an existing ticket.
112 if (%data == 0) {
113     my ($time, $key, $val, @data);
114
115     push @data, [ id    => "ticket/".$ticket->Id   ];
116     push @data, [ Queue => $ticket->QueueObj->Name ] 
117         if (!%$fields || exists $fields->{lc 'Queue'});
118     push @data, [ Owner => $ticket->OwnerObj->Name ]
119         if (!%$fields || exists $fields->{lc 'Owner'});
120     push @data, [ Creator => $ticket->CreatorObj->Name ]
121         if (!%$fields || exists $fields->{lc 'Creator'});
122
123     foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
124         next unless (!%$fields || (exists $fields->{lc $_}));
125         push @data, [$_ => $ticket->$_ ];
126     }
127
128     foreach $key (@people) {
129         next unless (!%$fields || (exists $fields->{lc $key}));
130         push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
131     }
132
133     $time = new RT::Date ($session{CurrentUser});
134     foreach $key (@dates) {
135         next unless (!%$fields || (exists $fields->{lc $key}));
136         $time->Set(Format => 'sql', Value => $ticket->$key);
137         push @data, [ $key => $time->AsString ];
138     }
139
140     $time = new RT::Date ($session{CurrentUser});
141     foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
142         next unless (!%$fields || (exists $fields->{lc $key}));
143         $val = $ticket->$key || 0;
144         $val = $time->DurationAsString($val*60) if $val;
145         push @data, [ $key => $val ];
146     }
147
148     my %k = map {@$_} @data;
149     $o = [ map {$_->[0]} @data ];
150     $k = \%k;
151 }
152 else {
153     my ($get, $set, $key, $val, $n, $s);
154
155     foreach $key (keys %data) {
156         $val = $data{$key};
157         $key = lc $key;
158         $n = 1;
159
160         if (ref $val eq 'ARRAY') {
161             unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
162                 $n = 0;
163                 $s = "$key may have only one value.";
164                 goto SET;
165             }
166         }
167
168         if ($key =~ /^queue$/i) {
169             next if $val eq $ticket->QueueObj->Name;
170             ($n, $s) = $ticket->SetQueue($val);
171         }
172         elsif ($key =~ /^owner$/i) {
173             next if $val eq $ticket->OwnerObj->Name;
174             ($n, $s) = $ticket->SetOwner($val);
175         }
176         elsif (exists $simple{$key}) {
177             $key = $simple{$key};
178             $set = "Set$key";
179
180             next if $val eq $ticket->$key;
181             ($n, $s) = $ticket->$set($val);
182         }
183         elsif (exists $dates{$key}) {
184             $key = $dates{$key};
185             $set = "Set$key";
186
187             my $time = new RT::Date $session{CurrentUser};
188             $time->Set(Format => 'sql', Value => $ticket->$key);
189             next if ($val =~ /^not set$/i || $val eq $time->AsString);
190             ($n, $s) = $ticket->$set($val);
191         }
192         elsif (exists $people{$key}) {
193             $key = $people{$key};
194             my ($p, @msgs);
195
196             my %new  = map {$_=>1} @{ vsplit($val) };
197             my %old  = map {$_=>1} $ticket->$key->MemberEmailAddresses;
198             my $type = $key eq 'Requestors' ? 'Requestor' : $key;
199
200             foreach $p (keys %old) {
201                 unless (exists $new{$p}) {
202                     ($s, $n) = $ticket->DeleteWatcher(Type => $type,
203                                                       Email => $p);
204                     push @msgs, [ $s, $n ];
205                 }
206             }
207             foreach $p (keys %new) {
208                 # XXX: This is a stupid test.
209                 unless ($p =~ /^[\w.+-]+\@([\w.-]+\.)*\w+.?$/) {
210                     $s = 0;
211                     $n = "$p is not a valid email address.";
212                     push @msgs, [ $s, $n ];
213                     next;
214                 }
215                 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
216                     ($s, $n) = $ticket->AddWatcher(Type => $type,
217                                                    Email => $p);
218                     push @msgs, [ $s, $n ];
219                 }
220             }
221
222             $n = 1;
223             if (@msgs = grep {$_->[0] == 0} @msgs) {
224                 $n = 0;
225                 $s = join "\n", map {"# ".$_->[1]} @msgs;
226                 $s =~ s/^# //;
227             }
228         }
229         elsif ($key ne 'id' && $key ne 'type') {
230             $n = 0;
231             $s = "Unknown field.";
232         }
233
234     SET:
235         if ($n == 0) {
236             $e = 1;
237             push @comments, "# $key: $s";
238             unless (@$o) {
239                 my %o = keys %$changes;
240                 delete $o{id};
241                 @$o = ("id", keys %o);
242                 $k = $changes;
243             }
244         }
245     }
246     push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
247 }
248
249 DONE:
250 $c ||= join("\n", @comments) if @comments;
251 return [$c, $o, $k, $e];
252
253 </%perl>