1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2022 Best Practical Solutions, LLC
%# <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
%#
%# Data flow here:
%# The page receives a Query from the previous page, and maybe arguments
%# corresponding to actions. (If it doesn't get a Query argument, it pulls
%# one out of the session hash. Also, it could be getting just a raw query from
%# Build/Edit.html (Advanced).)
%#
%# After doing some stuff with default arguments and saved searches, the ParseQuery
%# function (which is similar to, but not the same as, _parser in lib/RT/Tickets.pm)
%# converts the Query into a RT::Interface::Web::QueryBuilder::Tree. This mason file
%# then adds stuff to or modifies the tree based on the actions that had been requested
%# by clicking buttons. It then calls GetQueryAndOptionList on the tree to generate
%# the SQL query (which is saved as a hidden input) and the option list for the Clauses
%# box in the top right corner.
%#
%# Worthwhile refactoring: the tree manipulation code for the actions could use some cleaning
%# up. The node-adding code is different in the "add" actions from in ParseQuery, which leads
%# to things like ParseQuery correctly not quoting numbers in numerical fields, while the "add"
%# action does quote it (this breaks SQLite).
%#
<& /Elements/Header, Title => $title &>
<& /Elements/Tabs, %TabArgs &>
<form method="post" action="Build.html" name="BuildQuery" id="BuildQuery">
<input type="hidden" class="hidden" name="SavedSearchId" value="<% $saved_search{'Id'} %>" />
<input type="hidden" class="hidden" name="SavedChartSearchId" value="<% $ARGS{'SavedChartSearchId'} %>" />
<input type="hidden" class="hidden" name="Query" value="<% $query{'Query'} %>" />
<input type="hidden" class="hidden" name="Format" value="<% $query{'Format'} %>" />
<input type="hidden" class="hidden" name="ObjectType" value="<% $query{'ObjectType'} %>" />
<input type="hidden" class="hidden" name="Class" value="<% $Class %>" />
% if ( $ResultPage ) {
<input type="hidden" class="hidden" name="ResultPage" value="<% $ResultPage %>" />
% }
% if ( $query{ExtraQueryParams} ) {
% for my $input ( ref $query{ExtraQueryParams} eq 'ARRAY' ? @{$query{ExtraQueryParams}} : $query{ExtraQueryParams} ) {
<input type="hidden" class="hidden" name="ExtraQueryParams" value="<% $input %>" />
% if ( defined $query{$input} ) {
<input type="hidden" class="hidden" name="<% $input %>" value="<% $query{$input} %>" />
% }
% }
% }
<div class="row">
<div class="col-xl-7">
<div id="pick-criteria">
<& Elements/PickCriteria, query => $query{'Query'}, queues => $queues, catalogs => $catalogs, %ARGS &>
</div>
<div class="form-row">
<div class="col-12">
<& /Elements/Submit, Label => loc('Add these terms'), SubmitId => 'AddClause', Name => 'AddClause'&>
<& /Elements/Submit, Label => loc('Add these terms and Search'), SubmitId => 'DoSearch', Name => 'DoSearch'&>
</div>
</div>
</div>
<div id="editquery" class="col-xl-5">
<& Elements/EditQuery,
%ARGS,
actions => \@actions,
optionlist => $optionlist,
Description => $saved_search{'Description'},
&>
<div id="editsearches">
<& Elements/EditSearches, %ARGS, %saved_search, CurrentSearch => \%query, Title => loc('Saved searches') &>
</div>
</div>
</div>
<div id="display-options" class="row">
<& Elements/DisplayOptions,
%ARGS, %query,
AvailableColumns => $AvailableColumns,
CurrentFormat => $CurrentFormat,
&>
</div>
<div class="form-row">
<div class="col-12">
<& /Elements/Submit, Label => loc('Update format and Search'), Name => 'DoSearch', id=>"formatbuttons"&>
</div>
</div>
</form>
<%INIT>
use RT::Interface::Web::QueryBuilder;
use RT::Interface::Web::QueryBuilder::Tree;
$ARGS{SavedChartSearchId} ||= 'new';
my $title;
my @cf_field_names;
if ( $Class eq 'RT::Transactions' ) {
$title = loc('Transaction Query Builder');
@cf_field_names = grep { $RT::Transactions::FIELD_METADATA{$_}->[0] eq 'CUSTOMFIELD' }
sort keys %RT::Transactions::FIELD_METADATA;
}
elsif ( $Class eq 'RT::Assets' ) {
$title = loc('Asset Query Builder');
@cf_field_names = grep { $RT::Assets::FIELD_METADATA{$_}->[0] eq 'CUSTOMFIELD' }
sort keys %RT::Assets::FIELD_METADATA;
}
else {
$title = loc("Query Builder");
@cf_field_names = grep { $RT::Tickets::FIELD_METADATA{$_}->[0] eq 'CUSTOMFIELD' }
sort keys %RT::Tickets::FIELD_METADATA;
}
my %query;
for( qw(Query Format OrderBy Order RowsPerPage Class ObjectType ExtraQueryParams), @ExtraQueryParams ) {
$query{$_} = $ARGS{$_} if defined $ARGS{$_};
}
my %saved_search;
my @actions = $m->comp( 'Elements/EditSearches:Init', %ARGS, Query => \%query, SavedSearch => \%saved_search );
my $hash_name = join '-', 'CurrentSearchHash', $Class, $ObjectType || ();
my $session_name = join '-', 'collection', $Class, $ObjectType || ();
if ( $NewQuery ) {
# Wipe all data-carrying variables clear if we want a new
# search, or we're deleting an old one..
%query = map { $_ => $ARGS{$_} } grep { defined $ARGS{$_} } 'ExtraQueryParams', @ExtraQueryParams;
%saved_search = ( Id => 'new' );
# ..then wipe the session out..
delete $session{$hash_name};
# ..and the search results.
$session{$session_name}->CleanSlate if defined $session{$session_name};
}
{ # Attempt to load what we can from the session and preferences, set defaults
my $current = $session{$hash_name};
my $prefs;
if ( $Class eq 'RT::Tickets' ) {
# User-level search preferences currently apply only to tickets
$prefs = $session{'CurrentUser'}->UserObj->Preferences("SearchDisplay") || {};
}
my %orderby = (
'RT::Tickets' => RT->Config->Get('DefaultSearchResultOrderBy'),
'RT::Transactions' => RT->Config->Get('TransactionDefaultSearchResultOrderBy')->{$ObjectType},
'RT::Assets' => RT->Config->Get('AssetDefaultSearchResultOrderBy'),
);
my %order = (
'RT::Tickets' => RT->Config->Get('DefaultSearchResultOrder'),
'RT::Transactions' => RT->Config->Get('TransactionDefaultSearchResultOrder')->{$ObjectType},
'RT::Assets' => RT->Config->Get('AssetDefaultSearchResultOrder'),
);
my $default = {
Query => '',
Format => '',
OrderBy => $orderby{$Class},
Order => $order{$Class},
ObjectType => $ObjectType,
RowsPerPage => 50
};
for( qw(Query Format OrderBy Order RowsPerPage ObjectType) ) {
$query{$_} = $current->{$_} unless defined $query{$_};
if ( $prefs ) {
$query{$_} = $prefs->{$_} unless defined $query{$_};
}
$query{$_} = $default->{$_} unless defined $query{$_};
}
for( qw(Order OrderBy) ) {
if (ref $query{$_} eq "ARRAY") {
$query{$_} = join( '|', @{ $query{$_} } );
}
}
if ( $query{'Format'} ) {
# Clean unwanted junk from the format
$query{'Format'} = $m->comp( '/Elements/ScrubHTML', Content => $query{'Format'}, Restrictive => 1 );
}
if ( !$ARGS{SavedSearchLoad} and ( my $extra_params = $current->{ExtraQueryParams} ) ) {
$query{ExtraQueryParams} //= $extra_params;
for my $param ( ref $extra_params eq 'ARRAY' ? @$extra_params : $extra_params ) {
$query{$param} //= $current->{$param};
}
}
}
my $ParseQuery = sub {
my ($string, $results) = @_;
my $tree = RT::Interface::Web::QueryBuilder::Tree->new('AND');
@$results = $tree->ParseSQL( Query => $string, CurrentUser => $session{'CurrentUser'}, Class => $Class );
return $tree;
};
my @parse_results;
my $tree = $ParseQuery->( $query{'Query'}, \@parse_results );
# if parsing went poorly, send them to the edit page to fix it
if ( @parse_results ) {
push @actions, @parse_results;
return $m->comp(
"Edit.html",
Query => $query{'Query'},
Format => $query{'Format'},
SavedSearchId => $saved_search{'Id'},
SavedChartSearchId => $ARGS{'SavedChartSearchId'},
actions => \@actions,
);
}
my @options = $tree->GetDisplayedNodes;
my @current_values = grep defined, @options[@clauses];
my @new_values = ();
my $cf_field_names = join "|", map quotemeta, @cf_field_names;
# Try to find if we're adding a clause
foreach my $arg ( keys %ARGS ) {
next unless $arg =~ m/^ValueOf(\w+|($cf_field_names).\{.*?\}|CustomRole.\{.*?\})$/
&& ( ref $ARGS{$arg} eq "ARRAY"
? grep $_ ne '', @{ $ARGS{$arg} }
: $ARGS{$arg} ne '' );
# We're adding a $1 clause
my $field = $1;
my ($op, $value);
#figure out if it's a grouping
my $keyword = $ARGS{ $field . "Field" } || $field;
my ( @ops, @values );
if ( ref $ARGS{ 'ValueOf' . $field } eq "ARRAY" ) {
# we have many keys/values to iterate over, because there is
# more than one CF with the same name.
@ops = @{ $ARGS{ $field . 'Op' } };
@values = @{ $ARGS{ 'ValueOf' . $field } };
}
else {
@ops = ( $ARGS{ $field . 'Op' } );
@values = ( $ARGS{ 'ValueOf' . $field } );
}
$RT::Logger->error("Bad Parameters passed into Query Builder")
unless @ops == @values;
for ( my $i = 0; $i < @ops; $i++ ) {
my ( $op, $value ) = ( $ops[$i], $values[$i] );
next if !defined $value || $value eq '';
my $clause = {
Key => $keyword,
Op => $op,
Value => $value,
QuoteValue => $value =~ /^[+-]?[0-9]+$/ ? 0 : 1,
};
push @new_values, RT::Interface::Web::QueryBuilder::Tree->new($clause);
}
}
push @actions, $m->comp('Elements/EditQuery:Process',
%ARGS,
Tree => $tree,
Selected => \@current_values,
New => \@new_values,
);
# Rebuild $Query based on the additions / movements
my $optionlist_arrayref;
($query{'Query'}, $optionlist_arrayref) = $tree->GetQueryAndOptionList(\@current_values);
my $optionlist = join "\n", map { qq(<option value="$_->{INDEX}" $_->{SELECTED}>)
. (" " x (5 * $_->{DEPTH}))
. $m->interp->apply_escapes($_->{TEXT}, 'h') . qq(</option>) } @$optionlist_arrayref;
my $queues = $Class ne 'RT::Assets' ? $tree->GetReferencedQueues : {};
my $catalogs = $Class eq 'RT::Assets' ? $tree->GetReferencedCatalogs : {};
# Deal with format changes
my ( $AvailableColumns, $CurrentFormat );
( $query{'Format'}, $AvailableColumns, $CurrentFormat ) = $m->comp(
'Elements/BuildFormatString',
%ARGS,
queues => $queues,
catalogs => $catalogs,
Format => $query{'Format'},
);
# if we're asked to save the current search, save it
push @actions, $m->comp( 'Elements/EditSearches:Save', %ARGS, Query => \%query, SavedSearch => \%saved_search);
# Populate the "query" context with saved search data
if ($ARGS{SavedSearchSave}) {
$query{'SavedSearchId'} = $saved_search{'Id'};
}
# Push the updates into the session so we don't lose 'em
$session{$hash_name} = {
%query,
SearchId => $saved_search{'Id'},
Object => $saved_search{'Object'},
Description => $saved_search{'Description'},
};
# Show the results, if we were asked.
if ( $ARGS{'DoSearch'} ) {
my $redir_query_string = $m->comp(
'/Elements/QueryString',
%query,
SavedChartSearchId => $ARGS{'SavedChartSearchId'},
SavedSearchId => $saved_search{'Id'},
);
RT::Interface::Web::Redirect("$ResultPage?$redir_query_string");
$m->abort;
}
# Build a querystring for the tabs
my %TabArgs = ();
if ($NewQuery) {
$TabArgs{QueryString} = $m->comp(
'/Elements/QueryString',
NewQuery => 1,
$Class ne 'RT::Tickets' ? ( Class => $Class, ObjectType => $ObjectType ) : ()
);
}
elsif ( $query{'Query'} ) {
$TabArgs{QueryArgs} = \%query;
}
$m->callback( ARGSRef => \%ARGS, Query => \%query, CallbackName => 'BeforeDisplay' );
</%INIT>
<%ARGS>
$NewQuery => 0
@clauses => ()
$Class => 'RT::Tickets'
$ObjectType => $Class eq 'RT::Transactions' ? 'RT::Ticket' : ''
@ExtraQueryParams => ()
$ResultPage => RT->Config->Get('WebPath') . '/Search/Results.html'
</%ARGS>
|