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

Directory Depth Global

(Page 1 of 2)
> >
Quote Reply
Directory Depth Global
This is in reference to http://www.gossamer-threads.com/...orum.cgi?post=195612, but since this about a global I am posting here.

I just wrote my first global!!!Smile

This one prints out the number of categories with a directory depth of X.

Code:


sub {
#print number of categories by directory depth - depth stats

my $max_depth;
my $num_cats;
my $count;

my ($max_depth) = $DB->table('Category')->select(['MAX(Depth)'])->fetchrow_array;

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<br>";
}
}




The next step (which is way beyond me still) is to turn this and the perl module from the thread mentioned at the top of this page into a plugin which:
  • Assigns a category its directory depth when it is added or moved.
  • Creates a stats display under the Globals menu in admin which shows what my Global does
  • Has a function which does recalculates all directory depths, exactly as the above mentioned perl module does - for repair purposes, or after an import.


Most of this is already done, just how do I make this into a plug in?

Any help would be great, maybe I can move onto understand plugins a little more now!Smile
Quote Reply
Re: [sooke] Directory Depth Global In reply to
Congratulations.

As far as the plugin is concerned:

0. your install file needs to create the "Depth" column in the "Category" table.

1. you need to set PRE hook on "add_category" and "modify_category". The input will be a hashref of category fields. Output should also be a hashref of category fields, but now the hash should include a key "Depth".

2. your install can add the global you have to the globals automatically.

3. use the code I provided, and put it in a sub called "repair" in your plugin package. Add a menu that calls that function.

In general, try out the plugin wizard, it will do most of the things for you. Have a look at the Help pages, especially "Understanding Plugins", and "GT Module Documentation". You will find (almost) anything you need in there.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Thanks Ivan! That's a nice step by step guide for me to follow. I am going to give it a go first thing in the morning (its late here).

I'll post back here if I get stuck, if you don't mind. And thanks for letting me user your perl mod.

Smile
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Ok, step 0 underway:

Code:


Directory Depth Plugin

******************************************************

The Install File

*****************

unless (exists $DB->table('Category')->cols->{'Depth'}) {

my $editor = $DB->table ('Category');

unless ($editor->add_col ( 'Depth', { type => 'TEXT' }) {

$Plugins::Pluginname::error = "Unable to add column to Links: $GT::SQL::error";

return;

}

}




Do I have to consider the Form Display and Form Type fields when doing this?

How can I get this code to:

Form Display = "Directory Depth"
Form Type = Text

etc?

Last edited by:

sooke: May 13, 2002, 6:39 PM
Quote Reply
Re: [sooke] Directory Depth Global In reply to
Try something like:
Code:
# Add "Depth" column to Category table

my $editor = $DB->editor ('Category');

$editor->add_col ('depth', {
form_display => 'Category Depth',
form_size => '2',
form_type => 'INT',
not_null => '0',
default => '0'
type => 'INT' });

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Thanks Ivan,

That seems to make sense... I now have:



Code:


unless (exists $DB->table('Category')->cols->{'Depth'}) {

my $editor = $DB->editor ('Category');

unless (


$editor->add_col ('depth', {
form_display => 'Category Depth', form_size => '2',
form_type => 'INT',
not_null => '0', default => '0'
type => 'INT' });




)



{

$Plugins::Pluginname::error = "Unable to add column to Links: $GT::SQL::error";

return;

}

}
Quote Reply
Re: [sooke] Directory Depth Global In reply to
Does the first line work?

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
I am not sure.... how can I test at this stage (in-complete plugin)?

I copied the format from help_guide_plugins_install.html in the help manual.

Sorry if I sound lost, because I am!Wink
Quote Reply
Re: [sooke] Directory Depth Global In reply to
Ok, it will work. Haven't used that myself yet, but it's good to know...

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Yes, my understanding is that "if the table already exists don't write over it!"... which is good if you are upgrading or have to re-install. I am thinking of not putting in a table dump for the uninstall for this reason.

Onto step 1 now! Hooks...
Quote Reply
Re: [sooke] Directory Depth Global In reply to
Yes, I think you can just leave to field in the table when you uninstall.

If you want to be fancy, you can ask the user with a html select (in pre_uinstall) if they want to delecte the field or not. Then depending on their choice, you delete or not.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
You read my mind!

Ok... in the plugin wizard (step 3 with adding hooks)...

I just put:

add_category PRE Plugins::DirectoryDepth::add_category FIRST move_category PRE Plugins::DirectoryDepth::move_category FIRST

Plugin Wizard: Step 4 - Plugin Admin Menu for DirectoryDepth :

Name: repair
URL: admin.cgi?do=plugin&plugin=DirectoryDepth&func=repair

Step 5 is where I should put the option I guess.

Step 6 I skipped

Step 7 I put in my un-install code exactly as above

And when I hit finish I get an error.... so I then hit Perl Check and got:syntax error at Install.pm line 84, near "type"Install.pm had compilation errors.



Unable to load install file: Unable to load plugin DirectoryDepth Reason: Install.pm does not compile: syntax error at (eval 8) line 84, near "'0' type"
Quote Reply
Re: [sooke] Directory Depth Global In reply to
Put a comma after

default => '0'

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Ok did thatSmile

Now:
Code:
syntax error at Install.pm line 84, near ");"syntax error at Install.pm line 93, near "}"Install.pm had compilation errors.



Unable to load install file: Unable to load plugin DirectoryDepth Reason: Install.pm does not compile: syntax error at (eval 8) line 84, at EOF syntax error at (eval 8) line 93, near "} }"
Is it normal to get all these errors after using the wizard?
Quote Reply
Re: [sooke] Directory Depth Global In reply to
What I usually do is just let the wizard create a more or less empty plugin, possibly with all the hooks and menus, but with no install code or so. Like this, it will work for sure.

Then I add install code, etc etc. Then you can see what part of you install code causes errors...

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Good Idea.... I might do the wizard again, but without the install code. Thanks.
Quote Reply
Re: [sooke] Directory Depth Global In reply to
If you want to, you can PM me the plugin, and I will have a look at Install.pm for errors....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Thank Ivan... I REALLY appreciate your guidance through this plugin!

I did the bare bones plugin wizard and it compiled without error this time... I need to add hooks and install code now.... I am lost on what to do with the hooks to be honest...

When I put in my install code, this seems to cause the errorsCrazy



This is the install.pm (attachment) so far.

Last edited by:

sooke: May 13, 2002, 10:54 PM
Quote Reply
Re: [sooke] Directory Depth Global In reply to
You need to remove the semicolon ; after type =>'INT' }), then it should be OK.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Compiled now!! Thanks!Cool

Quote:
Name "Plugins::Pluginname::error" used only once: possible typo at Install.pm line 90.
Install.pm syntax OK

Quote Reply
Re: [sooke] Directory Depth Global In reply to
Do you have Pluginname or DirectoryDepth there? Should be the latter.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
I changed the error to Plugins::DirectoryDepth::error

and got:

Name "Plugins::DirectoryDepth::error" used only once: possible typo at Install.pm line 90.
Install.pm syntax OK


Must be something in that line.


EDIT: it looks to me like there are two many '}' brackets in this region:

}
return "The plugin has been successfully installed!";


Not sure...

Last edited by:

sooke: May 13, 2002, 11:18 PM
Quote Reply
Re: [sooke] Directory Depth Global In reply to
It's not a real problem. The perl syntax checker only tells you that you only used the variable once, so it thinks it could be a typo. But we know that you only use it once (correctly), and that there is no syntax error.

So everything is fine. Don't worry. Happy coding....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Directory Depth Global In reply to
Ok, thanks again for your help Ivan!
Quote Reply
Re: [sooke] Directory Depth Global In reply to
You're welcome. The thread might not be so interesting for others to read, though....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
> >