# AcceptCommentsPings is a plugin that allows you to switching entries
# 'Accept Comments/TrackBacks' status from plugin action menu in entries listing page.
#
# Release 1.4 (20051015)
#
# Author:  yosshi <yosshi at greenplastic.net>
# License: same as Perl
#

package MT::Plugin::AcceptCommentsPings;
use strict;
use MT;
use MT::Blog;
use MT::Entry;
use base qw( MT::Plugin );

sub BEGIN {
    MT->add_plugin(MT::Plugin::AcceptCommentsPings->new({
	name => 'AcceptCommentsPings',
	description => 'Accept or unaccept comments/pings.',
	doc_link => 'http://fake.greenplastic.net/2005/09/acceptcommentspings_plugin.php',
	version => '1.4',
	author_name => 'yosshi',
	author_link => 'http://profile.typekey.com/yosshi/',
    }));
}

sub init_app {
    my $plugin = shift;
    my($app) = @_;
    return unless $app->isa('MT::App::CMS');
    $app->add_itemset_action({
        type  => 'entry',
        key   => 'accept_comments',
        label => 'AcceptComments',
        code  => sub { action_accept_comments($plugin, @_) },
        condition => sub { $plugin->perm_check($app) },
    });
    $app->add_itemset_action({
        type  => 'entry',
        key   => 'unaccept_comments',
        label => 'UnacceptComments',
        code  => sub { action_unaccept_comments($plugin, @_) },
        condition => sub { $plugin->perm_check($app) },
    });
    $app->add_itemset_action({
        type  => 'entry',
        key   => 'accept_pings',
        label => 'AcceptPings',
        code  => sub { action_accept_pings($plugin, @_) },
        condition => sub { $plugin->perm_check($app) },
    });
    $app->add_itemset_action({
        type  => 'entry',
        key   => 'unaccept_pings',
        label => 'UnAcceptPings',
        code  => sub { action_unaccept_pings($plugin, @_) },
        condition => sub { $plugin->perm_check($app) },
    });
    $app->add_itemset_action({
        type  => 'entry',
        key   => 'accept_comments_pings',
        label => 'AcceptCommentsPings',
        code  => sub { action_accept_comments_pings($plugin, @_) },
        condition => sub { $plugin->perm_check($app) },
    });
    $app->add_itemset_action({
        type  => 'entry',
        key   => 'unaccept_comments_pings',
        label => 'UnacceptCommentsPings',
        code  => sub { action_unaccept_comments_pings($plugin, @_) },
        condition => sub { $plugin->perm_check($app) },
    });
}

sub perm_check {
    my $plugin = shift;
    my ($app) = @_;
    my $perms = $app->{perms};
    $perms ? $perms->can_edit_templates : $app->user->is_superuser;
}

sub action_accept_comments {
    my $plugin = shift;
    my($app) = @_;
    my @rebuild_list;
    my @ids = $app->param('id');
    my $blog_id = $app->param('blog_id');
foreach my $id (@ids) {
    my $entry = MT::Entry->load($id, {cached_ok=>1});
    push @rebuild_list, $entry;
    $entry->allow_comments(1);
    $entry->save;
}
    $app->rebuild_entry(Entry => $_, BuildDependencies => 1)
        foreach @rebuild_list;
    $app->call_return;
}

sub action_unaccept_comments {
    my $plugin = shift;
    my($app) = @_;
    my @rebuild_list;
    my @ids = $app->param('id');
    my $blog_id = $app->param('blog_id');
foreach my $id (@ids) {
    my $entry = MT::Entry->load($id, {cached_ok=>1});
    push @rebuild_list, $entry;
    $entry->allow_comments(0);
    $entry->save;
}
    $app->rebuild_entry(Entry => $_, BuildDependencies => 1)
        foreach @rebuild_list;
    $app->call_return;
}

sub action_accept_pings {
    my $plugin = shift;
    my($app) = @_;
    my @rebuild_list;
    my @ids = $app->param('id');
    my $blog_id = $app->param('blog_id');
foreach my $id (@ids) {
    my $entry = MT::Entry->load($id, {cached_ok=>1});
    push @rebuild_list, $entry;
    $entry->allow_pings(1);
    $entry->save;
}
    $app->rebuild_entry(Entry => $_, BuildDependencies => 1)
        foreach @rebuild_list;
    $app->call_return;
}

sub action_unaccept_pings {
    my $plugin = shift;
    my($app) = @_;
    my @rebuild_list;
    my @ids = $app->param('id');
    my $blog_id = $app->param('blog_id');
foreach my $id (@ids) {
    my $entry = MT::Entry->load($id, {cached_ok=>1});
    push @rebuild_list, $entry;
    $entry->allow_pings(0);
    $entry->save;
}
    $app->rebuild_entry(Entry => $_, BuildDependencies => 1)
        foreach @rebuild_list;
    $app->call_return;
}

sub action_accept_comments_pings {
    my $plugin = shift;
    my($app) = @_;
    my @rebuild_list;
    my @ids = $app->param('id');
    my $blog_id = $app->param('blog_id');
foreach my $id (@ids) {
    my $entry = MT::Entry->load($id, {cached_ok=>1});
    push @rebuild_list, $entry;
    $entry->allow_comments(1);
    $entry->allow_pings(1);
    $entry->save;
}
    $app->rebuild_entry(Entry => $_, BuildDependencies => 1)
        foreach @rebuild_list;
    $app->call_return;
}

sub action_unaccept_comments_pings {
    my $plugin = shift;
    my($app) = @_;
    my @rebuild_list;
    my @ids = $app->param('id');
    my $blog_id = $app->param('blog_id');
foreach my $id (@ids) {
    my $entry = MT::Entry->load($id, {cached_ok=>1});
    push @rebuild_list, $entry;
    $entry->allow_comments(0);
    $entry->allow_pings(0);
    $entry->save;
}
    $app->rebuild_entry(Entry => $_, BuildDependencies => 1)
        foreach @rebuild_list;
    $app->call_return;
}

1;
