Guest User

Untitled

a guest
Feb 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. package MT::App::RateIt;
  2.  
  3. use strict;
  4. use base 'MT::App';
  5. use MT::Entry;
  6. use MT::PluginData;
  7. use MT::Util;
  8.  
  9. sub init {
  10. my $app = shift;
  11. $app->SUPER::init (@_) or return;
  12. $app->add_methods (
  13. vote => \&vote,
  14. );
  15. $app->{default_mode} = 'vote';
  16. $app->{plugin_key} = 'ejh_MTVote';
  17. $app;
  18. }
  19.  
  20. sub load_plugindata {
  21. my $app = shift;
  22. my ($key) = @_;
  23. my $data = MT::PluginData->load({
  24. plugin => $app->{plugin_key}, key => $key
  25. });
  26. return 0 unless $data;
  27. $data->data;
  28. }
  29.  
  30. sub save_plugindata {
  31. my $app = shift;
  32. my ($key, $data) = @_;
  33. my $plugindata = MT::PluginData->load({
  34. 'plugin' => $app->{plugin_key}, 'key' => $key
  35. });
  36. if (!$plugindata) {
  37. $plugindata = MT::PluginData->new;
  38. $plugindata->plugin($app->{plugin_key});
  39. $plugindata->key($key);
  40. }
  41. $plugindata->data($data);
  42. $plugindata->save || return 0;
  43. }
  44.  
  45. sub vote {
  46. my $app = shift;
  47. my $q = $app->{query};
  48. my $entry_id = $q->param('entry_id');
  49. my $path = MT::ConfigMgr->instance->CGIPath;
  50. $path =~ m|https?://(?:www\.)(.*?)/|;
  51. my $domain = $1;
  52. if (defined $app->cookies && exists $app->cookies->{$entry_id}) {
  53. return $app->redirect($ENV{'HTTP_REFERER'});
  54. }
  55.  
  56. my $entry = MT::Entry->load ($entry_id) or return
  57. $app->error($app->translate("Entry does not exist: [_1]", $entry_id));
  58.  
  59. my $data = $app->load_plugindata( $entry_id ) || {};
  60.  
  61. my $vote = $q->param('value');
  62. $vote = 1 if $vote < 1;
  63. $vote = 5 if $vote > 5;
  64. unless (exists $data->{ips}->{$app->remote_ip}) {
  65. $data->{votes}++;
  66. $data->{value} += $vote;
  67. $data->{ips}->{$app->remote_ip}++;
  68. $app->save_plugindata($entry_id, $data);
  69. my $config = $app->load_plugindata('configuration') || {};
  70. $config->{rebuild} = MT::ConfigMgr->instance->LaunchBackgroundTasks ? 1 : 0
  71. unless exists $config->{rebuild};
  72. if ($config->{rebuild} == 1) {
  73. MT::Util::start_background_task( sub {
  74. $app->rebuild_indexes( BlogID => $entry->blog_id )
  75. or return $app->error($app->translate(
  76. "Rebuild failed: [_1]", $app->errstr));
  77. $app->rebuild_entry( Entry => $entry )
  78. or return $app->error($app->translate(
  79. "Rebuild failed: [_1]", $app->errstr));
  80. });
  81. } elsif ($config->{rebuild} == 0) {
  82. $app->rebuild_indexes( BlogID => $entry->blog_id )
  83. or return $app->error($app->translate(
  84. "Rebuild failed: [_1]", $app->errstr));
  85. $app->rebuild_entry( Entry => $entry )
  86. or return $app->error($app->translate(
  87. "Rebuild failed: [_1]", $app->errstr));
  88. }
  89. }
  90. $app->bake_cookie(
  91. -name => "RateIt_" . $entry->id,
  92. -value => $vote,
  93. -expires => "+333M",
  94. -path => '/',
  95. -domain => $domain,
  96. );
  97. return $app->redirect($ENV{'HTTP_REFERER'});
  98. }
  99.  
  100. 1;
Add Comment
Please, Sign In to add comment