# EntryCategoriesFiltered plugin for Movable Type # Author: Jay Allen, Six Apart (http://www.sixapart.com) # Released under the Artistic License # # $Id$ # MTEntryCategoriesFiltered # MTEntryCategories... But filtered #------------------------------------------------------------ # # # <$MTCategoryLabel$> #

# # exclude_regex - Allows you to exclude by regex match on the Label # exclude_branch - Allows you to exclude a branch of a subcategory # hierarchy by the parent's label. # # package MT::Plugin::entrycatsfilter; use strict; use vars qw($VERSION $plugin); use MT 3.2; @MT::Plugin::entrycatsfilter::ISA = ('MT::Plugin'); use base 'MT::Plugin'; $VERSION = '1.0'; $plugin = new MT::Plugin::entrycatsfilter({ name => 'Filtered entry categories', version => $VERSION, description => '', author_name => 'Jay Allen', author_link => 'http://www.jayallen.org' }); MT->add_plugin($plugin); require MT::Template::Context; MT::Template::Context->add_container_tag('EntryCategoriesFiltered', sub { _hdlr_entry_categories_filtered(@_); }); sub _hdlr_entry_categories_filtered { my($ctx, $args, $cond) = @_; my $e = $ctx->stash('entry') or return $ctx->_no_entry_error('MTEntryCategoriesFiltered'); my $cats = $e->categories; return '' unless $cats && @$cats; if ($args->{exclude_regex} ne '') { $cats = exclude_regex($cats, $args->{exclude_regex}); } if ($args->{exclude_branch} ne '') { $cats = exclude_branch($cats, $args->{exclude_branch}); } my $builder = $ctx->stash('builder'); my $tokens = $ctx->stash('tokens'); my @res; for my $cat (@$cats) { local $ctx->{__stash}->{category} = $cat; defined(my $out = $builder->build($ctx, $tokens, $cond)) or return $ctx->error( $builder->errstr ); push @res, $out; } my $sep = $args->{glue} || ''; join $sep, @res; } # Do something with array of categories -- this is just an example sub exclude_regex { my ($cats,$filter) = @_; my @filtered; foreach (@$cats) { push(@filtered, $_) unless $_->label =~ m/$filter/; } \@filtered; } sub exclude_branch { my ($cats, $exclude_label) = @_; my $exclude_cat = MT::Category->load({ label => $exclude_label }) || undef; return $cats if (!defined $exclude_cat); my @filtered; foreach (@$cats) { push(@filtered, $_) unless $_->is_descendant($exclude_cat); } \@filtered; }