%# BEGIN LICENSE BLOCK %# %# Copyright (c) 2002-2003 Jesse Vincent %# %# This program is free software; you can redistribute it and/or modify %# it under the terms of version 2 of the GNU General Public License %# as published by the Free Software Foundation. %# %# A copy of that license should have arrived with this %# software, but in any event can be snarfed from www.gnu.org. %# %# This program 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. %# %# END LICENSE BLOCK <& /Admin/RTFM/Elements/ClassTabs, id => $ClassObj->id, Title => $title, current_tab => "Admin/RTFM/Classes/Topics.html?id=$id" &> <& /Elements/ListActions, actions => \@results &>
% if (!$Modify) {
<&|/l&>Topic Name
<&|/l&>Description
% } else { New topic % } <& .tree, Element => $tree, Action => $Modify ? "Move" : "Add", Prefix => $Modify ? "Topic-$Modify-Parent" : "Insert", ClassObj => $ClassObj, Modify => $Modify &>
<%def .edit>  


<%args> $topic <%def .tree> % my $topic = $Element->getNodeValue; % unless ($Element->isRoot) { % if ($Modify and $topic->Id == $Modify) { % $Action = ""; <& .edit, topic => $topic &> % } else { <%$topic->Name || loc("(no name)") %> % } % } <%args> $Element $Action $Prefix $ClassObj $Modify <%INIT> my $ClassObj = new RT::FM::Class($session{'CurrentUser'}); $ClassObj->Load($ARGS{'id'}) || $m->comp("/RTFM/Elements/Error", Why => "Couldn't load class '$id'"); my $title = $Modify ? loc("Modify topic for [_1]", $ClassObj->Name) : loc("Edit topic hierarchy for [_1]", $ClassObj->Name); my @results; for my $k (keys %ARGS) { if ($k =~ /^Topic-(\d+)-(Name|Description)/) { my $topic = RT::FM::Topic->new($session{'CurrentUser'}); $topic->Load($1); if ($topic->Id) { next if $ARGS{$k} eq $topic->$2; my $proc = "Set$2"; my ($val, $msg) = $topic->$proc($ARGS{$k}); push @results, $msg; } else { $m->comp("/Elements/Error", Why => loc("Topic not found")); } } elsif ($k =~ /^Topic-(\d+)-Parent-(\d+)/) { my $topic = RT::FM::Topic->new($session{'CurrentUser'}); $topic->Load($1); if ($topic->Id) { next if $2 eq $topic->Parent; my $old = $topic->Parent; my $new = "$2"; my ($val, $msg) = $topic->setParent($new); push @results, $msg; } else { $m->comp("/Elements/Error", Why => loc("Topic not found")); } } elsif ($k =~ /^Insert-(\d+)/) { my $topic = RT::FM::Topic->new($session{'CurrentUser'}); my ($id, $msg) = $topic->Create( Parent => $1, Name => $ARGS{'Name'}, Description => $ARGS{'Description'}, ObjectType => ref($ClassObj), ObjectId => $ClassObj->Id, ); push @results, $msg; } } for my $k (keys %ARGS) { next unless $k =~ /^Delete-Topic-(\d+)/; my $topic = RT::FM::Topic->new($session{'CurrentUser'}); $topic->Load($1); if ($topic->Id) { my ($val, $msg) = $topic->DeleteAll(); push @results, $msg; } else { $m->comp("/Elements/Error", Why => loc("Topic not found")); } } my $topics = new RT::FM::TopicCollection($session{'CurrentUser'}); $topics->LimitToObject($ClassObj); $topics->OrderByCols({FIELD => 'Parent'}, {FIELD => 'id'}); use Tree::Simple; my $tree = Tree::Simple->new(Tree::Simple->ROOT); my %lookup = (0 => $tree); my @todo; while (my $topic = $topics->Next) { push @todo, $topic; } { my $changed = 0; my @work = @todo; @todo = (); for my $topic (@work) { if (defined $lookup{$topic->Parent}) { $lookup{$topic->Id} = Tree::Simple->new($topic, $lookup{$topic->Parent}); $changed = 1; } else { push @todo, $topic; } } redo unless $changed == 0; } for my $topic (@todo) { $topic->setParent(0); $lookup{$topic->Id} = Tree::Simple->new($topic, $tree); push @results, "Reparented orphan ".$topic->Id." to root"; } <%ARGS> $id => undef $Modify => ""