# ODP::Tree - An ODP public page tree handling class (Version 0.01) # Copyright (C)2002-2004 Richard P. Fuller # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # 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. # 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA package ODP::Tree; use strict; use ODP::Category; use UNIVERSAL qw(isa); # new - Initialises a new ODP::Tree object # Parameters: category # Returns: ODP::Tree object sub new ($) { my $object = {}; if (!$_[1]) { die 'No category supplied.'; } my $textcategory; if (isa($_[1], 'ODP::Category')) { $textcategory = $_[1]->{'category'}; } else { $textcategory = ODP::Category::normalise($_[1]); } $object->{'root'} = new ODP::Category($textcategory); return bless $object; } # expand_fully - Expands an ODP::Tree object fully # Parameters: # Returns: sub expand_fully { my $self = shift; my $n=-1; foreach my $item ($self->{'root'}->subcats_plain()) { $n++; $self->{'branches'}[$n] = new ODP::Tree (($self->{'root'}->{'category'})."/$item"); $self->{'branches'}[$n] -> expand_fully; } } # linearlist_string - Returns a list of all categories found in a tree as a multi-line string # Parameters: Ignore root element? (Boolean) # Returns: String (multi-line) containing a list of all categories found in the tree sub linearlist_string($) { my $self = shift; my @branches; if ($self->{'branches'}) { @branches = @{$self->{'branches'}}; } my $return; if (!$_[0]){$return = $self->{'root'}->{'category'}."\n"} foreach my $branch (@branches) { $return .= $branch->linearlist_string; } return $return; } # linearlist - Returns an array of ODP::Category objects of all categories found in a tree # Parameters: Ignore root element? (Boolean) # Returns: Array of ODP::Category objects of all categories found in the tree sub linearlist ($) { my $self = shift; my @branches; if ($self->{'branches'}) { @branches = @{$self->{'branches'}}; } my @return; if (!$_[0]){push @return, $self->{'root'}}; foreach my $branch (@branches) { push @return, $branch->linearlist; } return @return; } # sitecount - Returns the number of sites calculated by adding up values of subcats # Parameters: # Returns: site count (integer) sub sitecount () { my $self = shift; my $count = 0; my %count; foreach my $category ($self->linearlist) { $count{$category->{'category'}} = $category->sitecount(); } foreach my $category ($self->linearlist) { $count += $count{$category->{'category'}}; if ($category->subcats()) { foreach my $subcat ($category->subcats()) { $count -= $count{$subcat->{'category'}}; } } } return $count; } 1;