]> git.uio.no Git - usit-rt.git/blob - share/html/Search/Elements/EditQuery
5d312002e310d5b7aa657b10af5fd324cd94f071
[usit-rt.git] / share / html / Search / Elements / EditQuery
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
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 <& NewListActions, actions => $actions &>
49 <&|/Widgets/TitleBox, title => join(': ', grep defined, loc("Current search"), $Description) &>
50
51 <select size="10" name="clauses" style="width: 100%" multiple="multiple">
52 % $m->out($optionlist);
53 </select>
54
55 <p align="center">
56 <input type="submit" class="button" name="Up" value=" &uarr; " />
57 <input type="submit" class="button" name="Down" value=" &darr; " />
58 <input type="submit" class="button" name="Left" value=" &larr; " />
59 <input type="submit" class="button" name="Right" value=" &rarr; " />
60 <input type="submit" class="button" name="Toggle" value="<&|/l&>And/Or</&>" />
61 <input type="submit" class="button" name="DeleteClause" value="<&|/l&>Delete</&>" />
62 %#<input type="submit" class="button" name="EditQuery" value="Advanced" />
63 </p>
64
65 </&>
66 <%ARGS>
67 $Description => undef
68 $optionlist => ''
69 $actions => []
70 </%ARGS>
71
72 <%METHOD Process>
73 <%ARGS>
74 $Tree
75 $Selected
76 @New       => ()
77 </%ARGS>
78 <%INIT>
79
80 my @NewSelection = ();
81
82 my @results;
83 if ( $ARGS{'Up'} || $ARGS{'Down'} ) {
84     if (@$Selected) {
85         foreach my $value (@$Selected) {
86             my $parent = $value->getParent;
87             my $index = $value->getIndex;
88             my $newindex = $index;
89             $newindex++ if $ARGS{'Down'};
90             $newindex-- if $ARGS{'Up'};
91             if ( $newindex < 0 || $newindex >= $parent->getChildCount ) {
92                 push( @results, [ loc("error: can't move up"), -1 ] ) if $ARGS{'Up'};
93                 push( @results, [ loc("error: can't move down"), -1 ] ) if $ARGS{'Down'};
94                 next;
95             }
96
97             $parent->removeChild( $index );
98             $parent->insertChild( $newindex, $value );
99         }
100     }
101     else {
102         push( @results, [ loc("error: nothing to move"), -1 ] );
103     }
104 }
105 elsif ( $ARGS{"Left"} ) {
106     if (@$Selected) {
107         foreach my $value (@$Selected) {
108             my $parent = $value->getParent;
109             if( $value->isRoot || $parent->isRoot ) {
110                 push( @results, [ loc("error: can't move left"), -1 ] );
111                 next;
112             }
113
114             my $grandparent = $parent->getParent;
115             if( $grandparent->isRoot ) {
116                 push( @results, [ loc("error: can't move left"), -1 ] );
117                 next;
118             }
119             
120             my $index = $parent->getIndex;
121             $parent->removeChild($value);
122             $grandparent->insertChild( $index, $value );
123             if ( $parent->isLeaf ) {
124                 $grandparent->removeChild($parent);
125             }
126         }
127     }
128     else {
129         push( @results, [ loc("error: nothing to move"), -1 ] );
130     }
131 }
132 elsif ( $ARGS{"Right"} ) {
133     if (@$Selected) {
134         foreach my $value (@$Selected) {
135             my $parent = $value->getParent;
136             my $index  = $value->getIndex;
137
138             my $newparent;
139             if ( $index > 0 ) {
140                 my $sibling = $parent->getChild( $index - 1 );
141                 $newparent = $sibling unless $sibling->isLeaf;
142             }
143             $newparent ||= RT::Interface::Web::QueryBuilder::Tree->new( $ARGS{'AndOr'} || 'AND', $parent );
144
145             $parent->removeChild($value);
146             $newparent->addChild($value);
147         }
148     }
149     else {
150         push( @results, [ loc("error: nothing to move"), -1 ] );
151     }
152 }
153 elsif ( $ARGS{"DeleteClause"} ) {
154     if (@$Selected) {
155         my (@top);
156         my %Selected = map { $_ => 1 } @$Selected;
157         foreach my $node ( @$Selected ) {
158             my $tmp = $node->getParent;
159             while ( !$Selected{ $tmp } && !$tmp->isRoot ) {
160                 $tmp = $tmp->getParent;
161             }
162             next if $Selected{ $tmp };
163             push @top, $node;
164         }
165
166         my %seen;
167         my @non_siblings_top = grep !$seen{ $_->getParent }++, @top;
168
169         foreach ( @New ) {
170             my $add = $_->clone;
171             foreach my $selected( @non_siblings_top ) {
172                 my $newindex = $selected->getIndex + 1;
173                 $selected->insertSibling( $newindex, $add );
174             }
175             $add->getParent->setNodeValue( $ARGS{'AndOr'} );
176             push @NewSelection, $add;
177         }
178         @New = ();
179     
180         while( my $node = shift @top ) {
181             my $parent = $node->getParent;
182             $parent->removeChild($node);
183             $node->DESTROY;
184         }
185         @$Selected = ();
186     }
187     else {
188         push( @results, [ loc("error: nothing to delete"), -1 ] );
189     }
190 }
191 elsif ( $ARGS{"Toggle"} ) {
192     if (@$Selected) {
193         my %seen;
194         my @unique_nodes = grep !$seen{ $_ + 0 }++,
195             map ref $_->getNodeValue? $_->getParent: $_,
196             @$Selected;
197
198         foreach my $node ( @unique_nodes ) {
199             if ( $node->getNodeValue eq 'AND' ) {
200                 $node->setNodeValue('OR');
201             }
202             else {
203                 $node->setNodeValue('AND');
204             }
205         }
206     }
207     else {
208         push( @results, [ loc("error: nothing to toggle"), -1 ] );
209     }
210 }
211
212 if ( @New && @$Selected ) {
213     my %seen;
214     my @non_siblings_selected = grep !$seen{ $_->getParent }++, @$Selected;
215
216     foreach ( @New ) {
217         my $add = $_->clone;
218         foreach my $selected( @non_siblings_selected ) {
219             my $newindex = $selected->getIndex + 1;
220             $selected->insertSibling( $newindex, $add );
221         }
222         $add->getParent->setNodeValue( $ARGS{'AndOr'} );
223         push @NewSelection, $add;
224     }
225     @$Selected = ();
226 }
227 elsif ( @New ) {
228     foreach ( @New ) {
229         my $add = $_->clone;
230         $Tree->addChild( $add );
231         push @NewSelection, $add;
232     }
233     $Tree->setNodeValue( $ARGS{'AndOr'} );
234 }
235 $_->DESTROY foreach @New;
236
237 push @$Selected, @NewSelection;
238
239 $Tree->PruneChildlessAggregators;
240
241 return @results;
242 </%INIT>
243 </%METHOD>