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