rt 4.0.20 (RT#13852)
[freeside.git] / rt / share / html / Search / Results.html
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 <& /Elements/Header, Title => $title,
49     Refresh => $refresh,
50     LinkRel => \%link_rel &>
51 <& /Elements/Tabs &>
52
53 % $m->callback( ARGSRef => \%ARGS, CallbackName => 'BeforeResults' );
54
55 % unless ($ok) {
56 %    $msg =~ s{ at .*? line .*}{}s;
57 <&| /Widgets/TitleBox, title => loc("Error"), class => "error" &>
58 <&|/l_unsafe, "<i>".$m->interp->apply_escapes($msg, "h")."</i>" &>There was an error parsing your search query: [_1].  Your RT admin can find more information in the error logs.</&>
59 </&>
60 % } else {
61
62 <& /Elements/CollectionList, 
63     Query => $Query,
64     TotalFound => $ticketcount,
65     AllowSorting => 1,
66     OrderBy => $OrderBy,
67     Order => $Order,
68     Rows => $Rows,
69     Page => $Page,
70     Format => $Format,
71     Class => 'RT::Tickets',
72     BaseURL => $BaseURL
73
74    &>
75 % }
76 % $m->callback( ARGSRef => \%ARGS, CallbackName => 'AfterResults' );
77
78 % my %hiddens = (Query => $Query, Format => $Format, Rows => $Rows, OrderBy => $OrderBy, Order => $Order, HideResults => $HideResults, Page => $Page, SavedChartSearchId => $SavedChartSearchId );
79 <div align="right" class="refresh">
80 <form method="get" action="<%RT->Config->Get('WebPath')%>/Search/Results.html">
81 % foreach my $key (keys(%hiddens)) {
82 <input type="hidden" class="hidden" name="<%$key%>" value="<% defined($hiddens{$key})?$hiddens{$key}:'' %>" />
83 % }
84 <& /Elements/Refresh, Name => 'TicketsRefreshInterval', Default => $session{'tickets_refresh_interval'}||RT->Config->Get('SearchResultsRefreshInterval', $session{'CurrentUser'}) &>
85 <input type="submit" class="button" value="<&|/l&>Change</&>" />
86 </form>
87 </div>
88 <%INIT>
89 $m->callback( ARGSRef => \%ARGS, CallbackName => 'Initial' );
90
91 # Read from user preferences
92 my $prefs = $session{'CurrentUser'}->UserObj->Preferences("SearchDisplay") || {};
93
94 # These variables are what define a search_hash; this is also
95 # where we give sane defaults.
96 $Format      ||= $prefs->{'Format'} || RT->Config->Get('DefaultSearchResultFormat');
97 $Order       ||= $prefs->{'Order'} || 'ASC';
98 $OrderBy     ||= $prefs->{'OrderBy'} || 'id';
99
100 # Some forms pass in "RowsPerPage" rather than "Rows"
101 # We call it RowsPerPage everywhere else.
102
103 if ( !defined($Rows) ) {
104     if (defined $ARGS{'RowsPerPage'} ) {
105         $Rows = $ARGS{'RowsPerPage'};
106     } elsif ( defined $prefs->{'RowsPerPage'} ) {
107         $Rows = $prefs->{'RowsPerPage'};
108     } else {
109         $Rows = 50;
110     }
111 }
112 $Page = 1 unless $Page && $Page > 0;
113
114 $session{'i'}++;
115 $session{'tickets'} = RT::Tickets->new($session{'CurrentUser'}) ;
116 my ($ok, $msg) = $Query ? $session{'tickets'}->FromSQL($Query) : (1, "Vacuously OK");
117 # Provide an empty search if parsing failed
118 $session{'tickets'}->FromSQL("id < 0") unless ($ok);
119
120 if ($OrderBy =~ /\|/) {
121     # Multiple Sorts
122     my @OrderBy = split /\|/,$OrderBy;
123     my @Order = split /\|/,$Order;
124     $session{'tickets'}->OrderByCols(
125         map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } } ( 0
126         .. $#OrderBy ) );; 
127 } else {
128     $session{'tickets'}->OrderBy(FIELD => $OrderBy, ORDER => $Order); 
129 }
130 $session{'tickets'}->RowsPerPage( $Rows ) if $Rows;
131 $session{'tickets'}->GotoPage( $Page - 1 );
132
133 $session{'CurrentSearchHash'} = {
134     Format      => $Format,
135     Query       => $Query,
136     Page       => $Page,
137     Order       => $Order,
138     OrderBy     => $OrderBy,
139     RowsPerPage => $Rows
140 };
141
142
143 my ($title, $ticketcount) = (loc("Found tickets"), 0);
144 if ( $session{'tickets'}->Query()) {
145     $ticketcount = $session{tickets}->CountAll();
146     $title = loc('Found [quant,_1,ticket]', $ticketcount);
147 }
148
149 my $QueryString = "?".$m->comp('/Elements/QueryString',
150                                Query => $Query,
151                                Format => $Format,
152                                Rows => $Rows,
153                                OrderBy => $OrderBy,
154                                Order => $Order,
155                                Page => $Page);
156 my $ShortQueryString = "?".$m->comp('/Elements/QueryString', Query => $Query);
157
158 if ($ARGS{'TicketsRefreshInterval'}) {
159         $session{'tickets_refresh_interval'} = $ARGS{'TicketsRefreshInterval'};
160 }
161
162 my $refresh = $session{'tickets_refresh_interval'}
163     || RT->Config->Get('SearchResultsRefreshInterval', $session{'CurrentUser'} );
164
165 # Check $m->request_args, not $DECODED_ARGS, to avoid creating a new CSRF token on each refresh
166 if (RT->Config->Get('RestrictReferrer') and $refresh and not $m->request_args->{CSRF_Token}) {
167     my $token = RT::Interface::Web::StoreRequestToken( $session{'CurrentSearchHash'} );
168     $m->notes->{RefreshURL} = RT->Config->Get('WebURL')
169         . "Search/Results.html?CSRF_Token="
170             . $token;
171 }
172
173 my %link_rel;
174 my $genpage = sub {
175     return $m->comp(
176         '/Elements/QueryString',
177         Query   => $Query,
178         Format  => $Format,
179         Rows    => $Rows,
180         OrderBy => $OrderBy,
181         Order   => $Order,
182         Page    => shift(@_),
183     );
184 };
185
186 if ( RT->Config->Get('SearchResultsAutoRedirect') && $ticketcount == 1 &&
187     $session{tickets}->First ) {
188 # $ticketcount is not always precise unless $UseSQLForACLChecks is set to true,
189 # check $session{tickets}->First here is to make sure the ticket is there.
190     RT::Interface::Web::Redirect( RT->Config->Get('WebURL')
191             ."Ticket/Display.html?id=". $session{tickets}->First->id );
192 }
193
194 my $BaseURL = RT->Config->Get('WebPath')."/Search/Results.html?";
195 $link_rel{first} = $BaseURL . $genpage->(1)         if $Page > 1;
196 $link_rel{prev}  = $BaseURL . $genpage->($Page - 1) if $Page > 1;
197 $link_rel{next}  = $BaseURL . $genpage->($Page + 1) if ($Page * $Rows) < $ticketcount;
198 $link_rel{last}  = $BaseURL . $genpage->(POSIX::ceil($ticketcount/$Rows)) if $Rows and ($Page * $Rows) < $ticketcount;
199 </%INIT>
200 <%CLEANUP>
201 $session{'tickets'}->PrepForSerialization();
202 </%CLEANUP>
203 <%ARGS>
204 $Query => undef
205 $Format => undef 
206 $HideResults => 0
207 $Rows => undef
208 $Page => 1
209 $OrderBy => undef
210 $Order => undef
211 $SavedSearchId => undef
212 $SavedChartSearchId => undef
213 </%ARGS>