root/trunk/EntryCategoriesFiltered/entrycats_filtered.pl

Revision 63, 2.7 kB (checked in by jallen, 3 years ago)

Adding svn:keywords properties to all files

  • Property svn:keywords set to Author Date Id Revision
Line 
1# EntryCategoriesFiltered plugin for Movable Type
2# Author: Jay Allen, Six Apart (http://www.sixapart.com)
3# Released under the Artistic License
4#
5# $Id$
6
7# MTEntryCategoriesFiltered
8# MTEntryCategories...  But filtered
9#------------------------------------------------------------
10#
11# <MTEntryCategoriesFiltered exclude_regex="T.*" exclude_branch="Top" glue=",">
12#   <$MTCategoryLabel$>
13# </MTEntryCategoriesFiltered></p>
14#
15# exclude_regex -   Allows you to exclude by regex match on the Label
16# exclude_branch -  Allows you to exclude a branch of a subcategory
17#                   hierarchy by the parent's label.
18#
19#
20package MT::Plugin::entrycatsfilter;
21
22use strict;
23use vars qw($VERSION $plugin);
24
25use MT 3.2;
26@MT::Plugin::entrycatsfilter::ISA = ('MT::Plugin');
27use base 'MT::Plugin';
28
29
30$VERSION = '1.0';
31$plugin = new MT::Plugin::entrycatsfilter({
32    name => 'Filtered entry categories',
33    version => $VERSION,
34    description => '<MT_TRANS phrase="This plugin provides the a tag through which you can filter your entry categories listing.">',
35    author_name => 'Jay Allen',
36    author_link => 'http://www.jayallen.org'
37});
38
39
40MT->add_plugin($plugin);
41
42require MT::Template::Context;
43MT::Template::Context->add_container_tag('EntryCategoriesFiltered', 
44sub { _hdlr_entry_categories_filtered(@_); });
45
46sub _hdlr_entry_categories_filtered {
47    my($ctx, $args, $cond) = @_;
48
49    my $e = $ctx->stash('entry')
50        or return $ctx->_no_entry_error('MTEntryCategoriesFiltered');
51    my $cats = $e->categories;
52    return '' unless $cats && @$cats;
53
54    if ($args->{exclude_regex} ne '') {
55        $cats = exclude_regex($cats, $args->{exclude_regex});   
56    }
57
58    if ($args->{exclude_branch} ne '') {
59        $cats = exclude_branch($cats, $args->{exclude_branch});
60    }
61
62    my $builder = $ctx->stash('builder');
63    my $tokens = $ctx->stash('tokens');
64    my @res;
65    for my $cat (@$cats) {
66        local $ctx->{__stash}->{category} = $cat;
67        defined(my $out = $builder->build($ctx, $tokens, $cond))
68            or return $ctx->error( $builder->errstr );
69        push @res, $out;
70    }
71    my $sep = $args->{glue} || '';
72    join $sep, @res;
73}
74
75
76# Do something with array of categories -- this is just an example
77sub exclude_regex {
78    my ($cats,$filter) = @_;
79 
80    my @filtered;
81    foreach (@$cats) {
82        push(@filtered, $_) unless $_->label =~ m/$filter/;
83    }
84
85    \@filtered;
86}
87
88sub exclude_branch {
89    my ($cats, $exclude_label) = @_;
90
91    my $exclude_cat = MT::Category->load({ label => $exclude_label }) || undef;
92        return $cats if (!defined $exclude_cat);
93
94    my @filtered;
95    foreach (@$cats) {
96        push(@filtered, $_) unless $_->is_descendant($exclude_cat);
97    }
98
99    \@filtered;
100
101   
102}
Note: See TracBrowser for help on using the browser.