rt 4.2.16
[freeside.git] / rt / share / html / Admin / Articles / Elements / Topics
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2019 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/ListActions, actions => \@results &>
49
50 <form action="<%RT->Config->Get('WebPath')%><% $m->request_comp->path |n %>" method="post">
51 <input type="hidden" name="id" value="<%$RootObj->Id%>" />
52
53 % if (!$Modify) {
54 <table>
55 <tr>
56 <td><&|/l&>Topic Name</&></td>
57 <td><input type="text" name="Name" size="30" /></td>
58 </tr>
59 <tr>
60 <td><&|/l&>Description</&></td>
61 <td><input type="text" name="Description" size="50" /></td>
62 </tr>
63 </table>
64 % } else {
65 <a href="Topics.html">New topic</a>
66 % }
67
68 <& .tree, Element => $tree, Action => $Modify ? "Move" : "Add", Prefix => $Modify ? "Topic-$Modify-Parent" : "Insert", RootObj => $RootObj, Modify => $Modify &>
69
70 </form>
71
72 <%def .edit>
73 &nbsp;
74 <table style="margin-top: -0.75em">
75 <tr>
76     <td>Topic Name:</td>
77     <td><input type="text" name="Topic-<%$topic->Id%>-Name" size="20" value="<%$topic->Name%>" /></td>
78     <td><input type="submit" name="Update" value="Update"><br /></td>
79 </tr>
80 <tr>
81     <td>Description:</td>
82     <td> <input type="text" name="Topic-<%$topic->Id%>-Description" size="20" value="<%$topic->Description%>" /></td>
83     <td><input type="submit" name="Delete-Topic-<%$topic->Id%>" value="Delete" /></td>
84 </tr>
85 </table>
86 <%args>
87 $topic
88 </%args>
89 </%def>
90
91 <%def .tree>
92 % my $topic = $Element->getNodeValue;
93 % unless ($Element->isRoot) {
94 %   if ($Modify and $topic->Id == $Modify) {
95 %     $Action = "";
96 <& .edit, topic => $topic &>
97 %   } else {
98 <a href="<%RT->Config->Get('WebPath')%><% $m->request_comp->path |n %>?id=<%$RootObj->Id%>&Modify=<%$topic->Id%>"
99    title="<%$topic->Description%>"><%$topic->Name || loc("(no name)") %></a>
100 %   }
101 % }
102 <ul>
103 % for my $e (sort {$a->getNodeValue->Name cmp $b->getNodeValue->Name} $Element->getAllChildren) {
104 <li><& .tree, Element => $e, Action => $Action, Prefix => $Prefix, RootObj => $RootObj, Modify => $Modify &></li>
105 % }
106 % if ($Action) {
107 % unless ($Action eq "Move" and grep {$_->getNodeValue->Id == $Modify} $Element->getAllChildren) {
108 <li><input type="submit" name="<%$Prefix%>-<%$topic eq "root" ? 0 : $topic->Id%>" value="<% $Action eq 'Move' ? loc('Move here') : loc('Add here') %>" /></li>
109 % }
110 % }
111 </ul>
112 <%args>
113 $Element
114 $Action
115 $Prefix
116 $RootObj
117 $Modify
118 </%args>
119 </%def>
120
121 <%INIT>
122
123 my @results;
124
125 for my $k (keys %ARGS) {
126     if ($k =~ /^Topic-(\d+)-(Name|Description)/) {
127         my $topic = RT::Topic->new($session{'CurrentUser'});
128         $topic->Load($1);
129         if ($topic->Id) {
130             next if $ARGS{$k} eq $topic->$2;
131             my $proc = "Set$2";
132             my ($val, $msg) = $topic->$proc($ARGS{$k});
133             push @results, $msg;
134         } else {
135             $m->comp("/Elements/Error", Why => loc("Topic not found"));
136         }
137     } elsif ($k =~ /^Topic-(\d+)-Parent-(\d+)/) {
138         my $topic = RT::Topic->new($session{'CurrentUser'});
139         $topic->Load($1);
140         if ($topic->Id) {
141             next if $2 eq $topic->Parent;
142             my $old = $topic->Parent;
143             my $new = "$2";
144             my ($val, $msg) = $topic->setParent($new);
145             push @results, $msg;
146         } else {
147             $m->comp("/Elements/Error", Why => loc("Topic not found"));
148         }
149     } elsif ($k =~ /^Insert-(\d+)/) {
150         my $topic = RT::Topic->new($session{'CurrentUser'});
151         my ($id, $msg) = $topic->Create(
152                                         Parent => $1,
153                                         Name => $ARGS{'Name'},
154                                         Description => $ARGS{'Description'},
155                                         ObjectType => ref($RootObj),
156                                         ObjectId => $RootObj->Id,
157                                        );
158         push @results, $msg;
159     }
160 }
161 for my $k (keys %ARGS) {
162     next unless $k =~ /^Delete-Topic-(\d+)/;
163     my $topic = RT::Topic->new($session{'CurrentUser'});
164     $topic->Load($1);
165     if ($topic->Id) {
166         my ($val, $msg) = $topic->DeleteAll();
167         push @results, $msg;
168     } else {
169         $m->comp("/Elements/Error", Why => loc("Topic not found"));
170     }
171 }
172
173 my $topics = RT::Topics->new($session{'CurrentUser'});
174 $topics->LimitToObject($RootObj);
175 $topics->OrderByCols({FIELD => 'Parent'}, {FIELD => 'id'});
176
177 use Tree::Simple;
178 my $tree = Tree::Simple->new(Tree::Simple->ROOT);
179 my %lookup = (0 => $tree);
180
181 my @todo;
182 while (my $topic = $topics->Next) {
183     push @todo, $topic;
184 }
185
186 {
187     my $changed = 0;
188     my @work = @todo;
189     @todo = ();
190     for my $topic (@work) {
191         if (defined $lookup{$topic->Parent}) {
192             $lookup{$topic->Id} = Tree::Simple->new($topic, $lookup{$topic->Parent});
193             $changed = 1;
194         } else {
195             push @todo, $topic;
196         }
197     }
198     redo unless $changed == 0;
199 }
200
201 for my $topic (@todo) {
202     $topic->setParent(0);
203     $lookup{$topic->Id} = Tree::Simple->new($topic, $tree);
204     push @results, "Reparented orphan ".$topic->Id." to root";
205 }
206
207 </%INIT>
208
209
210 <%ARGS>
211 $RootObj => undef
212 $title => ""
213 $Modify => ""
214 </%ARGS>
215