summaryrefslogtreecommitdiff
path: root/rt/html/Search/Build.html
blob: fa84f42a7443f3e8988feadd3ffcf7a4a2dd6cd5 (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
%# BEGIN BPS TAGGED BLOCK {{{
%# 
%# COPYRIGHT:
%#  
%# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC 
%#                                          <jesse@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 RT/Tickets_Overlay_SQL)
%#   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 &>
<& /Ticket/Elements/Tabs, 
    current_tab => "Search/Build.html".$QueryString, 
    Title => $title,
    Format => $Format,
    Query => $Query,
    Order => $Order,
    OrderBy => $OrderBy,
    Rows => $RowsPerPage
&>

<form method="post" action="Build.html" name="BuildQuery">
<input type="hidden" class="hidden" name="SearchId" value="<%$SearchId%>" />
<input type="hidden" class="hidden" name="Query" value="<%$Query%>" />
<input type="hidden" class="hidden" name="Format" value="<%$Format%>" />
<table width="100%" border="0" cellpadding="5">
<tr valign="top">
<td class="boxcontainer" rowspan="2" width="65%">
<& Elements/PickCriteria, query => $Query, cfqueues => $queues &>
<& /Elements/Submit, Caption => loc('Add these terms to your search'), Label => loc('Add'), Name => 'AddClause'&>
</td>

<td>
<& Elements/EditQuery,
    %ARGS,
    actions => \@actions,
    optionlist => $optionlist,
    Description => $Description &>
<& /Elements/Submit, Label => loc('Add and Search'), Name => 'DoSearch'&>
</td>
</tr>

<tr valign="top">
<td>
<& Elements/EditSearches, CurrentSearch => $search_hash, Dirty => $dirty, SearchId => $SearchId &>
</td>
</tr>

<tr>
<td colspan="2" class="boxcontainer">

<& Elements/DisplayOptions, %ARGS, Format=> $Format,
AvailableColumns => $AvailableColumns,  CurrentFormat => $CurrentFormat, RowsPerPage => $RowsPerPage, OrderBy => $OrderBy, Order => $Order &>
<& /Elements/Submit, Label => loc('Add and Search'), Name => 'DoSearch'&>
</td>
</tr>
</table>
</form>

<%INIT>
use RT::Interface::Web::QueryBuilder;
use RT::Interface::Web::QueryBuilder::Tree;

my $search_hash = {};
my $search;
my $title = loc("Query Builder");

# {{{ Clear out unwanted data
if ( $NewQuery or $ARGS{'Delete'} ) {

    # Wipe all data-carrying variables clear if we want a new
    # search, or we're deleting an old one..
    $Query       = '';
    $Format      = '';
    $Description = '';
    $SearchId    = '';
    $Order       = '';
    $OrderBy     = '';
    $RowsPerPage = undef;

    # ($search hasn't been set yet; no need to clear)

    # ..then wipe the session out..
    undef $session{'CurrentSearchHash'};

    # ..and the search results.
    $session{'tickets'}->CleanSlate() if defined $session{'tickets'};
}

# }}}

if (ref $OrderBy eq "ARRAY") {
    $OrderBy = join("|", @$OrderBy);
}
if (ref $Order eq "ARRAY") {
    $Order = join("|", @$Order);
}

# {{{ Attempt to load what we can from the session, set defaults

# We don't read or write to the session again until the end
$search_hash = $session{'CurrentSearchHash'};

# Read from user preferences
my $prefs = $session{'CurrentUser'}->UserObj->Preferences("SearchDisplay") || {};

# These variables are what define a search_hash; this is also
# where we give sane defaults.
$Query       ||= $search_hash->{'Query'};
$Format      ||= $search_hash->{'Format'} || $prefs->{'Format'};
$Description ||= $search_hash->{'Description'};
$SearchId    ||= $search_hash->{'SearchId'} || 'new';
$Order       ||= $search_hash->{'Order'} || $prefs->{'Order'} || 'ASC';
$OrderBy     ||= $search_hash->{'OrderBy'} || $prefs->{'OrderBy'} || 'id';

unless ( defined $RowsPerPage ) {
    if ( defined $search_hash->{'RowsPerPage'} ) {
        $RowsPerPage = $search_hash->{'RowsPerPage'};
    }
    elsif ( defined $prefs->{'RowsPerPage'} ) {
        $RowsPerPage = $prefs->{'RowsPerPage'};
    }
    else {
        $RowsPerPage = 50;
    }
}

  $search ||= $search_hash->{'Object'};

# }}}

my @actions = ();

# Clean unwanted junk from the format
$Format = $m->comp( '/Elements/ScrubHTML', Content => $Format ) if ($Format);

# {{{ If we're asked to delete the current search, make it go away and reset the search parameters
if ( $ARGS{'Delete'} ) {

    # We set $SearchId to 'new' above already, so peek into the %ARGS
    my ($container_object, $search_id) = _parse_saved_search ($ARGS{'SearchId'});
    if ($container_object && $container_object->id) {
	# We have the object the entry is an attribute on; delete the
	# entry..
	$container_object->Attributes->DeleteEntry(
            Name => 'SavedSearch',
	    id   => $search_id
            );
    }
}

# }}}

# {{{ If the user wants to copy a search, uncouple from the one that this was based on, but don't erase the $Query or $Format
if ( $ARGS{'CopySearch'} ) {
    $SearchId    = 'new';
    $search      = undef;
    $Description = loc( "[_1] copy", $Description );
}

# }}}

# {{{ if we're asked to revert the current search, we just want to load it
if ( $ARGS{'Revert'} ) {
    $ARGS{'LoadSavedSearch'} = $SearchId;
}

# }}}

# {{{ if we're asked to load a search, load it.

if ( my ($container_object, $search_id ) = _parse_saved_search ($ARGS{'LoadSavedSearch'})) {
    $search = $container_object->Attributes->WithId($search_id);

    # We have a $search and now; import the others
    $SearchId    = $ARGS{'LoadSavedSearch'};
    $Description = $search->Description;
    $Format      = $search->SubValue('Format');
    $Query       = $search->SubValue('Query');
    $Order       = $search->SubValue('Order');
    $OrderBy     = $search->SubValue('OrderBy');
    $RowsPerPage = $search->SubValue('RowsPerPage');
}

# }}}

# {{{ if we're asked to save the current search, save it
if ( $ARGS{'Save'} ) {
    if ( $search && $search->id ) {
	# permission check
	if ($search->Object->isa('RT::System')) {
	    unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
		Abort("No permission to save system-wide searches");
	    }
	}

        # This search is based on a previously loaded search -- so
        # just update the current search object with new values
        $search->SetSubValues(
            Format      => $Format,
            Query       => $Query,
            Order       => $Order,
            OrderBy     => $OrderBy,
            RowsPerPage => $RowsPerPage,
        );
        $search->SetDescription($Description);

    }
    elsif ( $SearchId eq 'new' ) {
        my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
        my ( $ok, $search_msg ) = $saved_search->Save(
            Privacy      => $ARGS{'Owner'},
            Name         => $Description,
            SearchParams => {
                Format      => $Format,
                Query       => $Query,
                Order       => $Order,
                OrderBy     => $OrderBy,
                RowsPerPage => $RowsPerPage } );

	if ($ok) {
	    $search = $session{'CurrentUser'}->UserObj->Attributes->WithId($saved_search->Id);
	    # Build new SearchId
	    $SearchId =
		    ref( $session{'CurrentUser'}->UserObj ) . '-'
			. $session{'CurrentUser'}->UserObj->Id
			. '-SavedSearch-'
			. $search->Id;
	}
	else {
	    push @actions, [ loc("Can't find a saved search to work with").': '.loc($search_msg), 0 ];
	}
    }
    else {
        push @actions, [ loc("Can't save this search"), 0 ];
    }

}

# }}}


# {{{ Parse the query
use Regexp::Common qw /delimited/;

# States
use constant VALUE   => 1;
use constant AGGREG  => 2;
use constant OP      => 4;
use constant PAREN   => 8;
use constant KEYWORD => 16;

my $_match = sub {

    # Case insensitive equality
    my ( $y, $x ) = @_;
    return 1 if $x =~ /^$y$/i;

    #  return 1 if ((lc $x) eq (lc $y)); # Why isnt this equiv?
    return 0;
};

my $ParseQuery = sub {
    my $string  = shift;
    my $tree    = shift;
    my $actions = shift;
    my $want    = KEYWORD | PAREN;
    my $last    = undef;

    my $depth = 1;

    # make a tree root
    $$tree = RT::Interface::Web::QueryBuilder::Tree->new;
    my $root       = RT::Interface::Web::QueryBuilder::Tree->new( 'AND', $$tree );
    my $parentnode = $root;

    # on new searches, we're passed undef but still need to construct the
    # RT::Interface::Web::QueryBuilder::Tree.  Quiet warning
    return unless defined $string;

    # get the FIELDS from Tickets_Overlay
    my $tickets = new RT::Tickets( $session{'CurrentUser'} );
    my %FIELDS  = %{ $tickets->FIELDS };

    # Lower Case version of FIELDS, for case insensitivity
    my %lcfields = map { ( lc($_) => $_ ) } ( keys %FIELDS );

    my @tokens     = qw[VALUE AGGREG OP PAREN KEYWORD];
    my $re_aggreg  = qr[(?i:AND|OR)];
    my $re_value   = qr[$RE{delimited}{-delim=>qq{\'\"}}|\d+];
    my $re_keyword = qr[$RE{delimited}{-delim=>qq{\'\"}}|(?:\{|\}|\w|\.)+];
    my $re_op      =
      qr[=|!=|>=|<=|>|<|(?i:IS NOT)|(?i:IS)|(?i:NOT LIKE)|(?i:LIKE)]
      ;    # long to short
    my $re_paren = qr'\(|\)';

    # assume that $ea is AND if it is not set
    my ( $ea, $key, $op, $value ) = ( "AND", "", "", "" );

    # order of matches in the RE is important.. op should come early,
    # because it has spaces in it.  otherwise "NOT LIKE" might be parsed
    # as a keyword or value.

    while (
        $string =~ /(
                      $re_aggreg
                      |$re_op
                      |$re_keyword
                      |$re_value
                      |$re_paren
                     )/igx
      )
    {
        my $val     = $1;
        my $current = 0;

        # Highest priority is last
        $current = OP    if $_match->( $re_op,    $val );
        $current = VALUE if $_match->( $re_value, $val );
        $current = KEYWORD
          if $_match->( $re_keyword, $val ) && ( $want & KEYWORD );
        $current = AGGREG if $_match->( $re_aggreg, $val );
        $current = PAREN  if $_match->( $re_paren,  $val );

        unless ( $current && $want & $current ) {

            # Error
            # FIXME: I will only print out the highest $want value
            my $token = $tokens[ ( ( log $want ) / ( log 2 ) ) ];
            push @$actions,
              [
                loc("Error near ->[_1]<- expecting a [_2] in '[_3]'",
                                  $val,              $token, $string ),
                -1
              ];
        }

        # State Machine:
        my $parentdepth = $depth;

        # Parens are highest priority
        if ( $current & PAREN ) {
            if ( $val eq "(" ) {
                $depth++;

                # make a new node that the clauses can be children of
                $parentnode = RT::Interface::Web::QueryBuilder::Tree->new( $ea, $parentnode );
            }
            else {
                $depth--;
                $parentnode = $parentnode->getParent();
            }

            $want = KEYWORD | PAREN | AGGREG;
        }
        elsif ( $current & AGGREG ) {
            $ea   = $val;
            $parentnode->setNodeValue($ea);
            $want = KEYWORD | PAREN;
        }
        elsif ( $current & KEYWORD ) {
            $key  = $val;
            $want = OP;
        }
        elsif ( $current & OP ) {
            $op   = $val;
            $want = VALUE;
        }
        elsif ( $current & VALUE ) {
            $value = $val;

            # Remove surrounding quotes from $key, $val
            # (in future, simplify as for($key,$val) { action on $_ })
            if ( $key =~ /$RE{delimited}{-delim=>qq{\'\"}}/ ) {
                substr( $key, 0,  1 ) = "";
                substr( $key, -1, 1 ) = "";
            }
            if ( $val =~ /$RE{delimited}{-delim=>qq{\'\"}}/ ) {
                substr( $val, 0,  1 ) = "";
                substr( $val, -1, 1 ) = "";
            }

            # Unescape escaped characters
            $key =~ s!\\(.)!$1!g;
            $val =~ s!\\(.)!$1!g;

            my $class;

            my ($key_base, $subkey)  = split(/\./,$key,2);
            $key_base =~ s/\..*$//; # Strip off .EmailAddress, for example

            if ( exists $lcfields{lc $key_base } ) {
                $key   = $lcfields{lc $key_base } . (defined $subkey ? '.'.$subkey : '');
                $class = $FIELDS{$key_base}->[0];
            }
            elsif ( $key =~ /^C(?:ustom)?F(?:ield)?\.{(.*)}$/i ) {
                $class = $FIELDS{'CF'}->[0];
            }

            if ( $class ne 'INT' ) {
                $val = "'$val'";
            }

            push @$actions, [ loc("Unknown field: [_1]", $key), -1 ] unless $class;

            $want = PAREN | AGGREG;
        }
        else {
            push @$actions, [ loc("I'm lost"), -1 ];
        }

        if ( $current & VALUE ) {
            if ( $key =~ /^CF./ ) {
                $key = "'" . $key . "'";
            }
            my $clause = {
                Key   => $key,
                Op    => $op,
                Value => $val
            };

            # explicity add a child to it
            RT::Interface::Web::QueryBuilder::Tree->new( $clause, $parentnode );

            ( $ea, $key, $op, $value ) = ( "", "", "", "" );

        }

        $last = $current;
    }    # while

    push @$actions, [ loc("Incomplete query"), -1 ]
      unless ( ( $want | PAREN ) || ( $want | KEYWORD ) );

    push @$actions, [ loc("Incomplete Query"), -1 ]
      unless ( $last && ( $last | PAREN ) || ( $last || VALUE ) );

    # This will never happen, because the parser will complain
    push @$actions, [ loc("Mismatched parentheses"), -1 ]
      unless $depth == 1;
};

my $tree;
{
    my @parsing_errors;
    $ParseQuery->( $Query, \$tree, \@parsing_errors );

    # if parsing went poorly, send them to the edit page
    # to fix it
    if ( @parsing_errors ) {
        return $m->comp(
            "Edit.html",
            Query   => $Query,
            actions => \@parsing_errors
        );
    }
}

$Query  = "";

my @options = $tree->GetDisplayedNodes;

my @current_values = grep { defined } @options[@clauses];

# {{{ Move things around
if ( $ARGS{"Up"} ) {
    if (@current_values) {
        foreach my $value (@current_values) {
            my $index = $value->getIndex();
            if ( $value->getIndex() > 0 ) {
                my $parent = $value->getParent();
                $parent->removeChild($index);
                $parent->insertChild( $index - 1, $value );
                $value = $parent->getChild( $index - 1 );
            }
            else {
                push( @actions, [ loc("error: can't move up"), -1 ] );
            }
        }
    }
    else {
        push( @actions, [ loc("error: nothing to move"), -1 ] );
    }
}
elsif ( $ARGS{"Down"} ) {
    if (@current_values) {
        foreach my $value (@current_values) {
            my $index  = $value->getIndex();
            my $parent = $value->getParent();
            if ( $value->getIndex() < ( $parent->getChildCount - 1 ) ) {
                $parent->removeChild($index);
                $parent->insertChild( $index + 1, $value );
                $value = $parent->getChild( $index + 1 );
            }
            else {
                push( @actions, [ loc("error: can't move down"), -1 ] );
            }
        }
    }
    else {
        push( @actions, [ loc("error: nothing to move"), -1 ] );
    }
}
elsif ( $ARGS{"Left"} ) {
    if (@current_values) {
        foreach my $value (@current_values) {
            my $parent      = $value->getParent();
            my $grandparent = $parent->getParent();
            if ( !$grandparent->isRoot ) {
                my $index = $parent->getIndex();
                $parent->removeChild($value);
                $grandparent->insertChild( $index, $value );
                if ( $parent->isLeaf() ) {
                    $grandparent->removeChild($parent);
                }
            }
            else {
                push( @actions, [ loc("error: can't move left"), -1 ] );
            }
        }
    }
    else {
        push( @actions, [ loc("error: nothing to move"), -1 ] );
    }
}
elsif ( $ARGS{"Right"} ) {
    if (@current_values) {
        foreach my $value (@current_values) {
            my $parent = $value->getParent();
            my $index  = $value->getIndex();
            my $newparent;
            if ( $index > 0 ) {
                my $sibling = $parent->getChild( $index - 1 );
                if ( ref( $sibling->getNodeValue ) ) {
                    $parent->removeChild($value);
                    my $newtree = RT::Interface::Web::QueryBuilder::Tree->new( 'AND', $parent );
                    $newtree->addChild($value);
                }
                else {
                    $parent->removeChild($index);
                    $sibling->addChild($value);
                }
            }
            else {
                $parent->removeChild($value);
                $newparent = RT::Interface::Web::QueryBuilder::Tree->new( 'AND', $parent );
                $newparent->addChild($value);
            }
        }
    }
    else {
        push( @actions, [ loc("error: nothing to move"), -1 ] );
    }
}
elsif ( $ARGS{"DeleteClause"} ) {
    if (@current_values) {
        $_->getParent()->removeChild($_) for @current_values;
		@current_values = ();
    }
    else {
        push( @actions, [ loc("error: nothing to delete"), -1 ] );
    }
}
elsif ( $ARGS{"Toggle"} ) {
    my $ea;
    if (@current_values) {
        foreach my $value (@current_values) {
            my $parent = $value->getParent();

            if ( $parent->getNodeValue eq 'AND' ) {
                $parent->setNodeValue('OR');
            }
            else {
                $parent->setNodeValue('AND');
            }
        }
    }
    else {
        push( @actions, [ loc("error: nothing to toggle"), -1 ] );
    }
}

# {{{ Try to find if we're adding a clause
foreach my $arg ( keys %ARGS ) {
    if (
            $arg =~ m/^ValueOf(\w+|'CF.{.*?}')$/
        && ( ref $ARGS{$arg} eq "ARRAY"
            ? grep { $_ ne "" } @{ $ARGS{$arg} }
            : $ARGS{$arg} ne "" )
      )
    {

        # We're adding a $1 clause
        my $field = $1;
        my ( $keyword, $op, $value );

        #figure out if it's a grouping
        if ( $ARGS{ $field . "Field" } ) {
            $keyword = $ARGS{ $field . "Field" };
        }
        else {
            $keyword = $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 .. @ops - 1 ) {
            my ( $op, $value ) = ( $ops[$i], $values[$i] );
            next if $value eq "";

            if ( $value eq 'NULL' && $op =~ /=/ ) {
                if ( $op eq '=' ) {
                    $op = "IS";
                }
                elsif ( $op eq '!=' ) {
                    $op = "IS NOT";
                }

                # This isn't "right", but...
                # It has to be this way until #5182 is fixed
                $value = "'NULL'";
            }
            else {
                $value = "'$value'";
            }

            my $clause = {
                Key   => $keyword,
                Op    => $op,
                Value => $value
            };

            my $newnode = RT::Interface::Web::QueryBuilder::Tree->new($clause);
            if (@current_values) {
                foreach my $value (@current_values) {
                    my $newindex = $value->getIndex() + 1;
                    $value->insertSibling( $newindex, $newnode );
                    $value = $newnode;
                }
            }
            else {
                $tree->getChild(0)->addChild($newnode);
                @current_values = $newnode;
            }
            $newnode->getParent()->setNodeValue( $ARGS{'AndOr'} );
        }
    }
}

# }}}

$tree->PruneChildlessAggregators;

# }}}

# {{{ Rebuild $Query based on the additions / movements
$Query      = "";
my $optionlist_arrayref;

($Query, $optionlist_arrayref) = $tree->GetQueryAndOptionList(\@current_values);
  
my $optionlist = join "\n", map { qq(<option value="$_->{INDEX}" $_->{SELECTED}>) 
                                  . ("&nbsp;" x (5 * $_->{DEPTH}))
                                  . $m->interp->apply_escapes($_->{TEXT}, 'h') . qq(</option>) } @$optionlist_arrayref;




# }}}

# }}}

my $queues = $tree->GetReferencedQueues;

# {{{ Deal with format changes
my ( $AvailableColumns, $CurrentFormat );
( $Format, $AvailableColumns, $CurrentFormat ) = $m->comp(
    'Elements/BuildFormatString',
    cfqueues => $queues,
    %ARGS, Format => $Format
);

# }}}

# {{{ If we're modifying an old query, check if it has changed
my $dirty = 0;
$dirty = 1
  if defined $search
  and ($search->SubValue('Format') ne $Format
    or $search->SubValue('Query')       ne $Query
    or $search->SubValue('Order')       ne $Order
    or $search->SubValue('OrderBy')     ne $OrderBy
    or $search->SubValue('RowsPerPage') ne $RowsPerPage );

# }}}

# {{{ Push the updates into the session so we don't loose 'em
$search_hash->{'SearchId'}    = $SearchId;
$search_hash->{'Format'}      = $Format;
$search_hash->{'Query'}       = $Query;
$search_hash->{'Description'} = $Description;
$search_hash->{'Object'}      = $search;
$search_hash->{'Order'}       = $Order;
$search_hash->{'OrderBy'}     = $OrderBy;
$search_hash->{'RowsPerPage'} = $RowsPerPage;

$session{'CurrentSearchHash'} = $search_hash;

# }}}

# {{{ Show the results, if we were asked.
if ( $ARGS{"DoSearch"}) {
    $m->comp(
        "Results.html",
        Query   => $Query,
        Format  => $Format,
        Order   => $Order,
        OrderBy => $OrderBy,
        Rows    => $RowsPerPage
    );
    $m->comp('/Elements/Footer');
    $m->abort();
}

# }}}

# {{{ Build a querystring for the tabs

my $QueryString;
if ($NewQuery) {
    $QueryString = '?NewQuery=1';
}
else {
    $QueryString = '?'
      . $m->comp(
        '/Elements/QueryString',
        Query   => $Query,
        Format  => $Format,
        Order   => $Order,
        OrderBy => $OrderBy,
        Rows    => $RowsPerPage
      )
      if ($Query);
}

# }}}

</%INIT>

<%ARGS>
$NewQuery => 0
$SearchId => undef
$Query => undef
$Format => undef 
$Description => undef
$Order => undef
$OrderBy => undef
$RowsPerPage => undef
$HideResults => 0
@clauses => ()
</%ARGS>