This commit was generated by cvs2svn to compensate for changes in r8690,
[freeside.git] / rt / share / html / Search / Elements / EditSearches
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %# 
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 %#                                          <jesse@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         $container->Attributes->DeleteEntry( Name => 'SavedSearch', id => $id );
184     }
185     $SavedSearch->{'Id'}          = 'new';
186     $SavedSearch->{'Object'}      = undef;
187     $SavedSearch->{'Description'} = undef;
188     push @results, loc("Deleted saved search");
189 }
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'};
195     } else {
196         $SavedSearch->{'Description'} = loc( "[_1] copy", $SavedSearch->{'Object'}->Description );
197     }
198     $SavedSearch->{'Id'}          = 'new';
199     $SavedSearch->{'Object'}      = undef;
200 }
201
202 if ( $SavedSearch->{'Id'} && $SavedSearch->{'Id'} ne 'new'
203      && !$SavedSearch->{'Object'} )
204 {
205     my ($container, $id ) = _parse_saved_search( $ARGS{'SavedSearchId'} );
206     $SavedSearch->{'Object'} = $container->Attributes->WithId( $id );
207     $SavedSearch->{'Description'} ||= $SavedSearch->{'Object'}->Description;
208 }
209
210 return @results;
211
212 </%INIT>
213 </%METHOD>
214
215 <%METHOD Save>
216 <%ARGS>
217 $Query        => {}
218 $SavedSearch  => {}
219 @SearchFields => qw(Query Format OrderBy Order RowsPerPage)
220 </%ARGS>
221 <%INIT>
222
223 return unless $ARGS{'SavedSearchSave'} || $ARGS{'SavedSearchCopy'};
224
225 my @results;
226 my $obj  = $SavedSearch->{'Object'};
227 my $id   = $SavedSearch->{'Id'};
228 my $desc = $SavedSearch->{'Description'};
229 my $privacy = $SavedSearch->{'Privacy'};
230
231 my %params = map { $_ => $Query->{$_} } @SearchFields;
232 my ($new_obj_type, $new_obj_id) = split(/\-/, ($privacy || ''));
233
234 if ( $obj && $obj->id ) {
235     # permission check
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");
239             return @results;
240         }
241     }
242
243     $obj->SetSubValues( %params );
244     $obj->SetDescription( $desc );
245
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;
250
251     if ( $new_obj_type && $new_obj_id ) {
252         my ($val, $msg);
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 );
256         }
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 );
260         }
261     } else {
262         push @results, loc('Unable to determine object type or id');
263     }
264     push @results, loc('Updated saved search "[_1]"', $desc);
265 }
266 elsif ( $id eq 'new' ) {
267     my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
268     my ($status, $msg) = $saved_search->Save(
269         Privacy      => $privacy,
270         Name         => $desc,
271         Type         => $SavedSearch->{'Type'},
272         SearchParams => \%params,
273     );
274
275     if ( $status ) {
276         $SavedSearch->{'Object'} =
277             $session{'CurrentUser'}->UserObj->Attributes->WithId( $saved_search->Id );
278         # Build new SearchId
279         $SavedSearch->{'Id'} =
280                 ref( $session{'CurrentUser'}->UserObj ) . '-'
281                     . $session{'CurrentUser'}->UserObj->Id
282                     . '-SavedSearch-'
283                     . $SavedSearch->{'Object'}->Id;
284     }
285     else {
286         push @results, loc("Can't find a saved search to work with").': '.loc($msg);
287     }
288 }
289 else {
290     push @results, loc("Can't save this search");
291 }
292
293 return @results;
294
295 </%INIT>
296 </%METHOD>