
# 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;
<div class="setting">
<div class="field">
<p><MT_TRANS phrase="Display the"> <input value="<TMPL_VAR NAME=N_ENTRIES>" type="text" name="n_entries" id="n_entries" style="width:20px;" /> <MT_TRANS phrase="most recent entries for $article weblog on the Main Menu"></p>
</div> 
</div>
HTML
	return $html;
}

sub _template {
	my ($cb, $app, $template) = @_;
	my $old = qq{<a href="<TMPL_VAR NAME=SITE_URL>"><TMPL_VAR NAME=SITE_URL></a>};
	$old = quotemeta($old);
	my $new = <<HTML;
<a href="<TMPL_VAR NAME=SITE_URL>"><TMPL_VAR NAME=SITE_URL></a>
<TMPL_IF NAME=RECENT_ENTRIES></p><p><MT_TRANS phrase="Recent Entries:"><TMPL_LOOP NAME=RECENT_ENTRIES>
<a href="<TMPL_VAR NAME=SCRIPT_URL>?__mode=view&amp;_type=entry&amp;blog_id=<TMPL_VAR NAME=RECENT_ENTRY_BLOG_ID>&amp;id=<TMPL_VAR NAME=RECENT_ENTRY_ID>"><TMPL_VAR NAME=RECENT_ENTRY_TITLE></a><TMPL_UNLESS NAME=__LAST__>&nbsp;| </TMPL_UNLESS>
</TMPL_LOOP></TMPL_IF>
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;
