import rt 3.8.9
[freeside.git] / rt / share / html / Search / Elements / EditSearches
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
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 <&| /Widgets/TitleBox, title => loc($Title)&>
49
50 %# Hide all the save functionality if the user shouldn't see it.
51 % if ( $can_modify ) {
52 <&|/l&>Privacy:</&>
53 <& SelectSearchObject, Name => 'SavedSearchOwner', Objects => \@Objects, Object => ( $Object && $Object->id ) ? $Object->Object : '' &>
54 <br />
55 <&|/l&>Description</&>:
56 <input size="25" name="SavedSearchDescription" value="<% $Description || '' %>" />
57
58 % if ($Id ne 'new') {
59 <nobr>
60 % if ( $Dirty ) {
61 <input type="submit" class="button" name="SavedSearchRevert" value="<%loc('Revert')%>" />
62 % }
63 <input type="submit" class="button" name="SavedSearchDelete" value="<%loc('Delete')%>" />
64 % if ( $AllowCopy ) {
65 <input type="submit" class="button" name="SavedSearchCopy"   value="<%loc('Save as New')%>" />
66 % }
67 </nobr>
68 % }
69 % if ( $Object && $Object->Id ) {
70 <input type="submit" class="button" name="SavedSearchSave"   value="<%loc('Update')%>" />
71 % } else {
72 <input type="submit" class="button" name="SavedSearchSave"   value="<%loc('Save')%>" />
73 %}
74 % }
75 <br />
76 <hr />
77 <&|/l&>Load saved search:</&>
78 <& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@Objects, SearchType => $Type &>
79 <input type="submit" value="<% loc('Load') %>" class="button" />
80
81 </&>
82
83 <%INIT>
84 return unless $session{'CurrentUser'}->HasRight(
85     Right  => 'LoadSavedSearch',
86     Object => $RT::System,
87 );
88
89 my $can_modify = $session{'CurrentUser'}->HasRight(
90     Right  => 'CreateSavedSearch',
91     Object => $RT::System,
92 );
93
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' );
99
100 my $is_dirty = sub {
101     my %arg = (
102         Query       => {},
103         SavedSearch => {},
104         SearchFields => [qw(Query Format OrderBy Order RowsPerPage)],
105         @_
106     );
107
108     my $obj  = $arg{'SavedSearch'}->{'Object'};
109     return 0 unless $obj && $obj->id;
110
111     foreach( @{ $arg{'SearchFields'} } ) {
112         return 1 if $obj->SubValue( $_ ) ne $arg{'Query'}->{$_};
113     }
114
115     return 0;
116 };
117
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,
123 );
124
125 </%INIT>
126
127 <%ARGS>
128 $Id            => 'new'
129 $Object        => undef
130 $Type          => 'Ticket'
131 $Description   => ''
132 $CurrentSearch => {}
133 @SearchFields   => ()
134 $AllowCopy     => 1
135 $Title         => loc('Saved searches')
136 </%ARGS>
137
138 <%METHOD Init>
139 <%ARGS>
140 $Query       => {}
141 $SavedSearch => {}
142 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
143 </%ARGS>
144 <%INIT>
145
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;
150
151 my @results;
152
153 if ( $ARGS{'SavedSearchRevert'} ) {
154     $ARGS{'SavedSearchLoad'} = $SavedSearch->{'Id'};
155 }
156
157 if ( $ARGS{'SavedSearchLoad'} ) {
158     my ($container, $id ) = _parse_saved_search ($ARGS{'SavedSearchLoad'});
159     if ( $container ) {
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;
165
166         if ( $ARGS{'SavedSearchRevert'} ) {
167             push @results, loc('Loaded original "[_1]" saved search', $SavedSearch->{'Description'} );
168         } else {
169             push @results, loc('Loaded saved search "[_1]"', $SavedSearch->{'Description'} );
170         }
171     }
172     else {
173         push @results, loc( 'Can not load saved search "[_1]"',
174                 $ARGS{'SavedSearchLoad'} );
175         return @results;
176     }
177 }
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 );
184         unless ( $val ) {
185             push @results, $msg;
186             return @results;
187         }
188     }
189     $SavedSearch->{'Id'}          = 'new';
190     $SavedSearch->{'Object'}      = undef;
191     $SavedSearch->{'Description'} = undef;
192     push @results, loc("Deleted saved search");
193 }
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'};
199     } else {
200         $SavedSearch->{'Description'} = loc( "[_1] copy", $SavedSearch->{'Object'}->Description );
201     }
202     $SavedSearch->{'Id'}          = 'new';
203     $SavedSearch->{'Object'}      = undef;
204 }
205
206 if ( $SavedSearch->{'Id'} && $SavedSearch->{'Id'} ne 'new'
207      && !$SavedSearch->{'Object'} )
208 {
209     my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
210     $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
211     $SavedSearch->{'Description'} ||= $SavedSearch->{'Object'}->Description;
212 }
213
214 return @results;
215
216 </%INIT>
217 </%METHOD>
218
219 <%METHOD Save>
220 <%ARGS>
221 $Query        => {}
222 $SavedSearch  => {}
223 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
224 </%ARGS>
225 <%INIT>
226
227 return unless $ARGS{'SavedSearchSave'} || $ARGS{'SavedSearchCopy'};
228
229 my @results;
230 my $obj  = $SavedSearch->{'Object'};
231 my $id   = $SavedSearch->{'Id'};
232 my $desc = $SavedSearch->{'Description'};
233 my $privacy = $SavedSearch->{'Privacy'};
234
235 my %params = map { $_ => $Query->{$_} } @SearchFields;
236 my ($new_obj_type, $new_obj_id) = split(/\-/, ($privacy || ''));
237
238 if ( $obj && $obj->id ) {
239     # permission check
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");
243             return @results;
244         }
245     }
246
247     $obj->SetSubValues( %params );
248     $obj->SetDescription( $desc );
249
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;
254
255     if ( $new_obj_type && $new_obj_id ) {
256         my ($val, $msg);
257
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
261
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,
267                 );
268                 push @results, loc( 'Unable to set privacy object: [_1]', $msg )
269                   unless ($val);
270             }
271             if ( $new_obj_id != $obj_id ) {
272                 ( $val, $msg ) = $obj->__Set(
273                     Field => 'ObjectId',
274                     Value => $new_obj_id,
275                 );
276                 push @results, loc( 'Unable to set privacy id: [_1]', $msg )
277                   unless ($val);
278             }
279         }
280         else {
281             # two loc are just for convenience so we don't need to
282             # write an extra i18n translation item
283             push @results,
284               loc( 'Unable to set privacy object or id: [_1]',
285                 loc('Permission Denied') )
286         }
287     } else {
288         push @results, loc('Unable to determine object type or id');
289     }
290     push @results, loc('Updated saved search "[_1]"', $desc);
291 }
292 elsif ( $id eq 'new' ) {
293     my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
294     my ($status, $msg) = $saved_search->Save(
295         Privacy      => $privacy,
296         Name         => $desc,
297         Type         => $SavedSearch->{'Type'},
298         SearchParams => \%params,
299     );
300
301     if ( $status ) {
302         $SavedSearch->{'Object'} =
303             $session{'CurrentUser'}->UserObj->Attributes->WithId( $saved_search->Id );
304         # Build new SearchId
305         $SavedSearch->{'Id'} =
306                 ref( $session{'CurrentUser'}->UserObj ) . '-'
307                     . $session{'CurrentUser'}->UserObj->Id
308                     . '-SavedSearch-'
309                     . $SavedSearch->{'Object'}->Id;
310     }
311     else {
312         push @results, loc("Can't find a saved search to work with").': '.loc($msg);
313     }
314 }
315 else {
316     push @results, loc("Can't save this search");
317 }
318
319 return @results;
320
321 </%INIT>
322 </%METHOD>