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