1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 # <jesse@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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
45 # END BPS TAGGED BLOCK }}}
46 package RT::Interface::Web::QueryBuilder::Tree;
51 use base qw/Tree::Simple/;
55 RT::Interface::Web::QueryBuilder::Tree - subclass of Tree::Simple used in Query Builder
59 This class provides support functionality for the Query Builder (Search/Build.html).
60 It is a subclass of L<Tree::Simple>.
64 =head2 TraversePrePost PREFUNC POSTFUNC
66 Traverses the tree depth-first. Before processing the node's children,
67 calls PREFUNC with the node as its argument; after processing all of the
68 children, calls POSTFUNC with the node as its argument.
70 (Note that unlike Tree::Simple's C<traverse>, it actually calls its functions
71 on the root node passed to it.)
76 my ($self, $prefunc, $postfunc) = @_;
80 foreach my $child ($self->getAllChildren()) {
81 $child->TraversePrePost($prefunc, $postfunc);
87 =head2 GetReferencedQueues
89 Returns a hash reference with keys each queue name referenced in a clause in
90 the key (even if it's "Queue != 'Foo'"), and values all 1.
94 sub GetReferencedQueues {
103 return if $node->isRoot;
105 my $clause = $node->getNodeValue();
107 if ( ref($clause) and $clause->{Key} eq 'Queue' ) {
108 $queues->{ $clause->{Value} } = 1;
116 =head2 GetQueryAndOptionList SELECTED_NODES
118 Given an array reference of tree nodes that have been selected by the user,
119 traverses the tree and returns the equivalent SQL query and a list of hashes
120 representing the "clauses" select option list. Each has contains the keys
121 TEXT, INDEX, SELECTED, and DEPTH. TEXT is the displayed text of the option
122 (including parentheses, not including indentation); INDEX is the 0-based
123 index of the option in the list (also used as its CGI parameter); SELECTED
124 is either 'SELECTED' or '', depending on whether the node corresponding
125 to the select option was in the SELECTED_NODES list; and DEPTH is the
126 level of indentation for the option.
130 sub GetQueryAndOptionList {
132 my $selected_nodes = shift;
138 $self->TraversePrePost(
139 sub { # This is called before recursing to the node's children.
142 return if $node->isRoot or $node->getParent->isRoot;
144 my $clause = $node->getNodeValue();
146 my $aggregator_context = $node->getParent()->getNodeValue();
147 $str = $aggregator_context . " " if $node->getIndex() > 0;
149 if ( ref($clause) ) { # ie, it's a leaf
151 $clause->{Key} . " " . $clause->{Op} . " " . $clause->{Value};
154 unless ($node->getParent->getParent->isRoot) {
155 # used to check !ref( $parent->getNodeValue() ) )
156 if ( $node->getIndex() == 0 ) {
164 SELECTED => (grep { $_ == $node } @$selected_nodes) ? 'SELECTED' : '',
165 DEPTH => $node->getDepth() - 1,
170 # This is called after recursing to the node's children.
173 return if $node->isRoot or $node->getParent->isRoot or $node->getParent->getParent->isRoot;
175 # Only do this for the rightmost child.
176 return unless $node->getIndex == $node->getParent->getChildCount - 1;
178 $optionlist->[-1]{TEXT} .= ' )';
182 return (join ' ', map { $_->{TEXT} } @$optionlist), $optionlist;
185 =head2 PruneChildLessAggregators
187 If tree manipulation has left it in a state where there are ANDs, ORs,
188 or parenthesizations with no children, get rid of them.
192 sub PruneChildlessAggregators {
195 $self->TraversePrePost(
201 return if $node->isRoot or $node->getParent->isRoot;
203 # We're only looking for aggregators (AND/OR)
204 return if ref $node->getNodeValue;
206 return if $node->getChildCount != 0;
208 # OK, this is a childless aggregator. Remove self.
210 $node->getParent->removeChild($node);
212 # Deal with circular refs
218 =head2 GetDisplayedNodes
220 This function returns a list of the nodes of the tree in depth-first
221 order which correspond to options in the "clauses" multi-select box.
222 In fact, it's all of them but the root and its child.
226 sub GetDisplayedNodes {
230 $self->traverse(sub {
233 push @lines, $node unless $node->isRoot or $node->getParent->isRoot;
240 eval "require RT::Interface::Web::QueryBuilder::Tree_Vendor";
241 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Web/QueryBuilder/Tree_Vendor.pm});
242 eval "require RT::Interface::Web::QueryBuilder::Tree_Local";
243 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Web/QueryBuilder/Tree_Local.pm});