%# 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 }}}
<%args>
$Object         => undef
$CustomFields   => undef
$ARGSRef        => $DECODED_ARGS
</%args>
<%init>
my $id = ($Object and $Object->id)
    ? $Object->id
    : "new";

my $autocomplete = qq|data-autocomplete="LinkTargets" data-autocomplete-multiple="1"|;
my @excludes;
push @excludes, ( $Object->isa('RT::Asset') ? 'asset:' : '' ) . $id
    if $id ne 'new';

my %exclude_links = (
    Depend => [ qw( DependsOn DependedOnBy ) ],
    Member => [ qw( MemberOf Members ) ],
    Refer  => [ qw( RefersTo ReferredToBy ) ],
);
foreach my $exclude_type ( keys %exclude_links ) {
    my @ids;
    if ( $id ne 'new' ) {
        foreach my $link_type ( @{ $exclude_links{$exclude_type} } ) {
            my $links = $Object->$link_type;
            while ( my $link = $links->Next ) {
                my $LinkedObj = $link->TargetObj;
                # if $LinkedObj is same as $Object we want the BaseObj
                $LinkedObj = $link->BaseObj
                    if (
                        ( ref($Object) eq ref($LinkedObj) )
                        &&
                        ( $id == $LinkedObj->id )
                    );
                if ( $LinkedObj ) {
                    my $prefix = '';
                    if ( $LinkedObj->isa('RT::Asset') ) {
                        $prefix = 'asset:';
                    }
                    elsif ( $LinkedObj->isa('RT::Article') ) {
                        $prefix = 'a:';
                    }
                    elsif ( $LinkedObj->isa('RT::Group') ) {
                        $prefix = 'group:';
                    }
                    elsif ( $LinkedObj->isa('RT::User') ) {
                        $prefix = 'user:';
                    }
                    push @ids, $prefix . $LinkedObj->id;
                }
            }
        }
    }

    $exclude_links{$exclude_type}
        = ( @excludes || @ids )
        ? 'data-autocomplete-exclude="' . join( ' ', @excludes, @ids ) . '"'
        : '';
}
</%init>
% if (ref($Object) eq 'RT::Ticket') {
<i><&|/l&>Enter tickets or URIs to link tickets to. Separate multiple entries with spaces.</&>
<br /><&|/l&>You may enter links to Articles as "a:###", where ### represents the number of the Article.</&>
<br /><&|/l&>Enter links to assets as "asset:###", where ### represents the asset ID.</&>
<br /><&|/l&>Enter links to groups as "group:###", where ### represents the group ID.</&>
<br /><&|/l&>Enter links to users as "user:###", where ### represents the user ID.</&>
% $m->callback( CallbackName => 'ExtraLinkInstructions' );
</i><br />
% } elsif (ref($Object) eq 'RT::Queue') {
<i><&|/l&>Enter queues or URIs to link queues to. Separate multiple entries with spaces.</&>
</i><br />
% } else {
<i><&|/l&>Enter objects or URIs to link objects to. Separate multiple entries with spaces.</&></i><br />
% }
<div>


<&| /Elements/LabeledValue, RawLabel => $m->scomp('ShowRelationLabel', Object => $Object, Label => loc('Depends on'), LabelFor => $id."-dependson", Relation => 'DependsOn'), ReadOnly => 0 &>
  <input id="<%$id%>-dependson" type="text" class="form-control" name="<%$id%>-DependsOn" value="<% $ARGSRef->{"$id-DependsOn"} || '' %>" <% $autocomplete |n %>  <% $exclude_links{Depend} |n%>/>
</&>
<&| /Elements/LabeledValue, RawLabel => $m->scomp('ShowRelationLabel', Object => $Object, Label => loc('Depended on by'), LabelFor => "dependson-".$id, Relation => 'DependedOnBy'), ReadOnly => 0 &>
  <input id="dependson-<%$id%>" type="text" class="form-control" name="DependsOn-<%$id%>" value="<% $ARGSRef->{"DependsOn-$id"} || '' %>" <% $autocomplete |n %>  <% $exclude_links{Depend} |n%>/>
</&>

<&| /Elements/LabeledValue, RawLabel => $m->scomp('ShowRelationLabel', Object => $Object, Label => loc('Parents'), LabelFor => $id."-memberof", Relation => 'Parents'), ReadOnly => 0 &>
  <input id="<%$id%>-memberof" type="text" class="form-control" name="<%$id%>-MemberOf" value="<% $ARGSRef->{"$id-MemberOf"} || '' %>" <% $autocomplete |n %>  <% $exclude_links{Member} |n%>/>
</&>
<&| /Elements/LabeledValue, RawLabel => $m->scomp('ShowRelationLabel', Object => $Object, Label => loc('Children'), LabelFor => "memberof-".$id, Relation => 'Children'), ReadOnly => 0 &>
  <input id="memberof-<%$id%>" type="text" class="form-control" name="MemberOf-<%$id%>" value="<% $ARGSRef->{"MemberOf-$id"} || '' %>" <% $autocomplete |n %>  <% $exclude_links{Member} |n%>/>
</&>

<&| /Elements/LabeledValue, RawLabel => $m->scomp('ShowRelationLabel', Object => $Object, Label => loc('Refers to'), LabelFor => $id."-refersto", Relation => 'RefersTo'), ReadOnly => 0 &>
  <input id="<%$id%>-refersto" type="text" class="form-control" name="<%$id%>-RefersTo" value="<% $ARGSRef->{"$id-RefersTo"} || '' %>" <% $autocomplete |n %> <% $exclude_links{Refer} |n%>/>
</&>
<&| /Elements/LabeledValue, RawLabel => $m->scomp('ShowRelationLabel', Object => $Object, Label => loc('Referred to by'), LabelFor => "refersto-".$id, Relation => 'ReferredToBy'), ReadOnly => 0 &>
  <input id="refersto-<%$id%>" type="text" class="form-control" name="RefersTo-<%$id%>" value="<% $ARGSRef->{"RefersTo-$id"} || '' %>" <% $autocomplete |n %> <% $exclude_links{Refer} |n%>/>
</&>

<div class="edit-custom-fields-container" hx-get="<% RT->Config->Get('WebPath') %>/Views/Component/EditCustomFields?ObjectType=<% ref $Object %>&ObjectId=<% $Object->Id %>&Grouping=Links&InTable=1" hx-swap="innerHTML">
  <& /Elements/EditCustomFields,
        Object          => $Object,
        Grouping        => 'Links',
        InTable         => 1,
        ($CustomFields
            ? (CustomFields => $CustomFields)
            : ()),
        &>
</div>
% $m->callback( CallbackName => 'NewLink' );
</div>
