[Rt-commit] r4319 - in rt/branches/3.7-EXPERIMENTAL: . html/Search html/Search/Elements

ruz at bestpractical.com ruz at bestpractical.com
Sat Dec 17 21:11:09 EST 2005


Author: ruz
Date: Sat Dec 17 21:11:09 2005
New Revision: 4319

Modified:
   rt/branches/3.7-EXPERIMENTAL/   (props changed)
   rt/branches/3.7-EXPERIMENTAL/html/Search/Build.html
   rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditQuery
   rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditSearches
Log:
 r1535 at cubic-pc:  cubic | 2005-12-18 03:07:01 +0300
  r1528 at cubic-pc:  cubic | 2005-12-15 23:59:50 +0300
  * all args that are used to maniulate saved searches was renamed to SavedSearch*,
    for example SavedSearchLoad, SavedSearchDelete, etc.
  * move all work with saved searches into methods in Elements/EditSearches comp
  * move operations with $tree (Up, Down, Left...) to methods in Elements/EditQuery 
  
 


Modified: rt/branches/3.7-EXPERIMENTAL/html/Search/Build.html
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/html/Search/Build.html	(original)
+++ rt/branches/3.7-EXPERIMENTAL/html/Search/Build.html	Sat Dec 17 21:11:09 2005
@@ -65,51 +65,49 @@
 %#
 <& /Elements/Header, Title => $title &>
 <& /Ticket/Elements/Tabs, 
-    current_tab => "Search/Build.html".$QueryString, 
+    current_tab => "Search/Build.html?".$QueryString, 
     Title => $title,
-    Format => $Format,
-    Query => $Query,
-    Order => $Order,
-    OrderBy => $OrderBy,
-    Rows => $RowsPerPage
+    %query,
 &>
 
 <form method="POST" action="Build.html" name="BuildQuery">
-<input type="hidden" class="hidden" name="SearchId" value="<%$SearchId%>">
-<input type="hidden" class="hidden" name="Query" value="<%$Query%>">
-<input type="hidden" class="hidden" name="Format" value="<%$Format%>">
+<input type="hidden" class="hidden" name="SavedSearchId" value="<% $saved_search{'Id'} %>">
+<input type="hidden" class="hidden" name="Query" value="<% $query{'Query'} %>">
+<input type="hidden" class="hidden" name="Format" value="<% $query{'Format'} %>">
+
 <table width="100%" border="0" cellpadding="5">
-<tr valign="top">
-<td class="boxcontainer" rowspan="2" width="65%">
-<& Elements/PickCriteria, query => $Query, cfqueues => $queues &>
+<tr valign="top"><td class="boxcontainer" rowspan="2" width="65%">
+
+<& Elements/PickCriteria, query => $query{'Query'}, cfqueues => $queues &>
 <& /Elements/Submit, Caption => loc('Add these terms to your search'), Label => loc('Add'), Name => 'AddClause'&>
-</td>
 
-<td>
+</td><td>
+
 <& Elements/EditQuery,
     %ARGS,
     actions => \@actions,
     optionlist => $optionlist,
-    Description => $Description &>
+    Description => $saved_search{'Description'},
+&>
 <& /Elements/Submit, Label => loc('Add and Search'), Name => 'DoSearch'&>
-</td>
-</tr>
 
-<tr valign="top">
-<td>
-<& Elements/EditSearches, CurrentSearch => $search_hash, Dirty => $dirty, SearchId => $SearchId &>
-</td>
-</tr>
+</td></tr>
+<tr valign="top"><td>
 
-<tr>
-<td colspan="2" class="boxcontainer">
+<& Elements/EditSearches, %saved_search, CurrentSearch => \%query &>
+
+</td></tr>
+<tr><td colspan="2" class="boxcontainer">
+
+<& Elements/DisplayOptions,
+    %ARGS, %query,
+    AvailableColumns => $AvailableColumns,
+    CurrentFormat    => $CurrentFormat,
+&>
 
-<& Elements/DisplayOptions, %ARGS, Format=> $Format,
-AvailableColumns => $AvailableColumns,  CurrentFormat => $CurrentFormat, RowsPerPage => $RowsPerPage, OrderBy => $OrderBy, Order => $Order &>
 <& /Elements/Submit, Label => loc('Add and Search'), Name => 'DoSearch'&>
-</td>
-</tr>
-</table>
+
+</td></tr></table>
 </form>
 
 <%INIT>
@@ -117,127 +115,49 @@
 use RT::Interface::Web::QueryBuilder::Tree;
 use RT::SQL;
 
-my $search_hash = {};
-my $search;
 my $title = loc("Query Builder");
 
-# {{{ Clear out unwanted data
-if ( $NewQuery or $ARGS{'Delete'} ) {
+my (%query, %saved_search);
+my @actions = $m->comp( 'Elements/EditSearches:Init', %ARGS, Query => \%query, SavedSearch => \%saved_search);
+
+if ( $NewQuery ) {
 
     # Wipe all data-carrying variables clear if we want a new
     # search, or we're deleting an old one..
-    $Query       = '';
-    $Format      = '';
-    $Description = '';
-    $SearchId    = '';
-    $Order       = '';
-    $OrderBy     = '';
-    $RowsPerPage = '';
-
-    # ($search hasn't been set yet; no need to clear)
+    %query = ();
+    %saved_search = ( Id => 'new' );
 
     # ..then wipe the session out..
-    undef $session{'CurrentSearchHash'};
+    delete $session{'CurrentSearchHash'};
 
     # ..and the search results.
-    $session{'tickets'}->CleanSlate() if defined $session{'tickets'};
-}
-
-# }}}
-
-if (ref $OrderBy eq "ARRAY") {
-    $OrderBy = join("|", @$OrderBy);
+    $session{'tickets'}->CleanSlate if defined $session{'tickets'};
 }
-if (ref $Order eq "ARRAY") {
-    $Order = join("|", @$Order);
-}
-
-# {{{ Attempt to load what we can from the session, set defaults
 
-# We don't read or write to the session again until the end
-$search_hash = $session{'CurrentSearchHash'};
+{ # Attempt to load what we can from the session and preferences, set defaults
 
-# Read from user preferences
-my $prefs = $session{'CurrentUser'}->UserObj->Preferences("SearchDisplay") || {};
+    my $current = $session{'CurrentSearchHash'};
+    my $prefs = $session{'CurrentUser'}->UserObj->Preferences("SearchDisplay") || {};
+    my $default = { Query => '', Format => '', OrderBy => 'id', Order => 'ASC', RowsPerPage => 50 };
 
-# These variables are what define a search_hash; this is also
-# where we give sane defaults.
-$Query       ||= $search_hash->{'Query'};
-$Format      ||= $search_hash->{'Format'} || $prefs->{'Format'};
-$Description ||= $search_hash->{'Description'};
-$SearchId    ||= $search_hash->{'SearchId'} || 'new';
-$Order       ||= $search_hash->{'Order'} || $prefs->{'Order'} || 'ASC';
-$OrderBy     ||= $search_hash->{'OrderBy'} || $prefs->{'OrderBy'} || 'id';
-
-unless ( defined $RowsPerPage ) {
-    if ( defined $search_hash->{'RowsPerPage'} ) {
-        $RowsPerPage = $search_hash->{'RowsPerPage'};
+    for( qw(Query Format OrderBy Order RowsPerPage) ) {
+        $query{$_} = $ARGS{$_};
+        $query{$_} = $current->{$_} unless defined $query{$_};
+        $query{$_} = $prefs->{$_} unless defined $query{$_};
+        $query{$_} = $default->{$_} unless defined $query{$_};
     }
-    elsif ( defined $prefs->{'RowsPerPage'} ) {
-        $RowsPerPage = $prefs->{'RowsPerPage'};
-    }
-    else {
-        $RowsPerPage = 50;
-    }
-}
-
-$search ||= $search_hash->{'Object'};
 
-# }}}
-
-my @actions = ();
-
-# Clean unwanted junk from the format
-$Format = $m->comp( '/Elements/ScrubHTML', Content => $Format ) if $Format;
-
-# {{{ If we're asked to delete the current search, make it go away and reset the search parameters
-if ( $ARGS{'Delete'} ) {
-    # We set $SearchId to 'new' above already, so peek into the %ARGS
-    my ($container_object, $search_id) = _parse_saved_search ($ARGS{'SearchId'});
-    if ($container_object && $container_object->id) {
-	# We have the object the entry is an attribute on; delete the
-	# entry..
-	$container_object->Attributes->DeleteEntry(
-            Name => 'SavedSearch',
-	    id   => $search_id
-            );
+    for( qw(Order OrderBy) ) {
+        if (ref $query{$_} eq "ARRAY") {
+            $query{$_} = join( '|', @{ $ARGS{$_} } );
+        }
+    }
+    if ( $query{'Format'} ) {
+        # Clean unwanted junk from the format
+        $query{'Format'} = $m->comp( '/Elements/ScrubHTML', Content => $query{'Format'} );
     }
 }
 
-# }}}
-
-# {{{ If the user wants to copy a search, uncouple from the one that this was based on, but don't erase the $Query or $Format
-if ( $ARGS{'CopySearch'} ) {
-    $SearchId    = 'new';
-    $search      = undef;
-    $Description = loc( "[_1] copy", $Description );
-}
-
-# }}}
-
-# {{{ if we're asked to revert the current search, we just want to load it
-if ( $ARGS{'Revert'} ) {
-    $ARGS{'LoadSavedSearch'} = $SearchId;
-}
-
-# }}}
-
-# {{{ if we're asked to load a search, load it.
-
-if ( my ($container_object, $search_id ) = _parse_saved_search ($ARGS{'LoadSavedSearch'})) {
-    $search = $container_object->Attributes->WithId($search_id);
-
-    # We have a $search and now; import the others
-    $SearchId    = $ARGS{'LoadSavedSearch'};
-    $Description = $search->Description;
-    $Format      = $search->SubValue('Format');
-    $Query       = $search->SubValue('Query');
-    $Order       = $search->SubValue('Order');
-    $OrderBy     = $search->SubValue('OrderBy');
-    $RowsPerPage = $search->SubValue('RowsPerPage');
-}
-
-# }}}
 
 my $ParseQuery = sub {
     my ($string, $results) = shift;
@@ -277,17 +197,15 @@
     return $tree;
 };
 
-my $tree = $ParseQuery->( $Query, \@actions );
+my $tree = $ParseQuery->( $query{'Query'}, \@actions );
 
 # if parsing went poorly, send them to the edit page to fix it
 if ( $actions[0] ) {
-    $m->comp( "Edit.html", Query => $Query, actions => \@actions );
+    $m->comp( "Edit.html", Query => $query{'Query'}, actions => \@actions );
     $m->comp('/Elements/Footer');
     $m->abort;
 }
 
-$Query = '';
-
 my @options = $tree->GetDisplayedNodes;
 my @current_values = grep defined, @options[@clauses];
 my @new_values = ();
@@ -366,255 +284,58 @@
 
 # }}}
 
-# {{{ Move things around
-if ( $ARGS{'Up'} || $ARGS{'Down'} ) {
-    if (@current_values) {
-        foreach my $value (@current_values) {
-            my $parent = $value->getParent;
-            my $index = $value->getIndex;
-            my $newindex = $index;
-            $newindex++ if $ARGS{'Down'};
-            $newindex-- if $ARGS{'Up'};
-            if ( $newindex < 0 || $newindex >= $parent->getChildCount ) {
-                push( @actions, [ loc("error: can't move up"), -1 ] ) if $ARGS{'Up'};
-                push( @actions, [ loc("error: can't move down"), -1 ] ) if $ARGS{'Down'};
-                next;
-            }
-
-            $parent->removeChild( $index );
-            $parent->insertChild( $newindex, $value );
-        }
-    }
-    else {
-        push( @actions, [ loc("error: nothing to move"), -1 ] );
-    }
-}
-elsif ( $ARGS{"Left"} ) {
-    if (@current_values) {
-        foreach my $value (@current_values) {
-            my $parent = $value->getParent;
-            if( $value->isRoot || $parent->isRoot ) {
-                push( @actions, [ loc("error: can't move left"), -1 ] );
-                next;
-            }
-
-            my $grandparent = $parent->getParent;
-            if( $grandparent->isRoot ) {
-                push( @actions, [ loc("error: can't move left"), -1 ] );
-                next;
-            }
-            
-            my $index = $parent->getIndex;
-            $parent->removeChild($value);
-            $grandparent->insertChild( $index, $value );
-            if ( $parent->isLeaf ) {
-                $grandparent->removeChild($parent);
-            }
-        }
-    }
-    else {
-        push( @actions, [ loc("error: nothing to move"), -1 ] );
-    }
-}
-elsif ( $ARGS{"Right"} ) {
-    if (@current_values) {
-        foreach my $value (@current_values) {
-            my $parent = $value->getParent;
-            my $index  = $value->getIndex;
-
-            my $newparent;
-            if ( $index > 0 ) {
-                my $sibling = $parent->getChild( $index - 1 );
-                $newparent = $sibling unless $sibling->isLeaf;
-            }
-            $newparent ||= RT::Interface::Web::QueryBuilder::Tree->new( $ARGS{'AndOr'} || 'AND', $parent );
-
-            $parent->removeChild($value);
-            $newparent->addChild($value);
-        }
-    }
-    else {
-        push( @actions, [ loc("error: nothing to move"), -1 ] );
-    }
-}
-elsif ( $ARGS{"DeleteClause"} ) {
-    if (@current_values) {
-        while( my $node = shift @current_values ) {
-            my $parent = $node->getParent;
-            next unless $parent;
-            $parent->removeChild($node);
-            $node->DESTROY;
-        }
-    }
-    else {
-        push( @actions, [ loc("error: nothing to delete"), -1 ] );
-    }
-}
-elsif ( $ARGS{"Toggle"} ) {
-    if (@current_values) {
-        foreach my $value (@current_values) {
-            my $parent = $value->getParent;
-
-            if ( $parent->getNodeValue eq 'AND' ) {
-                $parent->setNodeValue('OR');
-            }
-            else {
-                $parent->setNodeValue('AND');
-            }
-        }
-    }
-    else {
-        push( @actions, [ loc("error: nothing to toggle"), -1 ] );
-    }
-}
-
-$tree->PruneChildlessAggregators;
+push @actions, $m->comp('Elements/EditQuery:Process',
+    %ARGS,
+    Tree => $tree,
+    Selected => \@current_values,
+);
 
-# }}}
+push @current_values, @new_values;
 
 # {{{ Rebuild $Query based on the additions / movements
-push @current_values, @new_values;
-$Query = '';
-my $optionlist_arrayref;
 
-($Query, $optionlist_arrayref) = $tree->GetQueryAndOptionList(\@current_values);
+my $optionlist_arrayref;
+($query{'Query'}, $optionlist_arrayref) = $tree->GetQueryAndOptionList(\@current_values);
 
 my $optionlist = join "\n", map { qq(<option value="$_->{INDEX}" $_->{SELECTED}>) 
                                   . ("&nbsp;" x (5 * $_->{DEPTH}))
                                   . $m->interp->apply_escapes($_->{TEXT}, 'h') . qq(</option>) } @$optionlist_arrayref;
 
-
-
-
-# }}}
-
 # }}}
 
 my $queues = $tree->GetReferencedQueues;
 
 # {{{ Deal with format changes
 my ( $AvailableColumns, $CurrentFormat );
-( $Format, $AvailableColumns, $CurrentFormat ) = $m->comp(
+( $query{'Format'}, $AvailableColumns, $CurrentFormat ) = $m->comp(
     'Elements/BuildFormatString',
+    %ARGS,
     cfqueues => $queues,
-    %ARGS, Format => $Format
+    Format => $query{'Format'},
 );
 
 # }}}
 
-# {{{ if we're asked to save the current search, save it
-if ( $ARGS{'Save'} ) {
-    if ( $search && $search->id ) {
-	# permission check
-	if ($search->Object->isa('RT::System')) {
-	    unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
-		Abort("No permission to save system-wide searches");
-	    }
-	}
-
-        # This search is based on a previously loaded search -- so
-        # just update the current search object with new values
-        $search->SetSubValues(
-            Format      => $Format,
-            Query       => $Query,
-            Order       => $Order,
-            OrderBy     => $OrderBy,
-            RowsPerPage => $RowsPerPage,
-        );
-        $search->SetDescription($Description);
-
-    }
-    elsif ( $SearchId eq 'new' && $ARGS{'Owner'} =~ /^(.*?)-(\d+)$/ ) {
-        # We're saving a new search
-        my $obj_type = $1;
-        my $obj_id   = $2;
-
-        # Find out if we're saving on the user, or a group
-        my $container_object = _load_container_object ($obj_type, $obj_id);
-
-        if ( $container_object->id ) {
-	    # permission check
-	    if ($container_object->isa('RT::System')) {
-		unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
-		    Abort("No permission to save system-wide searches");
-		}
-	    }
-
-	    my $name = $obj_type eq 'RT::System' ? "Search - $Description" : 'SavedSearch';
-            # If we got one or the other, add the saerch
-            my ( $search_id, $search_msg ) = $container_object->AddAttribute(
-                Name        => $name,
-                Description => $Description,
-                Content     => {
-                    Format      => $Format,
-                    Query       => $Query,
-                    Order       => $Order,
-                    OrderBy     => $OrderBy,
-                    RowsPerPage => $RowsPerPage,
-                }
-            );
-	    if ( $search_id ) {
-		$search =
-		    $session{'CurrentUser'}->UserObj->Attributes->WithId($search_id);
-		# Build new SearchId
-		$SearchId =
-		    ref( $session{'CurrentUser'}->UserObj ) . '-'
-			. $session{'CurrentUser'}->UserObj->Id
-			. '-SavedSearch-'
-			. $search->Id;
-	    }
-	    else {
-		push @actions, [ loc("Can't find a saved search to work with").': '.loc($search_msg), 0 ];
-	    }
-
-	}
-    }
-    else {
-        push @actions, [ loc("Can't save this search"), 0 ];
-    }
-
-}
-
-# }}}
-
-# {{{ If we're modifying an old query, check if it has changed
-my $dirty = 0;
-$dirty = 1
-  if defined $search
-  and ($search->SubValue('Format') ne $Format
-    or $search->SubValue('Query')       ne $Query
-    or $search->SubValue('Order')       ne $Order
-    or $search->SubValue('OrderBy')     ne $OrderBy
-    or $search->SubValue('RowsPerPage') ne $RowsPerPage );
-
-# }}}
+# if we're asked to save the current search, save it
+push @actions, $m->comp( 'Elements/EditSearches:Save', %ARGS, Query => \%query, SavedSearch => \%saved_search);
 
 # {{{ Push the updates into the session so we don't loose 'em
-$search_hash->{'SearchId'}    = $SearchId;
-$search_hash->{'Format'}      = $Format;
-$search_hash->{'Query'}       = $Query;
-$search_hash->{'Description'} = $Description;
-$search_hash->{'Object'}      = $search;
-$search_hash->{'Order'}       = $Order;
-$search_hash->{'OrderBy'}     = $OrderBy;
-$search_hash->{'RowsPerPage'} = $RowsPerPage;
 
-$session{'CurrentSearchHash'} = $search_hash;
+$session{'CurrentSearchHash'} = {
+    %query,
+    SearchId    => $saved_search{'Id'},
+    Object      => $saved_search{'Object'},
+    Description => $saved_search{'Description'},
+};
 
 # }}}
 
 # {{{ Show the results, if we were asked.
-if ( $ARGS{"DoSearch"}) {
-    $m->comp(
-        "Results.html",
-        Query   => $Query,
-        Format  => $Format,
-        Order   => $Order,
-        OrderBy => $OrderBy,
-        Rows    => $RowsPerPage
-    );
-    $m->comp('/Elements/Footer');
-    $m->abort();
+
+if ( $ARGS{'DoSearch'} ) {
+    $m->comp( 'Results.html', %query );
+    $m->comp( '/Elements/Footer' );
+    $m->abort;
 }
 
 # }}}
@@ -623,17 +344,10 @@
 
 my $QueryString = '';
 if ($NewQuery) {
-    $QueryString = '?NewQuery=1';
+    $QueryString = 'NewQuery=1';
 }
-elsif ( $Query ) {
-    $QueryString = '?'. $m->comp(
-        '/Elements/QueryString',
-        Query   => $Query,
-        Format  => $Format,
-        Order   => $Order,
-        OrderBy => $OrderBy,
-        Rows    => $RowsPerPage,
-    );
+elsif ( $query{'Query'} ) {
+    $QueryString = $m->comp('/Elements/QueryString', %query );
 }
 
 # }}}
@@ -642,13 +356,5 @@
 
 <%ARGS>
 $NewQuery => 0
-$SearchId => undef
-$Query => undef
-$Format => undef 
-$Description => undef
-$Order => undef
-$OrderBy => undef
-$RowsPerPage => undef
-$HideResults => 0
 @clauses => ()
 </%ARGS>

Modified: rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditQuery
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditQuery	(original)
+++ rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditQuery	Sat Dec 17 21:11:09 2005
@@ -45,9 +45,11 @@
 %# END BPS TAGGED BLOCK }}}
 <& NewListActions, actions => $actions &>
 <&|/Widgets/TitleBox, title => join(': ', grep defined, loc("Current search"), $Description) &>
+
 <select size="10" name="clauses" style="width: 100%" multiple>
 % $m->out($optionlist);
 </select>
+
 <p align="center">
 <input type="submit" class="button" name="Up" value="^">
 <input type="submit" class="button" name="Down" value="v">
@@ -57,9 +59,124 @@
 <input type="submit" class="button" name="DeleteClause" value="<&|/l&>Delete</&>">
 %#<input type="submit" class="button" name="EditQuery" value="Advanced">
 </p>
+
 </&>
 <%ARGS>
-$Description
-$optionlist
-$actions
+$Description => ''
+$optionlist => ''
+$actions => []
 </%ARGS>
+
+<%METHOD Process>
+<%ARGS>
+$Tree => undef,
+$Selected => [],
+</%ARGS>
+<%INIT>
+my @results;
+if ( $ARGS{'Up'} || $ARGS{'Down'} ) {
+    if (@$Selected) {
+        foreach my $value (@$Selected) {
+            my $parent = $value->getParent;
+            my $index = $value->getIndex;
+            my $newindex = $index;
+            $newindex++ if $ARGS{'Down'};
+            $newindex-- if $ARGS{'Up'};
+            if ( $newindex < 0 || $newindex >= $parent->getChildCount ) {
+                push( @results, [ loc("error: can't move up"), -1 ] ) if $ARGS{'Up'};
+                push( @results, [ loc("error: can't move down"), -1 ] ) if $ARGS{'Down'};
+                next;
+            }
+
+            $parent->removeChild( $index );
+            $parent->insertChild( $newindex, $value );
+        }
+    }
+    else {
+        push( @results, [ loc("error: nothing to move"), -1 ] );
+    }
+}
+elsif ( $ARGS{"Left"} ) {
+    if (@$Selected) {
+        foreach my $value (@$Selected) {
+            my $parent = $value->getParent;
+            if( $value->isRoot || $parent->isRoot ) {
+                push( @results, [ loc("error: can't move left"), -1 ] );
+                next;
+            }
+
+            my $grandparent = $parent->getParent;
+            if( $grandparent->isRoot ) {
+                push( @results, [ loc("error: can't move left"), -1 ] );
+                next;
+            }
+            
+            my $index = $parent->getIndex;
+            $parent->removeChild($value);
+            $grandparent->insertChild( $index, $value );
+            if ( $parent->isLeaf ) {
+                $grandparent->removeChild($parent);
+            }
+        }
+    }
+    else {
+        push( @results, [ loc("error: nothing to move"), -1 ] );
+    }
+}
+elsif ( $ARGS{"Right"} ) {
+    if (@$Selected) {
+        foreach my $value (@$Selected) {
+            my $parent = $value->getParent;
+            my $index  = $value->getIndex;
+
+            my $newparent;
+            if ( $index > 0 ) {
+                my $sibling = $parent->getChild( $index - 1 );
+                $newparent = $sibling unless $sibling->isLeaf;
+            }
+            $newparent ||= RT::Interface::Web::QueryBuilder::Tree->new( $ARGS{'AndOr'} || 'AND', $parent );
+
+            $parent->removeChild($value);
+            $newparent->addChild($value);
+        }
+    }
+    else {
+        push( @results, [ loc("error: nothing to move"), -1 ] );
+    }
+}
+elsif ( $ARGS{"DeleteClause"} ) {
+    if (@$Selected) {
+        while( my $node = shift @$Selected ) {
+            my $parent = $node->getParent;
+            next unless $parent;
+            $parent->removeChild($node);
+            $node->DESTROY;
+        }
+    }
+    else {
+        push( @results, [ loc("error: nothing to delete"), -1 ] );
+    }
+}
+elsif ( $ARGS{"Toggle"} ) {
+    if (@$Selected) {
+        foreach my $value (@$Selected) {
+            my $parent = $value->getParent;
+
+            if ( $parent->getNodeValue eq 'AND' ) {
+                $parent->setNodeValue('OR');
+            }
+            else {
+                $parent->setNodeValue('AND');
+            }
+        }
+    }
+    else {
+        push( @results, [ loc("error: nothing to toggle"), -1 ] );
+    }
+}
+
+$Tree->PruneChildlessAggregators;
+
+return @results;
+</%INIT>
+</%METHOD>

Modified: rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditSearches
==============================================================================
--- rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditSearches	(original)
+++ rt/branches/3.7-EXPERIMENTAL/html/Search/Elements/EditSearches	Sat Dec 17 21:11:09 2005
@@ -44,62 +44,211 @@
 %# 
 %# END BPS TAGGED BLOCK }}}
 <&| /Widgets/TitleBox, title => loc('Saved searches') &>
+
 %# Hide all the save functionality if the user shouldn't see it.
 % if ($session{'CurrentUser'}->HasRight( Right => 'CreateSavedSearch',
-%                                       Object=> $RT::System )) {
+%                                        Object=> $RT::System )) {
 <&|/l&>Privacy:</&>
-% if ($CurrentSearch->{'Object'} && $CurrentSearch->{'Object'}->id) {
-<& SearchPrivacy, Object => $CurrentSearch->{'Object'}->Object &><br>
+% if ( $Object && $Object->id ) {
+<& SearchPrivacy, Object => $Object->Object &>
 % } else {
-<& SelectSearchObject, Name => 'Owner', Objects => \@Objects &><br>
+<& SelectSearchObject, Name => 'SavedSearchOwner', Objects => \@Objects &>
 % }
-<&|/l&>Description</&>:<br>
-<font size="-1"><input size="25" name="Description" value="<% $CurrentSearch->{'Description'} || '' %>"></font>
+<br />
+<&|/l&>Description</&>:
+<input size="25" name="SavedSearchDescription" value="<% $Description %>">
+
 <nobr>
-% if ($SearchId ne 'new') {
+% if ($Id ne 'new') {
 % if ($Dirty) {
-<input type="submit" class="button" name="Revert" value="<%loc('Revert')%>">
+<input type="submit" class="button" name="SavedSearchRevert" value="<%loc('Revert')%>" />
 % }
-<input type="submit" class="button" name="Delete" value="<%loc('Delete')%>">
-<input type="submit" class="button" name="CopySearch" value="<%loc('Copy')%>">
+<input type="submit" class="button" name="SavedSearchDelete" value="<%loc('Delete')%>" />
+<input type="submit" class="button" name="SavedSearchCopy"   value="<%loc('Copy')%>" />
 </nobr>
 
 % }
-<input type="submit" name="Save" value="<%loc('Save')%>" class="button" />
-<hr>
+<input type="submit" name="SavedSearchSave" value="<%loc('Save')%>" class="button" />
 % }
-<&|/l&>Load saved search:</&><br>
-<& SelectSearchesForObjects, Name => 'LoadSavedSearch', Objects => \@Objects&>
-<input value="<%loc('Load')%>" type="submit" class="button" />
+<br />
+
+<&|/l&>Load saved search:</&>
+<& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@Objects&>
+<input type="submit" value="<% loc('Load') %>" class="button" />
+
 </&>
 
-<%init>
+<%INIT>
 
-unless ($session{'CurrentUser'}->HasRight( Right => 'LoadSavedSearch',
-                                          Object=> $RT::System )) {
-    return;
-}
-my @Objects;
+return unless $session{'CurrentUser'}->HasRight(
+    Right => 'LoadSavedSearch',
+    Object=> $RT::System,
+);
+
+# If we're modifying an old query, check if it has changed
+my $Dirty = $m->comp(
+    'EditSearches:IsDirty',
+    Query => $CurrentSearch,
+    SavedSearch => { Id => $Id, Object => $Object, Description => $Description }
+);
 
+my @Objects;
 push @Objects,  $session{CurrentUser}->UserObj;
 
-my $groups = RT::Groups->new($session{'CurrentUser'});
-
+my $groups = RT::Groups->new( $session{'CurrentUser'} );
 $groups->LimitToUserDefinedGroups;
-$groups->WithMember(PrincipalId => $session{'CurrentUser'}->Id,
-                    Recursively => 1);
+$groups->WithMember(
+    PrincipalId => $session{'CurrentUser'}->Id,
+    Recursively => 1,
+);
 
-push (@Objects, @{$groups->ItemsArrayRef()});
-push @Objects, RT::System->new($session{'CurrentUser'})
+push @Objects, @{ $groups->ItemsArrayRef };
+push @Objects, RT::System->new( $session{'CurrentUser'} )
     if $session{'CurrentUser'}->HasRight( Object=> $RT::System,
-					  Right => 'SuperUser');
+					                      Right => 'SuperUser' );
 </%INIT>
 
 <%ARGS>
-$SearchId => undef
+$Id => 'new'
+$Object => undef
+$Description => ''
 $CurrentSearch => {}
-# XXX: next argument is not used! instead used description
-# field from CurrentSearch hash
-$Description => undef
-$Dirty => 0
 </%ARGS>
+
+<%METHOD Init>
+<%ARGS>
+$Query   => {}
+$SavedSearch => {}
+</%ARGS>
+<%INIT>
+
+my @FIELDS = qw(Query Format OrderBy Order RowsPerPage);
+
+$SavedSearch->{'Id'}          = $ARGS{'SavedSearchId'}          || 'new';
+$SavedSearch->{'Description'} = $ARGS{'SavedSearchDescription'} || '';
+
+if ( $ARGS{'SavedSearchRevert'} ) {
+    $ARGS{'SavedSearchLoad'} = $SavedSearch->{'Id'};
+}
+
+if ( $ARGS{'SavedSearchLoad'} ) {
+    my ($container, $id ) = _parse_saved_search ($ARGS{'SavedSearchLoad'});
+    my $search = $container->Attributes->WithId( $id );
+
+    $SavedSearch->{'Id'}          = $ARGS{'SavedSearchLoad'};
+    $SavedSearch->{'Object'}      = $search;
+    $SavedSearch->{'Description'} = $search->Description;
+
+    $Query->{$_} = $search->SubValue($_) foreach @FIELDS;
+}
+elsif ( $ARGS{'SavedSearchDelete'} ) {
+    # We set $SearchId to 'new' above already, so peek into the %ARGS
+    my ($container, $id) = _parse_saved_search( $ARGS{'SearchId'} );
+    if ( $container && $container->id ) {
+        # We have the object the entry is an attribute on; delete the entry...
+        $container->Attributes->DeleteEntry( Name => 'SavedSearch', id => $id );
+    }
+    $SavedSearch->{'Id'}          = 'new';
+    $SavedSearch->{'Object'}      = undef;
+}
+elsif ( $ARGS{'SavedSearchCopy'} ) {
+    $SavedSearch->{'Id'}          = 'new';
+    $SavedSearch->{'Object'}      = undef;
+    $SavedSearch->{'Description'} = loc( "[_1] copy", $SavedSearch->{'Description'} );
+}
+
+</%INIT>
+</%METHOD>
+
+<%METHOD Save>
+<%ARGS>
+$Query   => {}
+$SavedSearch => {}
+</%ARGS>
+<%INIT>
+
+return unless $ARGS{'SavedSearchSave'};
+
+my @results;
+my $obj  = $SavedSearch->{'Object'};
+my $id   = $SavedSearch->{'Id'};
+my $desc = $SavedSearch->{'Description'};
+
+if ( $obj && $obj->id ) {
+    # permission check
+    if ($obj->Object->isa('RT::System')) {
+        unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
+            push @results, [ loc("No permission to save system-wide searches"), 0 ];
+            return @results;
+        }
+    }
+
+    $obj->SetSubValues( %$Query );
+    $obj->SetDescription( $desc );
+}
+elsif ( $id eq 'new' && $ARGS{'SavedSearchOwner'} =~ /^(.*?)-(\d+)$/ ) {
+    # We're saving a new search
+    my $obj_type = $1;
+    my $obj_id   = $2;
+
+    # Find out if we're saving on the user, or a group
+    my $container_object = _load_container_object ($obj_type, $obj_id);
+
+    if ( $container_object->id ) {
+        # permission check
+        if ( $container_object->isa('RT::System') ) {
+            unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
+                push @results, [ loc("No permission to save system-wide searches"), 0 ];
+                return @results;
+            }
+        }
+
+        my $name = $obj_type eq 'RT::System' ? "Search - $desc" : 'SavedSearch';
+        # If we got one or the other, add the saerch
+        my ( $search_id, $search_msg ) = $container_object->AddAttribute(
+            Name        => $name,
+            Description => $desc,
+            Content     => $Query,
+        );
+        unless ( $search_id ) {
+            push @results, [ loc("Can't find a saved search to work with").': '.loc($search_msg), 0 ];
+            return @results;
+        }
+
+        $SavedSearch->{'Object'} =
+            $session{'CurrentUser'}->UserObj->Attributes->WithId($search_id);
+        # Build new SearchId
+        $SavedSearch->{'Id'} =
+            ref( $session{'CurrentUser'}->UserObj ) . '-'
+                . $session{'CurrentUser'}->UserObj->Id
+                . '-SavedSearch-'
+                . $SavedSearch->{'Object'}->Id;
+    }
+}
+else {
+    push @results, [ loc("Can't save this search"), 0 ];
+}
+
+return @results;
+
+</%INIT>
+</%METHOD>
+
+<%METHOD IsDirty>
+<%ARGS>
+$Query   => {}
+$SavedSearch => {}
+</%ARGS>
+<%INIT>
+
+my $obj  = $SavedSearch->{'Object'};
+return 0 unless $obj && $obj->id;
+
+foreach( qw(Query Format OrderBy Order RowsPerPage) ) {
+    return 1 if $obj->SubValue( $_ ) ne $Query->{$_};
+}
+
+return 0;
+
+</%INIT>
+</%METHOD>


More information about the Rt-commit mailing list