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