Advertisement
Guest User

Untitled

a guest
Jun 16th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.45 KB | None | 0 0
  1. <?php if (!defined('APPLICATION')) exit();
  2.  
  3. /**
  4.  
  5.  *
  6.  */
  7.  
  8. $PluginInfo['LikeThis'] = array(
  9.    'Name' => 'I Like This ( Plugin )',
  10.    'Description' => 'Like Posts Like on Facebook -- now with More Karma (integration) - And Role Permissions',
  11.    'Version' => '1.1',
  12.    'RequiredApplications' => array('Vanilla' => '2.0.18'),
  13.    'RequiredTheme' => FALSE,
  14.    'RequiredPlugins' => FALSE,
  15.    'HasLocale' => TRUE,
  16.    'RegisterPermissions' =>array('Plugins.LikeThis.AllowedToLike'),
  17.    'Author' => "HBF",
  18.    'AuthorEmail' => 'sales@imperialcraftbrewery.com',
  19.    'AuthorUrl' => 'http://www.homebrewforums.net',
  20.    'MobileFriendly' => TRUE,
  21. );
  22.  
  23.  
  24.  
  25. /**
  26.  * LikePlugin class.
  27.  *
  28.  * This class does it all.
  29.  */
  30. class LikeThis extends Gdn_Plugin {
  31.  
  32.     /**
  33.     * @var LikeModel
  34.     */
  35.     public $LikeModel;
  36.  
  37.     /**
  38.     * Sets up database structure when plugin is enabled.
  39.     *
  40.     * @return void
  41.     */
  42.     public function Setup()
  43.     {
  44.         $this->Structure();
  45.     }
  46.  
  47.     //connect with Karma
  48.     /*public static function OperationDiffEquals($MetaValue,$Target,$Condition,$User,$LastTrans){
  49.     $Difference=$MetaValue-($LastTrans ? $LastTrans->LastTally : 0);
  50.     if(abs($Difference)%$Target!==0)
  51.         return FALSE;
  52.     return abs($Difference)!= $Difference? -1:1;
  53.     }*/
  54.  
  55.     public function KarmaBank_KarmaBankMetaMap_Handler($Sender)
  56.     {
  57.         $Sender->AddMeta('ILiked','(Requires I Liked This plugin) Counts Everytime you liked someone elses post');
  58.         $Sender->AddMeta('Liked','Counts Everytime someone likes one of your post');
  59.     }
  60.    
  61.     /**
  62.     * Loads and instantiates the model.
  63.     *
  64.     * @return void
  65.     */
  66.     public function __construct()
  67.     {
  68.         parent::__construct();
  69.         require_once (dirname(__FILE__) . '/LikeModel.php');
  70.         $this->LikeModel = new LikeModel();
  71.     }
  72.  
  73.     public function Base_BeforeDiscussionMeta_Handler($Sender, $Args)
  74.     {
  75.         // This method gets called before the Render method gets called on the DiscussionsController object.
  76.         $Discussion = $Args['Discussion'];
  77.         $ID = $Discussion->DiscussionID;
  78.         $Likes = $this->LikeModel->GetTotalDiscussionLikes($ID);
  79.         $CountLikes =  count($Likes);
  80.         if($CountLikes){
  81.             $Userlist = '';
  82.             foreach ($Likes as $value) {
  83.                 $UserModel = new UserModel();
  84.                 $User = $UserModel->GetID($value);
  85.                 $Userlist .= $User->Name . ', ';
  86.             }
  87.             $Src .= '<i class="icon-thumbs-up"></i><span class="likes" title="' . substr($Userlist, 0, -2) . '">' .$CountLikes. sprintf($CountLikes>1?T(' Likes'):T(' Like')).'</span> ';
  88.             echo $Src;
  89.         }
  90.     }
  91.    
  92.     public function CategoriesController_Render_Before($Sender)
  93.     {
  94.         $Sender->AddCssFile($this->GetResource('css/like.css', FALSE, FALSE));
  95.     }  
  96.    
  97.     public function DiscussionsController_Render_Before($Sender)
  98.     {
  99.         $Sender->AddCssFile($this->GetResource('css/like.css', FALSE, FALSE));
  100.     }
  101.  
  102.     /**
  103.     * A hook which creates a fake action "like" on the discussion controller.
  104.     * This action handles all the liking and unliking.
  105.     *
  106.     * @param Controller $Sender
  107.     * @return void
  108.     */
  109.    
  110.     public function DiscussionController_Like_Create($Sender)
  111.     {
  112.         $Session = Gdn::Session();
  113.         $User = $Session->User;
  114.         $UID = $User->UserID;
  115.         $Options = Array('UserID' => $User->UserID);
  116.         $DiscussionID = GetValue(0, $Sender->RequestArgs);
  117.         $DiscussionModel = new DiscussionModel();
  118.         $CommentModel = new CommentModel();
  119.         $Discussion = $DiscussionModel->GetID($DiscussionID);
  120.           // Check for permission.
  121.         if (!( $Session->IsValid() && $Session->CheckPermission('Plugins.LikeThis.AllowedToLike')))
  122.         {
  123.             return;
  124.         }
  125.        
  126.        
  127.         $Options["DiscussionID"] = $DiscussionID;
  128.         //$Sender->Permission('Vanilla.Discussions.View', TRUE, 'Category', $Discussion->CategoryID);
  129.  
  130.         if (GetValue(1, $Sender->RequestArgs) == 'comment')
  131.         {
  132.             $CommentID = GetValue(2, $Sender->RequestArgs);
  133.             $Remove = GetValue(3, $Sender->RequestArgs);
  134.             $Options["CommentID"] = $CommentID;
  135.             $Comment = $CommentModel->GetID($CommentID);
  136.             $Recipient = Gdn::UserModel()->GetID($Comment->InsertUserID);
  137.             $RedirectURL = Url("../discussion/{$DiscussionID}#Item_{$CommentID}");
  138.             $Likes = $this->LikeModel->GetCommentLikes($CommentID);
  139.         }
  140.         else
  141.         {
  142.             $Remove = GetValue(1, $Sender->RequestArgs);
  143.             $Recipient = Gdn::UserModel()->GetID($Discussion->InsertUserID);
  144.             $RedirectURL = Url("../discussion/{$DiscussionID}");
  145.             $Likes = $this->LikeModel->GetDiscussionLikes($DiscussionID);
  146.         }
  147.  
  148.         if (!$Remove)
  149.         {
  150.             if (!in_array($UID, $Likes))
  151.             {
  152.                 $this->LikeModel->Insert($Options); //$options includes current user id and discussion or comment id
  153.                 //Increment number of times someone liked one of the commentators posts
  154.                 Gdn::SQL()->Update('User',array('Liked'=>$Recipient->Liked+1))->Where(array('UserID'=>$Recipient->UserID))->Put();
  155.                 //Increment number of times this person has liked someone elses post
  156.                 Gdn::SQL()->Update('User',array('ILiked'=>$User->ILiked+1))->Where(array('UserID'=>$User->UserID))->Put();
  157.                 $Sender->InformMessage(T('<i class="icon-thumbs-up"></i>Liked.'));
  158.             }
  159.         }
  160.         else
  161.         {
  162.             if (in_array($UID, $Likes))
  163.             {
  164.                 $this->LikeModel->Delete($Options);
  165.                 //Decrement number of times someone liked one of the commentators posts
  166.                 Gdn::SQL()->Update('User',array('Liked'=>$Recipient->Liked-1))->Where(array('UserID'=>$Recipient->UserID))->Put();
  167.                 //Decrement number of times this person has liked someone elses post
  168.                 Gdn::SQL()->Update('User',array('ILiked'=>$User->ILiked-1))->Where(array('UserID'=>$User->UserID))->Put();
  169.                 $Sender->InformMessage( 'You don\'t like that anymore.', "Dismissable");
  170.             }
  171.         }
  172.  
  173.         if ($Sender->DeliveryType() == DELIVERY_TYPE_BOOL)
  174.         {
  175.             if (isSet($CommentID))
  176.             {
  177.                 $Likes = $this->LikeModel->GetCommentLikes($CommentID);
  178.                 $Url = "$DiscussionID/comment/$CommentID";
  179.                 $Sender->JSON('LikeNewLink', $this->FormatLikes($Likes, $Url, $UID, FALSE));
  180.             }
  181.             else
  182.             {
  183.                 $Likes = $this->LikeModel->GetDiscussionLikes($DiscussionID);
  184.                 $Url = "$DiscussionID";
  185.                 $Sender->JSON('LikeNewLink', $this->FormatLikes($Likes, $Url, $UID, FALSE));
  186.             }
  187.             $Sender->Render();
  188.             return;
  189.         }
  190.         Redirect($RedirectURL);
  191.     }
  192.  
  193.     /**
  194.     * This only adds the Like plugin's javascript file to the controller.
  195.     *
  196.     * @param Controller $Sender
  197.     * @return void
  198.     */
  199.     public function DiscussionController_BeforeDiscussionRender_Handler($Sender)
  200.     {
  201.         $Sender->AddCssFile($this->GetResource('css/like.css', FALSE, FALSE));
  202.         $Sender->AddJSFile($this->GetResource('like.js', FALSE, FALSE));
  203.     }
  204.  
  205.     /**
  206.     * A hook which displays info on how much the posts are liked or not.
  207.     *
  208.     * @param Controller $Sender
  209.     * @return void
  210.     */
  211.     public function PostController_CommentOptions_Handler($Sender)
  212.     {
  213.         $this->DiscussionController_CommentOptions_Handler($Sender);
  214.     }
  215.    
  216.     public function DiscussionController_DiscussionOptions_Handler($Sender)
  217.     {
  218.         $this->DiscussionController_CommentOptions_Handler($Sender);
  219.     }
  220.  
  221.     public function DiscussionController_CommentOptions_Handler($Sender)
  222.     {
  223.         $Session = Gdn::Session();
  224.         $User = $Session->User;
  225.         $UID = $User->UserID;
  226.        
  227.         $DiscussionModel = new DiscussionModel();
  228.         $CommentModel = new CommentModel();
  229.         $Discussion = $DiscussionModel->GetID($DiscussionID);
  230.  
  231.         if ($Sender->EventArguments['Type'] == 'Discussion')
  232.         {
  233.             $DiscussionID = $Sender->EventArguments['Discussion']->DiscussionID;
  234.             if ($Sender->Data['Comments'] instanceof Gdn_DataSet)
  235.                 $this->LikeModel->PreloadLikes($Sender->Data['Comments']);
  236.             $ID = $DiscussionID;
  237.             $Model = new DiscussionModel();
  238.             $Data = $Model->GetID($ID);
  239.             $Likes = $this->LikeModel->GetDiscussionLikes($ID);
  240.             $Url = $DiscussionID;
  241.         }
  242.         else
  243.         {
  244.             $DiscussionID = $Sender->EventArguments['Object']->DiscussionID;
  245.             $ID = $Sender->EventArguments['Object']->CommentID;
  246.             $Model = new CommentModel();
  247.             $Data = $Model->GetID($ID);
  248.             $Likes = $this->LikeModel->GetCommentLikes($ID);
  249.             $Url = $DiscussionID . '/comment/' . $ID;
  250.         }
  251.         $InsertID = $Data->InsertUserID;
  252.         if($InsertID == $UID)
  253.             $Self = TRUE;
  254.         else
  255.             $Self = FALSE;
  256.            
  257.         // Check for permission.
  258.         if (!Gdn::Session()->UserID) $Self = TRUE;
  259.         if(!CheckPermission('Plugins.LikeThis.AllowedToLike')) $Self = TRUE;
  260.    
  261.         $LikeDisplay = $this->FormatLikes($Likes, $Url, $UID, $Self);
  262.        
  263.         echo '<span class="Like">'.$LikeDisplay.'</span>';
  264.     }
  265.  
  266.     /**
  267.     * This formats the liking info.
  268.     *
  269.     * @param Array $Likes
  270.     * @param string $Url
  271.     * @param int CurrentUser User ID
  272.     * @param bool Comment posted by CurrentUser
  273.     * @return string
  274.     */
  275.     public function FormatLikes($Likes, $Url, $UID, $Self)
  276.     {
  277.         $CountLikes = count($Likes);
  278.         $Src = '';
  279.         $Userlist = '';
  280.         if($CountLikes) {
  281.             foreach ($Likes as $value) {
  282.                 $UserModel = new UserModel();
  283.                 $User = $UserModel->GetID($value);
  284.                 $Userlist .= $User->Name . ', ';
  285.             }
  286.            
  287.             $Src .= '<span class="likes" title="' . substr($Userlist, 0, -2) . '">' .$CountLikes. sprintf($CountLikes>1?T(' Likes'):T(' Like')).'</span> ';
  288.         }
  289.         if(!$Self)
  290.         {
  291.             if (in_array($UID, $Likes))
  292.             {
  293.                 $QuoteURL = Url("discussion/Like/{$Url}/remove",TRUE);
  294.                 $QuoteText = '<button class="btn btn-primary" type="button"><i class="icon-thumbs-up icon-white"></i>' .
  295.                     T('Unlike') .
  296.                     '</button>';
  297.             }
  298.             else
  299.             {
  300.                 $QuoteURL = Url("discussion/Like/{$Url}",TRUE);
  301.                 $QuoteText = '<button class="btn btn-primary" type="button"><i class="icon-thumbs-up icon-white"></i>' .
  302.                     T('Like') .
  303.                     '</button>';
  304.             }
  305.             $Src .= '<span class="LikeThis"><a href="'.$QuoteURL.'">'.$QuoteText.'</a></span>';
  306.         }
  307.         return $Src;
  308.     }
  309.  
  310.     /**
  311.     * This creates the database structure for the plugin.
  312.     *
  313.     * @return void
  314.     */
  315.     public function Structure()
  316.     {
  317.         GDN::Structure()
  318.             ->Table('AllLikes')
  319.             ->PrimaryKey('ID')
  320.             ->Column('CommentID', 'int(11)', TRUE)
  321.             ->Column('DiscussionID', 'int(11)', TRUE)
  322.             ->Column('UserID', 'int(11)', FALSE)
  323.             ->Set(TRUE);
  324.              
  325.         Gdn::Structure()
  326.             ->Table('User')
  327.             ->Column('Liked','int(11)',0)
  328.             ->Column('DisLiked','int(11)',0)
  329.             ->Column('ILiked','int(11)',0)
  330.             ->Set();
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement