Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MT::App::RateIt;
- use strict;
- use base 'MT::App';
- use MT::Entry;
- use MT::PluginData;
- use MT::Util;
- sub init {
- my $app = shift;
- $app->SUPER::init (@_) or return;
- $app->add_methods (
- vote => \&vote,
- );
- $app->{default_mode} = 'vote';
- $app->{plugin_key} = 'ejh_MTVote';
- $app;
- }
- sub load_plugindata {
- my $app = shift;
- my ($key) = @_;
- my $data = MT::PluginData->load({
- plugin => $app->{plugin_key}, key => $key
- });
- return 0 unless $data;
- $data->data;
- }
- sub save_plugindata {
- my $app = shift;
- my ($key, $data) = @_;
- my $plugindata = MT::PluginData->load({
- 'plugin' => $app->{plugin_key}, 'key' => $key
- });
- if (!$plugindata) {
- $plugindata = MT::PluginData->new;
- $plugindata->plugin($app->{plugin_key});
- $plugindata->key($key);
- }
- $plugindata->data($data);
- $plugindata->save || return 0;
- }
- sub vote {
- my $app = shift;
- my $q = $app->{query};
- my $entry_id = $q->param('entry_id');
- my $path = MT::ConfigMgr->instance->CGIPath;
- $path =~ m|https?://(?:www\.)(.*?)/|;
- my $domain = $1;
- if (defined $app->cookies && exists $app->cookies->{$entry_id}) {
- return $app->redirect($ENV{'HTTP_REFERER'});
- }
- my $entry = MT::Entry->load ($entry_id) or return
- $app->error($app->translate("Entry does not exist: [_1]", $entry_id));
- my $data = $app->load_plugindata( $entry_id ) || {};
- my $vote = $q->param('value');
- $vote = 1 if $vote < 1;
- $vote = 5 if $vote > 5;
- unless (exists $data->{ips}->{$app->remote_ip}) {
- $data->{votes}++;
- $data->{value} += $vote;
- $data->{ips}->{$app->remote_ip}++;
- $app->save_plugindata($entry_id, $data);
- my $config = $app->load_plugindata('configuration') || {};
- $config->{rebuild} = MT::ConfigMgr->instance->LaunchBackgroundTasks ? 1 : 0
- unless exists $config->{rebuild};
- if ($config->{rebuild} == 1) {
- MT::Util::start_background_task( sub {
- $app->rebuild_indexes( BlogID => $entry->blog_id )
- or return $app->error($app->translate(
- "Rebuild failed: [_1]", $app->errstr));
- $app->rebuild_entry( Entry => $entry )
- or return $app->error($app->translate(
- "Rebuild failed: [_1]", $app->errstr));
- });
- } elsif ($config->{rebuild} == 0) {
- $app->rebuild_indexes( BlogID => $entry->blog_id )
- or return $app->error($app->translate(
- "Rebuild failed: [_1]", $app->errstr));
- $app->rebuild_entry( Entry => $entry )
- or return $app->error($app->translate(
- "Rebuild failed: [_1]", $app->errstr));
- }
- }
- $app->bake_cookie(
- -name => "RateIt_" . $entry->id,
- -value => $vote,
- -expires => "+333M",
- -path => '/',
- -domain => $domain,
- );
- return $app->redirect($ENV{'HTTP_REFERER'});
- }
- 1;
Add Comment
Please, Sign In to add comment