2011年8月 6日
Movable Type 5.1のブログ選択メニューをカスタマイズする
「Movable Type 5.1のブログ選択メニューについて」でブログ選択メニューの基本動作について紹介しましたが、本エントリーではブログ選択メニューに表示するウェブサイトやブログの数をカスタマイズする方法を紹介します。
このカスタマイズを行うことでブログ選択メニューに表示するウェブサイト・ブログ数を増やすことができます。
変更前
変更後
1.ブログ数表示のカスタマイズ
任意のエディタでlib/MT/App/CMS.pmを開き、以下の赤色部分を青色の内容に変更します。MT5.12であれば3100行目辺りにあります。
変更前
sub build_blog_selector {
my $app = shift;
my ($param) = @_;
…中略…
# Load favorites blogs
my @fav_blogs = @{ $auth->favorite_blogs || [] };
if ( scalar @fav_blogs > 5 ) {
@fav_blogs = @fav_blogs[ 0 .. 4 ];
}
# How many blogs that the user can access?
my %args;
my %terms;
$args{join}
= MT::Permission->join_on( 'blog_id',
{ author_id => $auth->id, permissions => { not => "'comment'" } } )
if !$auth->is_superuser
&& !$auth->permissions(0)->can_do('edit_templates');
$terms{class} = 'blog';
$terms{parent_id} = \">0"; # baka editors ";
$args{limit} = 6; # Don't load over 6 blogs
my @blogs = $blog_class->load( \%terms, \%args );
# Special case. If this user can access 5 blogs or smaller then load those blogs.
$param->{selector_hide_blog_chooser} = 1;
if ( @blogs && scalar @blogs == 6 ) {
# This user can access over 6 blogs.
if (@fav_blogs) {
@blogs = $blog_class->load( { id => \@fav_blogs } );
}
else {
@blogs = ();
}
$param->{selector_hide_blog_chooser} = 0;
}
…後略…
変更後
sub build_blog_selector {
my $app = shift;
my ($param) = @_;
…中略…
# Load favorites blogs
my @fav_blogs = @{ $auth->favorite_blogs || [] };
if ( scalar @fav_blogs > 10 ) {
@fav_blogs = @fav_blogs[ 0 .. 9 ];
}
# How many blogs that the user can access?
my %args;
my %terms;
$args{join}
= MT::Permission->join_on( 'blog_id',
{ author_id => $auth->id, permissions => { not => "'comment'" } } )
if !$auth->is_superuser
&& !$auth->permissions(0)->can_do('edit_templates');
$terms{class} = 'blog';
$terms{parent_id} = \">0"; # baka editors ";
$args{limit} = 10; # Don't load over 6 blogs
my @blogs = $blog_class->load( \%terms, \%args );
# Special case. If this user can access 5 blogs or smaller then load those blogs.
$param->{selector_hide_blog_chooser} = 1;
if ( @blogs && scalar @blogs == 10 ) {
# This user can access over 6 blogs.
if (@fav_blogs) {
@blogs = $blog_class->load( { id => \@fav_blogs } );
}
else {
@blogs = ();
}
$param->{selector_hide_blog_chooser} = 0;
}
…後略…
この変更ではウェブサイト管理画面・システム管理画面・ユーザーダッシュボードで最大10ブログ、ブログ管理画面で最大9ブログを表示するようにしています。
また、「ブログを選択」のテキストリンクをクリックしたときに適切な表示にするために、lib/MT/App/CMS/Blog.pmを開き、以下の赤色部分を青色の内容に変更します。MT5.12であれば3100行目辺りにあります。
変更前
sub dialog_select_weblog {
my $app = shift;
my $favorites = $app->param('select_favorites');
my %favorite;
my $confirm_js;
my $terms = {};
my $args = {};
my $auth = $app->user or return;
if ($favorites) {
my @favs = @{ $auth->favorite_blogs || [] };
if (@favs) {
@favs = @favs[ 0 .. 4 ] if scalar @favs > 5;
$terms->{id} = { not => \@favs };
}
$confirm_js = 'saveFavorite';
}
…後略…
変更後
sub dialog_select_weblog {
my $app = shift;
my $favorites = $app->param('select_favorites');
my %favorite;
my $confirm_js;
my $terms = {};
my $args = {};
my $auth = $app->user or return;
if ($favorites) {
my @favs = @{ $auth->favorite_blogs || [] };
if (@favs) {
@favs = @favs[ 0 .. 9 ] if scalar @favs > 10;
$terms->{id} = { not => \@favs };
}
$confirm_js = 'saveFavorite';
}
…後略…
2.ウェブサイト表示数のカスタマイズ
ウェブサイト表示数は、1項で変更した箇所の少し下にあります。赤色部分を青色の内容に変更します。
変更前
…前略…
# Load favorites or all websites
my @fav_websites = @{ $auth->favorite_websites || [] };
my @websites;
@websites = $website_class->load( { id => \@fav_websites } )
if scalar @fav_websites;
my $max_load = $blog && $blog->is_blog ? 3 : 4;
if ( scalar @fav_websites < $max_load ) {
# Load more accessible websites
%args = ();
%terms = ();
$args{join} = MT::Permission->join_on( 'blog_id',
{ author_id => $auth->id, permissions => { not => "'comment'" } }
)
if !$auth->is_superuser
&& !$auth->permissions(0)->can_do('edit_templates');
$terms{class} = 'website';
my $not_ids;
push @$not_ids, @fav_websites;
push @$not_ids, $blog->website->id if $blog && $blog->is_blog;
$terms{id} = { not => $not_ids } if scalar @$not_ids;
$args{limit} = $max_load
- ( scalar @fav_websites ); # Don't load over 3 ~ 4 websites.
my @sites = $website_class->load( \%terms, \%args );
push @websites, @sites;
}
if ( @fav_websites ) {
my $i;
my %sorted = map{ $_ => $i++ } @fav_websites;
foreach ( @websites ) {
$sorted{$_->id} = scalar @websites if !exists $sorted{$_->id};
}
@websites = sort { ( $sorted{$a->id} || 0 ) <=> ( $sorted{$b->id} || 0 ) } @websites;
}
unshift @websites, $blog->website if $blog && $blog->is_blog;
# Special case. If this user can access 3 websites or smaller then load those websites.
$param->{selector_hide_website_chooser} = 1;
if ( @websites && scalar @websites == 4 ) {
pop @websites;
$param->{selector_hide_website_chooser} = 0;
}
…後略…
変更後
…前略…
# Load favorites or all websites
my @fav_websites = @{ $auth->favorite_websites || [] };
my @websites;
@websites = $website_class->load( { id => \@fav_websites } )
if scalar @fav_websites;
my $max_load = $blog && $blog->is_blog ? 9 : 10;
if ( scalar @fav_websites < $max_load ) {
# Load more accessible websites
%args = ();
%terms = ();
$args{join} = MT::Permission->join_on( 'blog_id',
{ author_id => $auth->id, permissions => { not => "'comment'" } }
)
if !$auth->is_superuser
&& !$auth->permissions(0)->can_do('edit_templates');
$terms{class} = 'website';
my $not_ids;
push @$not_ids, @fav_websites;
push @$not_ids, $blog->website->id if $blog && $blog->is_blog;
$terms{id} = { not => $not_ids } if scalar @$not_ids;
$args{limit} = $max_load
- ( scalar @fav_websites ); # Don't load over 3 ~ 4 websites.
my @sites = $website_class->load( \%terms, \%args );
push @websites, @sites;
}
if ( @fav_websites ) {
my $i;
my %sorted = map{ $_ => $i++ } @fav_websites;
foreach ( @websites ) {
$sorted{$_->id} = scalar @websites if !exists $sorted{$_->id};
}
@websites = sort { ( $sorted{$a->id} || 0 ) <=> ( $sorted{$b->id} || 0 ) } @websites;
}
unshift @websites, $blog->website if $blog && $blog->is_blog;
# Special case. If this user can access 3 websites or smaller then load those websites.
$param->{selector_hide_website_chooser} = 1;
if ( @websites && scalar @websites == 9 ) {
pop @websites;
$param->{selector_hide_website_chooser} = 0;
}
…後略…
この変更ではシステム管理画面・ユーザーダッシュボードで最大5ウェブサイト、ウェブサイト管理画面で最大4ウェブサイトを表示するようにしています。
Comments [2]
| Trackbacks [0]