package MT::Plugin::SKR::AltTemplatePreview; use strict; use MT 4; use MT::Template; use MT::TemplateMap; use MT::CMS::Entry; # preview use vars qw( $MYNAME $VERSION ); $MYNAME = 'AltTemplatePreview'; $VERSION = '0.01 DEVEL'; 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 => '', description => < PERLHEREDOC l10n_class => $MYNAME. '::L10N', }); MT->add_plugin ($plugin); sub instance { $plugin; } ### Registry sub init_registry { my $plugin = shift; $plugin->registry({ callbacks => { 'MT::App::CMS::template_source.edit_entry' => \&_hdlr_src_edit_entry, 'MT::App::CMS::template_param.edit_entry' => \&_hdlr_param_edit_entry, }, applications => { cms => { methods => { # Override the default handler preview_entry => \&_hdlr_preview_entry, }, }, }, }); } ### template_source.edit_entry sub _hdlr_src_edit_entry { my ($eh, $app, $tmpl) = @_; my $old = quotemeta (<<'HTMLHEREDOC'); HTMLHEREDOC my $new = <<'HTMLHEREDOC'; " label_class="top-label"> HTMLHEREDOC $$tmpl =~ s/($old)/$new$1/; } ### template_param.edit_entry sub _hdlr_param_edit_entry { my ($eh, $app, $param, $tmpl) = @_; my $blog_id = $param->{blog_id} or return; # never reach here my $type = $param->{object_type} || 'page'; my @preview_alt_tmpl; my $iter = MT::TemplateMap->load_iter ({ blog_id => $blog_id, archive_type => ( $type eq 'page' ? 'Page' : 'Individual' ), }); my %tmpl_map; while (my $tmplmap = $iter->()) { next if $tmpl_map{$tmplmap->template_id}++; my $tmpl = MT::Template->load ({ id => $tmplmap->template_id }) or next; # never reach here push @preview_alt_tmpl, { id => $tmpl->id, name => $tmpl->name, is_preferred => $tmplmap->is_preferred, }; } $param->{preview_alt_tmpl} = \@preview_alt_tmpl if @preview_alt_tmpl; } ### Override the default previewing method sub _hdlr_preview_entry { my ($app) = @_; my $blog_id = $app->param('blog_id'); my $type = $app->param('_type') || 'entry'; my $preview_alt_tmpl = $app->param('preview_alt_tmpl') || 0; # Save the current setting my $orig_tmpl_map = MT::TemplateMap->load ({ blog_id => $blog_id, archive_type => ( $type eq 'page' ? 'Page' : 'Individual' ), is_preferred => 1, }); my $orig_tmpl_map_id = $orig_tmpl_map && $orig_tmpl_map->id || 0; # Change temporally map { $_->is_preferred ($_->template_id == $preview_alt_tmpl); $_->update; } MT::TemplateMap->load ({ blog_id => $blog_id, archive_type => ( $type eq 'page' ? 'Page' : 'Individual' ), }) if $preview_alt_tmpl && $orig_tmpl_map_id; # Previewing my $html = MT::CMS::Entry::preview (@_); # Restore the original setting map { $_->is_preferred ($_->id == $orig_tmpl_map_id); $_->update; } MT::TemplateMap->load ({ blog_id => $blog_id, archive_type => ( $type eq 'page' ? 'Page' : 'Individual' ), }) if $preview_alt_tmpl && $orig_tmpl_map_id; $html; } 1;