1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 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., 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 }}}
48 package RT::Interface::Web::QueryBuilder::Tree;
53 use base qw/Tree::Simple/;
57 RT::Interface::Web::QueryBuilder::Tree - subclass of Tree::Simple used in Query Builder
61 This class provides support functionality for the Query Builder (Search/Build.html).
62 It is a subclass of L<Tree::Simple>.
66 =head2 TraversePrePost PREFUNC POSTFUNC
68 Traverses the tree depth-first. Before processing the node's children,
69 calls PREFUNC with the node as its argument; after processing all of the
70 children, calls POSTFUNC with the node as its argument.
72 (Note that unlike Tree::Simple's C<traverse>, it actually calls its functions
73 on the root node passed to it.)
78 my ($self, $prefunc, $postfunc) = @_;
82 foreach my $child ($self->getAllChildren()) {
83 $child->TraversePrePost($prefunc, $postfunc);
89 =head2 GetReferencedQueues
91 Returns a hash reference with keys each queue name referenced in a clause in
92 the key (even if it's "Queue != 'Foo'"), and values all 1.
96 sub GetReferencedQueues {
105 return if $node->isRoot;
107 my $clause = $node->getNodeValue();
109 if ( ref($clause) and $clause->{Key} eq 'Queue' ) {
110 $queues->{ $clause->{Value} } = 1;
118 =head2 GetQueryAndOptionList SELECTED_NODES
120 Given an array reference of tree nodes that have been selected by the user,
121 traverses the tree and returns the equivalent SQL query and a list of hashes
122 representing the "clauses" select option list. Each has contains the keys
123 TEXT, INDEX, SELECTED, and DEPTH. TEXT is the displayed text of the option
124 (including parentheses, not including indentation); INDEX is the 0-based
125 index of the option in the list (also used as its CGI parameter); SELECTED
126 is either 'SELECTED' or '', depending on whether the node corresponding
127 to the select option was in the SELECTED_NODES list; and DEPTH is the
128 level of indentation for the option.
132 sub GetQueryAndOptionList {
134 my $selected_nodes = shift;
140 $self->TraversePrePost(
141 sub { # This is called before recursing to the node's children.
144 return if $node->isRoot or $node->getParent->isRoot;
146 my $clause = $node->getNodeValue();
148 my $aggregator_context = $node->getParent()->getNodeValue();
149 $str = $aggregator_context . " " if $node->getIndex() > 0;
151 if ( ref($clause) ) { # ie, it's a leaf
153 $clause->{Key} . " " . $clause->{Op} . " " . $clause->{Value};
156 unless ($node->getParent->getParent->isRoot) {
157 # used to check !ref( $parent->getNodeValue() ) )
158 if ( $node->getIndex() == 0 ) {
166 SELECTED => (grep { $_ == $node } @$selected_nodes) ? 'SELECTED' : '',
167 DEPTH => $node->getDepth() - 1,
172 # This is called after recursing to the node's children.
175 return if $node->isRoot or $node->getParent->isRoot or $node->getParent->getParent->isRoot;
177 # Only do this for the rightmost child.
178 return unless $node->getIndex == $node->getParent->getChildCount - 1;
180 $optionlist->[-1]{TEXT} .= ' )';
184 return (join ' ', map { $_->{TEXT} } @$optionlist), $optionlist;
187 =head2 PruneChildLessAggregators
189 If tree manipulation has left it in a state where there are ANDs, ORs,
190 or parenthesizations with no children, get rid of them.
194 sub PruneChildlessAggregators {
197 $self->TraversePrePost(
203 return if $node->isRoot or $node->getParent->isRoot;
205 # We're only looking for aggregators (AND/OR)
206 return if ref $node->getNodeValue;
208 return if $node->getChildCount != 0;
210 # OK, this is a childless aggregator. Remove self.
212 $node->getParent->removeChild($node);
214 # Deal with circular refs
220 =head2 GetDisplayedNodes
222 This function returns a list of the nodes of the tree in depth-first
223 order which correspond to options in the "clauses" multi-select box.
224 In fact, it's all of them but the root and its child.
228 sub GetDisplayedNodes {
232 $self->traverse(sub {
235 push @lines, $node unless $node->isRoot or $node->getParent->isRoot;
242 eval "require RT::Interface::Web::QueryBuilder::Tree_Vendor";
243 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Web/QueryBuilder/Tree_Vendor.pm});
244 eval "require RT::Interface::Web::QueryBuilder::Tree_Local";
245 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Web/QueryBuilder/Tree_Local.pm});