Upgrade to 4.2.8
[usit-rt.git] / share / html / Search / Build.html
CommitLineData
84fb5b46
MKG
1%# BEGIN BPS TAGGED BLOCK {{{
2%#
3%# COPYRIGHT:
4%#
3ffc5f4f 5%# This software is Copyright (c) 1996-2014 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%#
49%# Data flow here:
50%# The page receives a Query from the previous page, and maybe arguments
51%# corresponding to actions. (If it doesn't get a Query argument, it pulls
52%# one out of the session hash. Also, it could be getting just a raw query from
53%# Build/Edit.html (Advanced).)
54%#
55%# After doing some stuff with default arguments and saved searches, the ParseQuery
3ffc5f4f 56%# function (which is similar to, but not the same as, _parser in lib/RT/Tickets.pm)
84fb5b46
MKG
57%# converts the Query into a RT::Interface::Web::QueryBuilder::Tree. This mason file
58%# then adds stuff to or modifies the tree based on the actions that had been requested
59%# by clicking buttons. It then calls GetQueryAndOptionList on the tree to generate
60%# the SQL query (which is saved as a hidden input) and the option list for the Clauses
61%# box in the top right corner.
62%#
63%# Worthwhile refactoring: the tree manipulation code for the actions could use some cleaning
64%# up. The node-adding code is different in the "add" actions from in ParseQuery, which leads
65%# to things like ParseQuery correctly not quoting numbers in numerical fields, while the "add"
66%# action does quote it (this breaks SQLite).
67%#
68<& /Elements/Header, Title => $title &>
69<& /Elements/Tabs, %TabArgs &>
70
71<form method="post" action="Build.html" name="BuildQuery" id="BuildQuery">
72<input type="hidden" class="hidden" name="SavedSearchId" value="<% $saved_search{'Id'} %>" />
73<input type="hidden" class="hidden" name="SavedChartSearchId" value="<% $ARGS{'SavedChartSearchId'} %>" />
74<input type="hidden" class="hidden" name="Query" value="<% $query{'Query'} %>" />
75<input type="hidden" class="hidden" name="Format" value="<% $query{'Format'} %>" />
76
77
78
79
80<div id="pick-criteria">
b5747ff2 81 <& Elements/PickCriteria, query => $query{'Query'}, queues => $queues &>
84fb5b46
MKG
82</div>
83<& /Elements/Submit, Label => loc('Add these terms'), SubmitId => 'AddClause', Name => 'AddClause'&>
84<& /Elements/Submit, Label => loc('Add these terms and Search'), SubmitId => 'DoSearch', Name => 'DoSearch'&>
85
86
87<div id="editquery">
88<& Elements/EditQuery,
89 %ARGS,
90 actions => \@actions,
91 optionlist => $optionlist,
92 Description => $saved_search{'Description'},
93 &>
94</div>
95<div id="editsearches">
96 <& Elements/EditSearches, %saved_search, CurrentSearch => \%query &>
97</div>
98
99<span id="display-options">
100<& Elements/DisplayOptions,
101 %ARGS, %query,
102 AvailableColumns => $AvailableColumns,
103 CurrentFormat => $CurrentFormat,
104&>
105</span>
106<& /Elements/Submit, Label => loc('Update format and Search'), Name => 'DoSearch', id=>"formatbuttons"&>
107</form>
108
109<%INIT>
110use RT::Interface::Web::QueryBuilder;
111use RT::Interface::Web::QueryBuilder::Tree;
112
113$ARGS{SavedChartSearchId} ||= 'new';
114
115my $title = loc("Query Builder");
116
117my %query;
118for( qw(Query Format OrderBy Order RowsPerPage) ) {
119 $query{$_} = $ARGS{$_};
120}
121
122my %saved_search;
123my @actions = $m->comp( 'Elements/EditSearches:Init', %ARGS, Query => \%query, SavedSearch => \%saved_search);
124
125if ( $NewQuery ) {
126
127 # Wipe all data-carrying variables clear if we want a new
128 # search, or we're deleting an old one..
129 %query = ();
130 %saved_search = ( Id => 'new' );
131
132 # ..then wipe the session out..
133 delete $session{'CurrentSearchHash'};
134
135 # ..and the search results.
136 $session{'tickets'}->CleanSlate if defined $session{'tickets'};
137}
138
139{ # Attempt to load what we can from the session and preferences, set defaults
140
141 my $current = $session{'CurrentSearchHash'};
142 my $prefs = $session{'CurrentUser'}->UserObj->Preferences("SearchDisplay") || {};
3ffc5f4f
MKG
143 my $default = { Query => '',
144 Format => '',
145 OrderBy => RT->Config->Get('DefaultSearchResultOrderBy'),
146 Order => RT->Config->Get('DefaultSearchResultOrder'),
147 RowsPerPage => 50 };
84fb5b46
MKG
148
149 for( qw(Query Format OrderBy Order RowsPerPage) ) {
150 $query{$_} = $current->{$_} unless defined $query{$_};
151 $query{$_} = $prefs->{$_} unless defined $query{$_};
152 $query{$_} = $default->{$_} unless defined $query{$_};
153 }
154
155 for( qw(Order OrderBy) ) {
156 if (ref $query{$_} eq "ARRAY") {
157 $query{$_} = join( '|', @{ $query{$_} } );
158 }
159 }
160 if ( $query{'Format'} ) {
161 # Clean unwanted junk from the format
162 $query{'Format'} = $m->comp( '/Elements/ScrubHTML', Content => $query{'Format'} );
163 }
164}
165
166my $ParseQuery = sub {
167 my ($string, $results) = @_;
168
169 my $tree = RT::Interface::Web::QueryBuilder::Tree->new('AND');
170 @$results = $tree->ParseSQL( Query => $string, CurrentUser => $session{'CurrentUser'} );
171
172 return $tree;
173};
174
175my @parse_results;
176my $tree = $ParseQuery->( $query{'Query'}, \@parse_results );
177
178# if parsing went poorly, send them to the edit page to fix it
179if ( @parse_results ) {
180 push @actions, @parse_results;
181 return $m->comp(
182 "Edit.html",
183 Query => $query{'Query'},
184 Format => $query{'Format'},
185 SavedSearchId => $saved_search{'Id'},
186 SavedChartSearchId => $ARGS{'SavedChartSearchId'},
187 actions => \@actions,
188 );
189}
190
191my @options = $tree->GetDisplayedNodes;
192my @current_values = grep defined, @options[@clauses];
193my @new_values = ();
194
3ffc5f4f
MKG
195my $cf_field_names =
196 join "|",
197 map quotemeta,
198 grep { $RT::Tickets::FIELD_METADATA{$_}->[0] eq 'CUSTOMFIELD' }
199 sort keys %RT::Tickets::FIELD_METADATA;
200
84fb5b46
MKG
201# Try to find if we're adding a clause
202foreach my $arg ( keys %ARGS ) {
3ffc5f4f 203 next unless $arg =~ m/^ValueOf(\w+|($cf_field_names).\{.*?\})$/
84fb5b46
MKG
204 && ( ref $ARGS{$arg} eq "ARRAY"
205 ? grep $_ ne '', @{ $ARGS{$arg} }
206 : $ARGS{$arg} ne '' );
207
208 # We're adding a $1 clause
209 my $field = $1;
210
211 my ($op, $value);
212
213 #figure out if it's a grouping
214 my $keyword = $ARGS{ $field . "Field" } || $field;
215
216 my ( @ops, @values );
217 if ( ref $ARGS{ 'ValueOf' . $field } eq "ARRAY" ) {
218 # we have many keys/values to iterate over, because there is
219 # more than one CF with the same name.
220 @ops = @{ $ARGS{ $field . 'Op' } };
221 @values = @{ $ARGS{ 'ValueOf' . $field } };
222 }
223 else {
224 @ops = ( $ARGS{ $field . 'Op' } );
225 @values = ( $ARGS{ 'ValueOf' . $field } );
226 }
227 $RT::Logger->error("Bad Parameters passed into Query Builder")
228 unless @ops == @values;
229
230 for ( my $i = 0; $i < @ops; $i++ ) {
231 my ( $op, $value ) = ( $ops[$i], $values[$i] );
232 next if !defined $value || $value eq '';
3ffc5f4f 233 my $rawvalue = $value;
84fb5b46 234
403d7b0b 235 if ( $value =~ /^NULL$/i && $op =~ /=/ ) {
84fb5b46
MKG
236 if ( $op eq '=' ) {
237 $op = "IS";
238 }
239 elsif ( $op eq '!=' ) {
240 $op = "IS NOT";
241 }
242 }
243 elsif ($value =~ /\D/) {
244 $value =~ s/(['\\])/\\$1/g;
245 $value = "'$value'";
246 }
247
3ffc5f4f
MKG
248 if ($keyword =~ s/(['\\])/\\$1/g or $keyword =~ /[^{}\w\.]/) {
249 $keyword = "'$keyword'";
84fb5b46
MKG
250 }
251
252 my $clause = {
253 Key => $keyword,
254 Op => $op,
3ffc5f4f
MKG
255 Value => $value,
256 RawValue => $rawvalue,
84fb5b46
MKG
257 };
258
259 push @new_values, RT::Interface::Web::QueryBuilder::Tree->new($clause);
260 }
261}
262
263
264push @actions, $m->comp('Elements/EditQuery:Process',
265 %ARGS,
266 Tree => $tree,
267 Selected => \@current_values,
268 New => \@new_values,
269);
270
271# Rebuild $Query based on the additions / movements
272
273my $optionlist_arrayref;
274($query{'Query'}, $optionlist_arrayref) = $tree->GetQueryAndOptionList(\@current_values);
275
276my $optionlist = join "\n", map { qq(<option value="$_->{INDEX}" $_->{SELECTED}>)
277 . ("&nbsp;" x (5 * $_->{DEPTH}))
278 . $m->interp->apply_escapes($_->{TEXT}, 'h') . qq(</option>) } @$optionlist_arrayref;
279
280
281my $queues = $tree->GetReferencedQueues;
282
283# Deal with format changes
284my ( $AvailableColumns, $CurrentFormat );
285( $query{'Format'}, $AvailableColumns, $CurrentFormat ) = $m->comp(
286 'Elements/BuildFormatString',
287 %ARGS,
b5747ff2 288 queues => $queues,
84fb5b46
MKG
289 Format => $query{'Format'},
290);
291
292
293# if we're asked to save the current search, save it
294push @actions, $m->comp( 'Elements/EditSearches:Save', %ARGS, Query => \%query, SavedSearch => \%saved_search);
295
296# Populate the "query" context with saved search data
297
298if ($ARGS{SavedSearchSave}) {
299 $query{'SavedSearchId'} = $saved_search{'Id'};
300}
301
302# Push the updates into the session so we don't lose 'em
303
304$session{'CurrentSearchHash'} = {
305 %query,
306 SearchId => $saved_search{'Id'},
307 Object => $saved_search{'Object'},
308 Description => $saved_search{'Description'},
309};
310
311
312# Show the results, if we were asked.
313
314if ( $ARGS{'DoSearch'} ) {
315 my $redir_query_string = $m->comp(
316 '/Elements/QueryString',
317 %query,
318 SavedChartSearchId => $ARGS{'SavedChartSearchId'},
319 SavedSearchId => $saved_search{'Id'},
320 );
b5747ff2 321 RT::Interface::Web::Redirect(RT->Config->Get('WebURL') . 'Search/Results.html?' . $redir_query_string);
84fb5b46
MKG
322 $m->abort;
323}
324
325
326# Build a querystring for the tabs
327
328my %TabArgs = ();
329if ($NewQuery) {
330 $TabArgs{QueryString} = 'NewQuery=1';
331}
332elsif ( $query{'Query'} ) {
333 $TabArgs{QueryArgs} = \%query;
334}
335
336</%INIT>
337
338<%ARGS>
339$NewQuery => 0
340@clauses => ()
341</%ARGS>