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