Example #1
0
 /**
  * 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);
     } else {
         $Remove = GetValue(1, $Sender->RequestArgs);
         $Recipient = Gdn::UserModel()->GetID($Discussion->InsertUserID);
         $RedirectURL = Url("../discussion/{$DiscussionID}");
         $Likes = $this->LikeModel->GetDiscussionLikes($DiscussionID);
     }
     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));
         } else {
             $Likes = $this->LikeModel->GetDiscussionLikes($DiscussionID);
             $Url = "{$DiscussionID}";
             $Sender->JSON('LikeNewLink', $this->FormatLikes($Likes, $Url, $UID, FALSE));
         }
         $Sender->Render();
         return;
     }
     Redirect($RedirectURL);
 }
Example #2
0
 /**
  * A hook which creates a fake action "kudos" on the discussion controller.
  * This action handles all the liking and unliking.
  *
  * @param Controller $Sender
  * @return void
  */
 public function DiscussionController_Kudos_Create(&$Sender)
 {
     $Session = Gdn::Session();
     $UserId = $Session->User->UserID;
     $DiscussionID = GetValue(0, $Sender->RequestArgs);
     $Action = GetValue(1, $Sender->RequestArgs);
     $CommentID = GetValue(2, $Sender->RequestArgs);
     $this->CommentID = $CommentID;
     $this->DiscussionID = $DiscussionID;
     $Options = array('UserID' => $UserId, 'Action' => $Action);
     if ($CommentID) {
         $Options['CommentID'] = $CommentID;
         $RedirectURL = "discussion/{$DiscussionID}#Item_{$CommentID}";
         $KudosID = $CommentID;
     } else {
         $Options['DiscussionID'] = $DiscussionID;
         $RedirectURL = "discussion/{$DiscussionID}";
         $KudosID = $DiscussionID;
     }
     if ($Action == 1) {
         $Sender->StatusMessage = $this->AddPoint($Options);
     } else {
         $Sender->StatusMessage = $this->RemovePoint($Options);
     }
     if ($Sender->DeliveryType() == DELIVERY_TYPE_BOOL) {
         $this->KudosModel->ClearSituation();
         $points = C('Plugins.Kudos.DeleteNumber') * -1;
         $rate = $this->KudosModel->CommentRate($CommentID);
         if ($rate <= $points && C('Plugins.Kudos.Delete')) {
             $Sender->JSON('CommentDelete', true);
         }
         $Sender->JSON('CommentRate', $rate);
         $Sender->JSON('KudosNewLink', $this->FormatKudos($DiscussionID, $CommentID));
         $Sender->JSON('KudosKudos', $this->_DrawVotes($Sender, 1));
         $Sender->JSON('KudosID', $KudosID);
         $Sender->JSON('KudosItem', $CommentID ? 'Comment' : 'Discussion');
         $Sender->Render();
         return;
     }
     Redirect($RedirectURL);
 }