1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
6 %# <sales@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 my ($val, $msg) = $container->Attributes->DeleteEntry( Name => 'SavedSearch', id => $id );
189 $SavedSearch->{'Id'} = 'new';
190 $SavedSearch->{'Object'} = undef;
191 $SavedSearch->{'Description'} = undef;
192 push @results, loc("Deleted saved search");
194 elsif ( $ARGS{'SavedSearchCopy'} ) {
195 my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
196 $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
197 if ( $ARGS{'SavedSearchDescription'} && $ARGS{'SavedSearchDescription'} ne $SavedSearch->{'Object'}->Description ) {
198 $SavedSearch->{'Description'} = $ARGS{'SavedSearchDescription'};
200 $SavedSearch->{'Description'} = loc( "[_1] copy", $SavedSearch->{'Object'}->Description );
202 $SavedSearch->{'Id'} = 'new';
203 $SavedSearch->{'Object'} = undef;
206 if ( $SavedSearch->{'Id'} && $SavedSearch->{'Id'} ne 'new'
207 && !$SavedSearch->{'Object'} )
209 my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
210 $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
211 $SavedSearch->{'Description'} ||= $SavedSearch->{'Object'}->Description;
223 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
227 return unless $ARGS{'SavedSearchSave'} || $ARGS{'SavedSearchCopy'};
230 my $obj = $SavedSearch->{'Object'};
231 my $id = $SavedSearch->{'Id'};
232 my $desc = $SavedSearch->{'Description'};
233 my $privacy = $SavedSearch->{'Privacy'};
235 my %params = map { $_ => $Query->{$_} } @SearchFields;
236 my ($new_obj_type, $new_obj_id) = split(/\-/, ($privacy || ''));
238 if ( $obj && $obj->id ) {
240 if ($obj->Object->isa('RT::System')) {
241 unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
242 push @results, loc("No permission to save system-wide searches");
247 $obj->SetSubValues( %params );
248 $obj->SetDescription( $desc );
250 my $obj_type = ref($obj->Object);
251 # We need to get current obj_id now, because when we change obj_type to
252 # RT::System, $obj->Object->Id returns 1, not the old one :(
253 my $obj_id = $obj->Object->Id;
255 if ( $new_obj_type && $new_obj_id ) {
258 # we need to check right before we change any of ObjectType and ObjectId,
259 # or it will fail the 2nd change if we use SetObjectType and
260 # SetObjectId sequentially
262 if ( $obj->CurrentUserHasRight('update') ) {
263 if ( $new_obj_type ne $obj_type ) {
264 ( $val, $msg ) = $obj->__Set(
265 Field => 'ObjectType',
266 Value => $new_obj_type,
268 push @results, loc( 'Unable to set privacy object: [_1]', $msg )
271 if ( $new_obj_id != $obj_id ) {
272 ( $val, $msg ) = $obj->__Set(
274 Value => $new_obj_id,
276 push @results, loc( 'Unable to set privacy id: [_1]', $msg )
281 # two loc are just for convenience so we don't need to
282 # write an extra i18n translation item
284 loc( 'Unable to set privacy object or id: [_1]',
285 loc('Permission Denied') )
288 push @results, loc('Unable to determine object type or id');
290 push @results, loc('Updated saved search "[_1]"', $desc);
292 elsif ( $id eq 'new' ) {
293 my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
294 my ($status, $msg) = $saved_search->Save(
297 Type => $SavedSearch->{'Type'},
298 SearchParams => \%params,
302 $SavedSearch->{'Object'} =
303 $session{'CurrentUser'}->UserObj->Attributes->WithId( $saved_search->Id );
305 $SavedSearch->{'Id'} =
306 ref( $session{'CurrentUser'}->UserObj ) . '-'
307 . $session{'CurrentUser'}->UserObj->Id
309 . $SavedSearch->{'Object'}->Id;
312 push @results, loc("Can't find a saved search to work with").': '.loc($msg);
316 push @results, loc("Can't save this search");