[Rt-commit] r3046 - in rt/branches/3.4-RELEASE: . html/Search lib/RT/Interface/Web lib/RT/Interface/Web/QueryBuilder

glasser at bestpractical.com glasser at bestpractical.com
Wed Jun 1 18:35:54 EDT 2005


Author: glasser
Date: Wed Jun  1 18:35:53 2005
New Revision: 3046

Modified:
   rt/branches/3.4-RELEASE/   (props changed)
   rt/branches/3.4-RELEASE/html/Search/Build.html
   rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder.pm
   rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder/Tree.pm
Log:
 r33107 at tin-foil:  glasser | 2005-05-26 01:56:25 -0400
 More Query Builder tree refactoring.


Modified: rt/branches/3.4-RELEASE/html/Search/Build.html
==============================================================================
--- rt/branches/3.4-RELEASE/html/Search/Build.html	(original)
+++ rt/branches/3.4-RELEASE/html/Search/Build.html	Wed Jun  1 18:35:53 2005
@@ -457,10 +457,9 @@
 
 # {{{ Rebuild $Query based on the additions / movements
 $Query      = "";
-my($optionlist_arrayref, $queues);
+my $optionlist_arrayref;
 
-($optionlist_arrayref, $Query, $queues) =
-  RT::Interface::Web::QueryBuilder::TreeToQueryAndOptionListAndQueues( $tree, \@current_values );
+($Query, $optionlist_arrayref) = $tree->GetQueryAndOptionList(\@current_values);
   
 my $optionlist = join "\n", map { qq(<option value="$_->{INDEX}" $_->{SELECTED}>) 
                                   . ("&nbsp;" x (5 * $_->{DEPTH}))
@@ -684,6 +683,8 @@
 
 # }}}
 
+my $queues = $tree->GetReferencedQueues;
+
 # {{{ Deal with format changes
 my ( $AvailableColumns, $CurrentFormat );
 ( $Format, $AvailableColumns, $CurrentFormat ) = $m->comp(

Modified: rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder.pm
==============================================================================
--- rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder.pm	(original)
+++ rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder.pm	Wed Jun  1 18:35:53 2005
@@ -48,85 +48,6 @@
 use strict;
 use warnings;
 
-sub TreeToQueryAndOptionListAndQueues {
-    my $tree           = shift;
-    my $selected_nodes = shift;
-    
-    my $Query = '';
-    my $queues = {};
-    my $optionlist = [];
-
-    my $i = 0;
-
-    $tree->traverse_pre_post(
-        sub { # This is called before recursing to the node's children.
-            my $node = shift;
-
-            return if $node->isRoot or $node->getParent->isRoot;
-
-            my $clause = $node->getNodeValue();
-            my $str = ' ';
-            my $aggregator_context = $node->getParent()->getNodeValue();
-            $str = $aggregator_context . " " if $node->getIndex() > 0;
-            
-            if ( ref($clause) ) { # ie, it's a leaf              
-                $str .=
-                  $clause->{Key} . " " . $clause->{Op} . " " . $clause->{Value};
-
-                if ( $clause->{Key} eq "Queue" ) {
-                    $queues->{ $clause->{Value} } = 1;
-                }
-            }
-
-            my $selected = '';
-            if ( grep { $_ == $node } @$selected_nodes ) {
-                $selected = "SELECTED";
-            }
-
-            unless ($node->getParent->getParent->isRoot) {
-        #        used to check !ref( $parent->getNodeValue() ) )
-                if ( $node->getIndex() == 0 ) {
-                    $str = '( ' . $str;
-                }
-            }
-            
-            $Query .= " " . $str . " ";
-
-            push @$optionlist, {
-                TEXT     => $str,
-                INDEX    => $i,
-                SELECTED => $selected,
-                DEPTH    => $node->getDepth() - 1,
-            };
-
-            $i++;
-        }, sub {
-            # This is called after recursing to the node's children.
-            my $node = shift;
-            
-            return if $node->isRoot or $node->getParent->isRoot or $node->getParent->getParent->isRoot;
-            
-            # Only do this for the rightmost child.
-            return unless $node->getIndex == $node->getParent->getChildCount - 1;
-            
-            $Query .= ' )';
-            $optionlist->[-1]{TEXT} .= ' )';
-        }
-    );
-
-    my $should_be_query = join ' ', map { $_->{TEXT} } @$optionlist;
-
-#   So, $should_be_query *ought* to be the same as $Query but calculated in a much
-#   simpler way, but this has not been tested enough to make sure, so I won't commit it. 
-#   my $sbq_tmp = $should_be_query;
-#   my $q_tmp = $Query;
-#   $sbq_tmp =~ tr/ //d; $q_tmp =~ tr/ //d;
-#   $RT::Logger->crit("query check: " . ($q_tmp eq $sbq_tmp ? 'yay' : 'nay') );
-
-    return $optionlist, $Query, $queues;
-}
-
-
 eval "require RT::Interface::Web::QueryBuilder_Vendor";
 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Web/QueryBuilder_Vendor.pm});
 eval "require RT::Interface::Web::QueryBuilder_Local";

Modified: rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder/Tree.pm
==============================================================================
--- rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder/Tree.pm	(original)
+++ rt/branches/3.4-RELEASE/lib/RT/Interface/Web/QueryBuilder/Tree.pm	Wed Jun  1 18:35:53 2005
@@ -50,26 +50,114 @@
 
 use base qw/Tree::Simple/;
 
-=head2 traverse_pre_post PREFUNC POSTFUNC
+=head2 TraversePrePost PREFUNC POSTFUNC
 
 Traverses the tree depth-first.  Before processing the node's children,
 calls PREFUNC with the node as its argument; after processing all of the
 children, calls POSTFUNC with the node as its argument.
 
+(Note that unlike Tree::Simple's C<traverse>, it actually calls its functions
+on the root node passed to it.)
+
 =cut
 
-sub traverse_pre_post {
+sub TraversePrePost {
    my ($self, $prefunc, $postfunc) = @_;
 
    $prefunc->($self);
    
    foreach my $child ($self->getAllChildren()) { 
-           $child->traverse_pre_post($prefunc, $postfunc);
+           $child->TraversePrePost($prefunc, $postfunc);
    }
    
    $postfunc->($self);
 }
 
+=head2 GetReferencedQueues
+
+Returns a hash reference with keys each queue name referenced in a clause in
+the key (even if it's "Queue != 'Foo'"), and values all 1.
+
+=cut
+
+sub GetReferencedQueues {
+    my $self = shift;
+
+    my $queues = {};
+
+    $self->traverse(
+        sub {
+            my $node = shift;
+
+            return if $node->isRoot;
+
+            my $clause = $node->getNodeValue();
+         
+            if ( ref($clause) and $clause->{Key} eq 'Queue' ) {
+                $queues->{ $clause->{Value} } = 1;
+            };
+        }
+    );
+
+    return $queues;
+}
+
+
+sub GetQueryAndOptionList {
+    my $self           = shift;
+    my $selected_nodes = shift;
+
+    my $optionlist = [];
+
+    my $i = 0;
+
+    $self->TraversePrePost(
+        sub { # This is called before recursing to the node's children.
+            my $node = shift;
+
+            return if $node->isRoot or $node->getParent->isRoot;
+
+            my $clause = $node->getNodeValue();
+            my $str = ' ';
+            my $aggregator_context = $node->getParent()->getNodeValue();
+            $str = $aggregator_context . " " if $node->getIndex() > 0;
+
+            if ( ref($clause) ) { # ie, it's a leaf              
+                $str .=
+                  $clause->{Key} . " " . $clause->{Op} . " " . $clause->{Value};
+            }
+
+            unless ($node->getParent->getParent->isRoot) {
+        #        used to check !ref( $parent->getNodeValue() ) )
+                if ( $node->getIndex() == 0 ) {
+                    $str = '( ' . $str;
+                }
+            }
+
+            push @$optionlist, {
+                TEXT     => $str,
+                INDEX    => $i,
+                SELECTED => (grep { $_ == $node } @$selected_nodes) ? 'SELECTED' : '',
+                DEPTH    => $node->getDepth() - 1,
+            };
+
+            $i++;
+        }, sub {
+            # This is called after recursing to the node's children.
+            my $node = shift;
+
+            return if $node->isRoot or $node->getParent->isRoot or $node->getParent->getParent->isRoot;
+
+            # Only do this for the rightmost child.
+            return unless $node->getIndex == $node->getParent->getChildCount - 1;
+
+            $optionlist->[-1]{TEXT} .= ' )';
+        }
+    );
+
+    return (join ' ', map { $_->{TEXT} } @$optionlist), $optionlist;
+}
+
 
 eval "require RT::Interface::Web::QueryBuilder::Tree_Vendor";
 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Web/QueryBuilder/Tree_Vendor.pm});


More information about the Rt-commit mailing list