This commit was generated by cvs2svn to compensate for changes in r8593,
[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-2009 Best Practical Solutions, LLC 
6 #                                          <jesse@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 package RT::Interface::Web::QueryBuilder::Tree;
49
50 use strict;
51 use warnings;
52
53 use base qw/Tree::Simple/;
54
55 =head1 NAME
56
57   RT::Interface::Web::QueryBuilder::Tree - subclass of Tree::Simple used in Query Builder
58
59 =head1 DESCRIPTION
60
61 This class provides support functionality for the Query Builder (Search/Build.html).
62 It is a subclass of L<Tree::Simple>.
63
64 =head1 METHODS
65
66 =head2 TraversePrePost PREFUNC POSTFUNC
67
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.
71
72 (Note that unlike Tree::Simple's C<traverse>, it actually calls its functions
73 on the root node passed to it.)
74
75 =cut
76
77 sub TraversePrePost {
78    my ($self, $prefunc, $postfunc) = @_;
79
80    $prefunc->($self);
81    
82    foreach my $child ($self->getAllChildren()) { 
83            $child->TraversePrePost($prefunc, $postfunc);
84    }
85    
86    $postfunc->($self);
87 }
88
89 =head2 GetReferencedQueues
90
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.
93
94 =cut
95
96 sub GetReferencedQueues {
97     my $self = shift;
98
99     my $queues = {};
100
101     $self->traverse(
102         sub {
103             my $node = shift;
104
105             return if $node->isRoot;
106
107             my $clause = $node->getNodeValue();
108          
109             if ( ref($clause) and $clause->{Key} eq 'Queue' ) {
110                 $queues->{ $clause->{Value} } = 1;
111             };
112         }
113     );
114
115     return $queues;
116 }
117
118 =head2 GetQueryAndOptionList SELECTED_NODES
119
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.
129
130 =cut 
131
132 sub GetQueryAndOptionList {
133     my $self           = shift;
134     my $selected_nodes = shift;
135
136     my $optionlist = [];
137
138     my $i = 0;
139
140     $self->TraversePrePost(
141         sub { # This is called before recursing to the node's children.
142             my $node = shift;
143
144             return if $node->isRoot or $node->getParent->isRoot;
145
146             my $clause = $node->getNodeValue();
147             my $str = ' ';
148             my $aggregator_context = $node->getParent()->getNodeValue();
149             $str = $aggregator_context . " " if $node->getIndex() > 0;
150
151             if ( ref($clause) ) { # ie, it's a leaf              
152                 $str .=
153                   $clause->{Key} . " " . $clause->{Op} . " " . $clause->{Value};
154             }
155
156             unless ($node->getParent->getParent->isRoot) {
157         #        used to check !ref( $parent->getNodeValue() ) )
158                 if ( $node->getIndex() == 0 ) {
159                     $str = '( ' . $str;
160                 }
161             }
162
163             push @$optionlist, {
164                 TEXT     => $str,
165                 INDEX    => $i,
166                 SELECTED => (grep { $_ == $node } @$selected_nodes) ? 'SELECTED' : '',
167                 DEPTH    => $node->getDepth() - 1,
168             };
169
170             $i++;
171         }, sub {
172             # This is called after recursing to the node's children.
173             my $node = shift;
174
175             return if $node->isRoot or $node->getParent->isRoot or $node->getParent->getParent->isRoot;
176
177             # Only do this for the rightmost child.
178             return unless $node->getIndex == $node->getParent->getChildCount - 1;
179
180             $optionlist->[-1]{TEXT} .= ' )';
181         }
182     );
183
184     return (join ' ', map { $_->{TEXT} } @$optionlist), $optionlist;
185 }
186
187 =head2 PruneChildLessAggregators
188
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.
191
192 =cut
193
194 sub PruneChildlessAggregators {
195     my $self = shift;
196
197     $self->TraversePrePost(
198         sub {
199         },
200         sub {
201             my $node = shift;
202
203             return if $node->isRoot or $node->getParent->isRoot;
204             
205             # We're only looking for aggregators (AND/OR)
206             return if ref $node->getNodeValue;
207             
208             return if $node->getChildCount != 0;
209             
210             # OK, this is a childless aggregator.  Remove self.
211             
212             $node->getParent->removeChild($node);
213             
214             # Deal with circular refs
215             $node->DESTROY;
216         }
217     );
218 }
219
220 =head2 GetDisplayedNodes
221
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.
225
226 =cut
227
228 sub GetDisplayedNodes {
229     my $self = shift;
230     my @lines;
231
232     $self->traverse(sub {
233         my $node = shift;
234
235         push @lines, $node unless $node->isRoot or $node->getParent->isRoot;
236     });
237
238     return @lines;
239 }
240
241
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});
246
247 1;