1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2012 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 <div class="edit-saved-searches">
49 <&| /Widgets/TitleBox, title => loc($Title)&>
51 %# Hide all the save functionality if the user shouldn't see it.
52 % if ( $can_modify ) {
53 <span class="label"><&|/l&>Privacy</&>:</span>
54 <& SelectSearchObject, Name => 'SavedSearchOwner', Objects => \@Objects, Object => ( $Object && $Object->id ) ? $Object->Object : '' &>
56 <span class="label"><&|/l&>Description</&>:</span>
57 <input size="25" name="SavedSearchDescription" value="<% $Description || '' %>" />
62 <input type="submit" class="button" name="SavedSearchRevert" value="<%loc('Revert')%>" />
64 <input type="submit" class="button" name="SavedSearchDelete" value="<%loc('Delete')%>" />
66 <input type="submit" class="button" name="SavedSearchCopy" value="<%loc('Save as New')%>" />
70 % if ( $Object && $Object->Id ) {
71 <input type="submit" class="button" id="SavedSearchSave" name="SavedSearchSave" value="<%loc('Update')%>" />
73 <input type="submit" class="button" id="SavedSearchSave" name="SavedSearchSave" value="<%loc('Save')%>" />
78 <span class="label"><&|/l&>Load saved search</&>:</span>
79 <& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@Objects, SearchType => $Type &>
80 <input type="submit" value="<% loc('Load') %>" id="SavedSearchLoadSubmit" name="SavedSearchLoadSubmit" class="button" />
85 return unless $session{'CurrentUser'}->HasRight(
86 Right => 'LoadSavedSearch',
87 Object => $RT::System,
90 my $can_modify = $session{'CurrentUser'}->HasRight(
91 Right => 'CreateSavedSearch',
92 Object => $RT::System,
96 my @Objects = RT::SavedSearch->new($session{CurrentUser})->_PrivacyObjects;
97 push @Objects, RT::System->new( $session{'CurrentUser'} )
98 if $session{'CurrentUser'}->HasRight( Object=> $RT::System,
99 Right => 'SuperUser' );
105 SearchFields => [qw(Query Format OrderBy Order RowsPerPage)],
109 my $obj = $arg{'SavedSearch'}->{'Object'};
110 return 0 unless $obj && $obj->id;
112 foreach( @{ $arg{'SearchFields'} } ) {
113 return 1 if $obj->SubValue( $_ ) ne $arg{'Query'}->{$_};
119 # If we're modifying an old query, check if it's been changed
120 my $Dirty = $is_dirty->(
121 Query => $CurrentSearch,
122 SavedSearch => { Id => $Id, Object => $Object, Description => $Description },
123 SearchFields => \@SearchFields,
136 $Title => loc('Saved searches')
143 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
147 $SavedSearch->{'Id'} = ( $ARGS{Type} && $ARGS{Type} eq 'Chart' ?
148 $ARGS{'SavedChartSearchId'} : $ARGS{'SavedSearchId'} ) || 'new';
149 $SavedSearch->{'Description'} = $ARGS{'SavedSearchDescription'} || undef;
150 $SavedSearch->{'Privacy'} = $ARGS{'SavedSearchOwner'} || undef;
154 if ( $ARGS{'SavedSearchRevert'} ) {
155 $ARGS{'SavedSearchLoad'} = $SavedSearch->{'Id'};
158 if ( $ARGS{'SavedSearchLoad'} ) {
159 my ($container, $id ) = _parse_saved_search ($ARGS{'SavedSearchLoad'});
161 my $search = $container->Attributes->WithId( $id );
162 $SavedSearch->{'Id'} = $ARGS{'SavedSearchLoad'};
163 $SavedSearch->{'Object'} = $search;
164 $SavedSearch->{'Description'} = $search->Description;
165 $Query->{$_} = $search->SubValue($_) foreach @SearchFields;
167 if ( $ARGS{'SavedSearchRevert'} ) {
168 push @results, loc('Loaded original "[_1]" saved search', $SavedSearch->{'Description'} );
170 push @results, loc('Loaded saved search "[_1]"', $SavedSearch->{'Description'} );
174 push @results, loc( 'Can not load saved search "[_1]"',
175 $ARGS{'SavedSearchLoad'} );
179 elsif ( $ARGS{'SavedSearchDelete'} ) {
180 # We set $SearchId to 'new' above already, so peek into the %ARGS
181 my ($container, $id) = _parse_saved_search( $SavedSearch->{'Id'} );
182 if ( $container && $container->id ) {
183 # We have the object the entry is an attribute on; delete the entry...
184 my ($val, $msg) = $container->Attributes->DeleteEntry( Name => 'SavedSearch', id => $id );
190 $SavedSearch->{'Id'} = 'new';
191 $SavedSearch->{'Object'} = undef;
192 $SavedSearch->{'Description'} = undef;
193 push @results, loc("Deleted saved search");
195 elsif ( $ARGS{'SavedSearchCopy'} ) {
196 my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
197 $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
198 if ( $ARGS{'SavedSearchDescription'} && $ARGS{'SavedSearchDescription'} ne $SavedSearch->{'Object'}->Description ) {
199 $SavedSearch->{'Description'} = $ARGS{'SavedSearchDescription'};
201 $SavedSearch->{'Description'} = loc( "[_1] copy", $SavedSearch->{'Object'}->Description );
203 $SavedSearch->{'Id'} = 'new';
204 $SavedSearch->{'Object'} = undef;
207 if ( $SavedSearch->{'Id'} && $SavedSearch->{'Id'} ne 'new'
208 && !$SavedSearch->{'Object'} )
210 my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
211 $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
212 $SavedSearch->{'Description'} ||= $SavedSearch->{'Object'}->Description;
224 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
228 return unless $ARGS{'SavedSearchSave'} || $ARGS{'SavedSearchCopy'};
231 my $obj = $SavedSearch->{'Object'};
232 my $id = $SavedSearch->{'Id'};
233 my $desc = $SavedSearch->{'Description'};
234 my $privacy = $SavedSearch->{'Privacy'};
236 my %params = map { $_ => $Query->{$_} } @SearchFields;
237 my ($new_obj_type, $new_obj_id) = split(/\-/, ($privacy || ''));
239 if ( $obj && $obj->id ) {
241 if ($obj->Object->isa('RT::System')) {
242 unless ($session{'CurrentUser'}->HasRight( Object=> $RT::System, Right => 'SuperUser')) {
243 push @results, loc("No permission to save system-wide searches");
248 $obj->SetSubValues( %params );
249 $obj->SetDescription( $desc );
251 my $obj_type = ref($obj->Object);
252 # We need to get current obj_id now, because when we change obj_type to
253 # RT::System, $obj->Object->Id returns 1, not the old one :(
254 my $obj_id = $obj->Object->Id;
256 if ( $new_obj_type && $new_obj_id ) {
259 # we need to check right before we change any of ObjectType and ObjectId,
260 # or it will fail the 2nd change if we use SetObjectType and
261 # SetObjectId sequentially
263 if ( $obj->CurrentUserHasRight('update') ) {
264 if ( $new_obj_type ne $obj_type ) {
265 ( $val, $msg ) = $obj->__Set(
266 Field => 'ObjectType',
267 Value => $new_obj_type,
269 push @results, loc( 'Unable to set privacy object: [_1]', $msg )
272 if ( $new_obj_id != $obj_id ) {
273 ( $val, $msg ) = $obj->__Set(
275 Value => $new_obj_id,
277 push @results, loc( 'Unable to set privacy id: [_1]', $msg )
282 # two loc are just for convenience so we don't need to
283 # write an extra i18n translation item
285 loc( 'Unable to set privacy object or id: [_1]',
286 loc('Permission Denied') )
289 push @results, loc('Unable to determine object type or id');
291 push @results, loc('Updated saved search "[_1]"', $desc);
293 elsif ( $id eq 'new' ) {
294 my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
295 my ($status, $msg) = $saved_search->Save(
298 Type => $SavedSearch->{'Type'},
299 SearchParams => \%params,
303 $SavedSearch->{'Object'} =
304 $session{'CurrentUser'}->UserObj->Attributes->WithId( $saved_search->Id );
306 $SavedSearch->{'Id'} =
307 ref( $session{'CurrentUser'}->UserObj ) . '-'
308 . $session{'CurrentUser'}->UserObj->Id
310 . $SavedSearch->{'Object'}->Id;
313 push @results, loc("Can't find a saved search to work with").': '.loc($msg);
317 push @results, loc("Can't save this search");