1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2009 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., 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
60 my ($c, $o, $k, $e) = ("", [], {}, 0);
62 my $ticket = new RT::Ticket $session{CurrentUser};
63 my @dates = qw(Created Starts Started Due Resolved Told LastUpdated);
64 my @people = qw(Requestors Cc AdminCc);
65 my @create = qw(Queue Requestor Subject Cc AdminCc Owner Status Priority
66 InitialPriority FinalPriority TimeEstimated TimeWorked
67 TimeLeft Starts Started Due Resolved);
68 my @simple = qw(Subject Status Priority Disabled TimeEstimated TimeWorked
69 TimeLeft InitialPriority FinalPriority);
70 my %dates = map {lc $_ => $_} @dates;
71 my %people = map {lc $_ => $_} @people;
72 my %create = map {lc $_ => $_} @create;
73 my %simple = map {lc $_ => $_} @simple;
75 # Are we dealing with an existing ticket?
79 return [ "# Ticket $id does not exist.", [], {}, 1 ];
81 elsif (!$ticket->CurrentUserHasRight('ShowTicket') ||
82 (%data && !$ticket->CurrentUserHasRight('ModifyTicket')))
84 my $act = %data ? "modify" : "display";
85 return [ "# You are not allowed to $act ticket $id.", [], {}, 1 ];
90 # GET ticket/new: Return a suitable default form.
91 # We get defaults from queue/1 (XXX: What if it isn't there?).
92 my $due = new RT::Date $session{CurrentUser};
93 my $queue = new RT::Queue $session{CurrentUser};
94 my $starts = new RT::Date $session{CurrentUser};
97 $due->AddDays($queue->DefaultDueIn) if $queue->DefaultDueIn;
101 "# Required: id, Queue",
102 [ qw(id Queue Requestor Subject Cc AdminCc Owner Status Priority
103 InitialPriority FinalPriority TimeEstimated Starts Due Text) ],
106 Queue => $queue->Name,
107 Requestor => $session{CurrentUser}->Name,
113 Priority => $queue->InitialPriority,
114 InitialPriority => $queue->InitialPriority,
115 FinalPriority => $queue->FinalPriority,
117 Starts => $starts->ISO,
125 # We'll create a new ticket, and fall through to set fields that
126 # can't be set in the call to Create().
129 foreach my $k (keys %data) {
130 if (exists $create{lc $k}) {
131 $v{$create{lc $k}} = delete $data{$k};
134 elsif ($k =~ /^CF-/i) {
135 my $cf = RT::CustomField->new( $RT::SystemUser );
138 unless($cf->LoadByName( Name => $cfk )) {
139 push @comments, "# Invalid custom field name ($cfk)";
143 $v{"CustomField-".$cf->Id()} = delete $data{$k};
145 elsif (lc $k eq 'text') {
146 $text = delete $data{$k};
150 # people fields allow multiple values
151 $v{$_} = vsplit($v{$_}) foreach ( grep $create{lc $_}, @people );
156 From => $session{CurrentUser}->EmailAddress,
157 Subject => $v{Subject},
162 my($tid,$trid,$terr) = $ticket->Create(%v);
164 push(@comments, "# Could not create ticket.");
165 push(@comments, "# " . $terr);
171 push(@comments, "# Ticket $id created.");
172 # see if the hash is empty
173 goto DONE if ! keys(%data);
177 # Now we know we're dealing with an existing ticket.
179 my ($time, $key, $val, @data);
181 push @data, [ id => "ticket/".$ticket->Id ];
182 push @data, [ Queue => $ticket->QueueObj->Name ]
183 if (!%$fields || exists $fields->{lc 'Queue'});
184 push @data, [ Owner => $ticket->OwnerObj->Name ]
185 if (!%$fields || exists $fields->{lc 'Owner'});
186 push @data, [ Creator => $ticket->CreatorObj->Name ]
187 if (!%$fields || exists $fields->{lc 'Creator'});
189 foreach (qw(Subject Status Priority InitialPriority FinalPriority)) {
190 next unless (!%$fields || (exists $fields->{lc $_}));
191 push @data, [$_ => $ticket->$_ ];
194 foreach $key (@people) {
195 next unless (!%$fields || (exists $fields->{lc $key}));
196 push @data, [ $key => [ $ticket->$key->MemberEmailAddresses ] ];
199 $time = new RT::Date ($session{CurrentUser});
200 foreach $key (@dates) {
201 next unless (!%$fields || (exists $fields->{lc $key}));
202 $time->Set(Format => 'sql', Value => $ticket->$key);
203 push @data, [ $key => $time->AsString ];
206 $time = new RT::Date ($session{CurrentUser});
207 foreach $key (qw(TimeEstimated TimeWorked TimeLeft)) {
208 next unless (!%$fields || (exists $fields->{lc $key}));
209 $val = $ticket->$key || 0;
210 $val = "$val minutes" if $val;
211 push @data, [ $key => $val ];
214 # Display custom fields
215 my $CustomFields = $ticket->QueueObj->TicketCustomFields();
216 while (my $cf = $CustomFields->Next()) {
217 next unless (!%$fields || (exists $fields->{"cf-".lc $cf->Name}));
218 my $vals = $ticket->CustomFieldValues($cf->Id());
220 while (my $v = $vals->Next()) {
221 push @out, $v->Content;
223 push @data, [ 'CF-' . $cf->Name => join ',', @out ];
226 my %k = map {@$_} @data;
227 $o = [ map {$_->[0]} @data ];
231 my ($get, $set, $key, $val, $n, $s);
233 foreach $key (keys %data) {
238 if (ref $val eq 'ARRAY') {
239 unless ($key =~ /^(?:Requestors|Cc|AdminCc)$/i) {
241 $s = "$key may have only one value.";
246 if ($key =~ /^queue$/i) {
247 next if $val eq $ticket->QueueObj->Name;
248 ($n, $s) = $ticket->SetQueue($val);
250 elsif ($key =~ /^owner$/i) {
251 next if $val eq $ticket->OwnerObj->Name;
252 ($n, $s) = $ticket->SetOwner($val);
254 elsif (exists $simple{$key}) {
255 $key = $simple{$key};
258 next if (($val eq $ticket->$key)|| ($ticket->$key =~ /^\d+$/ && $val == $ticket->$key));
259 ($n, $s) = $ticket->$set("$val");
261 elsif (exists $dates{$key}) {
265 my $time = new RT::Date $session{CurrentUser};
266 $time->Set(Format => 'sql', Value => $ticket->$key);
267 next if ($val =~ /^not set$/i || $val eq $time->AsString);
268 ($n, $s) = $ticket->$set($val);
270 elsif (exists $people{$key}) {
271 $key = $people{$key};
274 my %new = map {$_=>1} @{ vsplit($val) };
275 my %old = map {$_=>1} $ticket->$key->MemberEmailAddresses;
276 my $type = $key eq 'Requestors' ? 'Requestor' : $key;
278 foreach $p (keys %old) {
279 unless (exists $new{$p}) {
280 ($s, $n) = $ticket->DeleteWatcher(Type => $type,
282 push @msgs, [ $s, $n ];
285 foreach $p (keys %new) {
286 # XXX: This is a stupid test.
287 unless ($p =~ /^[\w.+-]+\@([\w.-]+\.)*\w+.?$/) {
289 $n = "$p is not a valid email address.";
290 push @msgs, [ $s, $n ];
293 unless ($ticket->IsWatcher(Type => $type, Email => $p)) {
294 ($s, $n) = $ticket->AddWatcher(Type => $type,
296 push @msgs, [ $s, $n ];
301 if (@msgs = grep {$_->[0] == 0} @msgs) {
303 $s = join "\n", map {"# ".$_->[1]} @msgs;
308 elsif ($key =~ /^CF-/i) {
309 my $cf = RT::CustomField->new( $RT::SystemUser );
311 if (not $cf->LoadByName( Name => $key )) {
313 $s = "Unknown custom field.";
316 ($n, $s) = $ticket->AddCustomFieldValue(
317 Field => $cf, Value => $val );
318 $s =~ s/^# // if defined $s;
321 elsif ($key ne 'id' && $key ne 'type' && $key ne 'creator') {
323 $s = "Unknown field.";
329 push @comments, "# $key: $s";
331 my %o = keys %$changes;
333 @$o = ("id", keys %o);
338 push(@comments, "# Ticket ".$ticket->id." updated.") unless $n == 0;
342 $c ||= join("\n", @comments) if @comments;
343 return [$c, $o, $k, $e];