# SimpleComments 1.1mp # # Copyright 2002 Kalsey Consulting Group # http://kalsey.com/ # # 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. # # Modified to allow use outside an entry context # Mark Paschal 18 February 2003 use MT::Template::Context; MT::Template::Context->add_container_tag(SimpleComments => sub { &SimpleComments; }); MT::Template::Context->add_tag(SimpleCommentCount => sub { &SimpleCommentCount; }); MT::Template::Context->add_container_tag(SimpleCommentIfTrackback => sub { &SimpleCommentIfTrackback; }); MT::Template::Context->add_container_tag(SimpleCommentIfComment => sub { &SimpleCommentIfComment; }); sub SimpleComments { my($ctx, $args) = @_; my $e = $ctx->stash('entry'); my $blog_id = $ctx->stash('blog_id'); my($tb, $cat); my $so = $args->{sort_order} || $ctx->stash('blog')->sort_order_comments; my $limit = $args->{lastn} ? $args->{lastn} : ''; my %args = ('sort' => 'created_on', 'direction' => $so); if($limit) { $args{direction} = 'descend'; $args{limit} = $limit; }; my (@comments, @pings); require MT::Trackback; require MT::TBPing; if($e) { @comments = @{$e->comments}; $tb = MT::Trackback->load({ entry_id => $e->id }); # MT::Trackback is how you convert entry IDs to TB IDs. if ($tb) { my %terms; $terms{tb_id} = $tb->id if $tb; $terms{blog_id} = $blog_id; @pings = MT::TBPing->load(\%terms, \%args); }; } else { @comments = MT::Comment->load({ blog_id => $blog_id }, \%args); @pings = MT::TBPing->load({ blog_id => $blog_id }, \%args); }; my @allComments = (@comments, @pings); return '' unless scalar(@allComments); @allComments = $so eq 'ascend' ? sort { $a->created_on <=> $b->created_on } @allComments : sort { $b->created_on <=> $a->created_on } @allComments; if ($limit) { my $max = $args->{lastn} > scalar(@allComments) ? scalar(@allComments)-1 : $args->{lastn}-1; @allComments = $so eq 'ascend' ? @allComments[-$max-1..-1] : @allComments[0..$max]; } my $builder = $ctx->stash('builder'); my $tokens = $ctx->stash('tokens'); my $res = ''; for my $c (@allComments) { $ctx->stash('comment' => $c); $ctx->stash('ping' => $c); local $ctx->{current_timestamp} = $c->created_on; my $out = $builder->build($ctx, $tokens); return $ctx->error( $builder->errstr ) unless defined $out; $res .= $out; } $res; } sub SimpleCommentCount { my $e = $_[0]->stash('entry') or return $_[0]->_no_entry_error('MTEntryTrackbackCount'); $e->ping_count + $e->comment_count; } sub SimpleCommentIfComment { my $ctx = shift; my $comment = $ctx->stash('comment') or return $ctx->error('MTSimpleCommentIfComment must be used inside a SimpleComments container'); return '' if (!defined($comment->text) || length($comment->text)==0); my $builder = $ctx->stash('builder'); my $tokens = $ctx->stash('tokens'); defined(my $out = $builder->build($ctx,$tokens)) or return ''; return $out; } sub SimpleCommentIfTrackback { my $ctx = shift; my $comment = $ctx->stash('comment') or return $ctx->error('MTSimpleCommentIfTrackback must be used inside a SimpleComments container'); return '' if (!defined($comment->source_url) && !defined($comment->title)); my $builder = $ctx->stash('builder'); my $tokens = $ctx->stash('tokens'); defined(my $out = $builder->build($ctx,$tokens)) or return ''; return $out; }