import rt 3.8.10
[freeside.git] / rt / html / REST / 1.0 / Forms / ticket / history
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2009 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., 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.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
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.)
37 %# 
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.
46 %# 
47 %# END BPS TAGGED BLOCK }}}
48 %# REST/1.0/Forms/ticket/history
49 %#
50 <%ARGS>
51 $id
52 $args => undef
53 $format => undef
54 $fields => undef
55 </%ARGS>
56 <%INIT>
57 my $ticket = new RT::Ticket $session{CurrentUser};
58 my ($c, $o, $k, $e) = ("", [], {}, "");
59
60 $ticket->Load($id);
61 unless ($ticket->Id) {
62     return [ "# Ticket $id does not exist.", [], {}, 1 ];
63 }
64
65 my $trans = $ticket->Transactions();
66 my $total = $trans->Count();
67
68 chomp $args;
69 my @arglist = split('/', $args);
70 my ($type, $tid);
71
72 if ($arglist[0] eq 'type') {
73     $type = $arglist[1];
74 } elsif ($arglist[0] eq 'id') {
75     $tid = $arglist[1];
76 } else {
77     $type = $args;
78 }
79
80 if ($type) {
81     # Create, Set, Status, Correspond, Comment, Give, Steal, Take, Told
82     # CustomField, AddLink, DeleteLink, AddWatcher, DelWatcher
83     if ($args =~ /^links?$/) {
84         $trans->Limit(FIELD => 'Type', OPERATOR => 'LIKE', VALUE => '%Link');
85     }
86     elsif ($args =~ /^watchers?$/) {
87         $trans->Limit(FIELD => 'Type', OPERATOR => 'LIKE', VALUE => '%Watcher');
88     }
89     else {
90         $trans->Limit(FIELD => 'Type', OPERATOR => '=', VALUE => $type);
91     }
92 } elsif ($tid) {
93     $trans->Limit(FIELD => 'Id', OPERATOR => '=', VALUE => $tid);
94 }
95
96 if ($tid) {
97     my @data;
98     my $t = new RT::Transaction $session{CurrentUser};
99     
100     # this paragraph limits the transaction ID query to transactions on this ticket. 
101     # Otherwise you can query any transaction from any ticket, which makes no sense.
102     my $Transactions = $ticket->Transactions;
103     my $tok=0;
104     while (my $T = $Transactions->Next()) {
105         $tok=1 if ($T->Id == $tid)
106     }
107     if ($tok) {
108         $t->Load($tid);
109     } else {
110         return [ "# Transaction $tid is not related to Ticket $id", [], {}, 1 ];
111     }
112
113     push @data, [ id    => $t->Id   ];
114     push @data, [ Ticket    => $t->Ticket   ]
115         if (!%$fields || exists $fields->{lc 'Ticket'});
116     push @data, [ TimeTaken    => $t->TimeTaken   ]
117         if (!%$fields || exists $fields->{lc 'TimeTaken'});
118     push @data, [ Type    => $t->Type   ]
119         if (!%$fields || exists $fields->{lc 'Type'});
120     push @data, [ Field    => $t->Field   ]
121         if (!%$fields || exists $fields->{lc 'Field'});
122     push @data, [ OldValue    => $t->OldValue   ]
123         if (!%$fields || exists $fields->{lc 'OldValue'});
124     push @data, [ NewValue    => $t->NewValue   ]
125         if (!%$fields || exists $fields->{lc 'NewValue'});
126     push @data, [ Data    => $t->Data   ]
127         if (!%$fields || exists $fields->{lc 'Data'});
128     push @data, [ Description    => $t->Description   ]
129         if (!%$fields || exists $fields->{lc 'Description'});
130     push @data, [ Content    => $t->Content   ]
131         if (!%$fields || exists $fields->{lc 'Content'});
132
133
134     if (!%$fields || exists $fields->{lc 'Content'}) {    
135         my $creator = new RT::User $session{CurrentUser};
136         $creator->Load($t->Creator);
137         push @data, [ Creator    => $creator->Name   ];
138     }
139     push @data, [ Created    => $t->Created   ]
140         if (!%$fields || exists $fields->{lc 'Created'});
141
142     if (!%$fields || exists $fields->{lc 'Attachments'}) {
143         my $attachlist;
144         my $attachments = $t->Attachments;
145         while (my $a = $attachments->Next) {
146             my $size = length($a->Content);
147             if ($size > 1024) { $size  = int($size/102.4)/10 . "k" }
148             else              { $size .= "b" }
149             $attachlist .= "\n" . $a->Id.": ".($a->Filename || "untitled")." (".$size.")";
150         }
151         
152         push @data, [Attachments => $attachlist];
153     }
154
155     my %k = map {@$_} @data;
156     $o = [ map {$_->[0]} @data ];
157     $k = \%k;
158
159 } else {
160     my (@data, $tids);
161     $format ||= "s";
162     $format = "l" if (%$fields);
163
164     while (my $t = $trans->Next) {
165         my $tid = $t->Id;
166
167         if ($format eq "l") {
168             $tids .= "," if $tids;
169             $tids .= $tid;
170         } else {
171             push @$o, $tid;
172             $k->{$tid} = $t->Description;
173         }
174     }
175
176     if ($format eq "l") {
177         my @tid;
178         push @tid, "ticket/$id/history/id/$tids";
179         my $fieldstring;
180         foreach my $key (keys %$fields) {
181             $fieldstring .= "," if $fieldstring;
182             $fieldstring .= $key;
183         }
184         my ($content, $forms);
185         $m->subexec("/REST/1.0/show", 
186                     id => \@tid, 
187                     format => $format,
188                     fields => $fieldstring);
189         return [ $c, $o, $k, $e ];
190     }
191 }
192
193 if (!$c) {
194     my $sub = $trans->Count();
195     $c = "# $sub/$total ($args/total)";
196 }
197
198 return [ $c, $o, $k, $e ];
199
200 </%INIT>