package MT::Plugin::SKR::SpeedySystemMenu; # SpeedySystemMenu - Add sub menus onto drop down menu of system overview. # Copyright (c) 2010 SKYARC System Co.,Ltd. # http://www.skyarc.co.jp/engineerblog/entry/speedysystemmenu.html use strict; use MT 5; use vars qw( $MYNAME $VERSION ); $MYNAME = 'SpeedySystemMenu'; $VERSION = '0.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/speedysystemmenu.html', description => < HTMLHEREDOC }); MT->add_plugin( $plugin ); sub instance { $plugin; } ### Registry sub init_registry { my $plugin = shift; $plugin->registry({ callbacks => { 'MT::App::CMS::template_source.header' => \&_hdlr_src_header, 'MT::App::CMS::template_param' => \&_hdlr_param, }, }); } ### template_source.header sub _hdlr_src_header { my ($cb, $app, $tmpl) = @_; ### HTMLHEREDOC $$tmpl =~ s/($old)/$new$1/; ### speedy_sys_menus addition $old = quotemeta (<<'HTMLHEREDOC'); ?blog_id=0&<$mt:var name="return_args" escape="html"$>"><__trans phrase="System Overview"> HTMLHEREDOC $new = <<'HTMLHEREDOC'; HTMLHEREDOC $$tmpl =~ s/($old)/$1$new/; } ### template_param.* sub _hdlr_param { my ($cb, $app, $param, $tmpl) = @_; my @speedy_sys_menus; my $menus = $app->registry ('menus'); my @topids = sort { $menus->{$a}->{order} <=> $menus->{$b}->{order} } grep { !/:/ } keys %$menus; foreach my $topid (@topids) { my @submenus; my $top_menu = $menus->{$topid}; my @subids = sort { $menus->{$a}->{order} <=> $menus->{$b}->{order} } grep { /^$topid:/ } keys %$menus; foreach my $subid (@subids) { my $sub_menu = $menus->{$subid}; my $has_system = 0; if (!exists $sub_menu->{view} || (exists $sub_menu->{display} && !$sub_menu->{display})) { # do nothing } elsif (ref $sub_menu->{view} eq 'CODE') { map { $has_system |= /^system$/ } @{$sub_menu->{view}->()}; } elsif (ref $sub_menu->{view} eq 'ARRAY') { map { $has_system |= /^system$/ } @{$sub_menu->{view}}; } else { $has_system |= $sub_menu->{view} =~ /^system$/; } next if !$has_system; my %query = ( __mode => $sub_menu->{mode}, $sub_menu->{args} ? %{$sub_menu->{args}} : (), ); $sub_menu->{query} = join '&', map { $_. '='. $query{$_} } keys %query; push @submenus, $sub_menu; } if (@submenus) { $top_menu->{sub_menus} = \@submenus; push @speedy_sys_menus, $top_menu; } } $param->{speedy_sys_menus} = \@speedy_sys_menus if @speedy_sys_menus; } 1;