%# Silverman's Law:
%# 	If Murphy's Law can go wrong, it will.

<& /Work/Elements/104Header, Title => 'Search', Refresh => $session{'tickets_refresh_interval'}, Method => 'Post' &>

<& /Elements/ListActions, actions => \@results &>

<TABLE WIDTH=100% border=0 cellpadding=2 CELLSPACING=1 class="tableEmboss">
<TR>
<TD class="tableTitle"><&|/l&>Update</&></TD>
%foreach my $col (@cols) {
% my $colalias = $col;
% $colalias =~ s/(Obj\-\>|)(Name|AsString)//;

<TD class="tableTitle"><% loc($colalias) %>&nbsp;</TD>
%}
</TR>

<%PERL>

my $i;


      
$session{'tickets'}->RedoSearch();
while (my $Ticket = $session{'tickets'}->Next) {
 $i++;
$bgcolor = "#ffffff";
</%PERL>
<TR bgcolor="<%$bgcolor%>">
<TD><input type=checkbox name="UpdateTicket<%$Ticket->Id%>" CHECKED></TD>
%# The ticket view is controlled by config.pm, WebOptions
%foreach my $col (@cols) {
<TD>
% if ($col eq 'id') {
<A HREF="<% $RT::WebPath%>/Ticket/Display.html?id=<%$Ticket->Id%>"><%$Ticket->Id()%></A>
% }
%else {
<% eval "\$Ticket->$col()" %>&nbsp;
%}
</TD>
%}
</TR>
%}



</TABLE>
<br>
<TABLE width="100%" class="poptable" cellpadding=2 cellspacing=1>
<COL width="20%"><COL width="30%"><COL width="20%"><COL width="30%">
<tr><td colspan="4" class="tableTitle"><&|/l&>Update selected tickets</&></td></tr>
<tr>
<td align="right"><&|/l&>Make Owner</&></td><td><& /Elements/SelectOwner, Name => "Owner" &>
(<input type=checkbox name="ForceOwnerChange"> <&|/l&>Force change</&>)
</td>
<td align="right"> <&|/l&>Make priority</&></td><td><INPUT Name="Priority" SIZE=4></td>
</tr><tr>
<td align="right"> <&|/l&>Make subject</&></td><td><INPUT Name="Subject" SIZE=20></td>
<td align="right"> <&|/l&>Make queue</&></td><td><& /Elements/SelectQueue, Name => "Queue" &></td>
</tr><tr>
<td align="right"> <&|/l&>Add Requestor</&></td><td><INPUT Name="AddRequestor" SIZE=20></td>
<td align="right"><&|/l&>Make Status</&></td><td><& /Elements/SelectStatus, Name => "Status" &></td>
</tr><tr>
<td align="right"> <&|/l&>Remove Requestor</&></td><td><INPUT Name="DeleteRequestor" SIZE=20></td>
<td align="right"> <&|/l&>Make date Starts</&></td><td><& /Elements/SelectDate, Name => "Starts_Date", ShowTime => 0, Default => '' &></td>
</tr><tr>
<td align="right"> <&|/l&>Add Cc</&></td><td><INPUT Name="AddCc" SIZE=20></td>
<td align="right"> <&|/l&>Make date Started</&></td><td><& /Elements/SelectDate, Name => "Started_Date", ShowTime => 0, Default => '' &></td>
</tr><tr>
<td align="right"> <&|/l&>Remove Cc</&></td><td><INPUT Name="DeleteCc" SIZE=20></td>
<td align="right"> <&|/l&>Make date Told</&></td><td><& /Elements/SelectDate, Name => "Told_Date", ShowTime => 0, Default => '' &></td>
</tr><tr>
<td align="right"> <&|/l&>Add AdminCc</&></td><td><INPUT Name="AddAdminCc" SIZE=20></td>
<td align="right"> <&|/l&>Make date Due</&></td><td><& /Elements/SelectDate, Name => "Due_Date", ShowTime => 0, Default => '' &></td>
</tr><tr>
<td align="right"> <&|/l&>Remove AdminCc</&></td><td><INPUT Name="DeleteAdminCc" SIZE=20></td>
<td align="right"> <&|/l&>Make date Resolved</&></td><td><& /Elements/SelectDate, Name => "Resolved_Date", ShowTime => 0, Default => '' &></td>
</TR>
<tr><td colspan="4" class="tableTitle"><&|/l&>Add comments or replies to selected tickets</&></td></tr>
<tr><td align=right><&|/l&>Update Type</&>:</td>
<td colspan="3"><select name="UpdateType">
  <option value="private" ><&|/l&>Comments (not sent to requestors)</&></option>
<option value="response" ><&|/l&>Response to requestors</&></option>
</select> 
</td></tr>
<tr><td align=right><&|/l&>Subject</&>:</td><td colspan="3"> <input name="UpdateSubject" size=60 value=""></td></tr>
 <tr><td align=right><&|/l&>Attach</&>:</td><td colspan="3"><input name="UpdateAttachment" type="file"></td></tr>
 <tr valign="top"><td align="right"><&|/l&>Comment</&>:</td><td colspan="3">
 <& /Elements/MessageBox, Name=>"UpdateContent"&>
 </td></tr>

<tr><td colspan="4" class="tableTitle"><&|/l&>Edit Relationships</&></td></tr>
<tr><td>&nbsp;</td><td colspan="3">
<i><&|/l&>Enter tickets or URIs to link tickets to. Seperate multiple entries with spaces.</&></i><br>
<& BulkLinks &>
</td></tr></table>

<table border=0 cellpadding=0 cellspacing=0 width="100%">
<tr><td>
<input type="button" value="&lt;&lt; <&|/l&>Back to Previous</&>" onClick="javascript:location.href='/Work/Search'">
</td><td align="right">
<input TYPE=SUBMIT NAME="Action" VALUE='<&|/l&>Submit</&>'>
</td></tr>
</table>

<& /Work/Elements/104Footer &>

<%INIT>

# Iterate through the ARGS hash and remove anything with a null value.
map ($ARGS{$_} =~ /^$/ && (delete $ARGS{$_}), keys %ARGS);

my ($bgcolor, @results);
my @cols = qw(id Status Priority Subject QueueObj->Name OwnerObj->Name RequestorAddresses DueAsString );

Abort(loc("No search to operate on.")) unless ($session{'tickets'});


my $do_comment_reply=0;
# Prepare for ticket updates
$ARGS{'UpdateContent'} =~ s/\r\n/\n/g;
chomp ($ARGS{'UpdateContent'}) ;

if ($ARGS{'UpdateContent'} &&
    $ARGS{'UpdateContent'} ne '' &&
    $ARGS{'UpdateContent'} ne  "-- \n" .
    $session{'CurrentUser'}->UserObj->Signature) {
            $do_comment_reply=1;
}

#Iterate through each ticket we've been handed
my @linkresults;

while (my $Ticket = $session{'tickets'}->Next) {
    $RT::Logger->debug( "Checking Ticket ".$Ticket->Id ."\n");
    next unless ($ARGS{"UpdateTicket".$Ticket->Id});
    $RT::Logger->debug ("Matched\n");
    #Update the basics.
    my @basicresults = ProcessTicketBasics(TicketObj => $Ticket, ARGSRef => \%ARGS);
    my @dateresults = ProcessTicketDates(TicketObj => $Ticket, ARGSRef => \%ARGS);
    #Update the watchers
    my @watchresults = ProcessTicketWatchers(TicketObj => $Ticket, ARGSRef => \%ARGS);    

    #Update the links
    $ARGS{'id'} = $Ticket;
    $ARGS{$Ticket->Id.'-MergeInto'} = $ARGS{'Ticket-MergeInto'};
    $ARGS{$Ticket->Id.'-DependsOn'} = $ARGS{'Ticket-DependsOn'};
    $ARGS{'DependsOn-'.$Ticket->Id} = $ARGS{'DependsOn-Ticket'};
    $ARGS{$Ticket->Id.'-MemberOf'} = $ARGS{'Ticket-MemberOf'};
    $ARGS{'MemberOf-'.$Ticket->Id} = $ARGS{'MemberOf-Ticket'};
    $ARGS{$Ticket->Id.'-RefersTo'} = $ARGS{'Ticket-RefersTo'};
    $ARGS{'RefersTo-'.$Ticket->Id} = $ARGS{'RefersTo-Ticket'};
    @linkresults = ProcessTicketLinks( TicketObj => $Ticket, ARGSRef => \%ARGS);
    delete $ARGS{'id'};
    delete $ARGS{$Ticket->Id.'-MergeInto'};
    delete $ARGS{$Ticket->Id.'-DependsOn'};
    delete $ARGS{'DependsOn-'.$Ticket->Id};
    delete $ARGS{$Ticket->Id.'-MemberOf'};
    delete $ARGS{'MemberOf-'.$Ticket->Id};
    delete $ARGS{$Ticket->Id.'-RefersTo'};
    delete $ARGS{'RefersTo-'.$Ticket->Id};
    
    my @updateresults; 
    if ($do_comment_reply) {
    ProcessUpdateMessage(TicketObj => $Ticket, ARGSRef => \%ARGS, Actions => \@updateresults); 
   } 
   my @tempresults = (@watchresults, @basicresults, @dateresults, @updateresults, @linkresults);
    @tempresults = map { loc("Ticket [_1]: [_2]",$Ticket->Id,$_) } @tempresults;

    @results = (@results, @tempresults);
}

</%INIT>
