# MainMenuRecent.pl
# MainMenuRecent plugin for Movable Type
# by Kevin Shay
# http://www.staggernation.com/mtplugins/MainMenuRecent/
use strict;
package MT::Plugin::MainMenuRecent;
use base qw(MT::Plugin);
use vars qw( $VERSION );
$VERSION = '1.1';
my $n_entries = 3;
require MT::Plugin;
require MT;
my $settings;
eval {
$settings = new MT::PluginSettings([
['n_entries', { Default => $n_entries }]
]);
};
my $plugin = MT::Plugin::MainMenuRecent->new({
name => "MainMenuRecent",
description => 'Display the latest entries for each weblog on your Main Menu.',
doc_link => 'http://www.staggernation.com/mtplugins/MainMenuRecent/',
author_name => 'Kevin Shay',
author_link => 'http://www.staggernation.com/',
version => $VERSION,
config_template => \&_config_template,
settings => $settings
});
MT->add_plugin($plugin);
MT->add_callback('MT::App::CMS::AppTemplateSource.list_blog', 9, $plugin, \&_template);
MT->add_callback('MT::App::CMS::AppTemplateParam.list_blog', 9, $plugin, \&_param);
sub _config_template {
my ($plugin, $param, $scope) = @_;
my $html = '';
my $article = ($scope eq 'system') ? 'each' : 'this';
$html .= <
HTML
return $html;
}
sub _template {
my ($cb, $app, $template) = @_;
my $old = qq{};
$old = quotemeta($old);
my $new = <
|
HTML
$$template =~ s/$old/$new/;
}
sub _param {
my ($cb, $app, $param) = @_;
for my $blog (@{$param->{'blog_loop'}}) {
$blog->{'recent_entries'} = [];
my $config = $plugin->blog_settings($blog->{'id'});
for my $e (MT::Entry->load({ 'blog_id' => $blog->{'id'} },
{ 'sort' => 'created_on',
direction => 'descend',
limit => $config->{'n_entries'} })) {
push(@{$blog->{'recent_entries'}}, {
'recent_entry_blog_id', $blog->{'id'},
'recent_entry_title' => $e->title
|| first_n_words($e->text, 5, '...') || '...',
'recent_entry_id' => $e->id
});
}
}
}
sub first_n_words {
# enhanced version of the MT::Util first_n_words() routine
my($text, $n, $append) = @_;
$append ||= '';
$text = MT::Util::remove_html($text);
my @words = split(/\s+/, $text);
if (@words > $n) {
my $max = $n;
$text = (join ' ', @words[0..$max-1]);
$text =~ s/\W+$//;
$text .= $append;
}
return $text;
}
sub apply_default_settings {
my ($plugin, $data, $scope_id) = @_;
if ($scope_id eq 'system') {
return $plugin->SUPER::apply_default_settings($data, $scope_id);
} else {
my $sys;
for my $setting (@{$plugin->{'settings'}}) {
my $key = $setting->[0];
next if exists($data->{$key});
# don't load system settings unless we need to
$sys ||= $plugin->get_config_obj('system')->data;
$data->{$key} = $sys->{$key};
}
}
}
sub blog_settings {
my $plugin = shift;
my ($blog_id) = @_;
return $plugin->get_config_hash('blog:' . $blog_id);
}
1;