# # CPEntries 0.9.2 for Movable Type by yu-ji # # Displays entries only have comments or pings. Properly shows N entries # when asked for lastn="N". # # To use: # 1. Put this CPEntries.pl in your Movable Type plugins directory. # 2. Modify your template to use the tag. The attributes # it recognizes are: # # days="N" The number of days to display. # offset="N" The number of entries (with 'lastn') or days (with # 'days') to skip before counting entries to display. # lastn="N" By itself or with 'offset,' the number of entries # to display. With 'days,' the number of entries at # most to display. This means you can write: # # to get the last three entries posted a week ago. # # History: # 0.9.2, 2004/07/17 fix to sort by created_on # 0.9.1, 2004/07/08 fix to show only same blog_id # 0.9.0, 2004/06/08 first release # # Copyright (C) yyyy name of author # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # package MT::Plugin::CPEntries; use vars qw( $VERSION ); $VERSION = 0.9.1; use POSIX qw(strftime); use MT::Template::Context; MT::Template::Context->add_container_tag(CPEntries => \&MTCPEntries); MT::Template::Context->add_container_tag(CPEntry => \&MTCPEntry); sub sortedEntryByCommentPing { my ($terms, $args) = @_; require MT::Comment; require MT::Trackback; require MT::TBPing; my @c = MT::Comment->load( { blog_id => $terms->{blog_id} }, { 'range' => $args->{'range'}, 'sort' => $args->{'sort'}, 'direction' => $args->{'direction'} } ); my @p = MT::TBPing->load( { blog_id => $terms->{blog_id} }, { 'range' => $args->{'range'}, 'sort' => $args->{'sort'}, 'direction' => $args->{'direction'} } ); my @t = (); foreach my $pi (@p) { my $tb = MT::Trackback->load($pi->tb_id); push @t, $tb; } my @ca = (@c,@t); my @sca = $args->{'direction'} eq 'ascend' ? sort { $a->created_on <=> $b->created_on } @ca : sort { $b->created_on <=> $a->created_on } @ca; my %entries_id = (); my @entries = (); my $limit = 0; foreach my $cp (@sca) { my $entry_id = $cp->entry_id; if(exists($entries_id{$entry_id}) == 0){ my $e = MT::Entry->load($entry_id); if($e->status eq $terms->{status}){ push @entries, $e; $entries_id{$entry_id} = 1; } }; }; return @entries; } sub MTCPEntries { my ($ctx, $args) = @_; my $blog_id = $ctx->stash('blog_id'); my %entryTerms = (blog_id => $blog_id, status => 2); # 2 == Publish my %entryArgs = ('sort' => $args->{'sort_by'} || 'created_on', 'direction' => $args->{'sort_order'} || $ctx->stash('blog')->sort_order_comments); my @entries = (); # days if($args->{'days'}) { my $daysOffset = $args->{'offset'} || 0; my @daysPast = localtime(time - 86400 * ($args->{'days'} + $daysOffset)); my @offsetNow = localtime(time - 86400 * $daysOffset); $entryTerms{'created_on'} = [strftime("%Y%m%d%H%M%S", @daysPast), strftime("%Y%m%d%H%M%S", @offsetNow)]; $entryArgs{'range'} = {'created_on' => 1}; @entries = sortedEntryByCommentPing(\%entryTerms, \%entryArgs); if($args->{'lastn'}) { while(scalar(@entries) > $args->{'lastn'}) { pop @entries; }; }; # lastn } elsif($args->{'lastn'}) { my $offset = $args->{'offset'} || 0; $entryArgs{'limit'} = $args->{'lastn'} + $offset; @entries = sortedEntryByCommentPing(\%entryTerms, \%entryArgs); while($offset--) { shift @entries; }; while(scalar(@entries) > $args->{'lastn'}) { pop @entries; }; } else { @entries = sortedEntryByCommentPing(\%entryTerms, \%entryArgs); }; my $builder = $ctx->stash('builder'); my $tokens = $ctx->stash('tokens'); my $res = ''; my $ordernumber = 1; for my $e (@entries) { $ctx->stash('_cpentry_order' => $ordernumber++); $ctx->stash('cpentry' => $e); local $ctx->{current_timestamp} = $e->created_on; my $out = $builder->build($ctx, $tokens); return $ctx->error( $builder->errstr ) unless defined $out; $res .= $out; }; $res; } sub MTCPEntry{ my $ctx = shift; my $entry = $ctx->stash('cpentry') or return $ctx->error('MTCPEntry must be used inside a MTCPEntries container'); local $ctx->{__stash}{entry} = $entry; local $ctx->{current_timestamp} = $entry->created_on; $ctx->stash('builder')->build($ctx, $ctx->stash('tokens')); } 1;