%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2023 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 &>

<a href="Topics.html"><&|/l&>All topics</&></a>
% if (defined $class) {
&gt; <a href="Topics.html?class=<%$currclass_id%>"><% $currclass_name %></a>
% }

% if ($id != 0) {
&gt; <& /Articles/Elements/ShowTopic, topic => $currtopic &>
% }
<br />
<h1><&|/l&>Browse by topic</&></h1>
% if (defined $class) {
<h2><a href="<% RT->Config->Get('WebPath') %>/Articles/Topics.html?class=<% $currclass_id %>"><% $currclass_name %></a></h2>
%     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><li><& /Articles/Elements/ShowTopicLink, Topic => $t, Class => $currclass_id &>
%             $ProduceTree->( $id ) if $t->Id == $id;
%         }
%         for ( @showtopics ) {
              </li></ul>
%         }
%     }
% } else {
<ul>
%     while (my $c = $Classes->Next) {
<li><h2><a href="<% RT->Config->Get('WebPath') %>/Articles/Topics.html?class=<% $c->Id %>"><% $c->Name %></a></h2></li>
%     }
<li><h2><a href="<% RT->Config->Get('WebPath') %>/Articles/Topics.html?class=0"><&|/l&>Global Topics</&></a></h2></li>
</ul>
% }

<br />

% if (@articles) {
%   if ($id) {
<h2><&|/l, $currtopic->Name&>Articles in [_1]</&></h2>
%   } elsif ($class) {
<h2><&|/l&>Articles with no topics</&></h2>
%   }
<ul>
%   for my $a (@articles) {
<li><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>
