rt 4.0.23
[freeside.git] / rt / share / html / Widgets / SavedSearch
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2015 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 <%method new>
49 <%init>
50 return \%ARGS;
51 </%init>
52 </%method>
53
54 <%method process>
55
56 <%init>
57 my @actions;
58 $self->{SearchId} ||= $args->{'SavedChartSearchId'} || 'new';
59
60 my $SearchParams = { map { $_ => $args->{$_} } @{$self->{SearchFields}} };
61
62 if ( my ( $container_object, $search_id ) = _parse_saved_search(
63             $args->{'SavedSearchLoad'} || $args->{'SavedChartSearchId'} ) ) {
64     my $search = $container_object->Attributes->WithId($search_id);
65     # We have a $search and now; import the others
66     $self->{SearchId} = $args->{'SavedSearchLoad'} ||
67         $args->{'SavedChartSearchId'};
68     $self->{CurrentSearch}{Object} = $search;
69     if ( $args->{'SavedSearchLoad'} ) {
70         for ( @{ $self->{SearchFields} } ) {
71             $args->{$_} = $search->SubValue($_) 
72         }
73     }
74
75     $args->{SavedChartSearchId} = $args->{'SavedSearchLoad'}
76         if $args->{'SavedSearchLoad'};
77 # saved search in /Search/Chart.html is different from /Search/Build.html
78 # the former is of type 'Chart', while the latter is of type 'Ticket'.
79 # After loading a saved search from the former after loading one from the
80 # latter, accessing /Search/Build.html will still show the old one, so we
81 # need to delete $session{CurrentSearchHash} to let it not show the old one.
82 # of course, the new one should not be shown there either because it's of
83 # different type
84     delete $session{'CurrentSearchHash'};
85 }
86
87 # look for the current one in the available saved searches
88 if ($self->{SearchId} eq 'new') {
89     my @Objects = RT::SavedSearch->new( $session{CurrentUser} )->ObjectsForLoading;
90     push @Objects, RT::System->new($session{'CurrentUser'})
91         if $session{'CurrentUser'}->HasRight( Object=> $RT::System,
92                                               Right => 'SuperUser' );
93     for my $obj (@Objects) {
94         for ( $m->comp( "/Search/Elements/SearchesForObject", Object => $obj ) ) {
95             my ( $desc, $loc_desc, $search ) = @$_;
96             use Data::Dumper;
97             # FFS
98             local $Data::Dumper::Sortkeys = 1;
99             if ( Dumper( $search->Content ) eq
100                  Dumper( { %$SearchParams, SearchType => $self->{SearchType} } ) ) {
101                 $self->{CurrentSearch}{Object}      = $search;
102                 $self->{SearchId}                   = $search->Id;
103             }
104         }
105     }
106 }
107
108 if ( $args->{SavedSearchSave} ) {
109     if ( my $search = $self->{CurrentSearch}{Object} ) {
110         # rename
111         $search->SetDescription( $args->{SavedSearchDescription} );
112         $search->SetSubValues(%$SearchParams);
113             push @actions, loc( '[_1] [_2] updated.', loc($self->{SearchType}), $args->{SavedSearchDescription} );
114     }
115     else {
116         # new saved search
117         my $saved_search = RT::SavedSearch->new( $session{'CurrentUser'} );
118         my ( $ok, $search_msg ) = $saved_search->Save(
119             Privacy      => $args->{'SavedSearchOwner'},
120             Name         => $args->{'SavedSearchDescription'},
121             Type         => $self->{'SearchType'},
122             SearchParams => $SearchParams
123         );
124         if ($ok) {
125                 $self->{CurrentSearch}{Object} = $saved_search->{Attribute};
126             $self->{SearchId} = $args->{SavedChartSearchId} = 'RT::User-' .
127                 $session{CurrentUser}->id . '-SavedSearch-' .
128                 $saved_search->Id;
129             push @actions, loc( '[_1] [_2] saved.', loc($self->{SearchType}), $args->{SavedSearchDescription} );
130         } else {
131             push @actions,
132                 [ loc("Can't save [_1]", loc($self->{SearchType})) . ': ' . loc($search_msg), 0 ];
133         }
134     }
135 }
136
137 if ( $args->{SavedSearchDelete} && $self->{CurrentSearch}{Object} ) {
138     my ($ok, $msg) = $self->{CurrentSearch}{Object}->Delete;
139     push @actions, $ok ? loc( '[_1] [_2] deleted.', loc($self->{SearchType}), $self->{CurrentSearch}{Object}->Description ) : $msg;
140     delete $self->{CurrentSearch}{Object};
141     delete $self->{SearchId};
142     delete $args->{SavedChartSearchId};
143 }
144
145 $self->{CurrentSearch}{Description} = $self->{CurrentSearch}{Object}->Description
146     if $self->{CurrentSearch}{Object};
147
148 return @actions;
149 </%init>
150 <%ARGS>
151 $self
152 $args
153 </%ARGS>
154
155 </%method>
156
157 <%method show>
158 <form method="post" action="<% $Action %>" name="SaveSearch">
159 <& /Search/Elements/EditSearches,
160     Id            => $self->{SearchId} || 'new',
161     Type          => $self->{SearchType},
162     CurrentSearch => $self->{CurrentSearch},
163     Title         => $Title,
164     AllowCopy     => 0,
165     $self->{CurrentSearch}{Object} ? 
166     ( Object        => $self->{CurrentSearch}{Object},
167     Description   => $self->{CurrentSearch}{Object}->Description, ) : (),
168 &><br />
169 <%PERL>
170 foreach my $field ( @{$self->{SearchFields}} ) {
171     if ( ref($ARGS{$field}) && ref($ARGS{$field}) ne 'ARRAY' ) {
172         $RT::Logger->error("Couldn't store '$field'. it's reference to ". ref($ARGS{$field}) );
173         next;
174     }
175     foreach my $value ( grep defined, ref($ARGS{$field})? @{ $ARGS{$field} } : $ARGS{$field} ) {
176 </%PERL>
177 <input type="hidden" class="hidden" name="<% $field %>" value="<% $value %>" />
178 %   }
179 % }
180 <input type="hidden" class="hidden" name="SavedChartSearchId" value="<% $self->{SearchId} || 'new'  %>" />
181 </form>
182 <%ARGS>
183 $self   => undef
184 $Action => ''
185 $Title  => loc('Saved searches')
186 </%ARGS>
187 <%init>
188 </%init>
189 </%method>