Advertisement
Guest User

class.thankfulpeople.plugin.php

a guest
May 7th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.19 KB | None | 0 0
  1. <?php if (!defined('APPLICATION')) exit();
  2.  
  3. $PluginInfo['ThankfulPeople'] = array(
  4.         'Name' => 'Thankful People',
  5.         'Description' => 'Remake of classic Vanilla One extension. Instead of having people post appreciation and thankyou notes they can simply click the thanks link and have their username appear under that post (MySchizoBuddy). Modifications by hgtonight, update using mods by Tama63',
  6.         'Version' => '2.14.2.0.18.x',
  7.         'Date' => 'Summer 2011',
  8.         'Author' => 'Jerl Liandri',
  9.         'AuthorUrl' => 'http://www.liandri-mining-corporation.com',
  10.         'RequiredApplications' => array('Vanilla' => '>=2.1b1'),
  11.         'RequiredTheme' => False,
  12.         'RequiredPlugins' => False,
  13.         'License' => 'X.Net License'
  14. );
  15.  
  16. // TODO: PERMISSION THANK FOR CATEGORY
  17. // TODO: AttachMessageThankCount
  18.  
  19. class ThankfulPeoplePlugin extends Gdn_Plugin {
  20.        
  21.         protected $ThankForComment = array(); // UserIDs array
  22.         protected $CommentGroup = array();
  23.         protected $DiscussionData = array();
  24.         private $Session;
  25.  
  26.         public function __construct() {
  27.                 $this->Session = Gdn::Session();
  28.         }
  29.        
  30. /*  public function DiscussionController_AfterCommentMeta_Handler(&$Sender) {
  31.                 $this->AttachMessageThankCount($Sender);
  32.         }
  33.        
  34.         protected function AttachMessageThankCount($Sender) {
  35.                 $ThankCount = mt_rand(1, 33);
  36.                 echo '<div class="ThankCount">'.Plural($Posts, 'Thanks: %s', 'Thanks: %s')), number_format($ThankCount, 0)).'</div>';
  37.         }
  38.         */
  39.        
  40.         public function PluginController_UnThankFor_Create($Sender) {
  41.                 $SessionUserID = GetValue('UserID', Gdn::Session());
  42.                 if ($SessionUserID > 0 && C('Plugins.ThankfulPeople.AllowTakeBack', False)) {
  43.                         $ThanksLogModel = new ThanksLogModel();
  44.                         $Type = GetValue(0, $Sender->RequestArgs);
  45.                         $ObjectID = GetValue(1, $Sender->RequestArgs);
  46.                         $ThanksLogModel->RemoveThank($Type, $ObjectID, $SessionUserID);
  47.                         if ($Sender->DeliveryType() == DELIVERY_TYPE_ALL) {
  48.                                 $Target = GetIncomingValue('Target', 'discussions');
  49.                                 Redirect($Target);
  50.                         }
  51.                         $ThankfulPeopleDataSet = $ThanksLogModel->GetThankfulPeople($Type, $ObjectID);
  52.                         $Sender->SetData('NewThankedByBox', self::ThankedByBox($ThankfulPeopleDataSet->Result(), False));
  53.                         $Sender->Render();
  54.                 }
  55.         }
  56.        
  57.         public function PluginController_ThankFor_Create($Sender) {
  58.                 $Session = $this->Session;
  59.                 if (!$Session->IsValid()) return;
  60.                 //$Sender->Permission('Plugins.ThankfulPeople.Thank'); // TODO: PERMISSION THANK FOR CATEGORY
  61.                 $ThanksLogModel = new ThanksLogModel();
  62.                 $Type = GetValue(0, $Sender->RequestArgs);
  63.                 $ObjectID = GetValue(1, $Sender->RequestArgs);
  64.                 $Field = $ThanksLogModel->GetPrimaryKeyField($Type);
  65.                 $UserID = $ThanksLogModel->GetObjectInserUserID($Type, $ObjectID);
  66.                 if ($UserID == False) throw new Exception('Object has no owner.');
  67.                 if ($UserID == $Session->UserID) throw new Exception('You cannot thank yourself.');
  68.                 if (!self::IsThankable($Type)) throw new Exception("Not thankable ($Type).");
  69.                
  70.                 // Make sure that user is not trying to say thanks twice.
  71.                 $Count = $ThanksLogModel->GetCount(array($Field => $ObjectID, 'InsertUserID' => $Session->User->UserID));
  72.                 if ($Count < 1) $ThanksLogModel->PutThank($Type, $ObjectID, $UserID);
  73.                
  74.                 if ($Sender->DeliveryType() == DELIVERY_TYPE_ALL) {
  75.                         $Target = GetIncomingValue('Target', 'discussions');
  76.                         Redirect($Target);
  77.                 }
  78.                
  79.                 $ThankfulPeopleDataSet = $ThanksLogModel->GetThankfulPeople($Type, $ObjectID);
  80.                 $Sender->SetData('NewThankedByBox', self::ThankedByBox($ThankfulPeopleDataSet->Result(), False));
  81.                 $Sender->Render();
  82.         }
  83.        
  84.         public function DiscussionController_Render_Before($Sender) {
  85.                 if (!($Sender->DeliveryType() == DELIVERY_TYPE_ALL && $Sender->SyndicationMethod == SYNDICATION_NONE)) return;
  86.                 $ThanksLogModel = new ThanksLogModel();
  87.                 $DiscussionID = $Sender->DiscussionID;
  88.                 // TODO: Permission view thanked
  89.                 $CommentIDs = ConsolidateArrayValuesByKey($Sender->CommentData->Result(), 'CommentID');
  90.                 $DiscussionCommentThankDataSet = $ThanksLogModel->GetDiscussionComments($DiscussionID, $CommentIDs);
  91.                
  92.                 // TODO: FireEvent here to allow collect thanks from other objects
  93.                
  94.                 // Consolidate.
  95.                 foreach ($DiscussionCommentThankDataSet as $ThankData) {
  96.                         $CommentID = $ThankData->CommentID;
  97.                         if ($CommentID > 0) {
  98.                                 $this->CommentGroup[$CommentID][] = $ThankData;
  99.                                 $this->ThankForComment[$CommentID][] = $ThankData->UserID;
  100.                         } elseif ($ThankData->DiscussionID > 0) {
  101.                                 $this->DiscussionData[$ThankData->UserID] = $ThankData;
  102.                         }
  103.                 }
  104.                
  105.                 $Sender->AddJsFile('jquery.expander.js');
  106.                 $Sender->AddCssFile('plugins/ThankfulPeople/design/thankfulpeople.css');
  107.                 $Sender->AddJsFile('plugins/ThankfulPeople/js/thankfulpeople.functions.js');
  108.                
  109.                 $Sender->AddDefinition('ExpandThankList', T(' Show More'));
  110.                 $Sender->AddDefinition('CollapseThankList', T(' Hide'));
  111.         }
  112.        
  113.         public static function IsThankable($Type) {
  114.                 static $ThankOnly, $ThankDisabled;
  115.                 $Type = strtolower($Type);
  116.                 if (is_null($ThankOnly)) $ThankOnly = C('Plugins.ThankfulPeople.Only');
  117.                 if (is_array($ThankOnly)) {
  118.                         if (!in_array($Type, $ThankOnly)) return False;
  119.                 }
  120.                 if (is_null($ThankDisabled)) $ThankDisabled = C('Plugins.ThankfulPeople.Disabled');
  121.                 if (is_array($ThankDisabled)) {
  122.                         if (in_array($Type, $ThankDisabled)) return False;
  123.                 }
  124.                 return True;
  125.         }
  126.        
  127.         public function DiscussionController_AfterCommentMeta_Handler($Sender) {
  128.                 $EventArguments =& $Sender->EventArguments;
  129.                 $Type = $EventArguments['Type'];
  130.                 $Object = $EventArguments['Object'];
  131.                 //$Session = Gdn::Session();
  132.                 $SessionUserID = $this->Session->UserID;
  133.                 if ($SessionUserID <= 0 || $Object->InsertUserID == $SessionUserID) return;
  134.                
  135.                 if (!self::IsThankable($Type)) return;
  136.                
  137.                 static $AllowTakeBack;
  138.                 if (is_null($AllowTakeBack)) $AllowTakeBack = C('Plugins.ThankfulPeople.AllowTakeBack', False);
  139.                 $AllowThank = True;
  140.                
  141.                 switch ($Type) {
  142.                         case 'Discussion': {
  143.                                 $DiscussionID = $ObjectID = $Object->DiscussionID;
  144.                                 if (array_key_exists($SessionUserID, $this->DiscussionData)) $AllowThank = False;
  145.                                 break;
  146.                         }
  147.                         case 'Comment': {
  148.                                 $CommentID = $ObjectID = $Object->CommentID;
  149.                                 if (array_key_exists($CommentID, $this->ThankForComment) && in_array($SessionUserID, $this->ThankForComment[$CommentID])) $AllowThank = False;
  150.                                 break;
  151.                         }
  152.                 }
  153.                
  154.        
  155.                 if ($AllowThank) {
  156.                         static $LocalizedThankButtonText;
  157.                         if ($LocalizedThankButtonText === Null) $LocalizedThankButtonText = T('ThankCommentOption', T('Thank'));
  158.                         $ThankUrl = 'plugin/thankfor/'.strtolower($Type).'/'.$ObjectID.'?Target='.$Sender->SelfUrl;
  159.                         $Option = '<span class="Thank">'.Anchor($LocalizedThankButtonText, $ThankUrl).'</span>';
  160.                         echo $Option;
  161.                         //$Sender->Options .= $Option;
  162.                 } elseif ($AllowTakeBack) {
  163.                         // Allow unthank
  164.                         static $LocalizedUnThankButtonText;
  165.                         if (is_null($LocalizedUnThankButtonText)) $LocalizedUnThankButtonText = T('UnThankCommentOption', T('Unthank'));
  166.                         $UnThankUrl = 'plugin/unthankfor/'.strtolower($Type).'/'.$ObjectID.'?Target='.$Sender->SelfUrl;
  167.                         $Option = '<span class="UnThank">'.Anchor($LocalizedUnThankButtonText, $UnThankUrl).'</span>';
  168.                         echo $Option;
  169.                         //$Sender->Options .= $Option;
  170.                 }
  171.         }
  172.  
  173.         public function DiscussionController_AfterDiscussionMeta_Handler($Sender) {
  174.                 $EventArguments =& $Sender->EventArguments;
  175.                 $Type = $EventArguments['Type'];
  176.                 $Object = $EventArguments['Object'];
  177.                 //$Session = Gdn::Session();
  178.                 $SessionUserID = $this->Session->UserID;
  179.                 if ($SessionUserID <= 0 || $Object->InsertUserID == $SessionUserID) return;
  180.                
  181.                 if (!self::IsThankable($Type)) return;
  182.                
  183.                 static $AllowTakeBack;
  184.                 if (is_null($AllowTakeBack)) $AllowTakeBack = C('Plugins.ThankfulPeople.AllowTakeBack', False);
  185.                 $AllowThank = True;
  186.                
  187.                 switch ($Type) {
  188.                         case 'Discussion': {
  189.                                 $DiscussionID = $ObjectID = $Object->DiscussionID;
  190.                                 if (array_key_exists($SessionUserID, $this->DiscussionData)) $AllowThank = False;
  191.                                 break;
  192.                         }
  193.                         case 'Comment': {
  194.                                 $CommentID = $ObjectID = $Object->CommentID;
  195.                                 if (array_key_exists($CommentID, $this->ThankForComment) && in_array($SessionUserID, $this->ThankForComment[$CommentID])) $AllowThank = False;
  196.                                 break;
  197.                         }
  198.                 }
  199.                
  200.        
  201.                 if ($AllowThank) {
  202.                         static $LocalizedThankButtonText;
  203.                         if ($LocalizedThankButtonText === Null) $LocalizedThankButtonText = T('ThankCommentOption', T('Thank'));
  204.                         $ThankUrl = 'plugin/thankfor/'.strtolower($Type).'/'.$ObjectID.'?Target='.$Sender->SelfUrl;
  205.                         $Option = '<span class="Thank">'.Anchor($LocalizedThankButtonText, $ThankUrl).'</span>';
  206.                         echo $Option;
  207.                         //$Sender->Options .= $Option;
  208.                 } elseif ($AllowTakeBack) {
  209.                         // Allow unthank
  210.                         static $LocalizedUnThankButtonText;
  211.                         if (is_null($LocalizedUnThankButtonText)) $LocalizedUnThankButtonText = T('UnThankCommentOption', T('Unthank'));
  212.                         $UnThankUrl = 'plugin/unthankfor/'.strtolower($Type).'/'.$ObjectID.'?Target='.$Sender->SelfUrl;
  213.                         $Option = '<span class="UnThank">'.Anchor($LocalizedUnThankButtonText, $UnThankUrl).'</span>';
  214.                         echo $Option;
  215.                         //$Sender->Options .= $Option;
  216.                 }
  217.         }
  218.        
  219.         public function DiscussionController_Discussion_Handler($Sender) {
  220.                 $EventArguments =& $Sender->EventArguments;
  221.                 $Type = $EventArguments['Type'];
  222.                 $Object = $EventArguments['Object'];
  223.                 //$Session = Gdn::Session();
  224.                 $SessionUserID = $this->Session->UserID;
  225.                 if ($SessionUserID <= 0 || $Object->InsertUserID == $SessionUserID) return;
  226.                
  227.                 if (!self::IsThankable($Type)) return;
  228.                
  229.                 static $AllowTakeBack;
  230.                 if (is_null($AllowTakeBack)) $AllowTakeBack = C('Plugins.ThankfulPeople.AllowTakeBack', False);
  231.                 $AllowThank = True;
  232.                
  233.                 switch ($Type) {
  234.                         case 'Discussion': {
  235.                                 $DiscussionID = $ObjectID = $Object->DiscussionID;
  236.                                 if (array_key_exists($SessionUserID, $this->DiscussionData)) $AllowThank = False;
  237.                                 break;
  238.                         }
  239.                         case 'Comment': {
  240.                                 $CommentID = $ObjectID = $Object->CommentID;
  241.                                 if (array_key_exists($CommentID, $this->ThankForComment) && in_array($SessionUserID, $this->ThankForComment[$CommentID])) $AllowThank = False;
  242.                                 break;
  243.                         }
  244.                 }
  245.                
  246.        
  247.                 if ($AllowThank) {
  248.                         static $LocalizedThankButtonText;
  249.                         if ($LocalizedThankButtonText === Null) $LocalizedThankButtonText = T('ThankCommentOption', T('Thank'));
  250.                         $ThankUrl = 'plugin/thankfor/'.strtolower($Type).'/'.$ObjectID.'?Target='.$Sender->SelfUrl;
  251.                         $Option = '<span class="Thank">'.Anchor($LocalizedThankButtonText, $ThankUrl).'</span>';
  252.                         echo $Option;
  253.                         //$Sender->Options .= $Option;
  254.                 } elseif ($AllowTakeBack) {
  255.                         // Allow unthank
  256.                         static $LocalizedUnThankButtonText;
  257.                         if (is_null($LocalizedUnThankButtonText)) $LocalizedUnThankButtonText = T('UnThankCommentOption', T('Unthank'));
  258.                         $UnThankUrl = 'plugin/unthankfor/'.strtolower($Type).'/'.$ObjectID.'?Target='.$Sender->SelfUrl;
  259.                         $Option = '<span class="UnThank">'.Anchor($LocalizedUnThankButtonText, $UnThankUrl).'</span>';
  260.                         echo $Option;
  261.                         //$Sender->Options .= $Option;
  262.                 }
  263.         }
  264.                
  265.         public function DiscussionController_AfterCommentBody_Handler($Sender) {
  266.                 $Object = $Sender->EventArguments['Object'];
  267.                 $Type = $Sender->EventArguments['Type'];
  268.                 $ThankedByBox = False;
  269.                 switch ($Type) {
  270.                         case 'Comment': {
  271.                                 $ThankedByCollection =& $this->CommentGroup[$Object->CommentID];
  272.                                 if ($ThankedByCollection) $ThankedByBox = self::ThankedByBox($ThankedByCollection);
  273.                                 break;
  274.                         }
  275.                         case 'Discussion': {
  276.                                 if (count($this->DiscussionData) > 0) $ThankedByBox = self::ThankedByBox($this->DiscussionData);
  277.                                 break;
  278.                         }
  279.                         default: throw new Exception('What...');
  280.                 }
  281.                 if ($ThankedByBox !== False) echo $ThankedByBox;
  282.        
  283.         }
  284.        
  285.         public function DiscussionController_AfterDiscussionBody_Handler($Sender) {
  286.                 $Object = $Sender->EventArguments['Object'];
  287.                 $Type = $Sender->EventArguments['Type'];
  288.                 $ThankedByBox = False;
  289.                 switch ($Type) {
  290.                         case 'Comment': {
  291.                                 $ThankedByCollection =& $this->CommentGroup[$Object->CommentID];
  292.                                 if ($ThankedByCollection) $ThankedByBox = self::ThankedByBox($ThankedByCollection);
  293.                                 break;
  294.                         }
  295.                         case 'Discussion': {
  296.                                 if (count($this->DiscussionData) > 0) $ThankedByBox = self::ThankedByBox($this->DiscussionData);
  297.                                 break;
  298.                         }
  299.                         default: throw new Exception('What...');
  300.                 }
  301.                 if ($ThankedByBox !== False) echo $ThankedByBox;
  302.        
  303.         }
  304.        
  305.         public static function ThankedByBox($Collection, $Wrap = True) {
  306.                 $List = implode(' ', array_map('UserAnchor', $Collection));
  307.                 $ThankCount = count($Collection);
  308.                 //$ThankCountHtml = Wrap($ThankCount);
  309.                 $LocalizedPluralText = Plural($ThankCount, 'Thanked by %1$s', 'Thanked by %1$s');
  310.                 $Html = '<span class="ThankedBy">'.$LocalizedPluralText.'</span>'.$List;
  311.                 if ($Wrap) $Html = Wrap($Html, 'div', array('class' => 'ThankedByBox'));
  312.                 return $Html;
  313.         }
  314.        
  315.         public function UserInfoModule_OnBasicInfo_Handler($Sender) {
  316.                 echo Wrap(T('Thanked '), 'dt', array('class' => 'ReceivedThankCount'));
  317.                 echo Wrap($Sender->User->ReceivedThankCount, 'dd', array('class' => 'ReceivedThankCount'));
  318.         }
  319.        
  320.         public function ProfileController_Render_Before($Sender) {
  321.                 if (!($Sender->DeliveryType() == DELIVERY_TYPE_ALL && $Sender->SyndicationMethod == SYNDICATION_NONE)) return;
  322.                 $Sender->AddCssFile('plugins/ThankfulPeople/design/thankfulpeople.css');
  323.         }
  324.        
  325.         public function ProfileController_AddProfileTabs_Handler($Sender) {
  326.                 $ReceivedThankCount = GetValue('ReceivedThankCount', $Sender->User);
  327.                 if ($ReceivedThankCount > 0) {
  328.                         $UserReference = ArrayValue(0, $Sender->RequestArgs, '');
  329.                         $Username = ArrayValue(1, $Sender->RequestArgs, '');
  330.                         $Thanked = T('Profile.Tab.Thanked', T('Thanked ')).'<span> '.$ReceivedThankCount.'</span>';
  331.                         $Sender->AddProfileTab($Thanked, 'profile/receivedthanks/'.$UserReference.'/'.$Username, 'Thanked');
  332.                 }
  333.         }
  334.        
  335.         public function ProfileController_ReceivedThanks_Create($Sender) {
  336.                 $UserReference = ArrayValue(0, $Sender->RequestArgs, '');
  337.                 $Username = ArrayValue(1, $Sender->RequestArgs, '');
  338.                 $Sender->GetUserInfo($UserReference, $Username);
  339.                 $ViewingUserID = $Sender->User->UserID;
  340.                
  341.                 $ReceivedThankCount = $Sender->User->ReceivedThankCount;
  342.                 $Thanked = T('Profile.Tab.Thanked', T('Thanked')).'<span>'.$ReceivedThankCount.'</span>';
  343.                 $View = $this->GetView('receivedthanks.php');
  344.                 $Sender->SetTabView($Thanked, $View);
  345.                 $ThanksLogModel = new ThanksLogModel();
  346.                 // TODO: PAGINATION
  347.                 list($Sender->ThankData, $Sender->ThankObjects) = $ThanksLogModel->GetReceivedThanks(array('t.UserID' => $ViewingUserID), 0, 50);
  348.                 $Sender->Render();
  349.         }
  350.        
  351.         public function Tick_Every_720_Hours_Handler($Sender) {
  352.                 ThanksLogModel::CleanUp();
  353.                 ThanksLogModel::RecalculateUserReceivedThankCount();
  354.         }
  355.        
  356.         public function Structure() {
  357. /*              Gdn::Structure()
  358.                         ->Table('Comment')
  359.                         ->Column('ThankCount', 'usmallint', 0)
  360.                         ->Set();
  361.                
  362.                 Gdn::Structure()
  363.                         ->Table('Discussion')
  364.                         ->Column('ThankCount', 'usmallint', 0)
  365.                         ->Set();*/
  366.                 Gdn::Structure()
  367.                         ->Table('User')
  368.                         //->Column('ThankCount', 'usmallint', 0)
  369.                         ->Column('ReceivedThankCount', 'usmallint', 0)
  370.                         ->Set();
  371.                
  372.                 Gdn::Structure()
  373.                         ->Table('ThanksLog')
  374.                         ->Column('UserID', 'umediumint', False, 'key')
  375.                         ->Column('CommentID', 'umediumint', 0)
  376.                         ->Column('DiscussionID', 'umediumint', 0)
  377.                         ->Column('DateInserted', 'datetime')
  378.                         ->Column('InsertUserID', 'umediumint', False, 'key')
  379.                         ->Engine('MyISAM')
  380.                         ->Set();
  381.                        
  382.                 $RequestArgs = Gdn::Controller()->RequestArgs;
  383.                 if (ArrayHasValue($RequestArgs, 'vanilla')) {
  384.                         ThanksLogModel::RecalculateUserReceivedThankCount();
  385.                 }
  386.                
  387.                 //ThanksLogModel::RecalculateCommentThankCount();
  388.                 //ThanksLogModel::RecalculateDiscussionThankCount();
  389.         }
  390.                
  391.         public function Setup() {
  392.                 $this->Structure();
  393.         }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement