Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / rt / share / html / Search / Bulk.html
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2014 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 % } else {
206 %   $RT::Logger->crit("Unknown CustomField type: " . $cf->Type);
207 % }
208 </tr>
209 % }
210 </table>
211 </&>
212 % }
213
214 <&|/Widgets/TitleBox, title => loc('Edit Links'), color => "#336633"&>
215 <em><&|/l&>Enter tickets or URIs to link tickets to. Separate multiple entries with spaces.</&></em><br />
216 <& /Ticket/Elements/BulkLinks, Tickets => $Tickets, $ARGS{'AddMoreAttach'} ? %ARGS : () &>
217 </&>
218
219 <& /Elements/Submit, Label => loc('Update') &>
220
221
222 </form>
223
224
225 <%INIT>
226 unless ( defined $Rows ) {
227     $Rows = $RowsPerPage;
228     $ARGS{Rows} = $RowsPerPage;
229 }
230 my $title = loc("Update multiple tickets");
231
232 # Iterate through the ARGS hash and remove anything with a null value.
233 map ( $ARGS{$_} =~ /^$/ && ( delete $ARGS{$_} ), keys %ARGS );
234
235 my (@results);
236
237 ProcessAttachments(ARGSRef => \%ARGS);
238
239 $Page ||= 1;
240
241 $Format ||= RT->Config->Get('DefaultSearchResultFormat');
242
243 # inject _CHECKBOX to the first field.
244 $Format =~ s/'?([^']+)'?,/'___CHECKBOX__$1',/;      #'
245
246 my $Tickets = RT::Tickets->new( $session{'CurrentUser'} );
247 $Tickets->FromSQL($Query);
248 if ( $OrderBy =~ /\|/ ) {
249
250   # Multiple Sorts
251   my @OrderBy = split /\|/, $OrderBy;
252   my @Order   = split /\|/, $Order;
253   $Tickets->OrderByCols(
254     map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } }
255       ( 0 .. $#OrderBy ) );
256 }
257 else {
258   $Tickets->OrderBy( FIELD => $OrderBy, ORDER => $Order );
259 }
260
261 $Tickets->RowsPerPage($Rows) if ($Rows);
262 $Tickets->GotoPage( $Page - 1 );    # SB uses page 0 as the first page
263
264 Abort( loc("No search to operate on.") ) unless ($Tickets);
265
266 # build up a list of all custom fields for tickets that we're displaying, so
267 # we can display sane edit widgets.
268
269 my $fields      = {};
270 my $seen_queues = {};
271 while ( my $ticket = $Tickets->Next ) {
272     next if $seen_queues->{ $ticket->Queue }++;
273
274     my $custom_fields = $ticket->CustomFields;
275     while ( my $field = $custom_fields->Next ) {
276         $fields->{ $field->id } = $field;
277     }
278 }
279
280 #Iterate through each ticket we've been handed
281 my @linkresults;
282
283 $Tickets->RedoSearch();
284
285 # pull out the labels for any custom fields we want to update
286
287 my $cf_del_keys;
288 @$cf_del_keys = grep { /^Bulk-Delete-CustomField/ } keys %ARGS;
289 my $cf_add_keys;
290 @$cf_add_keys = grep { /^Bulk-Add-CustomField/ } keys %ARGS;
291
292 if ( defined($ARGS{'Priority'})
293      and ($ARGS{'Priority-Mode'} || '') eq 'relative' ) {
294     # magic in Ticket::SetPriority
295     $ARGS{'Priority'} = 'R'.$ARGS{'Priority'};
296 }
297 delete $ARGS{'Priority-Mode'};
298
299 unless ( $ARGS{'AddMoreAttach'} ) {
300     # Add session attachments if any to be processed by ProcessUpdateMessage
301     $ARGS{'UpdateAttachments'} = $session{'Attachments'} if ( $session{'Attachments'} );
302
303     while ( my $Ticket = $Tickets->Next ) {
304         next unless ( $ARGS{ "UpdateTicket" . $Ticket->Id } );
305
306         #Update the links
307         $ARGS{'id'} = $Ticket->id;
308
309         my @updateresults = ProcessUpdateMessage(
310                 TicketObj => $Ticket,
311                 ARGSRef   => \%ARGS,
312             );
313
314         #Update the basics.
315         my @basicresults =
316           ProcessTicketBasics( TicketObj => $Ticket, ARGSRef => \%ARGS );
317         my @dateresults =
318           ProcessTicketDates( TicketObj => $Ticket, ARGSRef => \%ARGS );
319
320         #Update the watchers
321         my @watchresults =
322           ProcessTicketWatchers( TicketObj => $Ticket, ARGSRef => \%ARGS );
323
324         foreach my $type (qw(MergeInto DependsOn MemberOf RefersTo)) {
325             $ARGS{ $Ticket->id . "-" . $type } = $ARGS{"Ticket-$type"};
326             $ARGS{ $type . "-" . $Ticket->id } = $ARGS{"$type-Ticket"};
327         }
328         @linkresults =
329           ProcessTicketLinks( TicketObj => $Ticket, ARGSRef => \%ARGS );
330         foreach my $type (qw(MergeInto DependsOn MemberOf RefersTo)) {
331             delete $ARGS{ $type . "-" . $Ticket->id };
332             delete $ARGS{ $Ticket->id . "-" . $type };
333         }
334
335         my @cfresults;
336
337         foreach my $list ( $cf_add_keys, $cf_del_keys ) {
338             next unless $list->[0];
339
340
341             my $op;
342             if ( $list->[0] =~ /Add/ ) {
343                 $op = 'add';
344
345             }
346             elsif ( $list->[0] =~ /Del/ ) {
347                 $op = 'del';
348             }
349             else {
350                 $RT::Logger->crit(
351                     "Got an op that was neither add nor delete. can never happen"
352                       . $list->[0] );
353                 last;
354             }
355
356             foreach my $key (@$list) {
357                 my ( $cfid, $cf );
358                 next if $key =~ /CustomField-(\d+)-Category$/;
359                 if ( $key =~ /CustomField-(\d+)-/ ) {
360                     $cfid = $1;
361                     $cf   = RT::CustomField->new( $session{'CurrentUser'} );
362                     $cf->Load($cfid);
363                 }
364                 else {next}
365                 my @values =
366                   ref( $ARGS{$key} ) eq 'ARRAY'
367                   ? @{ $ARGS{$key} }
368                   : ( $ARGS{$key} );
369                 map { s/(\r\n|\r)/\n/g; } @values;    # fix the newlines
370                      # now break the multiline values into multivalues
371                 @values = map { split( /\n/, $_ ) } @values
372                   unless ( $cf->SingleValue );
373
374                 my $current_values = $Ticket->CustomFieldValues($cfid);
375                 foreach my $value (@values) {
376                     if ( $op eq 'del' && $current_values->HasEntry($value) ) {
377                         my ( $id, $msg ) = $Ticket->DeleteCustomFieldValue(
378                             Field => $cfid,
379                             Value => $value
380                         );
381                         push @cfresults, $msg;
382                     }
383
384                     elsif ( $op eq 'add' && !$current_values->HasEntry($value) ) {
385                         my ( $id, $msg ) = $Ticket->AddCustomFieldValue(
386                             Field => $cfid,
387                             Value => $value
388                         );
389                         push @cfresults, $msg;
390                     }
391                 }
392             }
393         }
394         my @statusresults =
395           ProcessTicketStatus( TicketObj => $Ticket, ARGSRef => \%ARGS );
396
397           my @tempresults = (
398             @watchresults,  @basicresults, @dateresults,
399             @updateresults, @linkresults,  @cfresults,
400             @statusresults
401         );
402
403         @tempresults =
404           map { 
405               $_ =~ /^Ticket \d+:/ ?  $_ : 
406               loc( "Ticket [_1]: [_2]", $Ticket->Id, $_ ) 
407             } @tempresults;
408
409         @results = ( @results, @tempresults );
410     }
411
412     # Cleanup WebUI
413     delete $session{'Attachments'};
414 }
415
416 my $TxnCFs = RT::CustomFields->new( $session{CurrentUser} );
417 $TxnCFs->LimitToLookupType( RT::Transaction->CustomFieldLookupType );
418 $TxnCFs->LimitToGlobalOrObjectId( keys %$seen_queues );
419
420 </%INIT>
421 <%args>
422 $Format => undef
423 $Page => 1
424 $Rows => undef
425 $RowsPerPage => undef
426 $Order => 'ASC'
427 $OrderBy => 'id'
428 $Query => undef
429 $SavedSearchId => undef
430 $SavedChartSearchId => undef
431 </%args>