%# 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 }}}
<& /Elements/Header, Title => loc('Browse by topic') &>
<& /Elements/Tabs &>

<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item">
      <a href="Topics.html"><&|/l&>All topics</&></a>
    </li>
% if (defined $class) {
    <li class="breadcrumb-item">
      <a href="Topics.html?class=<%$currclass_id%>"><% $currclass_name %></a>
    </li>
% }

% if ($id != 0) {
<& /Articles/Elements/ShowTopic, topic => $currtopic, InBreadcrumb => 1 &>
% }
  </ol>
</nav>

% if (defined $class) {
<h5><a href="<% RT->Config->Get('WebPath') %>/Articles/Topics.html?class=<% $currclass_id %>"><% $currclass_name %></a></h5>
%     my $rtopic = RT::Topic->new( $session{'CurrentUser'} );
%     $rtopic->Load($id);
%     unless ( $rtopic->Id()
%         && $rtopic->ObjectId() == $currclass->Id )
%     {
%         # Show all of them
%         $ProduceTree->( 0 );
%     } else {
%         my @showtopics = ( $rtopic );
%         my $parent = $rtopic->ParentObj;
%         while ( $parent->Id ) {
%             unshift @showtopics, $parent;
%             $parent = $parent->ParentObj;
%         }
%         # List the topics.
%         for my $t ( @showtopics ) {
<ul class="list-group list-group-compact"><li class="list-group-item"><& /Articles/Elements/ShowTopicLink, Topic => $t, Class => $currclass_id &>
%             $ProduceTree->( $id ) if $t->Id == $id;
%         }
%         for ( @showtopics ) {
              </li></ul>
%         }
%     }
% } else {
<ul class="list-group list-group-compact">
%     while (my $c = $Classes->Next) {
<li class="list-group-item"><h5><a href="<% RT->Config->Get('WebPath') %>/Articles/Topics.html?class=<% $c->Id %>"><% $c->Name %></a></h5></li>
%     }
<li class="list-group-item"><h5><a href="<% RT->Config->Get('WebPath') %>/Articles/Topics.html?class=0"><&|/l&>Global Topics</&></a></h5></li>
</ul>
% }

<br />

% if (@articles) {
%   if ($id) {
<h5><&|/l, $currtopic->Name&>Articles in [_1]</&></h5>
%   } elsif ($class) {
<h2><&|/l&>Articles with no topics</&></h2>
%   }
<ul class="list-group list-group-compact">
%   for my $a (@articles) {
<li class="list-group-item"><a href="Article/Display.html?id=<% $a->Id %>"><% $a->Name || loc("(no name)") %></a></li>
%   }
</ul>
% }

<%init>
my $Classes;
my $currclass;
my $currclass_id;
my $currclass_name;
my $currtopic;

if ( defined $class ) {
    if ($class) {
        $currclass = RT::Class->new( $session{'CurrentUser'} );
        $currclass->Load($class);
        $currclass_id   = $currclass->Id;
        $currclass_name = $currclass->Name;
    } else {
        $currclass      = $RT::System;
        $currclass_id   = 0;
        $currclass_name = 'Global Topics';
    }
} else {
    $Classes = RT::Classes->new( $session{'CurrentUser'} );
    $Classes->LimitToEnabled();
}

if ($id) {
    $currtopic = RT::Topic->new( $session{'CurrentUser'} );
    $currtopic->Load($id);
}

my $ProduceTree;
$ProduceTree = sub {
    my ( $parentid ) = @_;
    my $topics = RT::Topics->new( $session{'CurrentUser'} );
    $topics->LimitToObject($currclass);
    $topics->LimitToKids($parentid || 0);
    $topics->OrderBy( FIELD => 'Name' );
    return unless $topics->Count;
    $m->out("<ul>");
    while ( my $t = $topics->Next ) {
        $m->out("<li>");
        $m->comp("/Articles/Elements/ShowTopicLink",
                 Topic => $t,
                 Class => $currclass_id,
             );
        $ProduceTree->( $t->Id ) if $t->Children->Count;
        $m->out("</li>");
    }
    $m->out("</ul>");
};

my @articles;
if ($id) {
    my $Articles = RT::ObjectTopics->new($session{'CurrentUser'});
    $Articles->Limit(FIELD => 'ObjectType', VALUE => 'RT::Article');
    $Articles->Limit(FIELD => 'Topic', VALUE => $id);
    while (my $objtopic = $Articles->Next) {
        my $a = RT::Article->new($session{'CurrentUser'});
        $a->Load($objtopic->ObjectId);
        push @articles, $a;
    }
} elsif ($class) {
    my $Articles = RT::Articles->new($session{'CurrentUser'});
    my $TopicsAlias = $Articles->Join(
        TYPE   => 'left',
        ALIAS1 => 'main',
        FIELD1 => 'id',
        TABLE2 => 'ObjectTopics',
        FIELD2 => 'ObjectId',
    );
    $Articles->Limit(
        LEFTJOIN => $TopicsAlias,
        FIELD    => 'ObjectType',
        VALUE    => 'RT::Article',
    );
    $Articles->Limit(
        ALIAS      => $TopicsAlias,
        FIELD      => 'Topic',
        OPERATOR   => 'IS',
        VALUE      => 'NULL',
        QUOTEVALUE => 0,
    );
    $Articles->Limit(
        FIELD      => 'Class',
        OPERATOR   => '=',
        VALUE      => $class,
    );
    @articles = @{$Articles->ItemsArrayRef};
}
</%init>

<%args>
$id => 0
$class => undef
$showall => undef
</%args>
