<%init>

my $time_worked;
if ( $TicketObj->TimeWorked ) {
    $time_worked = {};
    my $transactions = $TicketObj->Transactions;
    $transactions->Limit(
        FIELD           => 'TimeTaken',
        VALUE           => 0,
        OPERATOR        => '!=',
    );

    while ( my $txn = $transactions->Next ) {
        $time_worked->{ $txn->CreatorObj->Name } += $txn->TimeTaken;
    }
}

# The self service interface doesn't support updating time fields.
return unless $session{CurrentUser}->Privileged
          and $session{CurrentUser}->HasRight( Object => $TicketObj, Right => "ModifyTicket" );

my $date_cf = RT::CustomField->new($session{CurrentUser});
$date_cf->LoadByName( Name => 'Worked Date', LookupType => 'RT::Queue-RT::Ticket-RT::Transaction');

# The Mason code below calls directly to SelectDate to avoid printing the
# parenthesized date and save some space. Build the CF name normally
# built in EditCustomField and EditCustomFieldDate.
my $cf_name = 'Object-RT::Transaction--CustomField-'
    . $date_cf->Id . '-Values';

# Default to today and also signal that the field is a date field.
my $today = RT::Date->new($session{CurrentUser});
$today->SetToNow();

# Escape : so id declaration is parsed properly
my $cf_style = $cf_name;
$cf_style =~ s/:/\\:/g;

</%init>
<style type="text/css">
input#<% $cf_style %> {
    width: 85px;
}
</style>
<tr>
  <td class="label"><&|/l&>Add to time</&><br /><&|/l&>worked:</&></td>
  <td class="value">
    <form action="<% RT->Config->Get("WebPath") %>/Ticket/Display.html" method="POST">
      <input type="hidden" name="id" value="<% $TicketObj->id %>">
      <& /Elements/SelectDate, Name => "$cf_name", current => 0, ShowTime => 0, Default => $today->Date( Timezone => 'user') &>
      <& /Elements/EditTimeValue,
          Name    => "UpdateTimeWorked",
          Default => $ARGS{UpdateTimeWorked} || '',
          InUnits => $ARGS{'UpdateTimeWorked-TimeUnits'} || 'minutes',
      &>
      <input type="submit" value="<% loc('Add') %>">
    </form>
  </td>
</tr>

% # Render if less than RT 4.4.0, (which includes per user time)
% if ( RT::Handle::cmp_version($RT::VERSION,'4.4.0') < 0 && $TicketObj->TimeWorked ) {
<tr class="time worked">
    <td class="label"><&|/l&>Worked</&>:</td>
    <td>
    <table>
% for my $user ( keys %$time_worked ) {
    <tr>
% # add "value" class for $user label is to make the font the same size as value
    <td class="value"><% $user %>:</td>
    <td class="value"><& /Ticket/Elements/ShowTime, minutes => $time_worked->{$user} &></td>
    </tr>
% }
    </table>
    </td>
</tr>
% }

<%args>
$TicketObj
</%args>
