import rt 3.8.11
[freeside.git] / rt / html / Widgets / SavedSearch
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 <%method new>
49 <%init>
50 return \%ARGS;
51 </%init>
52 </%method>
53
54 <%method process>
55
56 <%init>
57 my @actions;
58 my @Objects = RT::SavedSearches->new( $session{CurrentUser} )->_PrivacyObjects;
59 push @Objects, RT::System->new($session{'CurrentUser'})
60     if $session{'CurrentUser'}->HasRight( Object=> $RT::System,
61                                           Right => 'SuperUser');
62 $self->{SearchId} ||= 'new';
63 my $SearchParams = { map { $_ => $args->{$_} } @{$self->{SearchFields}} };
64
65 if ( my ( $container_object, $search_id ) = _parse_saved_search( $args->{'LoadSavedSearch'} ) ) {
66     my $search = $container_object->Attributes->WithId($search_id);
67     # We have a $search and now; import the others
68     $self->{SearchId} = $args->{'LoadSavedSearch'};
69     $self->{CurrentSearch}{Object} = $search;
70     $args->{$_} = $search->SubValue($_) for @{ $self->{SearchFields} };
71 }
72
73 # look for the current one in the available saved searches
74 if ($self->{SearchId} eq 'new') {
75     for my $obj (@Objects) {
76         for ( $m->comp( "/Search/Elements/SearchesForObject", Object => $obj ) ) {
77             my ( $desc, $search ) = @$_;
78             use Data::Dumper;
79             # FFS
80             local $Data::Dumper::Sortkeys = 1;
81             if ( Dumper( $search->Content ) eq
82                  Dumper( { %$SearchParams, SearchType => $self->{SearchType} } ) ) {
83                 $self->{CurrentSearch}{Object}      = $search;
84                 $self->{SearchId}                   = $search->Id;
85             }
86         }
87     }
88 }
89
90 if ( $args->{Save} ) {
91     if ( my $search = $self->{CurrentSearch}{Object} ) {
92         # rename
93         $search->SetDescription( $args->{Description} );
94         push @actions, loc($self->{SearchType}).loc( ' [_1] renamed to [_2].', $self->{CurrentSearch}{Description}, $args->{Description} );
95     }
96     else {
97         # new saved search
98         my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
99         my ( $ok, $search_msg ) = $saved_search->Save(
100             Privacy      => $args->{'Owner'},
101             Name         => $args->{'Description'},
102             Type         => $self->{'SearchType'},
103             SearchParams => $SearchParams
104         );
105         if ($ok) {
106             $self->{CurrentSearch}{Object} = $saved_search->{Attribute};
107             push @actions, loc($self->{SearchType}).loc( ' [_1] saved.', $args->{Description} );
108         } else {
109             push @actions,
110                 [ loc("Can't save [_1]", loc($self->{SearchType})) . ': ' . loc($search_msg), 0 ];
111         }
112     }
113 }
114
115 if ( $args->{Delete} && $self->{CurrentSearch}{Object} ) {
116     my ($ok, $msg) = $self->{CurrentSearch}{Object}->Delete;
117     push @actions, $ok ? loc($self->{SearchType}).loc( ' [_1] deleted.', $self->{CurrentSearch}{Object}->Description ) : $msg;
118     delete $self->{CurrentSearch}{Object};
119     delete $self->{SearchId};
120
121 }
122
123 $self->{CurrentSearch}{Description} = $self->{CurrentSearch}{Object}->Description
124     if $self->{CurrentSearch}{Object};
125
126 return @actions;
127 </%init>
128 <%ARGS>
129 $self
130 $args
131 </%ARGS>
132
133 </%method>
134
135 <%method show>
136 <form method="post" action="<% $Action %>" name="SaveSearch">
137 <& /Search/Elements/EditSearches, Name => 'Owner', SearchType => $self->{SearchType}, AllowCopy => 0,
138    CurrentSearch => $self->{CurrentSearch}, SearchId => $self->{SearchId}, Title => $Title  &><br />
139 <%PERL>
140 foreach my $field ( @{$self->{SearchFields}} ) {
141     if ( ref($ARGS{$field}) && ref($ARGS{$field}) ne 'ARRAY' ) {
142         $RT::Logger->error("Couldn't store '$field'. it's reference to ". ref($ARGS{$field}) );
143         next;
144     }
145     foreach my $value ( grep defined, ref($ARGS{$field})? @{ $ARGS{$field} } : $ARGS{$field} ) {
146 </%PERL>
147 <input type="hidden" class="hidden" name="<% $field %>" value="<% $value %>" />
148 %   }
149 % }
150 </form>
151 <%ARGS>
152 $self => undef
153 $Action => ''
154 $Title => loc('Saved searches')
155 </%ARGS>
156 <%init>
157 </%init>
158 </%method>