import of rt 3.0.9
[freeside.git] / rt / html / REST / 1.0 / search / ticket
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/search/ticket
25 %#
26 <%ARGS>
27 $query
28 $format => undef
29 $orderby => undef
30 $fields => undef
31 </%ARGS>
32 <%INIT>
33 my $output = "";
34 my $status = "200 Ok";
35 my $tickets = new RT::Tickets $session{CurrentUser};
36
37 # Parse and validate any field specifications.
38 my $field  = '[a-zA-Z][a-zA-Z0-9_-]*';
39 my (%fields, @fields);
40 if ($fields) {
41     $format = "l";
42     unless ($fields =~ /^(?:$field,)*$field$/) {
43         $status = "400 Bad Request";
44         $output = "Invalid field specification: $fields";
45         goto OUTPUT;
46     }
47     @fields = map lc, split /,/, $fields;
48     @fields{@fields} = ();
49     unless (exists $fields{id}) {
50         unshift @fields, "id";
51         $fields{id} = ();
52     }
53 }
54
55 $format ||= "s";
56 if ($format !~ /^[isl]$/) {
57     $status = "400 Bad request";
58     $output = "Unknown listing format: $format. (Use i, s, or l.)\n";
59     goto OUTPUT;
60 }
61
62 my ($n, $s);
63 eval {
64     ($n, $s) = $tickets->FromSQL($query);
65 };
66 my $sortstring = "";
67 if ($orderby) {
68     $sortstring = 'FIELD => ';
69     my $order = substr($orderby, 0, 1);
70     if ($order eq '+' || $order eq '-') {
71         $sortstring .= 'substr($orderby, 1)';
72         if ($order eq '+') {
73             $sortstring .= ", ORDER => 'ASC'";
74         } elsif ($order eq '-') {
75             $sortstring .= ", ORDER => 'DESC'";
76         }
77     } else {
78         $sortstring .= '$orderby';
79     }
80     my $foo = 'FIELD => ';
81     $foo .= '$orderby';
82     $tickets->OrderBy(eval $sortstring);
83 }
84 if ($@ || $n == 0) {
85     $s ||= $@;
86     $status = "400 Bad request";
87     $output = "Invalid query: '$s'.\n";
88     goto OUTPUT;
89 }
90
91 $n = 0;
92 my @output;
93 while (my $ticket = $tickets->Next) {
94     $n++;
95
96     if ($format eq "i") {
97         $output .= "ticket/" . $ticket->Id . "\n";
98     }
99     elsif ($format eq "s") {
100         $output .= $ticket->Id . ": ". $ticket->Subject . "\n";
101     }
102     else {
103         my $id = $ticket->Id;
104         my $d = $m->comp("$RT::WebPath/REST/1.0/Forms/ticket/default", id => $id, format => $format, fields => \%fields);
105         my ($c, $o, $k, $e) = @$d;
106         push @output, [ $c, $o, $k ];
107     }
108 }
109 if ($n == 0 && $format ne "i") {
110     $output = "No matching results.\n";
111 }
112
113 $output = form_compose(\@output) if @output;
114
115 OUTPUT:
116 </%INIT>
117 RT/<% $RT::VERSION %> <% $status %>
118
119 <% $output |n %>