# ================================================================== # Plugins::DirectoryDepth - Auto Generated Program Module # # Plugins::DirectoryDepth # Author : Ian McGregor # Version : 0.0.1 # Updated : Tue May 14 13:18:14 2002 # # ================================================================== # package Plugins::DirectoryDepth; # ================================================================== use strict; use GT::Base; use GT::Plugins qw/STOP CONTINUE/; use Links qw/$CFG $IN $DB/; # Inherit from base class for debug and error methods @Plugins::DirectoryDepth::ISA = qw(GT::Base); # Your code begins here! Good Luck! # PLUGIN HOOKS # =================================================================== sub add_category { # ------------------------------------------------------------------- # This subroutine will get called whenever the hook 'add_category' # is run. You should call GT::Plugins->action ( STOP ) if you don't # want the regular code to run, otherwise the code will continue as # normal. # my $tags = shift; #determine directory depth for new category, FatherID is passed, as the category_id is not yet known. $tags->{Depth} = find_depth($tags->{FatherID}); return $tags; } sub move_category { # ------------------------------------------------------------------- # This subroutine will get called whenever the hook 'move_category' # is run. You should call GT::Plugins->action ( STOP ) if you don't # want the regular code to run, otherwise the code will continue as # normal. # my $tags = shift; #determine directory depth for new category, FatherID is passed, as the category_id is not yet known. $tags->{Depth} = find_depth($tags->{FatherID}); return $tags; } # MY SUBS FOR HOOKS # =================================================================== sub find_depth{ #Determine the directory depth for the current category &find_depth($category_id); my $category_id = shift; my $level = 0; my $FatherID = $DB->table('Category')->select( 'FatherID',{ 'ID' => $category_id})->fetchrow_array; # Loop until FatherID = 0, incrementing level each time, thus determining depth of category until ( $FatherID == 0) { my $FatherID = $DB->table('Category')->select( 'FatherID',{ 'ID' => $category_id})->fetchrow_array; $category_id = $FatherID; $level++; } return $level; } # ADMIN MENU OPTIONS # =================================================================== sub repair { # ------------------------------------------------------------------- # This subroutine will get called whenever the user clicks # on 'repair' in the admin menu. Remember, you need to print # your own content-type headers; you should use # print $IN->header(); # #---------------------------------------------------------------------------- my $l_cat = $DB->table('Category'); my @categories; my $level = 0; print $IN->header; # Immediate subcats my @category_level; my $sth = $l_cat->select ({FatherID => 0}); while (my $category = $sth->fetchrow_hashref) { push @category_level, $category; &update_cat($category,$level); } @categories[$level] = \@category_level; $level++; # Fetch all subcategories below while ($#{$categories[$level-1]} > 0) { my @category_level; my $num_cat_father_level = $#{$categories[$level-1]} + 1; for (my $i=0; $i < $num_cat_father_level; $i++) { my $father_id = ${$categories[$level-1]}[$i]->{ID}; my $sth = $l_cat->select ({FatherID => $father_id}); while (my $category = $sth->fetchrow_hashref) { push @category_level, $category; &update_cat($category,$level); } } @categories[$level] = \@category_level; $level++; } return; sub update_cat { # ---------------------------------------------------------------------------- my $category = shift; my $level = shift; my $table = $DB->table ('Category'); my $catdetails = { Depth => $level }; $table->update ( { Depth => $level}, { ID => $category->{ID} }) or die $GT::SQL::error; print $category->{Name} . ' has level ' . $level . "
"; return; } #------------------------------------------------------------------------------- } sub stats { # ------------------------------------------------------------------- # This subroutine will get called whenever the user clicks # on 'stats' in the admin menu. Remember, you need to print # your own content-type headers; you should use # print $IN->header(); # #print number of categories by directory depth - depth stats my $max_depth; my $num_cats; my $count; print "Calculating Depths...
"; $max_depth = $DB->table('Category')->select(['MAX(Depth)'])->fetchrow_array; if ($max_depth > 50) { print "Directory depth exceeds a depth of 50. Only the first 50 will be displayed.
"; $max_depth = 50; } for ($count=0; $count<=$max_depth; $count++) { my ($num_cats) = $DB->table('Category')->select(['COUNT(Depth)'],{ 'Depth' => $count})->fetchrow_array; print "Directory Depth: $count Number of Categories: $num_cats
"; } print "Done.
"; } # Always end with a 1. 1;