%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2021 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
<div class="<% join ' ', @classes %>" data-transaction-id="<% $Transaction->id %>">
  <div class="metadata">
% if ( $actions ) {
    <span class="actions"><% $actions |n %></span>
% }
    <span class="type">
      <a name="txn-<% $Transaction->id %>" \
% if ( $DisplayPath ) {
      href="<% $DisplayPath %>?id=<% $Object->id %>#txn-<% $Transaction->id %>" \
% } elsif ( $HTML::Mason::Commands::r->path_info =~ m{^/SelfService/} ) {
      href="<% RT->Config->Get('WebPath') %>/SelfService/Transaction/Display.html?id=<% $Transaction->id %>" \
% } else {
      href="<% RT->Config->Get('WebPath') %>/Transaction/Display.html?id=<% $Transaction->id %>" \
% }
% my $alt = loc('Show transaction link');
      ><span class="fas fa-link" alt="<% $alt %>" data-toggle="tooltip" data-placement="top" data-original-title="<% $alt %>"></span></a>
    </span>
% $m->callback( %ARGS, Transaction => $Transaction, CallbackName => 'AfterAnchor' );
    <span class="date"><% $date |n %></span>
    <span class="description">
      <& /Elements/ShowUser, User => $CreatorObj &> - <% $desc |n %>
% $m->callback( %ARGS, Transaction => $Transaction, CallbackName => 'AfterDescription' );
    </span>
% if ( $Object->isa("RT::Ticket") and $Object->CurrentUserCanSeeTime ) {
    <span class="time-taken"><% $time %></span>
% }
  </div>

  <div class="content">
<%PERL>
$m->comp('/Elements/ShowCustomFields', Object => $Transaction, HideEmpty => 1, LabelCols => 2, ValueCols => 10 ) if $HasTxnCFs;
$m->comp(
    'ShowTransactionAttachments',
    %ARGS,
    Parent => 0
) if $ShowBody;
</%PERL>
  </div>
% if ($Transaction->Type eq 'CustomField' && $Transaction->Field ) {
%     my ($old, $new);
%     my $cf = RT::CustomField->new( $session{CurrentUser} );
%     $cf->SetContextObject( $Transaction->Object );
%     $cf->Load( $Transaction->Field );
%     if ($cf->Id && $cf->Type =~ /text/i) {
%        $old = $Transaction->OldValue // loc('(no value)');
%        $old = $m->comp('/Elements/ScrubHTML', Content => $old);
%        $old =~ s|\n|<br />|g;
%        $new = $Transaction->NewValue // loc('(no value)');
%        $new = $m->comp('/Elements/ScrubHTML', Content => $new);
%        $new =~ s|\n|<br />|g;
<div class="details hidden" id="txn-<% $Transaction->Id %>-details">
% if ( $old eq loc('(no value)')  ) {
  <div class="form-row">
    <div class="label col-2"><% loc('Added') %>:</div>
    <div class="value col-10"><% $new |n %></div>
  </div>
% }
% elsif ( $new eq loc('(no value)')  ) {
  <div class="form-row">
    <div class="label col-2"><% loc('Deleted') %>:</div>
    <div class="value col-10"><% $old |n %></div>
  </div>
% }
% else {
  <div class="form-row">
    <div class="label col-2"><% loc('From') %>:</div>
    <div class="value col-10"><% $old |n %></div>
  </div>
  <div class="form-row">
    <div class="label col-2"><% loc('To') %>:</div>
    <div class="value col-10"><% $new |n %></div>
  </div>
  <div class="form-row diff">
    <div class="label col-2"><% loc('Changes') %>:</div>
    <div class="value col-10"><% loc('Loading...') %></div>
  </div>
% }
</div>
%     }
% }
% $m->callback( %ARGS, Transaction => $Transaction, CallbackName => 'AfterContent' );
</div>

<%ARGS>
$Transaction
$Object => $Transaction->Object

$Attachments => undef
$AttachmentContent => undef
$HasTxnCFs => 1

$ShowBody => 1
$ShowActions => 1
$RowNum => 1

$DisplayPath => undef
$AttachmentPath => undef
$UpdatePath => undef
$ForwardPath => undef
$EncryptionPath => undef
$EmailRecordPath => undef
</%ARGS>

<%ONCE>

</%ONCE>
<%INIT>
my $record_type = $Object->RecordType;
my $type_class  = $Object->ClassifyTransaction( $Transaction );

$m->callback(
    CallbackName => 'MassageTypeClass',
    Transaction  => $Transaction,
    TypeClassRef => \$type_class,
    ARGSRef      => \%ARGS,
);

my @classes = (
    "transaction",
    "$record_type-transaction",
    $type_class,
    ($RowNum % 2 ? 'odd' : 'even')
);

my $desc = $Transaction->BriefDescriptionAsHTML;
if ( $Object->id != $Transaction->ObjectId ) {
    # merged objects
    $desc = join " - ",
        $m->interp->apply_escapes(
            loc("[_1] #[_2]:", loc($record_type), $Transaction->ObjectId), 'h'),
        $desc;
}

my $date = $Transaction->CreatedAsString;

my $time = '';
$time = loc('[quant,_1,minute,minutes]', $Transaction->TimeTaken)
    if $Transaction->TimeTaken;

if ( $ShowBody && !$Attachments ) {
    $ARGS{'Attachments'} = $Attachments = {};

    my $attachments = $Transaction->Attachments( WithHeaders => 1 );
    push @{ $Attachments->{ $_->Parent || 0 } ||= [] }, $_
        foreach @{ $attachments->ItemsArrayRef };
}

my @actions = ();
my $txn_type = $Transaction->Type;
if ( $txn_type =~ /EmailRecord$/ ) {
    my $alt = loc('Show email contents');
    push @actions, {
        title  => qq[<span class="fas fa-envelope-open-text" alt="$alt" data-toggle="tooltip" data-placement="top" data-original-title="$alt"></span>],
        target => '_blank',
        path   => $EmailRecordPath
            .'?id='. $Object->id
            .'&Transaction='. $Transaction->id
            .'&Attachment='. ( $Attachments->{0}[0] && $Attachments->{0}[0]->id ),
    } if $EmailRecordPath;

    $ShowBody = 0;
}
elsif ($txn_type eq 'CustomField' && $Transaction->Field) {
    my $cf = RT::CustomField->new( $session{CurrentUser} );
    $cf->SetContextObject( $Transaction->Object );
    $cf->Load( $Transaction->Field );
    if ($cf->Id && $cf->Type =~ /text/i) {
        push @actions, { class => 'toggle-txn-details', title => loc('Show Details'), path => '#' };
    }
}
# If the transaction has anything attached to it at all
elsif ( %$Attachments && $ShowActions ) {
    my %has_right = map {
        $_ => RT::ACE->CanonicalizeRightName( $_ . $record_type )
    } qw(Modify CommentOn ReplyTo);
    $has_right{'Forward'} = RT::ACE->CanonicalizeRightName('ForwardMessage');

    my $can_modify = $has_right{'Modify'}
        && $Object->CurrentUserHasRight( $has_right{'Modify'} );

    if ( $UpdatePath && $has_right{'ReplyTo'}
        && ( $can_modify
            || $Object->CurrentUserHasRight( $has_right{'ReplyTo'} )
        )
    ) {
        my $alt = loc('Reply');
        push @actions, {
            class  => "reply-link",
            title  => qq[<span class="fas fa-reply" alt="$alt" data-toggle="tooltip" data-placement="top" data-original-title="$alt"></span>],
            path   => $UpdatePath
                .'?id='. $Object->id
                .'&QuoteTransaction='. $Transaction->id
                .'&Action=Respond'
            ,
        };
    }
    if ( $UpdatePath && $has_right{'CommentOn'}
        && ( $can_modify
            || $Object->CurrentUserHasRight( $has_right{'CommentOn'} )
        )
    ) {
        my $alt = loc('Comment');
        push @actions, {
            class  => "comment-link",
            title  => qq[<span class="fas fa-comment" alt="$alt" data-toggle="tooltip" data-placement="top" data-original-title="$alt"></span>],
            path   => $UpdatePath
                .'?id='. $Object->id
                .'&QuoteTransaction='. $Transaction->id
                .'&Action=Comment'
            ,
        };
    }
    if ( $ForwardPath && $has_right{'Forward'}
        && $Object->CurrentUserHasRight( $has_right{'Forward'} )
    ) {
        my $alt = loc('Forward');
        push @actions, {
            class  => "forward-link",
            title  => qq[<span class="fas fa-forward" alt="$alt" data-toggle="tooltip" data-placement="top" data-original-title="$alt"></span>],
            path   => $ForwardPath
                .'?id='. $Object->id
                .'&QuoteTransaction='. $Transaction->id
            ,
        };
    }
    if ( $EncryptionPath && $can_modify
        && RT->Config->Get('Crypt')->{'Enable'}
        && RT->Config->Get('Crypt')->{'AllowEncryptDataInDB'}
    ) {
        my $alt = loc('Encrypt/Decrypt');
        push @actions, {
            class  => "encryption-link",
            title  => qq[<span class="fas fa-key" alt="$alt" data-toggle="tooltip" data-placement="top" data-original-title="$alt"></span>],
            path   => $EncryptionPath
                .'?id='. $Transaction->id
                .'&QuoteTransaction='. $Transaction->id
            ,
        };
    }
}

my $CreatorObj = $Transaction->CreatorObj;

$m->callback(
    %ARGS,
    Transaction => $Transaction,
    Object      => $Object,

    Classes     => \@classes,
    Actions     => \@actions,
    Created     => \$date,
    TimeTaken   => \$time,
    Description => \$desc,
    ShowBody    => \$ShowBody,
    CreatorObj  => \$CreatorObj,
    HasTxnCFs   => \$HasTxnCFs,
    ARGSRef     => \%ARGS,
);

my $actions = '';
if ( @actions ) {
    my $i = $m->interp;

    foreach my $a ( @actions ) {
        $a = '<a'
            .' href="'. $i->apply_escapes( $a->{'path'}, 'h' ) .'"'
            . ($a->{'target'}
                ? ' target="'. $i->apply_escapes( $a->{'target'}, 'h' ) .'"'
                : ''
            )
            . ($a->{'class'}
                ? ' class="'. $i->apply_escapes( $a->{'class'}, 'h' ) .'"'
                : ''
            )
            .'>'. $a->{'title'} .'</a>'
        ;
    }
    $actions = join '&nbsp;', @actions;
}

# make date unbreakable
$date = $m->interp->apply_escapes( $date, 'h' );
$date =~ s/\s/&nbsp;/g;
</%INIT>
