add RT ACL for bulk updating tickets, #72964
[freeside.git] / rt / share / html / Search / Bulk.html
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 <& /Elements/Header, Title => $title &>
49 <& /Elements/Tabs &>
50
51 <& /Elements/ListActions, actions => \@results &>
52 <form method="post" action="<% RT->Config->Get('WebPath') %>/Search/Bulk.html" enctype="multipart/form-data" name="BulkUpdate" id="BulkUpdate">
53 % foreach my $var (qw(Query Format OrderBy Order Rows Page SavedChartSearchId)) {
54 <input type="hidden" class="hidden" name="<%$var%>" value="<%$ARGS{$var} || ''%>" />
55 %}
56 <& /Elements/CollectionList, 
57     Query => $Query,
58     DisplayFormat => $Format,
59     Format => $ARGS{'Format'},
60     Verbatim => 1,
61     AllowSorting => 1,
62     OrderBy => $OrderBy,
63     Order => $Order,
64     Rows => $Rows,
65     Page => $Page,
66     BaseURL => RT->Config->Get('WebPath')."/Search/Bulk.html?",
67     Class => 'RT::Tickets'
68    &>
69
70 % $m->callback(CallbackName => 'AfterTicketList', ARGSRef => \%ARGS);
71
72 <hr />
73
74 <& /Elements/Submit, Label => loc('Update'), CheckboxNameRegex => '/^UpdateTicket\d+$/', CheckAll => 1, ClearAll => 1 &>
75 <br />
76 <&|/Widgets/TitleBox, title => $title &>
77 <table>
78 <tr>
79 <td valign="top">
80 <table>
81 <tr><td class="label"> <&|/l&>Make Owner</&>: </td>
82 <td class="value"> <& /Elements/SelectOwner, Name => "Owner", Default => $ARGS{Owner} || '' &>
83 (<input type="checkbox" class="checkbox" name="ForceOwnerChange"
84  <% $ARGS{ForceOwnerChange} ? 'checked="checked"' : '' %> /> <&|/l&>Force change</&>) </td></tr>
85 <tr><td class="label"> <&|/l&>Add Requestor</&>: </td>
86 <td class="value"> <input name="AddRequestor" size="20" value="<% $ARGS{AddRequestor} || '' %>" /> </td></tr>
87 <tr><td class="label"> <&|/l&>Remove Requestor</&>: </td>
88 <td class="value"> <input name="DeleteRequestor" size="20" value="<% $ARGS{DeleteRequestor} || '' %>"/> </td></tr>
89 <tr><td class="label"> <&|/l&>Add Cc</&>: </td>
90 <td class="value"> <input name="AddCc" size="20" value="<% $ARGS{AddCc} || '' %>" /> </td></tr>
91 <tr><td class="label"> <&|/l&>Remove Cc</&>: </td>
92 <td class="value"> <input name="DeleteCc" size="20" value="<% $ARGS{DeleteCc} || '' %>" /> </td></tr>
93 <tr><td class="label"> <&|/l&>Add AdminCc</&>: </td>
94 <td class="value"> <input name="AddAdminCc" size="20" value="<% $ARGS{AddAdminCc} || '' %>" /> </td></tr>
95 <tr><td class="label"> <&|/l&>Remove AdminCc</&>: </td>
96 <td class="value"> <input name="DeleteAdminCc" size="20" value="<% $ARGS{DeleteAdminCc} || '' %>" /> </td></tr>
97 </table>
98 </td>
99 <td valign="top">
100 <table>
101 <tr><td class="label"> <&|/l&>Make subject</&>: </td>
102 <td class="value"> <input name="Subject" size="20" value="<% $ARGS{Subject} || '' %>"/> </td></tr>
103 <tr><td class="label"> <&|/l&>Make priority</&>: </td>
104 % my $rel = ($ARGS{Priority} =~ s/^R//);
105 <td class="value"> <& /Elements/SelectPriority, Name => "Priority", Default => $ARGS{Priority} &> 
106 <select name="Priority-Mode">
107 <option value="absolute" <% !$rel && 'selected' %>>absolute</option>
108 <option value="relative" <%  $rel && 'selected' %>>relative</option>
109 </select>
110 </td></tr>
111 <tr><td class="label"> <&|/l&>Make queue</&>: </td>
112 <td class="value"> <& /Elements/SelectQueue, Name => "Queue", Default => $ARGS{Queue} &> </td></tr>
113 <tr><td class="label"> <&|/l&>Make Status</&>: </td>
114 <td class="value"> <& /Elements/SelectStatus, Name => "Status", Default => $ARGS{Status}, Queues => $seen_queues &> </td></tr>
115 <tr><td class="label"> <&|/l&>Make date Starts</&>: </td>
116 <td class="value"> <& /Elements/SelectDate, Name => "Starts_Date", Default => $ARGS{Starts_Date} || '' &> </td></tr>
117 <tr><td class="label"> <&|/l&>Make date Started</&>: </td>
118 <td class="value"> <& /Elements/SelectDate, Name => "Started_Date", Default => $ARGS{Started_Date} || '' &> </td></tr>
119 <tr><td class="label"> <&|/l&>Make date Told</&>: </td>
120 <td class="value"> <& /Elements/SelectDate, Name => "Told_Date", Default => $ARGS{Told_Date} || '' &> </td></tr>
121 <tr><td class="label"> <&|/l&>Make date Due</&>: </td>
122 <td class="value"> <& /Elements/SelectDate, Name => "Due_Date", Default => $ARGS{Due_Date} || '' &> </td></tr>
123 <tr><td class="label"> <&|/l&>Make date Resolved</&>: </td>
124 <td class="value"> <& /Elements/SelectDate, Name => "Resolved_Date", Default => $ARGS{Resolved_Date} || '' &> </td></tr>
125 </table>
126
127 </td>
128 </tr>
129 </table>
130 </&>
131 <&| /Widgets/TitleBox, title => loc('Add comments or replies to selected tickets') &>
132 <table>
133 <tr><td align="right"><&|/l&>Update Type</&>:</td>
134 <td><select name="UpdateType">
135   <option value="private" <% $ARGS{UpdateType} && $ARGS{UpdateType} eq 'private' ? 'selected="selected"' : '' %> ><&|/l&>Comments (Not sent to requestors)</&></option>
136 <option value="response" <% $ARGS{UpdateType} && $ARGS{UpdateType} eq 'response' ? 'selected="selected"' : '' %>><&|/l&>Reply to requestors</&></option>
137 </select> 
138 </td></tr>
139 <tr><td align="right"><&|/l&>Subject</&>:</td><td> <input name="UpdateSubject"
140 size="60" value="<% $ARGS{UpdateSubject} || "" %>" /></td></tr>
141 % while (my $CF = $TxnCFs->Next()) {
142 <tr>
143 <td align="right"><% $CF->Name %>:</td>
144 <td><& /Elements/EditCustomField, 
145     CustomField => $CF, 
146     NamePrefix => "Object-RT::Transaction--CustomField-",
147     Default => $ARGS{"Object-RT::Transaction--CustomField-" . $CF->id . '-Values'} || 
148             $ARGS{"Object-RT::Transaction--CustomField-" . $CF->id . '-Value'},
149     &><em><% $CF->FriendlyType %></em></td>
150 </td></tr>
151 % } # end if while
152
153 <& /Ticket/Elements/AddAttachments, %ARGS &>
154
155  <tr><td class="labeltop"><&|/l&>Message</&>:</td><td>
156 %# Currently, bulk update always starts with Comment not Reply selected, so we check this unconditionally
157 % my $IncludeSignature = RT->Config->Get('MessageBoxIncludeSignatureOnComment');
158 <& /Elements/MessageBox, Name => "UpdateContent", 
159     $ARGS{UpdateContent} ? ( Default => $ARGS{UpdateContent}, IncludeSignature => 0 ) :
160                         ( IncludeSignature => $IncludeSignature ),
161         &>
162  </td></tr>
163  </table>
164
165 </&>
166
167 <%perl>
168 my $cfs = RT::CustomFields->new($session{'CurrentUser'});
169 $cfs->LimitToGlobal();
170 $cfs->LimitToQueue($_) for keys %$seen_queues;
171 </%perl>
172
173 % if ($cfs->Count) {
174 <&|/Widgets/TitleBox, title => loc('Edit Custom Fields'), color => "#336633"&>
175 <table>
176 <tr>
177 <th><&|/l&>Name</&></th>
178 <th><&|/l&>Add values</&></th>
179 <th><&|/l&>Delete values</&></th>
180 </tr>
181 % while (my $cf = $cfs->Next()) {
182 <tr>
183 <td class="label"><% loc($cf->Name) %><br />
184 <em>(<%$cf->FriendlyType%>)</em></td>
185 % my $rows = 5;
186 % my $cf_id = $cf->id;
187 % my @add = (NamePrefix => 'Bulk-Add-CustomField-', CustomField => $cf, Rows => $rows,
188 %   Multiple => ($cf->MaxValues ==1 ? 0 : 1) , Cols => 25, 
189 %   Default => $ARGS{"Bulk-Add-CustomField-$cf_id-Values"} || $ARGS{"Bulk-Add-CustomField-$cf_id-Value"}, );
190 % my @del = (NamePrefix => 'Bulk-Delete-CustomField-', CustomField => $cf,
191 %   Rows => $rows, Multiple => 1, Cols => 25,
192 %   Default => $ARGS{"Bulk-Delete-CustomField-$cf_id-Values"} || $ARGS{"Bulk-Delete-CustomField-$cf_id-Value"}, );
193 % if ($cf->Type eq 'Select') {
194 <td><& /Elements/EditCustomFieldSelect, @add &></td>
195 <td><& /Elements/EditCustomFieldSelect, @del &></td>
196 % } elsif ($cf->Type eq 'Combobox') {
197 <td><& /Elements/EditCustomFieldCombobox, @add &></td>
198 <td><& /Elements/EditCustomFieldCombobox, @del &></td>
199 % } elsif ($cf->Type eq 'Freeform') {
200 <td><& /Elements/EditCustomFieldFreeform, @add &></td>
201 <td><& /Elements/EditCustomFieldFreeform, @del &></td>
202 % } elsif ($cf->Type eq 'Text') {
203 <td><& /Elements/EditCustomFieldText, @add &></td>
204 <td>&nbsp;</td>
205 % } elsif ($cf->Type eq 'Date') {
206 <td><& /Elements/EditCustomFieldDate, @add, Default => undef &></td>
207 <td><& /Elements/EditCustomFieldDate, @del, Default => undef &></td>
208 % } elsif ($cf->Type eq 'DateTime') {
209 % # Pass datemanip format to prevent another tz date conversion
210 <td><& /Elements/EditCustomFieldDateTime, @add, Default => undef, Format => 'datemanip' &></td>
211 <td><& /Elements/EditCustomFieldDateTime, @del, Default => undef, Format => 'datemanip' &></td>
212 % } else {
213 %   $RT::Logger->crit("Unknown CustomField type: " . $cf->Type);
214 % }
215 </tr>
216 % }
217 </table>
218 </&>
219 % }
220
221 <&|/Widgets/TitleBox, title => loc('Edit Links'), color => "#336633"&>
222 <em><&|/l&>Enter tickets or URIs to link tickets to. Separate multiple entries with spaces.</&></em><br />
223 <& /Ticket/Elements/BulkLinks, Tickets => $Tickets, $ARGS{'AddMoreAttach'} ? %ARGS : () &>
224 </&>
225
226 <& /Elements/Submit, Label => loc('Update') &>
227
228
229 </form>
230
231
232 <%INIT>
233 unless ( defined $Rows ) {
234     $Rows = $RowsPerPage;
235     $ARGS{Rows} = $RowsPerPage;
236 }
237 my $title = loc("Update multiple tickets");
238
239 #freeside
240 unless ( $session{'CurrentUser'}
241          ->HasRight( Right => 'BulkUpdateTickets', Object => RT->System) )
242 {
243     Abort('You are not allowed to bulk-update tickets.');
244 }
245
246 # Iterate through the ARGS hash and remove anything with a null value.
247 map ( $ARGS{$_} =~ /^$/ && ( delete $ARGS{$_} ), keys %ARGS );
248
249 my (@results);
250
251 ProcessAttachments(ARGSRef => \%ARGS);
252
253 $Page ||= 1;
254
255 $Format ||= RT->Config->Get('DefaultSearchResultFormat');
256
257 # inject _CHECKBOX to the first field.
258 $Format =~ s/'?([^']+)'?,/'___CHECKBOX__$1',/;      #'
259
260 my $Tickets = RT::Tickets->new( $session{'CurrentUser'} );
261 $Tickets->FromSQL($Query);
262 if ( $OrderBy =~ /\|/ ) {
263
264   # Multiple Sorts
265   my @OrderBy = split /\|/, $OrderBy;
266   my @Order   = split /\|/, $Order;
267   $Tickets->OrderByCols(
268     map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } }
269       ( 0 .. $#OrderBy ) );
270 }
271 else {
272   $Tickets->OrderBy( FIELD => $OrderBy, ORDER => $Order );
273 }
274
275 $Tickets->RowsPerPage($Rows) if ($Rows);
276 $Tickets->GotoPage( $Page - 1 );    # SB uses page 0 as the first page
277
278 Abort( loc("No search to operate on.") ) unless ($Tickets);
279
280 # build up a list of all custom fields for tickets that we're displaying, so
281 # we can display sane edit widgets.
282
283 my $fields      = {};
284 my $seen_queues = {};
285 while ( my $ticket = $Tickets->Next ) {
286     next if $seen_queues->{ $ticket->Queue }++;
287
288     my $custom_fields = $ticket->CustomFields;
289     while ( my $field = $custom_fields->Next ) {
290         $fields->{ $field->id } = $field;
291     }
292 }
293
294 #Iterate through each ticket we've been handed
295 my @linkresults;
296
297 $Tickets->RedoSearch();
298
299 # pull out the labels for any custom fields we want to update
300
301 my $cf_del_keys;
302 @$cf_del_keys = grep { /^Bulk-Delete-CustomField/ } keys %ARGS;
303 my $cf_add_keys;
304 @$cf_add_keys = grep { /^Bulk-Add-CustomField/ } keys %ARGS;
305
306 if ( defined($ARGS{'Priority'})
307      and ($ARGS{'Priority-Mode'} || '') eq 'relative' ) {
308     # magic in Ticket::SetPriority
309     $ARGS{'Priority'} = 'R'.$ARGS{'Priority'};
310 }
311 delete $ARGS{'Priority-Mode'};
312
313 unless ( $ARGS{'AddMoreAttach'} ) {
314     # Add session attachments if any to be processed by ProcessUpdateMessage
315     $ARGS{'UpdateAttachments'} = $session{'Attachments'} if ( $session{'Attachments'} );
316
317     while ( my $Ticket = $Tickets->Next ) {
318         next unless ( $ARGS{ "UpdateTicket" . $Ticket->Id } );
319
320         #Update the links
321         $ARGS{'id'} = $Ticket->id;
322
323         my @updateresults = ProcessUpdateMessage(
324                 TicketObj => $Ticket,
325                 ARGSRef   => \%ARGS,
326             );
327
328         #Update the basics.
329         my @basicresults =
330           ProcessTicketBasics( TicketObj => $Ticket, ARGSRef => \%ARGS );
331         my @dateresults =
332           ProcessTicketDates( TicketObj => $Ticket, ARGSRef => \%ARGS );
333
334         #Update the watchers
335         my @watchresults =
336           ProcessTicketWatchers( TicketObj => $Ticket, ARGSRef => \%ARGS );
337
338         foreach my $type (qw(MergeInto DependsOn MemberOf RefersTo)) {
339             $ARGS{ $Ticket->id . "-" . $type } = $ARGS{"Ticket-$type"};
340             $ARGS{ $type . "-" . $Ticket->id } = $ARGS{"$type-Ticket"};
341         }
342         @linkresults =
343           ProcessTicketLinks( TicketObj => $Ticket, ARGSRef => \%ARGS );
344         foreach my $type (qw(MergeInto DependsOn MemberOf RefersTo)) {
345             delete $ARGS{ $type . "-" . $Ticket->id };
346             delete $ARGS{ $Ticket->id . "-" . $type };
347         }
348
349         my @cfresults;
350
351         foreach my $list ( $cf_add_keys, $cf_del_keys ) {
352             next unless $list->[0];
353
354
355             my $op;
356             if ( $list->[0] =~ /Add/ ) {
357                 $op = 'add';
358
359             }
360             elsif ( $list->[0] =~ /Del/ ) {
361                 $op = 'del';
362             }
363             else {
364                 $RT::Logger->crit(
365                     "Got an op that was neither add nor delete. can never happen"
366                       . $list->[0] );
367                 last;
368             }
369
370             foreach my $key (@$list) {
371                 my ( $cfid, $cf );
372                 next if $key =~ /CustomField-(\d+)-Category$/;
373                 if ( $key =~ /CustomField-(\d+)-/ ) {
374                     $cfid = $1;
375                     $cf   = RT::CustomField->new( $session{'CurrentUser'} );
376                     $cf->Load($cfid);
377                 }
378                 else {next}
379                 my @values =
380                   ref( $ARGS{$key} ) eq 'ARRAY'
381                   ? @{ $ARGS{$key} }
382                   : ( $ARGS{$key} );
383                 map { s/(\r\n|\r)/\n/g; } @values;    # fix the newlines
384                      # now break the multiline values into multivalues
385                 @values = map { split( /\n/, $_ ) } @values
386                   unless ( $cf->SingleValue );
387
388                 my $current_values = $Ticket->CustomFieldValues($cfid);
389
390                 if ( $cf->Type eq 'DateTime' || $cf->Type eq 'Date' ){
391                     # Clear out empty string submissions to avoid
392                     # Not set changed to Not set
393                     @values = grep length, @values;
394                 }
395
396                 foreach my $value (@values) {
397
398                     if ( $op eq 'del' ) {
399                         if ( my $entry = $current_values->HasEntry($value) ) {
400                             my ( $id, $msg ) = $Ticket->DeleteCustomFieldValue(
401                                 Field => $cfid,
402                                 ValueId => $entry->id,
403                             );
404                             push @cfresults, $msg;
405                         }
406                     }
407
408                     elsif ( $op eq 'add' && !$current_values->HasEntry($value) ) {
409                         my ( $id, $msg ) = $Ticket->AddCustomFieldValue(
410                             Field => $cfid,
411                             Value => $value
412                         );
413                         push @cfresults, $msg;
414                     }
415                 }
416             }
417         }
418         my @statusresults =
419           ProcessTicketStatus( TicketObj => $Ticket, ARGSRef => \%ARGS );
420
421           my @tempresults = (
422             @watchresults,  @basicresults, @dateresults,
423             @updateresults, @linkresults,  @cfresults,
424             @statusresults
425         );
426
427         @tempresults =
428           map { 
429               $_ =~ /^Ticket \d+:/ ?  $_ : 
430               loc( "Ticket [_1]: [_2]", $Ticket->Id, $_ ) 
431             } @tempresults;
432
433         @results = ( @results, @tempresults );
434     }
435
436     # Cleanup WebUI
437     delete $session{'Attachments'};
438
439     $Tickets->RedoSearch();
440 }
441
442 my $TxnCFs = RT::CustomFields->new( $session{CurrentUser} );
443 $TxnCFs->LimitToLookupType( RT::Transaction->CustomFieldLookupType );
444 $TxnCFs->LimitToGlobalOrObjectId( keys %$seen_queues );
445
446 </%INIT>
447 <%args>
448 $Format => undef
449 $Page => 1
450 $Rows => undef
451 $RowsPerPage => undef
452 $Order => 'ASC'
453 $OrderBy => 'id'
454 $Query => undef
455 $SavedSearchId => undef
456 $SavedChartSearchId => undef
457 </%args>