first pass RT4 merge, RT#13852
[freeside.git] / rt / share / html / Articles / Topics.html
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 <& /Elements/Header, Title => loc('Browse by topic') &>
49 <& /Elements/Tabs &>
50
51 <& /Elements/ListActions, actions => \@Actions &>
52 <a href="Topics.html"><&|/l&>All topics</&></a>
53 % if (defined $class) {
54 &gt; <a href="Topics.html?class=<%$currclass_id%>"><% $currclass_name %></a>
55 % }
56
57 % if ($id != 0) {
58 &gt; <& /Articles/Elements/ShowTopic, topic => $currtopic &>
59 % }
60 <br />
61 <h1><&|/l&>Browse by topic</&></h1>
62 <%perl>
63 if (defined $class) {
64    $m->print('<h2>'.'<a href="'.
65    RT->Config->Get('WebPath')."/Articles/Topics.html?class=" . $currclass_id
66    .'">'.$currclass_name."</a></h2>\n");
67    ProduceTree(\@Actions, $currclass, $currclass_id, $currclass_name, 0, $id);
68 } else {
69     $m->print("<ul>\n");
70     while (my $c = $Classes->Next) {
71         $m->print('<li><h2>'.'<a href="'.
72         RT->Config->Get('WebPath')."/Articles/Topics.html?class=" . $c->Id
73         .'">'.$c->Name."</a></h2>\n");
74         $m->print("\n</li>\n");
75     }
76     $m->print(qq|<li><h2><a href="|.RT->Config->Get('WebPath').qq|/Articles/Topics.html?class=0">|.loc('Global Topics').qq|</a></h2></li>\n|);
77     $m->print("</ul>\n");
78 }
79 </%perl>
80
81 <br />
82 <%perl>
83 my @articles;
84 if ($id or $showall) {
85     my $Articles = RT::ObjectTopics->new($session{'CurrentUser'});
86     $Articles->Limit(FIELD => 'ObjectType', VALUE => 'RT::Article');
87     if ($id) {
88         $Articles->Limit(FIELD => 'Topic', VALUE => $id, ENTRYAGGREGATOR => 'OR');
89         if ($showall) {
90             my $kids = $currtopic->Children;
91             while (my $k = $kids->Next) {
92                 $Articles->Limit(FIELD => 'Topic', VALUE => $k->Id,
93                                  ENTRYAGGREGATOR => 'OR');
94             }
95         }
96     }
97     @articles = map {$a = RT::Article->new($session{'CurrentUser'}); $a->Load($_->ObjectId); $a} @{$Articles->ItemsArrayRef}
98 } elsif ($class) {
99     my $Articles = RT::Articles->new($session{'CurrentUser'});
100     my $TopicsAlias = $Articles->Join(
101         TYPE   => 'left',
102         ALIAS1 => 'main',
103         FIELD1 => 'id',
104         TABLE2 => 'ObjectTopics',
105         FIELD2 => 'ObjectId',
106     );
107     $Articles->Limit(
108         LEFTJOIN => $TopicsAlias,
109         FIELD    => 'ObjectType',
110         VALUE    => 'RT::Article',
111     );
112     $Articles->Limit(
113         ALIAS      => $TopicsAlias,
114         FIELD      => 'Topic',
115         OPERATOR   => 'IS',
116         VALUE      => 'NULL',
117         QUOTEVALUE => 0,
118     );
119     $Articles->Limit(
120         FIELD      => 'Class',
121         OPERATOR   => '=',
122         VALUE      => $class,
123     );
124     @articles = @{$Articles->ItemsArrayRef};
125 }
126 </%perl>
127
128 % if (@articles) {
129 %   if ($id) {
130 <h2><&|/l, $currtopic->Name&>Articles in [_1]</&></h2>
131 %   } elsif ($class) {
132 <h2><&|/l&>Articles with no topics</&></h2>
133 %   }
134 <ul>
135 %   for my $a (@articles) {
136 <li><a href="Article/Display.html?id=<% $a->Id %>"><% $a->Name || loc("(no name)") %></a></li>
137 %   }
138 </ul>
139 % }
140
141 <%init>
142 my @Actions;
143 my $Classes;
144 my $currclass;
145 my $currclass_id;
146 my $currclass_name;
147 my $currtopic;
148
149 if ( defined $class ) {
150     if ($class) {
151         $currclass = RT::Class->new( $session{'CurrentUser'} );
152         $currclass->Load($class);
153         $currclass_id   = $currclass->Id;
154         $currclass_name = $currclass->Name;
155     } else {
156         $currclass      = $RT::System;
157         $currclass_id   = 0;
158         $currclass_name = 'Global Topics';
159     }
160 } else {
161     $Classes = RT::Classes->new( $session{'CurrentUser'} );
162     $Classes->LimitToEnabled();
163 }
164
165 if ($id) {
166     $currtopic = RT::Topic->new( $session{'CurrentUser'} );
167     $currtopic->Load($id);
168 }
169
170 # A subroutine that iterates through topics and their children, producing
171 # the necessary ul, li, and href links for the table of contents.  Thank
172 # heaven for query caching.  The $restrict variable is used to display only
173 # the branch of the hierarchy which contains that topic ID.
174
175 sub ProduceTree {
176     my ( $Actions, $currclass, $currclass_id, $currclass_name, $parentid, $restrictid ) = @_;
177     $parentid = 0 unless $parentid;
178
179     # Deal with tree restriction, if any.
180     if ($restrictid) {
181         my $rtopic = RT::Topic->new( $session{'CurrentUser'} );
182         $rtopic->Load($restrictid);
183         unless ( $rtopic->Id()
184             && $rtopic->ObjectId() == $currclass_id )
185         {
186             push( @{$Actions},"Could not restrict view to topic $restrictid");
187
188             # Start over, without the restriction.
189             &ProduceTree( $Actions, $currclass, $currclass_id, $currclass_name, $parentid, undef );
190         } else {
191             my @showtopics;
192             push( @showtopics, $rtopic );
193             my $parent = $rtopic->ParentObj;
194             while ( $parent->Id ) {
195                 push( @showtopics, $parent );
196                 my $newparent = $parent->ParentObj;
197                 $parent = $newparent;
198             }
199
200             # List the topics.
201             my $indents = @showtopics;
202             while ( my $t = pop @showtopics ) {
203                 print "<ul>";
204                 print &MakeLinks( $t, $currclass, $currclass_id, $currclass_name, $t->Children->Count );
205                 if ( $t->Id == $restrictid ) {
206                     &ProduceTree( $Actions, $currclass, $currclass_id, $currclass_name, $restrictid, undef );
207                 }
208             }
209             print "</ul>" x $indents;
210         }
211     } else {
212
213         # No restriction in place.  Build the entire tree.
214         my $topics = RT::Topics->new( $session{'CurrentUser'} );
215         $topics->LimitToObject($currclass);
216         $topics->LimitToKids($parentid);
217         $topics->OrderBy( FIELD => 'Name' );
218         print "<ul>" if $topics->Count;
219         while ( my $t = $topics->Next ) {
220             if ( $t->Children->Count ) {
221                 print &MakeLinks( $t, $currclass, $currclass_id, $currclass_name, 1 );
222                 &ProduceTree( $Actions, $currclass, $currclass_id, $currclass_name, $t->Id );
223             } else {
224                 print &MakeLinks( $t, $currclass, $currclass_id, $currclass_name, 0 );
225             }
226         }
227         print "</ul>\n" if $topics->Count;
228     }
229 }
230
231 sub MakeLinks {
232     my ( $topic, $currclass, $currclass_id, $currclass_name, $haschild ) = @_;
233     my $query;
234     my $output;
235
236     if ( ref($topic) eq 'RT::Topic' ) {
237
238         my $topic_info = $topic->Name() || loc("(no name)");
239         $topic_info .= ": " . $topic->Description() if $topic->Description;
240
241         if ($haschild) { # has topics below it
242             $query  = "Topics.html?id=" . $topic->Id . "&class=" . $currclass_id;
243             $output = qq(<li><a href="$query">$topic_info</a>);
244         } else {
245             $output = qq(<li>$topic_info);
246         }
247
248         my $Articles = RT::ObjectTopics->new( $session{'CurrentUser'} );
249         $Articles->Limit( FIELD => 'ObjectType', VALUE => 'RT::Article' );
250         $Articles->Limit( FIELD => 'Topic',      VALUE => $topic->Id );
251         if ( $Articles->Count ) {
252             my $article_text = " (" . loc( "[quant,_1,article]", $Articles->Count ) . ")";
253             my $query  = "Topics.html?id=" . $topic->Id . "&class=$currclass_id&showall=1";
254             $output .= qq(<a href="$query">$article_text</a>);
255         }
256
257         $output .= "</li>\n";
258
259     } else {
260
261         # This builds a link for the class specified, with no particular topic.
262         $query  = "Topics.html?class=" . $currclass_id;
263         $output = "<li><a href=\"$query\">" . $currclass_name . "</a>";
264         $output .= ": " . $currclass->Description if $currclass->Description;
265     }
266
267     return $output;
268 }
269
270 </%init>
271
272 <%args>
273 $id => 0
274 $class => undef
275 $showall => undef
276 </%args>