package MT::Plugin::SKR::EnableAssetOverSystem;

use strict;

use vars qw( $MYNAME $VERSION );
$MYNAME = (split /::/, __PACKAGE__)[-1];
$VERSION = '0.00_01';

use base qw( MT::Plugin );
my $plugin = __PACKAGE__->new ({
        id => $MYNAME,
        key => $MYNAME,
        name => $MYNAME,
        version => $VERSION,
        author_name => 'SKYARC System Co.,Ltd.',
        author_link => 'http://www.skyarc.co.jp/',
        doc_link => 'http://www.skyarc.co.jp/engineerblog/entry/enableassetoversystem.html',
        description => <<HTMLHEREDOC,
<__trans phrase="Enable asset types in custom fields over all the system.">
HTMLHEREDOC
        registry => {
            customfield_types => {
                file    => { context => 'all' },
                image   => { context => 'all' },
                video   => { context => 'all' },
                audio   => { context => 'all' },
            },
            callbacks => {
                'MT::App::CMS::template_param' => {
                    priority => 10,
                    code => \&_cb_template_param,
                },
            },
        },
});
MT->add_plugin ($plugin);

### Callbacks - MT::App::CMS::template_param
sub _cb_template_param {
    my ($cb, $app, $param) = @_;

    my $blog_id = $app->param ('blog_id')
        or return;;

    my @field_loop;
    foreach (@{$param->{field_loop}}) {
        if ($_->{field_id} && $_->{field_id} =~ /^customfield_/) {
            $_->{field_html} =~ s/(blog_id=)0/$1$blog_id/g;
        }
        push @field_loop, $_;
    }
    $param->{field_loop} = \@field_loop if @field_loop;
}

1;