# # CatEntries 2.0 for Movable Type # Mark Paschal # 4 March 2003 # # Displays entries from the given categories. Properly shows N entries # when asked for lastn="N". # # To use: # 1. Put this catentries.pl in your Movable Type plugins directory. # 2. Modify your template to use the tag. The attributes # it recognizes are: # # lastn="N" As normal for MTEntries. # offset="N" # days="N" # # include="cat,cat,cat" Displays only items that are in at least # one of the given categories. If not # given, all entries, including those in # no category, will be shown. # exclude="cat,cat,cat" Omits items found in at least one of the # given categories. If not given, entries # in all included categories will be shown. # # Excludes are considered after includes, so 'include="Cats" exclude= # "Mice"' will *not* show entries posted to both Cats and Mice. # # # Versions: # 1.0, 23 February 2003 # 2.0, 4 March 2003: Rewritten quite a bit to support lastn properly. # Describes CatEntries in top comment instead of # RandomOTDEntry. # # # Copyright 2003 Mark Paschal # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # use POSIX qw(strftime); MT::Template::Context->add_container_tag(CatEntries => \&MTCatEntries); sub filterByCategories { ($entriesToFilter, $catsIn, $catsEx) = @_; my @entries = (); ENTRY: foreach my $e (@$entriesToFilter) { foreach my $cat (@$catsEx) { next ENTRY if $e->is_in_category($cat); }; if(@$catsIn) { foreach my $cat (@$catsIn) { if($e->is_in_category($cat)) { push @entries, $e; next ENTRY; }; }; } else { push @entries, $e; }; }; return @entries; } sub MTCatEntries { my ($ctx, $args) = @_; my $blog_id = $ctx->stash('blog_id'); my %entryTerms = ('blog_id' => $blog_id); my %entryArgs = ('sort' => 'created_on', 'direction' => 'descend'); # Cats my @catsIn = (); my @catsEx = (); @catsIn = map { MT::Category->load({ blog_id => $blog_id, label => $_ }, {}) } split /,/, $args->{'include'} if $args->{'include'}; @catsEx = map { MT::Category->load({ blog_id => $blog_id, label => $_ }, {}) } split /,/, $args->{'exclude'} if $args->{'exclude'}; # lastn or days? my @entries; if($args->{'lastn'}) { $entryArgs{'limit'} = $args->{'lastn'}; $entryArgs{'offset'} = $args->{'offset'} || 0; @entries = (); while(scalar(@entries) < $args->{'lastn'}) { my @newEntries = MT::Entry->load(\%entryTerms, \%entryArgs); break unless @newEntries; push @entries, filterByCategories(\@newEntries, \@catsIn, \@catsEx); $entryArgs{'offset'} += $entryArgs{'limit'}; }; while(scalar(@entries) > $args->{'lastn'}) { pop @entries; }; } elsif($args->{'days'}) { my @daysPast = localtime; $daysPast[3] -= $args->{'days'}; $entryTerms{'created_on'} = [strftime("%Y%m%d%H%M%S", @daysPast), strftime("%Y%m%d%H%M%S", localtime)]; $entryArgs{'range'} = {'created_on' => 1}; @someEntries = MT::Entry->load(\%entryTerms, \%entryArgs); @entries = filterByCategories(\@someEntries, \@catsIn, \@catsEx) if @someEntries; }; # Yay! my $ret = ""; my $builder = $ctx->stash('builder'); my $tokens = $ctx->stash('tokens'); foreach (@entries) { $ctx->{'__stash'}{'entry'} = $_; $ret .= $builder->build($ctx, $tokens) || return $ctx->error($builder->errstr); }; return $ret; }