]> git.uio.no Git - usit-rt.git/blame - share/html/Search/Build.html
Ported move-script from 3.8 (no api-changes)
[usit-rt.git] / share / html / Search / Build.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%#
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
56%# function (which is similar to, but not the same as, _parser in lib/RT/Tickets_SQL.pm)
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") || {};
143 my $default = { Query => '', Format => '', OrderBy => 'id', Order => 'ASC', RowsPerPage => 50 };
144
145 for( qw(Query Format OrderBy Order RowsPerPage) ) {
146 $query{$_} = $current->{$_} unless defined $query{$_};
147 $query{$_} = $prefs->{$_} unless defined $query{$_};
148 $query{$_} = $default->{$_} unless defined $query{$_};
149 }
150
151 for( qw(Order OrderBy) ) {
152 if (ref $query{$_} eq "ARRAY") {
153 $query{$_} = join( '|', @{ $query{$_} } );
154 }
155 }
156 if ( $query{'Format'} ) {
157 # Clean unwanted junk from the format
158 $query{'Format'} = $m->comp( '/Elements/ScrubHTML', Content => $query{'Format'} );
159 }
160}
161
162my $ParseQuery = sub {
163 my ($string, $results) = @_;
164
165 my $tree = RT::Interface::Web::QueryBuilder::Tree->new('AND');
166 @$results = $tree->ParseSQL( Query => $string, CurrentUser => $session{'CurrentUser'} );
167
168 return $tree;
169};
170
171my @parse_results;
172my $tree = $ParseQuery->( $query{'Query'}, \@parse_results );
173
174# if parsing went poorly, send them to the edit page to fix it
175if ( @parse_results ) {
176 push @actions, @parse_results;
177 return $m->comp(
178 "Edit.html",
179 Query => $query{'Query'},
180 Format => $query{'Format'},
181 SavedSearchId => $saved_search{'Id'},
182 SavedChartSearchId => $ARGS{'SavedChartSearchId'},
183 actions => \@actions,
184 );
185}
186
187my @options = $tree->GetDisplayedNodes;
188my @current_values = grep defined, @options[@clauses];
189my @new_values = ();
190
191# Try to find if we're adding a clause
192foreach my $arg ( keys %ARGS ) {
193 next unless $arg =~ m/^ValueOf(\w+|'CF.{.*?}')$/
194 && ( ref $ARGS{$arg} eq "ARRAY"
195 ? grep $_ ne '', @{ $ARGS{$arg} }
196 : $ARGS{$arg} ne '' );
197
198 # We're adding a $1 clause
199 my $field = $1;
200
201 my ($op, $value);
202
203 #figure out if it's a grouping
204 my $keyword = $ARGS{ $field . "Field" } || $field;
205
206 my ( @ops, @values );
207 if ( ref $ARGS{ 'ValueOf' . $field } eq "ARRAY" ) {
208 # we have many keys/values to iterate over, because there is
209 # more than one CF with the same name.
210 @ops = @{ $ARGS{ $field . 'Op' } };
211 @values = @{ $ARGS{ 'ValueOf' . $field } };
212 }
213 else {
214 @ops = ( $ARGS{ $field . 'Op' } );
215 @values = ( $ARGS{ 'ValueOf' . $field } );
216 }
217 $RT::Logger->error("Bad Parameters passed into Query Builder")
218 unless @ops == @values;
219
220 for ( my $i = 0; $i < @ops; $i++ ) {
221 my ( $op, $value ) = ( $ops[$i], $values[$i] );
222 next if !defined $value || $value eq '';
223
403d7b0b 224 if ( $value =~ /^NULL$/i && $op =~ /=/ ) {
84fb5b46
MKG
225 if ( $op eq '=' ) {
226 $op = "IS";
227 }
228 elsif ( $op eq '!=' ) {
229 $op = "IS NOT";
230 }
231 }
232 elsif ($value =~ /\D/) {
233 $value =~ s/(['\\])/\\$1/g;
234 $value = "'$value'";
235 }
236
237 if ($keyword =~ /^'CF\.{(.*)}'/) {
238 my $cf = $1;
239 $cf =~ s/(['\\])/\\$1/g;
240 $keyword = "'CF.{$cf}'";
241 }
242
243 my $clause = {
244 Key => $keyword,
245 Op => $op,
246 Value => $value
247 };
248
249 push @new_values, RT::Interface::Web::QueryBuilder::Tree->new($clause);
250 }
251}
252
253
254push @actions, $m->comp('Elements/EditQuery:Process',
255 %ARGS,
256 Tree => $tree,
257 Selected => \@current_values,
258 New => \@new_values,
259);
260
261# Rebuild $Query based on the additions / movements
262
263my $optionlist_arrayref;
264($query{'Query'}, $optionlist_arrayref) = $tree->GetQueryAndOptionList(\@current_values);
265
266my $optionlist = join "\n", map { qq(<option value="$_->{INDEX}" $_->{SELECTED}>)
267 . ("&nbsp;" x (5 * $_->{DEPTH}))
268 . $m->interp->apply_escapes($_->{TEXT}, 'h') . qq(</option>) } @$optionlist_arrayref;
269
270
271my $queues = $tree->GetReferencedQueues;
272
273# Deal with format changes
274my ( $AvailableColumns, $CurrentFormat );
275( $query{'Format'}, $AvailableColumns, $CurrentFormat ) = $m->comp(
276 'Elements/BuildFormatString',
277 %ARGS,
b5747ff2 278 queues => $queues,
84fb5b46
MKG
279 Format => $query{'Format'},
280);
281
282
283# if we're asked to save the current search, save it
284push @actions, $m->comp( 'Elements/EditSearches:Save', %ARGS, Query => \%query, SavedSearch => \%saved_search);
285
286# Populate the "query" context with saved search data
287
288if ($ARGS{SavedSearchSave}) {
289 $query{'SavedSearchId'} = $saved_search{'Id'};
290}
291
292# Push the updates into the session so we don't lose 'em
293
294$session{'CurrentSearchHash'} = {
295 %query,
296 SearchId => $saved_search{'Id'},
297 Object => $saved_search{'Object'},
298 Description => $saved_search{'Description'},
299};
300
301
302# Show the results, if we were asked.
303
304if ( $ARGS{'DoSearch'} ) {
305 my $redir_query_string = $m->comp(
306 '/Elements/QueryString',
307 %query,
308 SavedChartSearchId => $ARGS{'SavedChartSearchId'},
309 SavedSearchId => $saved_search{'Id'},
310 );
b5747ff2 311 RT::Interface::Web::Redirect(RT->Config->Get('WebURL') . 'Search/Results.html?' . $redir_query_string);
84fb5b46
MKG
312 $m->abort;
313}
314
315
316# Build a querystring for the tabs
317
318my %TabArgs = ();
319if ($NewQuery) {
320 $TabArgs{QueryString} = 'NewQuery=1';
321}
322elsif ( $query{'Query'} ) {
323 $TabArgs{QueryArgs} = \%query;
324}
325
326</%INIT>
327
328<%ARGS>
329$NewQuery => 0
330@clauses => ()
331</%ARGS>