Here is what I have come up with so far. Please let me know if there is anything more I can provide.
rayners::SubCategory will extend the MT::Category class and provide the following additional methods:
has_parent: Returns the id value of the parent (0 if the category has no parent)has_children: Returns true if the list of children for the category is definedchildren_categories: Returns the list of immediate children categories (i.e. only the children on the next level down) asrayners::SubCategoryobjectsis_ancestor: Returns true if the category is an ancestor of the given category (e.g. true if$parent->is_ancestor ($child))is_descendant: Returns true if the category is a descendant of the given category (e.g. true if$child->is_descendant ($parent))
Here is some Perl code that can be used to integrate this with a plugin:
my $category_class = "MT::Category";
my $subcategories_available = 0;
eval {
require rayners::SubCategory;
$category_class = "rayners::SubCategory";
$subcategories_available = 1;
};
my $cat = $category_class->load ($some_category_id);
Any thoughts, questions, or concerns?
I'm wondering about the best way to loop through to find all parents for a given category. For instance:
Would do (I think) to get the immediate parent. But what would be the best way to keep looping over that construct to build an entire list of parents? Perhaps something like this?
my @parents; my $load_parent; my $save_parent = $some_category_id; while $save_parent { $load_parent = $category_class->load ($save_parent); if $load_parent { push @parents, $load_parent; $save_parent = $load_parent } }That sounds perfectly reasonable to me. Would it be useful enough, you think, to put into the rayners::SubCategory class itself?
For me it would be useful. :)
Perhaps something like MT::Util::start_end_month, which returns a single value for a scalar request (immediate parent) and an array for an array request (all parents)?