Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Directory Depth Global

(Page 2 of 2)
> >
Quote Reply
Re: [yogi] Directory Depth Global In reply to
You could be right.... but there are some real good basics covered here for those who are clueless when it comes to writing their very first plugin... so I am sure someone will get value from it (besides myself).

Smile

Last edited by:

sooke: May 13, 2002, 11:23 PM
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Yogi,

Each time a category is moved or added, it needs to be assigned its directory depth. So it needs a function to do this in the hooks. I am guessing, but it looks like this is the part of your module which would do this (see below), and then I would just call the (sub update_cat also from your module) minus the print statement to effect this in the database?

In your pm module, the code:

Code:
sub calculate_depth {
# calculate directory depth for one category only my $l_cat = $DB->table('Category'); my @categories;
my $level = 0; print $IN->header; # I don't need this in here for this function do I?# 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++;



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;

return;
}




I hope I am not way offCrazy
Quote Reply
Re: [sooke] Directory Depth Global In reply to
Yes, in the functions that are called for the PRE add_link and PRE modify_link hooks, you need to calculate the new directory depth of a category.

I would probably be better to do it different from the script I gave you, since you don't need to calculate the depth of every single category, only of one.

You could write a seperate function that calculates the depth of a category, where the input would be its FatherID.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Ok, I am attempting my first perl subs here. This one is meant to be called by the add and move hooks for determining the actual category depth.

Would someone mind looking at this for possible errors (as I am not sure how to test until full routines are completed).

Code:


sub {

#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 father = 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; #just edited this

$level++;


}

return $level;

}

Last edited by:

sooke: May 14, 2002, 1:39 PM
> >