Advertisement
Guest User

Thank plugin

a guest
Nov 11th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.04 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).',
  6.     'Version' => '2.14.2.0.18',
  7.     'Date' => 'Summer 2011',
  8.     'Author' => 'Jerl Liandri',
  9.     'AuthorUrl' => 'http://www.liandri-mining-corporation.com',
  10.     'RequiredApplications' => array('Vanilla' => '>=2.0.18'),
  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', 'plugins/ThankfulPeople');
  106.         $Sender->addCssFile('thankfulpeople.css', 'plugins/ThankfulPeople');
  107.         $Sender->addJsFile('thankfulpeople.functions.js', 'plugins/ThankfulPeople');
  108.        
  109.         $Sender->AddDefinition('ExpandThankList', T('ExpandThankList'));
  110.         $Sender->AddDefinition('CollapseThankList', T('CollapseThankList'));
  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_CommentOptions_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('Thanks'));
  158.             $ThankUrl = 'plugin/thankfor/'.strtolower($Type).'/'.$ObjectID.'?Target='.$Sender->SelfUrl;
  159.             $Option = '<span class="Thank">'.Anchor($LocalizedThankButtonText, $ThankUrl).'</span>';
  160.       // $Sender->Options .= $Option;
  161.       echo $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.       // $Sender->Options .= $Option;
  169.       echo $Option;
  170.         }
  171.     }
  172.    
  173.     public function DiscussionController_AfterCommentBody_Handler($Sender) {
  174.         $Object = $Sender->EventArguments['Object'];
  175.         $Type = $Sender->EventArguments['Type'];
  176.         $ThankedByBox = False;
  177.         switch ($Type) {
  178.             case 'Comment': {
  179.                 $ThankedByCollection =& $this->CommentGroup[$Object->CommentID];
  180.                 if ($ThankedByCollection) $ThankedByBox = self::ThankedByBox($ThankedByCollection);
  181.                 break;
  182.             }
  183.             case 'Discussion': {
  184.                 if (count($this->DiscussionData) > 0) $ThankedByBox = self::ThankedByBox($this->DiscussionData);
  185.                 break;
  186.             }
  187.             default: throw new Exception('What...');
  188.         }
  189.         if ($ThankedByBox !== False) echo $ThankedByBox;
  190.     }
  191.    
  192.     public static function ThankedByBox($Collection, $Wrap = True) {
  193.         $List = implode(' ', array_map('UserAnchor', $Collection));
  194.         $ThankCount = count($Collection);
  195.         //$ThankCountHtml = Wrap($ThankCount);
  196.         $LocalizedPluralText = Plural($ThankCount, 'Thanked by %1$s', 'Thanked by %1$s');
  197.         $Html = '<span class="ThankedBy">'.$LocalizedPluralText.'</span>'.$List;
  198.         if ($Wrap) $Html = Wrap($Html, 'div', array('class' => 'ThankedByBox'));
  199.         return $Html;
  200.     }
  201.    
  202.     public function UserInfoModule_OnBasicInfo_Handler($Sender) {
  203.         echo Wrap(T('UserInfoModule.Thanked'), 'dt', array('class' => 'ReceivedThankCount'));
  204.         echo Wrap($Sender->User->ReceivedThankCount, 'dd', array('class' => 'ReceivedThankCount'));
  205.     }
  206.    
  207.     public function ProfileController_Render_Before($Sender) {
  208.         if (!($Sender->DeliveryType() == DELIVERY_TYPE_ALL && $Sender->SyndicationMethod == SYNDICATION_NONE)) return;
  209.         $Sender->AddCssFile('plugins/ThankfulPeople/design/thankfulpeople.css');
  210.     }
  211.    
  212.     public function ProfileController_AddProfileTabs_Handler($Sender) {
  213.         $ReceivedThankCount = GetValue('ReceivedThankCount', $Sender->User);
  214.         if ($ReceivedThankCount > 0) {
  215.             $UserReference = ArrayValue(0, $Sender->RequestArgs, '');
  216.             $Username = ArrayValue(1, $Sender->RequestArgs, '');
  217.             $Thanked = T('Profile.Tab.Thanked', T('Thanked')).'<span>'.$ReceivedThankCount.'</span>';
  218.             $Sender->AddProfileTab($Thanked, 'profile/receivedthanks/'.$UserReference.'/'.$Username, 'Thanked');
  219.         }
  220.     }
  221.    
  222.     public function ProfileController_ReceivedThanks_Create($Sender) {
  223.         $UserReference = ArrayValue(0, $Sender->RequestArgs, '');
  224.         $Username = ArrayValue(1, $Sender->RequestArgs, '');
  225.         $Sender->GetUserInfo($UserReference, $Username);
  226.         $ViewingUserID = $Sender->User->UserID;
  227.        
  228.         $ReceivedThankCount = $Sender->User->ReceivedThankCount;
  229.         $Thanked = T('Profile.Tab.Thanked', T('Thanked')).'<span>'.$ReceivedThankCount.'</span>';
  230.         $View = $this->GetView('receivedthanks.php');
  231.         $Sender->SetTabView($Thanked, $View);
  232.         $ThanksLogModel = new ThanksLogModel();
  233.         // TODO: PAGINATION
  234.         list($Sender->ThankData, $Sender->ThankObjects) = $ThanksLogModel->GetReceivedThanks(array('t.UserID' => $ViewingUserID), 0, 50);
  235.         $Sender->Render();
  236.     }
  237.    
  238.     public function Tick_Every_720_Hours_Handler($Sender) {
  239.         ThanksLogModel::CleanUp();
  240.         ThanksLogModel::RecalculateUserReceivedThankCount();
  241.     }
  242.    
  243.     public function Structure() {
  244. /*      Gdn::Structure()
  245.             ->Table('Comment')
  246.             ->Column('ThankCount', 'usmallint', 0)
  247.             ->Set();
  248.        
  249.         Gdn::Structure()
  250.             ->Table('Discussion')
  251.             ->Column('ThankCount', 'usmallint', 0)
  252.             ->Set();*/
  253.         Gdn::Structure()
  254.             ->Table('User')
  255.             //->Column('ThankCount', 'usmallint', 0)
  256.             ->Column('ReceivedThankCount', 'usmallint', 0)
  257.             ->Set();
  258.        
  259.         Gdn::Structure()
  260.             ->Table('ThanksLog')
  261.             ->Column('UserID', 'umediumint', False, 'key')
  262.             ->Column('CommentID', 'umediumint', 0)
  263.             ->Column('DiscussionID', 'umediumint', 0)
  264.             ->Column('DateInserted', 'datetime')
  265.             ->Column('InsertUserID', 'umediumint', False, 'key')
  266.             ->Engine('MyISAM')
  267.             ->Set();
  268.            
  269.         $RequestArgs = Gdn::Controller()->RequestArgs;
  270.         if (ArrayHasValue($RequestArgs, 'vanilla')) {
  271.             ThanksLogModel::RecalculateUserReceivedThankCount();
  272.         }
  273.        
  274.         //ThanksLogModel::RecalculateCommentThankCount();
  275.         //ThanksLogModel::RecalculateDiscussionThankCount();
  276.     }
  277.        
  278.     public function Setup() {
  279.         $this->Structure();
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement