Gossamer Forum
Quote Reply
Help with Hook
    

How do I set $category_id to equal the category_id of the category which was just added, so I can use it in my hook below? Is it something I get from args? Sorry if this sounds like a dumb question.... I know args contains stuff, but just not sure if it has this, and how I get it out of it!Blush

Code:


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 (@args) = @_;

my $category_id = shift;

#determine directory depth for new category

my $catdepth = &find_depth($category_id);

#update the depth to the categories 'Depth' Column

&update_catdepth ($category_id, $catdepth);

return @args;

}

Last edited by:

sooke: May 14, 2002, 2:01 PM
Quote Reply
Re: [sooke] Help with Hook In reply to
Try printing @_ to see what it contanis :) ...otherwise you'll need to do a select.

Last edited by:

Paul: May 15, 2002, 2:39 AM
Quote Reply
Re: [sooke] Help with Hook In reply to
You can do

my $tags = shift;

instead of my (@args) = @_;, because the input to this hook is a hashref with all the category information.

Further, you want to calculate the depth using the FatherID of the category, because when you add a new category, the ID of this new category is assigned only when it is saved, but FatherID is know before it is saved. You can then use the same code for adding a category and modifying a category.

So, your code for add_category could be
Code:
sub add_category {
my $tags = shift;
$tags->{Depth} = find_depth($tags->{FatherID});
return $tags;
}

All the information that is returned in $tags will be saved, so you don't have to manually update the Depth field.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Help with Hook In reply to
Wow! ThanksSmile

You guys are practically writing this plugin for meAngelic. But at least this way I am learning how to do it myself!
Quote Reply
Re: [yogi] Help with Hook In reply to
Ok, here is my find_depth sub: It accepts the father_id, even though it is named category_id hereCrazy

Code:


sub {

#Determine the directory depth for the current category &find_depth($Father_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;

}

Last edited by:

sooke: May 15, 2002, 10:15 AM
Quote Reply
Re: [yogi] Help with Hook In reply to
Paul or Ivan (or anyone),

I just tried the plugin with the add and move hooks, and links just 'hangs' when I try to add a category. Endless loop?

Would you mind having a look?

Last edited by:

sooke: May 15, 2002, 11:09 AM
Quote Reply
Re: [sooke] Help with Hook In reply to
I guess its something to do with:

until ( $FatherID == 0) {
Quote Reply
Re: [Paul] Help with Hook In reply to
Do you think the "my" in:

until ( $FatherID == 0) {
my $FatherID = $DB->table('Category')->select( 'FatherID',{ 'ID' => $category_id})->fetchrow_array;
$category_id = $FatherID;
$level++;
}

should be inside a loop? is it even necessary?

I have not used the "until" before... maybe I should use a while or for next instead.
Quote Reply
Re: [sooke] Help with Hook In reply to
You might want to add in some code to prevent eternal loops.

You already have $level++ so add something like:

if ($level > 25) {
exit the loop
}
Quote Reply
Re: [Paul] Help with Hook In reply to
You read my mind.

I added:

until ( ($FatherID == 0) || ($level > 25)) {


Edit:

I stand corrected, it assigned a depth of 26 in this case, so did not work.Unsure

Food.... I need coffee!

Last edited by:

sooke: May 15, 2002, 1:12 PM
Quote Reply
Re: [sooke] Help with Hook In reply to
Code:
while (1) {
{
my $father = $DB->table('Category')->select( { ID => $category_id }, 'FatherID' )->fetchrow;
($father != 0 && $level < 25) and $category_id = $father, $level++, redo;
}
last;
}

How about that....just a guess.....I need food.

Last edited by:

Paul: May 15, 2002, 1:43 PM
Quote Reply
Re: [Paul] Help with Hook In reply to
Ok, Paul your version worked (of course!), except it is one out... it assigned 2 instead of three... so I still need to check logic, or just add one to $level at the end!



Edit: ok, putting $level++; seems to work...

so add works, I dont see hooks for modify_category or move_category in the manual. I put these into my module... but don't work yet.

Thanks Paul, hope you are enjoying your dinner!

Last edited by:

sooke: May 15, 2002, 1:29 PM
Quote Reply
Re: [sooke] Help with Hook In reply to
Its because you have $level set to 0, use:

my $level = 1;

Top-Level is level one, hence you need to start at 1.
Quote Reply
Re: [Paul] Help with Hook In reply to
Ahhh.... yes!

Any thoughts on why the same code wont work for modify_category or move_category subs? I am assuming those hooks are correctly named.



EDIT: disgregard, I got it working!!!!!!! Thanks Paul.

Last edited by:

sooke: May 15, 2002, 1:53 PM
Quote Reply
Re: [sooke] Help with Hook In reply to
Well, other than a few imporvements, and add-ons, this plug in is done!

I just want to thank everyone who helped me write my first plugin!

Watch out, sooke is dangerous nowWink