The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::Tree::NestedSet::Manage

SYNOPSIS

A CGI::Application and HTML::Template based helper class that provides an interface to DBIx::Tree::NestedSet methods.

DESCRIPTION

The idea of this module is that you subclass it and add your own cgiapp_prerun(), denied(), and cgiapp_postrun() methods. You should probably tweak the add_child_form() and delete_node() methods too to include the metadata you want in your tree.

confirm_node_can_be_deleted() should be overridden too, it's used to "confirm" whether or not a node can be deleted without messing up your database. Returning a true value means the node is OK to delete.

See the "templates", "cgi-bin", and "graphics" directories of this distribution for an example HTML::Template, graphics (thank you to WebGUI) and an instance script.

Example Module:

 package My::NestedSetTree;
 use base 'DBIx::Tree::NestedSet::Manage';
 use strict;

 sub cgiapp_prerun{
     #Controls access to this module.
     my $self=shift;
     if ($self->access_not_allowed()) {
        $self->prerun_mode('denied');
     } else {
        return;
     }
 }


 sub denied{
     #Content returned if a user isn't allowed to access this module
     return 'Access is denied.';
 }


 sub cgiapp_postrun {
     #HTML content to "wrap around" this module.
     my $self = shift;
     my $output_ref = shift;
     
     my $new_output = "<html><head><title>My Tree</title></head><body>";
     $new_output .= $$output_ref;
     $new_output .= "</body></html>";
     
     # Replace old output with new output
     $$output_ref = $new_output;
 }

 sub confirm_node_can_be_deleted{
     #You should customize this method to check for your own
     #criteria as to what nodes may be deleted.
     my $self=shift;
     my $dbh=$self->param('dbh');
     my $q=$self->query();
     my $tree=$self->param('tree');
     my $nodes=$tree->get_self_and_children_flat(id=>$q->param('id'));
     my @ids=map{$dbh->quote($_->{id})} @$nodes;
     my $id_sql=join(',',@ids);
     #Check to see if we have any documents assigned to this category.
     my ($count)=$dbh->selectrow_array(qq|select count(*) from doc_categories where primary_cat in($id_sql)|);
     #If there's a positive count, we can't delete.
     return ($count) ? 0 : 1 ;
 }


 1;

SEE ALSO

CGI::Application, HTML::Template and DBIx::Tree::NestedSet.

AUTHOR

Dan Collis Puro, Geekuprising.com. Email: dan at geekuprising dot com.

This model was inspired by the perlmonks.org thread below:

http://www.perlmonks.org/index.pl?node_id=354049

See "Tilly's" response in particular. I'm "Hero Zzyzzx".

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.