%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2026 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="ticket-links">
% if ( $EmptyHint && !$links_total ) {
  <p class="links-empty-hint text-center fst-italic"><&|/l&>Click the pencil to add links</&></p>
% }
% if ($links_total) {
%# Shown when the default filters leave nothing to render; the client filter toggles it after.
  <p class="links-no-match text-center fst-italic<% @sections ? ' d-none' : '' %>"><&|/l&>No links match</&></p>
% }
% for my $section (@sections) {
<% $section |n %>
% }
  <span class="links-total d-none" data-links-total="<% $links_total %>"<% $partial ? ' data-links-partial="1"' : '' |n %>></span>
</div>
% $m->callback( %ARGS );
% if ($DualMode) {
  <div class="links-cf-display"><& /Elements/ShowCustomFields, Object => $Object, Grouping => 'Links', Table => 0 &></div>
  <div class="links-cf-edit"><& /Elements/EditCustomFields, Object => $Object, Grouping => 'Links', InTable => 0 &></div>
%#  A second Save after the tables, so it's clear Save also commits the removals staged in them.
%#  Rendered here rather than in EditLinks so the listing refresh keeps it in step with the links.
%   if ( $Edit && $links_total ) {
  <div class="links-edit-save-end"><& /Elements/Submit, Label => loc('Save'), FullWidth => 1 &></div>
%   }
% } elsif ($Edit) {
  <& /Elements/EditCustomFields, Object => $Object, Grouping => 'Links', InTable => 0 &>
% } else {
  <& /Elements/ShowCustomFields, Object => $Object, Grouping => 'Links', Table => 0 &>
% }
%#  In DualMode the Links widget renders this outside its inline-edit <form> (a nested <form> is
%#  invalid HTML and gets dropped); render it inline only for the standalone/display path.
%   if ( $Object->isa('RT::Ticket') && !$Embedded && !$DualMode ) {
<& /Elements/CreateLinkedTicket, Object => $Object &>
%   }

<%INIT>
my @display = qw(DependsOn DependedOnBy MemberOf Members RefersTo ReferredToBy);
$m->callback( %ARGS, CallbackName => 'ChangeDisplay', display => \@display );
my %labels  = (
    DependsOn       => loc('Depends on'),
    DependedOnBy    => loc('Depended on by'),
    MemberOf        => loc('Parents'),
    Members         => loc('Children'),
    RefersTo        => loc('Refers to'),
    ReferredToBy    => loc('Referred to by'),
);

my @render = @display;

my $links_total = RT::Links->LinkListingCount(
    CurrentUser => $session{CurrentUser},
    Object      => $Object,
    Types       => \@display,
);

# An empty ListCount (the page layout didn't set one) falls back to $LinksListCount;
# anything else that isn't a count is ignored rather than trusted from the query string.
$ListCount = RT->Config->Get('LinksListCount') if !defined $ListCount || $ListCount eq '';
$ListCount = undef unless defined $ListCount && $ListCount =~ /^\d+$/;
my $partial = 0;

my @sections = grep { defined && /\S/ } map {
    $m->scomp(
        '/Elements/ShowLinksSection',
        Object           => $Object,
        Type             => $_,
        Label            => $labels{$_},
        Edit             => $Edit,
        DualMode         => $DualMode,
        Embedded         => $Embedded,
        ListCount        => $ListCount,
        ShowAll          => $ShowAll,
        HideInactive     => $HideInactive,
        ShowRelationship => \@ShowRelationship,
        ShowObjectType   => \@ShowObjectType,
        Partial          => \$partial,
    )
} @render;
</%INIT>

<%ARGS>
$Object
$Edit             => 0
$DualMode         => 0
$Embedded         => 0
$ListCount        => undef
$ShowAll          => 0
$HideInactive     => 0
@ShowRelationship => ()
@ShowObjectType   => ()
$EmptyHint        => 0
</%ARGS>

<%attr>
directly_accessible => 1
</%attr>
