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