# # CatEntries 1.0 for Movable Type # Mark Paschal # 23 February 2003 # # Shows a random entry from the current day in some other year. The same # historical entry is shown no matter how many times you rebuild your # templates. # # 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 # # # 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 MTCatEntries { my ($ctx, $args) = @_; my $blog_id = $ctx->stash('blog_id'); my %entryTerms = ('blog_id' => $blog_id); my %entryArgs = ('sort' => 'created_on', 'direction' => 'descend'); # lastn or days? if($args->{'lastn'}) { $entryArgs{'limit'} = $args->{'lastn'}; $entryArgs{'offset'} = $args->{'offset'} if $args->{'offset'}; } 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}; }; # Cats my @catsIn = (); my @catsEx = (); @catsIn = split /,/, $args->{'include'} if $args->{'include'}; @catsEx = split /,/, $args->{'exclude'} if $args->{'exclude'}; # Get the entries. my @entries = MT::Entry->load(\%entryTerms, \%entryArgs); # OK, let's go. my @included = (); my %categories = (); if(@catsIn) { foreach my $e (@entries) { my $isIn = 0; foreach my $cat (@catsIn) { $categories{$cat} = MT::Category->load({ blog_id => $blog_id, label => $cat }, {}) unless $categories{$cat}; if($e->is_in_category($categories{$cat})) { $isIn = 1; last; }; }; push @included, $e if $isIn; }; } else { @included = @entries; }; my @notExcluded = (); if(@catsEx) { foreach my $e (@included) { my $isIn = 0; foreach my $cat (@catsEx) { $categories{$cat} = MT::Category->load({ blog_id => $blog_id, label => $cat }, {}) unless $categories{$cat}; if($e->is_in_category($categories{$cat})) { $isIn = 1; last; }; }; push @notExcluded, $e unless $isIn; }; } else { @notExcluded = @included; }; # Yay! my $ret = ""; my $builder = $ctx->stash('builder'); my $tokens = $ctx->stash('tokens'); foreach (@notExcluded) { $ctx->{'__stash'}{'entry'} = $_; $ret .= $builder->build($ctx, $tokens) || return $ctx->error($builder->errstr); }; return $ret; }