1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2016 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 $queues->{ $clause->{RawValue} } = 1;
123 =head2 GetQueryAndOptionList SELECTED_NODES
125 Given an array reference of tree nodes that have been selected by the user,
126 traverses the tree and returns the equivalent SQL query and a list of hashes
127 representing the "clauses" select option list. Each has contains the keys
128 TEXT, INDEX, SELECTED, and DEPTH. TEXT is the displayed text of the option
129 (including parentheses, not including indentation); INDEX is the 0-based
130 index of the option in the list (also used as its CGI parameter); SELECTED
131 is either 'SELECTED' or '', depending on whether the node corresponding
132 to the select option was in the SELECTED_NODES list; and DEPTH is the
133 level of indentation for the option.
137 sub GetQueryAndOptionList {
139 my $selected_nodes = shift;
141 my $list = $self->__LinearizeTree;
142 foreach my $e( @$list ) {
143 $e->{'DEPTH'} = $e->{'NODE'}->getDepth;
144 $e->{'SELECTED'} = (grep $_ == $e->{'NODE'}, @$selected_nodes)? qq[ selected="selected"] : '';
147 return (join ' ', map $_->{'TEXT'}, @$list), $list;
150 =head2 PruneChildLessAggregators
152 If tree manipulation has left it in a state where there are ANDs, ORs,
153 or parenthesizations with no children, get rid of them.
157 sub PruneChildlessAggregators {
160 $self->TraversePrePost(
164 return unless $node->isLeaf;
166 # We're only looking for aggregators (AND/OR)
167 return if ref $node->getNodeValue;
169 return if $node->isRoot;
171 # OK, this is a childless aggregator. Remove self.
172 $node->getParent->removeChild($node);
178 =head2 GetDisplayedNodes
180 This function returns a list of the nodes of the tree in depth-first
181 order which correspond to options in the "clauses" multi-select box.
182 In fact, it's all of them but the root and its child.
186 sub GetDisplayedNodes {
187 return map $_->{NODE}, @{ (shift)->__LinearizeTree };
191 sub __LinearizeTree {
194 my ($list, $i) = ([], 0);
196 $self->TraversePrePost( sub {
198 return if $node->isRoot;
201 if( $node->getIndex > 0 ) {
202 $str .= " ". $node->getParent->getNodeValue ." ";
205 unless( $node->isLeaf ) {
209 my $clause = $node->getNodeValue;
210 $str .= $clause->{Key};
211 $str .= " ". $clause->{Op};
212 $str .= " ". $clause->{Value};
215 $str =~ s/^\s+|\s+$//;
226 return if $node->isRoot;
227 return if $node->isLeaf;
228 $list->[-1]->{'TEXT'} .= ' )';
238 CurrentUser => '', #XXX: Hack
241 my $string = $args{'Query'};
245 my %field = %{ RT::Tickets->new( $args{'CurrentUser'} )->FIELDS };
246 my %lcfield = map { ( lc($_) => $_ ) } keys %field;
251 $callback{'OpenParen'} = sub {
252 $node = __PACKAGE__->new( 'AND', $node );
254 $callback{'CloseParen'} = sub { $node = $node->getParent };
255 $callback{'EntryAggregator'} = sub { $node->setNodeValue( $_[0] ) };
256 $callback{'Condition'} = sub {
257 my ($key, $op, $value) = @_;
258 my $rawvalue = $value;
260 my ($main_key) = split /[.]/, $key;
263 if ( exists $lcfield{ lc $main_key } ) {
264 $key =~ s/^[^.]+/ $lcfield{ lc $main_key } /e;
265 ($main_key) = split /[.]/, $key; # make the case right
266 $class = $field{ $main_key }->[0];
269 push @results, [ $args{'CurrentUser'}->loc("Unknown field: [_1]", $key), -1 ]
272 if ( lc $op eq 'is' || lc $op eq 'is not' ) {
273 $value = 'NULL'; # just fix possible mistakes here
274 } elsif ( $value !~ /^[+-]?[0-9]+$/ ) {
275 $value =~ s/(['\\])/\\$1/g;
279 if ($key =~ s/(['\\])/\\$1/g or $key =~ /[^{}\w\.]/) {
283 my $clause = { Key => $key, Op => $op, Value => $value, RawValue => $rawvalue };
284 $node->addChild( __PACKAGE__->new( $clause ) );
286 $callback{'Error'} = sub { push @results, @_ };
289 RT::SQL::Parse($string, \%callback);
293 RT::Base->_ImportOverlays();