
# IfModified.pl
# Movable Type plugin tag for displaying whether an entry has been modified
# by Kevin Shay
# http://www.staggernation.com/mtplugins/
# last modified February 23, 2005

package MT::Plugin::IfModified;
use strict;
use vars qw( $VERSION );
$VERSION = '1.4';

use MT;
use MT::Template::Context;
use MT::Util qw( format_ts );

eval{ require MT::Plugin };
unless ($@) {
    my $plugin = {
        name => "IfModified $VERSION",
        description => 'Display conditionally based on whether or not an entry was modified since it was created.',
        doc_link => 'http://www.staggernation.com/mtplugins/IfModified'
    }; 
    MT->add_plugin(new MT::Plugin($plugin));
}

MT::Template::Context->add_conditional_tag('IfModified' => sub{&_hdlr_if_modified;});

sub _hdlr_if_modified {
	my ($ctx, $args) = @_;
	my $e = $ctx->stash('entry')
		or return $ctx->_no_entry_error('MTLastModified');
	my $display;
	my $modified = $e->modified_on;
	$modified =~ s/[^0-9]//g;
	if (my $leeway = $args->{'leeway'}) {
		$leeway *= 60;
		my $diff = ts_to_local($modified) - ts_to_local($e->created_on);
		$display = ($diff > $leeway);
	} else {
		$display = ($modified ne $e->created_on);
	} 
	return $display;
}

sub ts_to_local {
	my ($ts) = @_;
	require Time::Local;
	import Time::Local qw(timelocal);
	my ($y, $m, $d, $h, $min, $s) = unpack('A4A2A2A2A2A2', $ts);
	$m--;
	return timelocal($s, $min, $h, $d, $m, $y);
}

1;
