%# 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 }}}
% if ( $ARGS{id} && $ARGS{id} ne 'new' && RT->Config->Get('LinkArticlesOnInclude') ) {
<input type="hidden" name="<%$ARGS{'id'}%>-RefersTo" value="<% join(' ',grep {$_} sort keys %uri) %>" />
% }

<div class="articles-select-article">
% unless (RT->Config->Get('HideArticleSearchOnReplyCreate')) {
  <&| /Elements/LabeledValue, Label => loc("Include Article"), LabelFor => 'select-article' &>
    <& /Elements/SelectArticle, QueueObj => $QueueObj, AutoSubmit => 1, AutoSubmitAction => $ArticleAutoSubmitAction  &>
  </&>
% }
% if ( @$topics ) {
  <&| /Elements/LabeledValue, Label => loc('Choose from Topics for [_1]', $QueueObj->Name) &>
    <select name="<% $name_prefix %>Articles-Include-Topic" onchange="<% $ArticleAutoSubmitAction  %>" class="form-select selectpicker">
      <option value="" selected>-</option>
% for ( @$topics ) {
        <option value="<% $_->{id} %>"><%'&nbsp;' x $_->{depth} . ($_->{name}|| loc('(no name)')) |n%>
        </option>
% }
    </select>
  </&>

% if ( $ARGS{$name_prefix .'Articles-Include-Topic'} ) {
  <&| /Elements/LabeledValue, Label => loc('Select an Article from [_1]', $included_topic->Name) &>
    <select name="IncludeArticleId" onchange="<% $ArticleAutoSubmitAction %>" class="form-select selectpicker">
      <option value="" selected>-</option>
% while ( my $art = $topic_articles->Next ) {
      <option value="<% $art->id %>"><%$art->Name||loc('(no name)')%>: <%$art->Summary%></option>
% }
    </select>
  </&>
% } # End if Articles-Include-Topic
% } # End if @$topics

</div>

<%init>
my $QueueObj = $ARGS{QueueObj};
if ( $ARGS{id} && $ARGS{id} ne 'new' && !$QueueObj ) {
    my $ticket = RT::Ticket->new( $session{CurrentUser} );
    $ticket->Load( $ARGS{id} );
    $QueueObj = $ticket->QueueObj;
}

my $ArticleAutoSubmitAction = $ARGS{ArticleAutoSubmitAction} || "reloadElement(this.closest('[hx-post]'))";

my $skip = 0;
$m->callback(CallbackName => "Init", skip => \$skip, Queue => $QueueObj);
return if $skip;

my $name_prefix = '';
if ( $ARGS{'MessageBoxName'} ) {
    $name_prefix = $ARGS{'MessageBoxName'} .'-';
}

my %uri;
if ( $ARGS{id} && $ARGS{id} ne 'new' ) {
    $uri{$_}++ for split ' ', ($ARGS{$ARGS{'id'}.'-RefersTo'} || '');

    my $article = RT::Article->new($session{'CurrentUser'});
    my ($ret, $msg) = $article->Load($ARGS{'IncludeArticleId'});

    if ($ret && $article->Id and not $article->ClassObj->FirstAttribute('Skip-LinkToTicket')) {
        unless ( $QueueObj && !$article->ClassObj->IsApplied(0) && !$article->ClassObj->IsApplied( $QueueObj->id ) ) {
            $uri{$article->URI}++;
        }
    }
}

my ( $topic_articles, $topics, $included_topic );
$topic_articles = RT::Articles->new( $session{CurrentUser} );
$topics = [];

my $top_topic = RT::Topic->new( $session{CurrentUser} );
$top_topic->LoadByCols( Name => 'Queues', Parent => 0 , ObjectType => 'RT::System', ObjectId => 1);

if ( $top_topic->id ) {
    my $queue_topic = RT::Topic->new( $session{CurrentUser} );
    $queue_topic->LoadByCols( Name => $QueueObj->Name, Parent => $top_topic->id );
    if ( $queue_topic->id ) {

        # store all topics below $queue_topic to $topics
        topics( $queue_topic, $topics, 0 );

        if ( my $tmp = $ARGS{ $name_prefix .'Articles-Include-Topic'}  ) {
            $included_topic = RT::Topic->new( $session{CurrentUser} );
            $included_topic->Load( $tmp );
            $topic_articles->LimitTopics( $tmp );
            $topic_articles->OrderBy( FIELD => 'Name' );
        }
    }
}


# recursively get all the topics given a top topic
sub topics {
    my $parent = shift;
    my $out = shift;
    my $depth = shift;
    while ( my $topic = $parent->Children->Next ) {
        push @$out, { id => $topic->id, name => $topic->Name, depth => $depth };
        topics( $topic, $out, $depth+1 );
    }
}

</%init>


