1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 %# <jesse@bestpractical.com>
8 %# (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 %# CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 %# END BPS TAGGED BLOCK }}}
48 <&| /Widgets/TitleBox, title => loc($Title)&>
50 %# Hide all the save functionality if the user shouldn't see it.
51 % if ( $can_modify ) {
53 <& SelectSearchObject, Name => 'SavedSearchOwner', Objects => \@Objects, Object => ( $Object && $Object->id ) ? $Object->Object : '' &>
55 <&|/l&>Description</&>:
56 <input size="25" name="SavedSearchDescription" value="<% $Description || '' %>" />
61 <input type="submit" class="button" name="SavedSearchRevert" value="<%loc('Revert')%>" />
63 <input type="submit" class="button" name="SavedSearchDelete" value="<%loc('Delete')%>" />
65 <input type="submit" class="button" name="SavedSearchCopy" value="<%loc('Save as New')%>" />
69 % if ( $Object && $Object->Id ) {
70 <input type="submit" class="button" name="SavedSearchSave" value="<%loc('Update')%>" />
72 <input type="submit" class="button" name="SavedSearchSave" value="<%loc('Save')%>" />
77 <&|/l&>Load saved search:</&>
78 <& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@Objects, SearchType => $Type &>
79 <input type="submit" value="<% loc('Load') %>" class="button" />
84 return unless $session{'CurrentUser'}->HasRight(
85 Right => 'LoadSavedSearch',
86 Object => $RT::System,
89 my $can_modify = $session{'CurrentUser'}->HasRight(
90 Right => 'CreateSavedSearch',
91 Object => $RT::System,
94 use RT::SavedSearches;
95 my @Objects = RT::SavedSearches->new($session{CurrentUser})->_PrivacyObjects;
96 push @Objects, RT::System->new( $session{'CurrentUser'} )
97 if $session{'CurrentUser'}->HasRight( Object=> $RT::System,
98 Right => 'SuperUser' );
104 SearchFields => [qw(Query Format OrderBy Order RowsPerPage)],
108 my $obj = $arg{'SavedSearch'}->{'Object'};
109 return 0 unless $obj && $obj->id;
111 foreach( @{ $arg{'SearchFields'} } ) {
112 return 1 if $obj->SubValue( $_ ) ne $arg{'Query'}->{$_};
118 # If we're modifying an old query, check if it's been changed
119 my $Dirty = $is_dirty->(
120 Query => $CurrentSearch,
121 SavedSearch => { Id => $Id, Object => $Object, Description => $Description },
122 SearchFields => \@SearchFields,
135 $Title => loc('Saved searches')
142 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
146 $SavedSearch->{'Id'} = ( $ARGS{Type} && $ARGS{Type} eq 'Chart' ?
147 $ARGS{'SavedChartSearchId'} : $ARGS{'SavedSearchId'} ) || 'new';
148 $SavedSearch->{'Description'} = $ARGS{'SavedSearchDescription'} || undef;
149 $SavedSearch->{'Privacy'} = $ARGS{'SavedSearchOwner'} || undef;
153 if ( $ARGS{'SavedSearchRevert'} ) {
154 $ARGS{'SavedSearchLoad'} = $SavedSearch->{'Id'};
157 if ( $ARGS{'SavedSearchLoad'} ) {
158 my ($container, $id ) = _parse_saved_search ($ARGS{'SavedSearchLoad'});
160 my $search = $container->Attributes->WithId( $id );
161 $SavedSearch->{'Id'} = $ARGS{'SavedSearchLoad'};
162 $SavedSearch->{'Object'} = $search;
163 $SavedSearch->{'Description'} = $search->Description;
164 $Query->{$_} = $search->SubValue($_) foreach @SearchFields;
166 if ( $ARGS{'SavedSearchRevert'} ) {
167 push @results, loc('Loaded original "[_1]" saved search', $SavedSearch->{'Description'} );
169 push @results, loc('Loaded saved search "[_1]"', $SavedSearch->{'Description'} );
173 push @results, loc( 'Can not load saved search "[_1]"',
174 $ARGS{'SavedSearchLoad'} );
178 elsif ( $ARGS{'SavedSearchDelete'} ) {
179 # We set $SearchId to 'new' above already, so peek into the %ARGS
180 my ($container, $id) = _parse_saved_search( $SavedSearch->{'Id'} );
181 if ( $container && $container->id ) {
182 # We have the object the entry is an attribute on; delete the entry...
183 $container->Attributes->DeleteEntry( Name => 'SavedSearch', id => $id );
185 $SavedSearch->{'Id'} = 'new';
186 $SavedSearch->{'Object'} = undef;
187 $SavedSearch->{'Description'} = undef;
188 push @results, loc("Deleted saved search");
190 elsif ( $ARGS{'SavedSearchCopy'} ) {
191 my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
192 $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
193 if ( $ARGS{'SavedSearchDescription'} && $ARGS{'SavedSearchDescription'} ne $SavedSearch->{'Object'}->Description ) {
194 $SavedSearch->{'Description'} = $ARGS{'SavedSearchDescription'};
196 $SavedSearch->{'Description'} = loc( "[_1] copy", $SavedSearch->{'Object'}->Description );
198 $SavedSearch->{'Id'} = 'new';
199 $SavedSearch->{'Object'} = undef;
202 if ( $SavedSearch->{'Id'} && $SavedSearch->{'Id'} ne 'new'
203 && !$SavedSearch->{'Object'} )
205 my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
206 $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
207 $SavedSearch->{'Description'} ||= $SavedSearch->{'Object'}->Description;
219 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
223 return unless $ARGS{'SavedSearchSave'} || $ARGS{'SavedSearchCopy'};
226 my $obj = $SavedSearch->{'Object'};
227 my $id = $SavedSearch->{'Id'};
228 my $desc = $SavedSearch->{'Description'};
229 my $privacy = $SavedSearch->{'Privacy'};
231 my %params = map { $_ => $Query->{$_} } @SearchFields;
232 my ($new_obj_type, $new_obj_id) = split(/\-/, ($privacy || ''));
234 if ( $obj && $obj->id ) {
236 if ($obj->Object->isa('RT::System')) {
237 unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
238 push @results, loc("No permission to save system-wide searches");
243 $obj->SetSubValues( %params );
244 $obj->SetDescription( $desc );
246 my $obj_type = ref($obj->Object);
247 # We need to get current obj_id now, because when we change obj_type to
248 # RT::System, $obj->Object->Id returns 1, not the old one :(
249 my $obj_id = $obj->Object->Id;
251 if ( $new_obj_type && $new_obj_id ) {
253 if ( $new_obj_type ne $obj_type ) {
254 ($val, $msg ) = $obj->SetObjectType($new_obj_type);
255 push @results, loc ('Unable to set privacy object: [_1]', $msg) unless ( $val );
257 if ( $new_obj_id != $obj_id ) {
258 ($val, $msg) = $obj->SetObjectId($new_obj_id);
259 push @results, loc ('Unable to set privacy id: [_1]', $msg) unless ( $val );
262 push @results, loc('Unable to determine object type or id');
264 push @results, loc('Updated saved search "[_1]"', $desc);
266 elsif ( $id eq 'new' ) {
267 my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
268 my ($status, $msg) = $saved_search->Save(
271 Type => $SavedSearch->{'Type'},
272 SearchParams => \%params,
276 $SavedSearch->{'Object'} =
277 $session{'CurrentUser'}->UserObj->Attributes->WithId( $saved_search->Id );
279 $SavedSearch->{'Id'} =
280 ref( $session{'CurrentUser'}->UserObj ) . '-'
281 . $session{'CurrentUser'}->UserObj->Id
283 . $SavedSearch->{'Object'}->Id;
286 push @results, loc("Can't find a saved search to work with").': '.loc($msg);
290 push @results, loc("Can't save this search");