import of rt 3.0.9
[freeside.git] / rt / html / REST / 1.0 / Forms / ticket / attachments
1 %# BEGIN LICENSE BLOCK
2 %# 
3 %# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4 %# 
5 %# (Except where explictly superceded by other copyright notices)
6 %# 
7 %# This work is made available to you under the terms of Version 2 of
8 %# the GNU General Public License. A copy of that license should have
9 %# been provided with this software, but in any event can be snarfed
10 %# from www.gnu.org.
11 %# 
12 %# This work is distributed in the hope that it will be useful, but
13 %# WITHOUT ANY WARRANTY; without even the implied warranty of
14 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 %# General Public License for more details.
16 %# 
17 %# Unless otherwise specified, all modifications, corrections or
18 %# extensions to this work which alter its source code become the
19 %# property of Best Practical Solutions, LLC when submitted for
20 %# inclusion in the work.
21 %# 
22 %# 
23 %# END LICENSE BLOCK
24 %# REST/1.0/Forms/ticket/attachments
25 %#
26 <%ARGS>
27 $id
28 $args => undef
29 </%ARGS>
30 <%perl>
31 my @data;
32 my ($c, $o, $k, $e) = ("", [], {}, "");
33 my $ticket = new RT::Ticket $session{CurrentUser};
34
35 $ticket->Load($id);
36 unless ($ticket->Id) {
37     return [ "# Ticket $id does not exist.", [], {}, 1 ];
38 }
39
40 my @arglist = split('/', $args);
41 my ($aid, $content);
42
43 if ($arglist[1] eq 'content') {
44     $aid = $arglist[0];
45     $content = 1;
46 } else {
47     $aid = $args;
48     $content = 0;
49 }
50
51 if ($aid) {
52     unless ($aid =~ /^\d+$/) {
53         return [ "# Invalid attachment id: $aid", [], {}, 1 ];
54     }
55     my $attachment = new RT::Attachment $session{CurrentUser};
56     $attachment->Load($aid);
57     unless ($attachment->Id eq $aid) {
58         return [ "# Invalid attachment id: $aid", [], {}, 1 ];
59     }
60     if ($content) {
61         $c = $attachment->Content;
62     } else {
63         my @data;
64         push @data, [ id    => $attachment->Id   ];
65         push @data, [ Subject    => $attachment->Subject   ];
66         push @data, [ Creator    => $attachment->Creator   ];
67         push @data, [ Created    => $attachment->Created   ];
68         push @data, [ Transaction    => $attachment->TransactionId   ];
69         push @data, [ Parent    => $attachment->Parent   ];
70         push @data, [ MessageId    => $attachment->MessageId   ];
71         push @data, [ Filename    => $attachment->Filename   ];
72         push @data, [ ContentType    => $attachment->ContentType   ];
73         push @data, [ ContentEncoding    => $attachment->ContentEncoding   ];
74         push @data, [ Headers    => $attachment->Headers   ];
75         push @data, [ Content    => $attachment->Content   ];
76
77         my %k = map {@$_} @data;
78         $o = [ map {$_->[0]} @data ];
79         $k = \%k;
80     }
81
82 }
83 else {
84     my @attachments;
85     my $transactions = $ticket->Transactions;
86     while (my $t = $transactions->Next) {
87         my $attachments = $t->Attachments;
88         while (my $a = $attachments->Next) {
89             next unless $a->Filename;
90             my $size = length($a->Content);
91             if ($size > 1024) { $size  = int($size/102.4)/10 . "k" }
92             else              { $size .= "b" }
93             push @attachments, $a->Id.": ".$a->Filename." (".$size.")";
94         }
95     }
96
97     if (@attachments) {
98         $o = [ "id", "Attachments" ];
99         $k = {
100             id => "ticket/".$ticket->Id."/attachments",
101             Attachments => \@attachments
102         };
103     }
104 }
105
106 return [ $c, $o, $k, $e ];
107 </%perl>