%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2022 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 }}}
<&| /Widgets/TitleBox, title => loc('Attachments'), 
        title_class=> 'inverse',  
        class => 'ticket-info-attachments',
        color => "#336699",
        hide_chrome => $HideTitleBox &>

% $m->callback( %ARGS, CallbackName => 'BeforeList', TicketObj => $Ticket, Attachments => $Attachments, Documents => \%documents, IsChecked => \%is_checked, ShowMore => \$show_more );

<div class="attachment-list">

% foreach my $key (sort { lc($a) cmp lc($b) } keys %documents) {

<span class="filename"><%$key%></span>
<ul class="list-group list-group-compact" <% $Selectable ? 'class="selectable"' : '' |n %> >
% foreach my $rev (@{$documents{$key}}) {
% if ($rev->ContentLength) {
<li class="list-group-item">

  <div class="custom-control custom-checkbox">
% if ($Selectable) {
      <input type="checkbox" class="custom-control-input" id="AttachExisting-<% $rev->Id %>" name="AttachExisting" value="<% $rev->Id %>" \
        <% $is_checked{$rev->Id} ? 'checked' : '' %>
      />
% }

<label <% $Selectable ? 'class="custom-control-label"' : '' |n%> for="AttachExisting-<% $rev->Id %>">
<span class="revision">
% if (my $url = RT->System->ExternalStorageURLFor($rev)) {
<a href="<%$url%>">
% } else {
<a href="<%RT->Config->Get('WebPath')%>/<% $DisplayPath %>/Attachment/<%$rev->TransactionId%>/<%$rev->Id%>/<%$rev->Filename | un %>">
% }
% my $desc = loc("[_1] ([_2]) by [_3]", $rev->CreatedAsString, $rev->FriendlyContentLength, $m->scomp('/Elements/ShowUser', User => $rev->CreatorObj));
<% $desc |n%>
</a>

%# RT-Extension-7zDownload Starts here
%# also provide a safe download option 
<a href="<%RT->Config->Get('WebPath')%>/<% $DisplayPath %>/Attachment/AsZip/<%$rev->TransactionId%>/
<%$rev->Id%>/<%$rev->Filename | un %>">
<b>7z-Download</b>
</a>
%# RT-Extension-7zDownload Ends here

</span>
    </label>
  </div>
</li>
% }
% }
</ul>

% }

% $m->callback( %ARGS, CallbackName => 'AfterList', TicketObj => $Ticket, Attachments => $Attachments, Documents => \%documents, IsChecked => \%is_checked, ShowMore => \$show_more );

% if ($show_more) {
<span class="show-more-link">
% my %params = %ARGS;
% delete $params{Ticket};
% delete $params{Attachments};
% delete $params{Count};
% my $query = $m->comp('/Elements/QueryString', %params, id => $Ticket->id );
% my $url   = RT->Config->Get('WebPath')."/Helpers/TicketAttachments?$query";

<script type="text/javascript">
    function showAllAttachments(node) {
        var container = node.closest('.attachment-list');
        var params = node.closest('form').find('input[name=AttachExisting]').serialize();

        node.parent().text(<% loc('Loading...') | n,j%>);

        var url = <% $url |n,j %>;
        if (params) url += '&' + params;
        container.load(url);
    }
</script>

<a href="#" class="button btn btn-primary" onclick="showAllAttachments(jQuery(this)); return false;" ><% loc('Show all') %></a>

</span>
% }

</div>
</&>

<%INIT>

# If we haven't been passed in an Attachments object (through the precaching mechanism)
# then we need to find one
$Attachments ||= $Ticket->Attachments;

# Avoid applying limits to this collection that may be used elsewhere
# (e.g. transaction display)
$Attachments = $Attachments->Clone;

# Remember, each message in a transaction is an attachment; we only
# want named attachments (real files)
$Attachments->LimitHasFilename;

my $show_more = 0;
my %documents;

# show newest first
$Attachments->OrderByCols(
    { FIELD => 'Created', ORDER => 'DESC' },
    { FIELD => 'id',      ORDER => 'DESC' },
);

while ( my $attach = $Attachments->Next() ) {
   # display "show more" only when there will be more attachments
   if (defined($Count) && --$Count < 0) {
       $show_more = 1;
       last;
   }
   push @{ $documents{ $attach->Filename } }, $attach;
}

my %is_checked = map { $_ => 1 } @Checked;

return if !$show_more && keys %documents == 0;
</%INIT>
<%ARGS>
$Ticket => undef
$Attachments => undef
$DisplayPath => $session{'CurrentUser'}->Privileged ? 'Ticket' : 'SelfService'
$HideTitleBox => 0
$Selectable => 0
$Count => undef
@Checked => ()
</%ARGS>

