コード例 #1
0
ファイル: default.php プロジェクト: Nordic-T/vanilla-plugins
 /**
  * A hook which creates a fake action "like" on the discussion controller.
  * This action handles all the liking and unliking.
  *
  * @param Controller $Sender
  * @return void
  */
 public function DiscussionController_Like_Create($Sender)
 {
     $Session = Gdn::Session();
     $User = $Session->User;
     $UID = $User->UserID;
     $Options = array('UserID' => $User->UserID);
     $DiscussionID = GetValue(0, $Sender->RequestArgs);
     $DiscussionModel = new DiscussionModel();
     $CommentModel = new CommentModel();
     $Discussion = $DiscussionModel->GetID($DiscussionID);
     // Check for permission.
     if (!($Session->IsValid() && $Session->CheckPermission('Plugins.LikeThis.AllowedToLike'))) {
         return;
     }
     $Options["DiscussionID"] = $DiscussionID;
     //$Sender->Permission('Vanilla.Discussions.View', TRUE, 'Category', $Discussion->CategoryID);
     if (GetValue(1, $Sender->RequestArgs) == 'comment') {
         $CommentID = GetValue(2, $Sender->RequestArgs);
         $Remove = GetValue(3, $Sender->RequestArgs);
         $Options["CommentID"] = $CommentID;
         $Comment = $CommentModel->GetID($CommentID);
         $Recipient = Gdn::UserModel()->GetID($Comment->InsertUserID);
         $RedirectURL = Url("../discussion/{$DiscussionID}#Item_{$CommentID}");
         $Likes = $this->LikeModel->GetCommentLikes($CommentID);
     }
     if (!$Remove) {
         if (!in_array($UID, $Likes)) {
             $this->LikeModel->Insert($Options);
             //$options includes current user id and discussion or comment id
             //Increment number of times someone liked one of the commentators posts
             Gdn::SQL()->Update('User', array('Liked' => $Recipient->Liked + 1))->Where(array('UserID' => $Recipient->UserID))->Put();
             //Increment number of times this person has liked someone elses post
             Gdn::SQL()->Update('User', array('ILiked' => $User->ILiked + 1))->Where(array('UserID' => $User->UserID))->Put();
             $Sender->InformMessage(T('<i class="icon-thumbs-up"></i>Liked.'));
         }
     } else {
         if (in_array($UID, $Likes)) {
             $this->LikeModel->Delete($Options);
             //Decrement number of times someone liked one of the commentators posts
             Gdn::SQL()->Update('User', array('Liked' => $Recipient->Liked - 1))->Where(array('UserID' => $Recipient->UserID))->Put();
             //Decrement number of times this person has liked someone elses post
             Gdn::SQL()->Update('User', array('ILiked' => $User->ILiked - 1))->Where(array('UserID' => $User->UserID))->Put();
             $Sender->InformMessage('You don\'t like that anymore.', "Dismissable");
         }
     }
     if ($Sender->DeliveryType() == DELIVERY_TYPE_BOOL) {
         if (isset($CommentID)) {
             $Likes = $this->LikeModel->GetCommentLikes($CommentID);
             $Url = "{$DiscussionID}/comment/{$CommentID}";
             $Sender->JSON('LikeNewLink', $this->FormatLikes($Likes, $Url, $UID, FALSE));
         }
         $Sender->Render();
         return;
     }
     Redirect($RedirectURL);
 }