[Rt-commit] r6565 - in rt/branches/3.6-RELEASE: . html/Search

jesse at bestpractical.com jesse at bestpractical.com
Fri Dec 1 13:55:02 EST 2006


Author: jesse
Date: Fri Dec  1 13:55:01 2006
New Revision: 6565

Modified:
   rt/branches/3.6-RELEASE/   (props changed)
   rt/branches/3.6-RELEASE/html/Search/Build.html
   rt/branches/3.6-RELEASE/lib/RT/SavedSearch.pm

Log:
 r45289 at pinglin (orig r6499):  clkao | 2006-11-22 07:21:00 -0500
 Refactor Search/Build.html to save searches using RT::SavedSearch.


Modified: rt/branches/3.6-RELEASE/html/Search/Build.html
==============================================================================
--- rt/branches/3.6-RELEASE/html/Search/Build.html	(original)
+++ rt/branches/3.6-RELEASE/html/Search/Build.html	Fri Dec  1 13:55:01 2006
@@ -712,49 +712,29 @@
         $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 =
+    elsif ( $SearchId eq 'new' ) {
+        my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
+        my ( $ok, $search_msg ) = $saved_search->Save(
+            Privacy      => $ARGS{'Owner'},
+            Name         => $Description,
+            SearchParams => {
+                Format      => $Format,
+                Query       => $Query,
+                Order       => $Order,
+                OrderBy     => $OrderBy,
+                RowsPerPage => $RowsPerPage } );
+
+	if ($ok) {
+	    $search = $session{'CurrentUser'}->UserObj->Attributes->WithId($saved_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 find a saved search to work with").': '.loc($search_msg), 0 ];
 	}
     }
     else {

Modified: rt/branches/3.6-RELEASE/lib/RT/SavedSearch.pm
==============================================================================
--- rt/branches/3.6-RELEASE/lib/RT/SavedSearch.pm	(original)
+++ rt/branches/3.6-RELEASE/lib/RT/SavedSearch.pm	Fri Dec  1 13:55:01 2006
@@ -148,23 +148,34 @@
 
     $params{'SearchType'} = $type;
     my $object = $self->_GetObject($privacy);
-    if ($object) {
-	my ($att_id, $att_msg) = $object->AddAttribute(
-						       'Name' => 'SavedSearch',
-						       'Description' => $name,
-						       'Content' => \%params);
-	if ($att_id) {
-	    $self->{'Attribute'} = $object->Attributes->WithId($att_id);
-	    $self->{'Id'} = $att_id;
-	    $self->{'Privacy'} = $privacy;
-	    $self->{'Type'} = $type;
-	    return (1, $self->loc("Saved search [_1]", $name));
-	} else {
-	    $RT::Logger->error("SavedSearch save failure: $att_msg");
-	    return (0, $self->loc("Failed to create search attribute"));
-	}
-    } else {
-	return (0, $self->loc("Failed to load object for [_1]", $privacy));
+
+    return (0, $self->loc("Failed to load object for [_1]", $privacy))
+        unless $object;
+
+    if ( $object->isa('RT::System') ) {
+        return ( 0, $self->loc("No permission to save system-wide searches") )
+            unless $self->CurrentUser->HasRight(
+            Object => $RT::System,
+            Right  => 'SuperUser'
+        );
+    }
+
+    my $attr_name = $object->isa('RT::System') ? "Search - $name" : 'SavedSearch';
+    my ( $att_id, $att_msg ) = $object->AddAttribute(
+        'Name'        => $attr_name,
+        'Description' => $name,
+        'Content'     => \%params
+    );
+    if ($att_id) {
+        $self->{'Attribute'} = $object->Attributes->WithId($att_id);
+        $self->{'Id'}        = $att_id;
+        $self->{'Privacy'}   = $privacy;
+        $self->{'Type'}      = $type;
+        return ( 1, $self->loc( "Saved search [_1]", $name ) );
+    }
+    else {
+        $RT::Logger->error("SavedSearch save failure: $att_msg");
+        return ( 0, $self->loc("Failed to create search attribute") );
     }
 }
 


More information about the Rt-commit mailing list