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).
May 14, 2002, 8:59 AM
Veteran / Moderator (2577 posts)
May 14, 2002, 8:59 AM
Post #27 of 29
Views: 2609
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:
# 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 off
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 off
May 14, 2002, 9:08 AM
Veteran / Moderator (2199 posts)
May 14, 2002, 9:08 AM
Post #28 of 29
Views: 2705
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
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
May 14, 2002, 1:05 PM
Veteran / Moderator (2577 posts)
May 14, 2002, 1:05 PM
Post #29 of 29
Views: 2585
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).
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;
}
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;
}