1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Interface::Web::QueryBuilder::Tree;
54 use Tree::Simple qw/use_weak_refs/;
55 use base qw/Tree::Simple/;
59 RT::Interface::Web::QueryBuilder::Tree - subclass of Tree::Simple used in Query Builder
63 This class provides support functionality for the Query Builder (Search/Build.html).
64 It is a subclass of L<Tree::Simple>.
68 =head2 TraversePrePost PREFUNC POSTFUNC
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.
74 (Note that unlike Tree::Simple's C<traverse>, it actually calls its functions
75 on the root node passed to it.)
80 my ($self, $prefunc, $postfunc) = @_;
82 # XXX: if pre or post action changes siblings (delete or adds)
83 # we could have problems
84 $prefunc->($self) if $prefunc;
86 foreach my $child ($self->getAllChildren()) {
87 $child->TraversePrePost($prefunc, $postfunc);
90 $postfunc->($self) if $postfunc;
93 =head2 GetReferencedQueues
95 Returns a hash reference; each queue referenced with an '=' operation
96 will appear as a key whose value is 1.
100 sub GetReferencedQueues {
109 return if $node->isRoot;
110 return unless $node->isLeaf;
112 my $clause = $node->getNodeValue();
113 return unless $clause->{Key} eq 'Queue';
114 return unless $clause->{Op} eq '=';
116 my $value = $clause->{Value};
117 $value =~ s/\\(.)/$1/g if $value =~ s/^'(.*)'$/$1/;
118 $queues->{ $value } = 1;
125 =head2 GetQueryAndOptionList SELECTED_NODES
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.
139 sub GetQueryAndOptionList {
141 my $selected_nodes = shift;
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"] : '';
149 return (join ' ', map $_->{'TEXT'}, @$list), $list;
152 =head2 PruneChildLessAggregators
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.
159 sub PruneChildlessAggregators {
162 $self->TraversePrePost(
166 return unless $node->isLeaf;
168 # We're only looking for aggregators (AND/OR)
169 return if ref $node->getNodeValue;
171 return if $node->isRoot;
173 # OK, this is a childless aggregator. Remove self.
174 $node->getParent->removeChild($node);
180 =head2 GetDisplayedNodes
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.
188 sub GetDisplayedNodes {
189 return map $_->{NODE}, @{ (shift)->__LinearizeTree };
193 sub __LinearizeTree {
196 my ($list, $i) = ([], 0);
198 $self->TraversePrePost( sub {
200 return if $node->isRoot;
203 if( $node->getIndex > 0 ) {
204 $str .= " ". $node->getParent->getNodeValue ." ";
207 unless( $node->isLeaf ) {
211 my $clause = $node->getNodeValue;
212 $str .= $clause->{Key};
213 $str .= " ". $clause->{Op};
214 $str .= " ". $clause->{Value};
217 $str =~ s/^\s+|\s+$//;
228 return if $node->isRoot;
229 return if $node->isLeaf;
230 $list->[-1]->{'TEXT'} .= ' )';
240 CurrentUser => '', #XXX: Hack
243 my $string = $args{'Query'};
247 my %field = %{ RT::Tickets->new( $args{'CurrentUser'} )->FIELDS };
248 my %lcfield = map { ( lc($_) => $_ ) } keys %field;
253 $callback{'OpenParen'} = sub {
254 $node = __PACKAGE__->new( 'AND', $node );
256 $callback{'CloseParen'} = sub { $node = $node->getParent };
257 $callback{'EntryAggregator'} = sub { $node->setNodeValue( $_[0] ) };
258 $callback{'Condition'} = sub {
259 my ($key, $op, $value) = @_;
261 my ($main_key) = split /[.]/, $key;
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];
270 push @results, [ $args{'CurrentUser'}->loc("Unknown field: [_1]", $key), -1 ]
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;
280 if ($key =~ s/(['\\])/\\$1/g or $key =~ /[^{}\w\.]/) {
284 my $clause = { Key => $key, Op => $op, Value => $value };
285 $node->addChild( __PACKAGE__->new( $clause ) );
287 $callback{'Error'} = sub { push @results, @_ };
290 RT::SQL::Parse($string, \%callback);
294 RT::Base->_ImportOverlays();