rt 4.0.23
[freeside.git] / rt / lib / RT / Interface / Web / QueryBuilder / Tree.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 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
49 package RT::Interface::Web::QueryBuilder::Tree;
50
51 use strict;
52 use warnings;
53
54 use Tree::Simple qw/use_weak_refs/;
55 use base qw/Tree::Simple/;
56
57 =head1 NAME
58
59   RT::Interface::Web::QueryBuilder::Tree - subclass of Tree::Simple used in Query Builder
60
61 =head1 DESCRIPTION
62
63 This class provides support functionality for the Query Builder (Search/Build.html).
64 It is a subclass of L<Tree::Simple>.
65
66 =head1 METHODS
67
68 =head2 TraversePrePost PREFUNC POSTFUNC
69
70 Traverses the tree depth-first.  Before processing the node's children,
71 calls PREFUNC with the node as its argument; after processing all of the
72 children, calls POSTFUNC with the node as its argument.
73
74 (Note that unlike Tree::Simple's C<traverse>, it actually calls its functions
75 on the root node passed to it.)
76
77 =cut
78
79 sub TraversePrePost {
80    my ($self, $prefunc, $postfunc) = @_;
81
82    # XXX: if pre or post action changes siblings (delete or adds)
83    # we could have problems
84    $prefunc->($self) if $prefunc;
85
86    foreach my $child ($self->getAllChildren()) { 
87            $child->TraversePrePost($prefunc, $postfunc);
88    }
89    
90    $postfunc->($self) if $postfunc;
91 }
92
93 =head2 GetReferencedQueues
94
95 Returns a hash reference; each queue referenced with an '=' operation
96 will appear as a key whose value is 1.
97
98 =cut
99
100 sub GetReferencedQueues {
101     my $self = shift;
102
103     my $queues = {};
104
105     $self->traverse(
106         sub {
107             my $node = shift;
108
109             return if $node->isRoot;
110             return unless $node->isLeaf;
111
112             my $clause = $node->getNodeValue();
113             return unless $clause->{Key} eq 'Queue';
114             return unless $clause->{Op} eq '=';
115
116             my $value = $clause->{Value};
117             $value =~ s/\\(.)/$1/g if $value =~ s/^'(.*)'$/$1/;
118             $queues->{ $value } = 1;
119         }
120     );
121
122     return $queues;
123 }
124
125 =head2 GetQueryAndOptionList SELECTED_NODES
126
127 Given an array reference of tree nodes that have been selected by the user,
128 traverses the tree and returns the equivalent SQL query and a list of hashes
129 representing the "clauses" select option list.  Each has contains the keys
130 TEXT, INDEX, SELECTED, and DEPTH.  TEXT is the displayed text of the option
131 (including parentheses, not including indentation); INDEX is the 0-based
132 index of the option in the list (also used as its CGI parameter); SELECTED
133 is either 'SELECTED' or '', depending on whether the node corresponding
134 to the select option was in the SELECTED_NODES list; and DEPTH is the
135 level of indentation for the option.
136
137 =cut 
138
139 sub GetQueryAndOptionList {
140     my $self           = shift;
141     my $selected_nodes = shift;
142
143     my $list = $self->__LinearizeTree;
144     foreach my $e( @$list ) {
145         $e->{'DEPTH'}    = $e->{'NODE'}->getDepth;
146         $e->{'SELECTED'} = (grep $_ == $e->{'NODE'}, @$selected_nodes)? qq[ selected="selected"] : '';
147     }
148
149     return (join ' ', map $_->{'TEXT'}, @$list), $list;
150 }
151
152 =head2 PruneChildLessAggregators
153
154 If tree manipulation has left it in a state where there are ANDs, ORs,
155 or parenthesizations with no children, get rid of them.
156
157 =cut
158
159 sub PruneChildlessAggregators {
160     my $self = shift;
161
162     $self->TraversePrePost(
163         undef,
164         sub {
165             my $node = shift;
166             return unless $node->isLeaf;
167
168             # We're only looking for aggregators (AND/OR)
169             return if ref $node->getNodeValue;
170
171             return if $node->isRoot;
172
173             # OK, this is a childless aggregator.  Remove self.
174             $node->getParent->removeChild($node);
175             $node->DESTROY;
176         }
177     );
178 }
179
180 =head2 GetDisplayedNodes
181
182 This function returns a list of the nodes of the tree in depth-first
183 order which correspond to options in the "clauses" multi-select box.
184 In fact, it's all of them but the root and its child.
185
186 =cut
187
188 sub GetDisplayedNodes {
189     return map $_->{NODE}, @{ (shift)->__LinearizeTree };
190 }
191
192
193 sub __LinearizeTree {
194     my $self = shift;
195
196     my ($list, $i) = ([], 0);
197
198     $self->TraversePrePost( sub {
199         my $node = shift;
200         return if $node->isRoot;
201
202         my $str = '';
203         if( $node->getIndex > 0 ) {
204             $str .= " ". $node->getParent->getNodeValue ." ";
205         }
206
207         unless( $node->isLeaf ) {
208             $str .= '( ';
209         } else {
210
211             my $clause = $node->getNodeValue;
212             $str .= $clause->{Key};
213             $str .= " ". $clause->{Op};
214             $str .= " ". $clause->{Value};
215
216         }
217         $str =~ s/^\s+|\s+$//;
218
219         push @$list, {
220             NODE     => $node,
221             TEXT     => $str,
222             INDEX    => $i,
223         };
224
225         $i++;
226     }, sub {
227         my $node = shift;
228         return if $node->isRoot;
229         return if $node->isLeaf;
230         $list->[-1]->{'TEXT'} .= ' )';
231     });
232
233     return $list;
234 }
235
236 sub ParseSQL {
237     my $self = shift;
238     my %args = (
239         Query => '',
240         CurrentUser => '', #XXX: Hack
241         @_
242     );
243     my $string = $args{'Query'};
244
245     my @results;
246
247     my %field = %{ RT::Tickets->new( $args{'CurrentUser'} )->FIELDS };
248     my %lcfield = map { ( lc($_) => $_ ) } keys %field;
249
250     my $node =  $self;
251
252     my %callback;
253     $callback{'OpenParen'} = sub {
254         $node = __PACKAGE__->new( 'AND', $node );
255     };
256     $callback{'CloseParen'} = sub { $node = $node->getParent };
257     $callback{'EntryAggregator'} = sub { $node->setNodeValue( $_[0] ) };
258     $callback{'Condition'} = sub {
259         my ($key, $op, $value) = @_;
260
261         my ($main_key) = split /[.]/, $key;
262
263         my $class;
264         if ( exists $lcfield{ lc $main_key } ) {
265             $key =~ s/^[^.]+/ $lcfield{ lc $main_key } /e;
266             ($main_key) = split /[.]/, $key;  # make the case right
267             $class = $field{ $main_key }->[0];
268         }
269         unless( $class ) {
270             push @results, [ $args{'CurrentUser'}->loc("Unknown field: [_1]", $key), -1 ]
271         }
272
273         if ( lc $op eq 'is' || lc $op eq 'is not' ) {
274             $value = 'NULL'; # just fix possible mistakes here
275         } elsif ( $value !~ /^[+-]?[0-9]+$/ ) {
276             $value =~ s/(['\\])/\\$1/g;
277             $value = "'$value'";
278         }
279
280         if ($key =~ s/(['\\])/\\$1/g or $key =~ /[^{}\w\.]/) {
281             $key = "'$key'";
282         }
283
284         my $clause = { Key => $key, Op => $op, Value => $value };
285         $node->addChild( __PACKAGE__->new( $clause ) );
286     };
287     $callback{'Error'} = sub { push @results, @_ };
288
289     require RT::SQL;
290     RT::SQL::Parse($string, \%callback);
291     return @results;
292 }
293
294 RT::Base->_ImportOverlays();
295
296 1;