package MT::Plugin::HandSort;
use base qw(MT::Plugin);
use strict;
use vars qw( $VERSION );
$VERSION = '1.0b3';
require MT;
my $handsort = MT::Plugin::HandSort->new({
'settings' => new MT::PluginSettings([
['enabled', { Default => 0 }],
['warning', { Default => 0 }],
['warning_msg', { Default => 'Reordering entries will change their Authored On dates. Proceed?' }],
['rebuild', { Default => 1 }]
])
});
MT->add_plugin($handsort);
sub name { 'HandSort' }
sub description { 'Manually change the order of your entries.' }
sub doc_link { 'http://www.staggernation.com/mtplugins/HandSort/' }
sub author_name { 'Kevin Shay' }
sub author_link { 'http://www.staggernation.com/' }
sub version { $VERSION }
sub config_template { &_config_template }
MT->add_callback('MT::App::CMS::AppTemplateSource.list_entry', 9, $handsort,
\&list_entry_template);
MT->add_callback('MT::App::CMS::AppTemplateSource.entry_actions', 9, $handsort,
\&entry_actions_template);
MT->add_callback('MT::App::CMS::AppTemplateSource.entry_table', 9, $handsort,
\&entry_table_template);
MT->add_callback('MT::App::CMS::AppTemplateParam.list_entry', 9, $handsort,
\&list_entry_param);
# cache settings to avoid repeated loading
my $blog_settings = {};
sub init_app {
my ($plugin, $app) = @_;
return unless $app->isa('MT::App::CMS');
$app->add_methods(
'handsort_reorder' => sub { reorder_entries($plugin, @_) }
);
}
sub perm_check {
my ($app) = @_;
return $app->{'perms'} ? $app->{'perms'}->can_edit_all_posts
: $app->user->is_superuser;
}
sub enabled {
# is HandSort enabled/permitted for the current blog?
my ($plugin, $app) = @_;
return 0 unless perm_check($app);
my $settings = $plugin->blog_settings($app->{'query'}->param('blog_id'));
return 0 unless $settings->{'enabled'};
return 1;
}
sub _config_template {
return <
checked="checked" />
checked="checked" />
checked="checked" />
HTML
}
sub reorder_entries {
my ($plugin, $app) = @_;
my $q = $app->{'query'};
my $blog_id = $q->param('blog_id');
my $n = $q->param('handsort_entry_count');
my @old = ();
my @new = ();
my $settings = $handsort->blog_settings($app->{'query'}->param('blog_id'));
if ($q->param('handsort_reverse_field')) {
for my $i (1 .. $n) {
unshift(@new, { 'id' => $q->param("handsort_entry_${i}_id") });
}
} else {
for my $i (1 .. $n) {
my $id = $q->param("handsort_entry_${i}_id");
my $pos = $q->param("handsort_entry_${id}");
# not a number: move to end of list
$pos =~ s/[^\d]+//g;
$pos = $n + 1 unless $pos;
push(@old, { 'id' => $id, 'pos' => $pos });
}
my $oldpos = 0;
for my $oldentry (@old) {
$oldpos++;
my $newpos = $oldentry->{'pos'};
my $where = 0;
for my $new (@new) {
last if ($new->{'pos'} > $newpos);
if ($new->{'pos'} == $newpos) {
$where++ if ($newpos > $oldpos);
last;
}
$where++;
}
splice(@new, $where, 0, $oldentry);
}
}
my $start_time = time - ($n + 1);
my $i = 0;
use MT::Util qw( offset_time_list );
for my $new (reverse @new) {
$i++;
my @ts = offset_time_list($start_time + $i, $blog_id);
my $ts = sprintf "%04d%02d%02d%02d%02d%02d",
$ts[5]+1900, $ts[4]+1, @ts[3,2,1,0];
my $entry = MT::Entry->load($new->{'id'});
if ($entry) {
$entry->created_on($ts);
$entry->save || return $app->error($app->translate(
"Saving entry '[_1]' failed: [_2]", $entry->title,
$entry->errstr));
}
}
my $redirect_param;
if ($settings->{'rebuild'}) {
# save all first with new date/time, then rebuild
for my $new (@new) {
$app->rebuild_entry(Entry => $new->{'id'}, BuildDependencies => 1);
}
$redirect_param = 'handsorted_rebuilt';
} else {
$redirect_param = 'handsorted';
}
$app->redirect($app->uri . '?' . $app->{'return_args'} . "&$redirect_param=1");
}
sub list_entry_template {
my ($cb, $app, $template) = @_;
$$template =~ s/tableSelect.rowSelect = true;//;
my $old = q{};
$old = quotemeta($old);
my $new = <
EOH
$$template =~ s/$old/$new/;
}
sub entry_table_template {
my ($cb, $app, $template) = @_;
return 0 unless $handsort->enabled($app);
my $old = qq{
};
$old = quotemeta($old);
$new = <
Sort
HTML
$$template =~ s/$old/$new/;
}
sub entry_actions_template {
my ($cb, $app, $template) = @_;
return 0 unless $handsort->enabled($app);
my $old = qq{" />};
$old = quotemeta($old);
my $new = <" />
" title="" />
" title="" />
HTML
$$template =~ s/$old/$new/;
}
sub list_entry_param {
my ($cb, $app, $param) = @_;
return 0 unless $handsort->enabled($app);
my $pos = 0;
for my $row (@{$param->{'entry_table'}[0]{'object_loop'}}) {
$pos++;
$row->{'handsort_pos'} = $pos;
}
my $settings = $handsort->blog_settings($param->{'blog_id'});
$param->{'handsort_entry_count'} = $pos;
# not showing all entries
if (($param->{'list_start'} > 1)
|| ($param->{'list_end'} < $param->{'list_total'})) {
$param->{'handsort_not_all'} = 1;
}
$param->{'handsort_warning'} = $settings->{'warning'};
$param->{'handsort_warning_msg'} = $settings->{'warning_msg'};
if ($app->{'query'}->param('handsorted')) {
$param->{'handsorted'} = 1;
} elsif ($app->{'query'}->param('handsorted_rebuilt')) {
$param->{'handsorted_rebuilt'} = 1;
}
}
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) = @_;
$blog_settings->{$blog_id} ||=
$plugin->get_config_hash('blog:' . $blog_id);
return $blog_settings->{$blog_id};
}
1;