]> git.uio.no Git - usit-rt.git/blame - share/html/Search/Elements/EditSearches
Ported move-script from 3.8 (no api-changes)
[usit-rt.git] / share / html / Search / Elements / EditSearches
CommitLineData
84fb5b46
MKG
1%# BEGIN BPS TAGGED BLOCK {{{
2%#
3%# COPYRIGHT:
4%#
403d7b0b 5%# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
84fb5b46
MKG
6%# <sales@bestpractical.com>
7%#
8%# (Except where explicitly superseded by other copyright notices)
9%#
10%#
11%# LICENSE:
12%#
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
16%# from www.gnu.org.
17%#
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.
22%#
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.
28%#
29%#
30%# CONTRIBUTION SUBMISSION POLICY:
31%#
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.)
37%#
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.
46%#
47%# END BPS TAGGED BLOCK }}}
48<div class="edit-saved-searches">
49<&| /Widgets/TitleBox, title => loc($Title)&>
50
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 : '' &>
55<br />
56<span class="label"><&|/l&>Description</&>:</span>
57<input size="25" name="SavedSearchDescription" value="<% $Description || '' %>" />
58
59% if ($Id ne 'new') {
60<nobr>
61% if ( $Dirty ) {
62<input type="submit" class="button" name="SavedSearchRevert" value="<%loc('Revert')%>" />
63% }
64<input type="submit" class="button" name="SavedSearchDelete" value="<%loc('Delete')%>" />
65% if ( $AllowCopy ) {
66<input type="submit" class="button" name="SavedSearchCopy" value="<%loc('Save as New')%>" />
67% }
68</nobr>
69% }
70% if ( $Object && $Object->Id ) {
71<input type="submit" class="button" id="SavedSearchSave" name="SavedSearchSave" value="<%loc('Update')%>" />
72% } else {
73<input type="submit" class="button" id="SavedSearchSave" name="SavedSearchSave" value="<%loc('Save')%>" />
74%}
75% }
76<br />
77<hr />
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" />
81
82</&>
83</div>
84<%INIT>
85return unless $session{'CurrentUser'}->HasRight(
86 Right => 'LoadSavedSearch',
87 Object => $RT::System,
88);
89
90my $can_modify = $session{'CurrentUser'}->HasRight(
91 Right => 'CreateSavedSearch',
92 Object => $RT::System,
93);
94
95use RT::SavedSearch;
96my @Objects = RT::SavedSearch->new($session{CurrentUser})->_PrivacyObjects;
97push @Objects, RT::System->new( $session{'CurrentUser'} )
98 if $session{'CurrentUser'}->HasRight( Object=> $RT::System,
99 Right => 'SuperUser' );
100
101my $is_dirty = sub {
102 my %arg = (
103 Query => {},
104 SavedSearch => {},
105 SearchFields => [qw(Query Format OrderBy Order RowsPerPage)],
106 @_
107 );
108
109 my $obj = $arg{'SavedSearch'}->{'Object'};
110 return 0 unless $obj && $obj->id;
111
112 foreach( @{ $arg{'SearchFields'} } ) {
113 return 1 if $obj->SubValue( $_ ) ne $arg{'Query'}->{$_};
114 }
115
116 return 0;
117};
118
119# If we're modifying an old query, check if it's been changed
120my $Dirty = $is_dirty->(
121 Query => $CurrentSearch,
122 SavedSearch => { Id => $Id, Object => $Object, Description => $Description },
123 SearchFields => \@SearchFields,
124);
125
126</%INIT>
127
128<%ARGS>
129$Id => 'new'
130$Object => undef
131$Type => 'Ticket'
132$Description => ''
133$CurrentSearch => {}
134@SearchFields => ()
135$AllowCopy => 1
136$Title => loc('Saved searches')
137</%ARGS>
138
139<%METHOD Init>
140<%ARGS>
141$Query => {}
142$SavedSearch => {}
143@SearchFields => qw(Query Format OrderBy Order RowsPerPage)
144</%ARGS>
145<%INIT>
146
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;
151
152my @results;
153
154if ( $ARGS{'SavedSearchRevert'} ) {
155 $ARGS{'SavedSearchLoad'} = $SavedSearch->{'Id'};
156}
157
158if ( $ARGS{'SavedSearchLoad'} ) {
159 my ($container, $id ) = _parse_saved_search ($ARGS{'SavedSearchLoad'});
160 if ( $container ) {
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;
166
167 if ( $ARGS{'SavedSearchRevert'} ) {
168 push @results, loc('Loaded original "[_1]" saved search', $SavedSearch->{'Description'} );
169 } else {
170 push @results, loc('Loaded saved search "[_1]"', $SavedSearch->{'Description'} );
171 }
172 }
173 else {
174 push @results, loc( 'Can not load saved search "[_1]"',
175 $ARGS{'SavedSearchLoad'} );
176 return @results;
177 }
178}
179elsif ( $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 );
185 unless ( $val ) {
186 push @results, $msg;
187 return @results;
188 }
189 }
190 $SavedSearch->{'Id'} = 'new';
191 $SavedSearch->{'Object'} = undef;
192 $SavedSearch->{'Description'} = undef;
193 push @results, loc("Deleted saved search");
194}
195elsif ( $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'};
200 } else {
201 $SavedSearch->{'Description'} = loc( "[_1] copy", $SavedSearch->{'Object'}->Description );
202 }
203 $SavedSearch->{'Id'} = 'new';
204 $SavedSearch->{'Object'} = undef;
205}
206
207if ( $SavedSearch->{'Id'} && $SavedSearch->{'Id'} ne 'new'
208 && !$SavedSearch->{'Object'} )
209{
210 my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
211 $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
212 $SavedSearch->{'Description'} ||= $SavedSearch->{'Object'}->Description;
213}
214
215return @results;
216
217</%INIT>
218</%METHOD>
219
220<%METHOD Save>
221<%ARGS>
222$Query => {}
223$SavedSearch => {}
224@SearchFields => qw(Query Format OrderBy Order RowsPerPage)
225</%ARGS>
226<%INIT>
227
228return unless $ARGS{'SavedSearchSave'} || $ARGS{'SavedSearchCopy'};
229
230my @results;
231my $obj = $SavedSearch->{'Object'};
232my $id = $SavedSearch->{'Id'};
233my $desc = $SavedSearch->{'Description'};
234my $privacy = $SavedSearch->{'Privacy'};
235
236my %params = map { $_ => $Query->{$_} } @SearchFields;
237my ($new_obj_type, $new_obj_id) = split(/\-/, ($privacy || ''));
238
239if ( $obj && $obj->id ) {
240 # permission check
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");
244 return @results;
245 }
246 }
247
248 $obj->SetSubValues( %params );
249 $obj->SetDescription( $desc );
250
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;
255
256 if ( $new_obj_type && $new_obj_id ) {
257 my ($val, $msg);
258
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
262
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,
268 );
269 push @results, loc( 'Unable to set privacy object: [_1]', $msg )
270 unless ($val);
271 }
272 if ( $new_obj_id != $obj_id ) {
273 ( $val, $msg ) = $obj->__Set(
274 Field => 'ObjectId',
275 Value => $new_obj_id,
276 );
277 push @results, loc( 'Unable to set privacy id: [_1]', $msg )
278 unless ($val);
279 }
280 }
281 else {
282 # two loc are just for convenience so we don't need to
283 # write an extra i18n translation item
284 push @results,
285 loc( 'Unable to set privacy object or id: [_1]',
286 loc('Permission Denied') )
287 }
288 } else {
289 push @results, loc('Unable to determine object type or id');
290 }
291 push @results, loc('Updated saved search "[_1]"', $desc);
292}
293elsif ( $id eq 'new' ) {
294 my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
295 my ($status, $msg) = $saved_search->Save(
296 Privacy => $privacy,
297 Name => $desc,
298 Type => $SavedSearch->{'Type'},
299 SearchParams => \%params,
300 );
301
302 if ( $status ) {
303 $SavedSearch->{'Object'} =
304 $session{'CurrentUser'}->UserObj->Attributes->WithId( $saved_search->Id );
305 # Build new SearchId
306 $SavedSearch->{'Id'} =
307 ref( $session{'CurrentUser'}->UserObj ) . '-'
308 . $session{'CurrentUser'}->UserObj->Id
309 . '-SavedSearch-'
310 . $SavedSearch->{'Object'}->Id;
311 }
312 else {
313 push @results, loc("Can't find a saved search to work with").': '.loc($msg);
314 }
315}
316else {
317 push @results, loc("Can't save this search");
318}
319
320return @results;
321
322</%INIT>
323</%METHOD>