package MT::Plugin::SKR::SearchIncludeBlogsLimiter;
########################################################################
#   SearchIncludeBlogsLimiter
#           Copyright (c) 2007 Skyarc System Co.,Ltd.
#           @see http://www.skyarc.co.jp/
########################################################################
use strict;

use vars qw( $MYNAME $VERSION );
$MYNAME = 'SearchIncludeBlogsLimiter';
$VERSION = '1.00';

use base qw( MT::Plugin );
my $plugin = new MT::Plugin ({
        name => $MYNAME,
        version => $VERSION,
        author_name => q{ SKYARC System Co.,Ltd. },
        author_link => q{ http://www.skyarc.co.jp },
        doc_link => qw{ http://www.skyarc.co.jp/ },
        description => <<HTMLHEREDOC,
Force limiter to avoid the searching through the private blogs.
HTMLHEREDOC
});
MT->add_plugin( $plugin );

sub instance { $plugin }



use MT::Template::Context;
MT::Template::Context->add_container_tag (SearchIncludeBlogsLimiter => \&search_include_blogs_limiter);
sub search_include_blogs_limiter {
    my ($ctx, $args, $cond) = @_;

    # Limit the retrieved results
    my $include_blogs = $args->{IncludeBlogs};
    if ($include_blogs) {
        my %IncludeBlogs = map { $_ => 1 } split /\s*,\s*/, $include_blogs;

        # This extension is supplied for search result template.
        my $results = $ctx->stash ('results');
        if ($results) {
            my @res = grep { $IncludeBlogs{$_->{blog}->id} } @{ $results };

            # Replace with modified results
            $ctx->stash ('results', \@res);
        }
    }

    # Pass throught
    my $builder = $ctx->stash ('builder');
    my $tokens = $ctx->stash ('tokens');
    defined (my $out = $builder->build ($ctx, $tokens, $cond))
        or return $ctx->error ($builder->errstr);
    $out;
}

1;
__END__
########################################################################
# '07/12/28 1.00    create: by pirolix
#                   check: MT153 OK by pirolix
