Proposed SubCategories API

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 defined
  • children_categories: Returns the list of immediate children categories (i.e. only the children on the next level down) as rayners::SubCategory objects
  • is_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?

3 Comments

I'm wondering about the best way to loop through to find all parents for a given category. For instance:

my $parent = $category_class->has_parent ($some_category_id);

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)?

Leave a comment

Recent Entries

  • Plugin Writing 101

    So, I’m planning on writing up some entries about various aspects of plugin development. While I have a couple topics already in mind, I thought...

  • The Olympics Make Me Want To Compete Again

    Every time the Olympics come around (most the summer ones) I always start to delude myself into thinking I could complete once again in the...

  • Minimalist plugins are fun!

    Last night I whipped up one of the smallest plugins I’ve ever written. It is so small in fact that I was able to stuff...

  • Feedburner Widget on MT News

    Movable Type News A WordPress 2.5 Upgrade Guide: And of course there are lots of third-party plugins for the MT dashboard, to integrate statistics and...

  • AD&D Monster Stats for the Presidential Canditates

    Charles Stross (scifi author, D&D nerd, and former perl columnist) posted Politics as she is Played with 3d6: The recent death of Gary Gygax, who...

Close