« SubCategories API | Main | SubCategories Will Be Backwards Compatible »

May 26, 2004

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?

Posted by rayners at May 26, 2004 02:06 PM

Trackback Pings

TrackBack URL for this entry:
http://mt.rayners.org/mt-tb.cgi/299

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
  }
}

Posted by: jayseae at May 27, 2004 10:46 AM

That sounds perfectly reasonable to me. Would it be useful enough, you think, to put into the rayners::SubCategory class itself?

Posted by: rayners [TypeKey Profile Page] at May 27, 2004 11:26 AM

For me it would be useful. :)

Perhaps something like MT::Util::startendmonth, which returns a single value for a scalar request (immediate parent) and an array for an array request (all parents)?

Posted by: jayseae at May 31, 2004 04:20 PM

Post a comment




Remember Me?

(you may use HTML tags for style)