3 %# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 %# (Except where explictly superceded by other copyright notices)
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
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.
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.
24 %# REST/1.0/search/ticket
34 my $status = "200 Ok";
35 my $tickets = new RT::Tickets $session{CurrentUser};
37 # Parse and validate any field specifications.
38 my $field = '[a-zA-Z][a-zA-Z0-9_-]*';
39 my (%fields, @fields);
42 unless ($fields =~ /^(?:$field,)*$field$/) {
43 $status = "400 Bad Request";
44 $output = "Invalid field specification: $fields";
47 @fields = map lc, split /,/, $fields;
48 @fields{@fields} = ();
49 unless (exists $fields{id}) {
50 unshift @fields, "id";
56 if ($format !~ /^[isl]$/) {
57 $status = "400 Bad request";
58 $output = "Unknown listing format: $format. (Use i, s, or l.)\n";
64 ($n, $s) = $tickets->FromSQL($query);
68 $sortstring = 'FIELD => ';
69 my $order = substr($orderby, 0, 1);
70 if ($order eq '+' || $order eq '-') {
71 $sortstring .= 'substr($orderby, 1)';
73 $sortstring .= ", ORDER => 'ASC'";
74 } elsif ($order eq '-') {
75 $sortstring .= ", ORDER => 'DESC'";
78 $sortstring .= '$orderby';
80 my $foo = 'FIELD => ';
82 $tickets->OrderBy(eval $sortstring);
86 $status = "400 Bad request";
87 $output = "Invalid query: '$s'.\n";
93 while (my $ticket = $tickets->Next) {
97 $output .= "ticket/" . $ticket->Id . "\n";
99 elsif ($format eq "s") {
100 $output .= $ticket->Id . ": ". $ticket->Subject . "\n";
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 ];
109 if ($n == 0 && $format ne "i") {
110 $output = "No matching results.\n";
113 $output = form_compose(\@output) if @output;
117 RT/<% $RT::VERSION %> <% $status %>