/**
  * Delete Reply
  *
  * @param Int $replyID
  * @return bool
  */
 public static function deleteReply($replyID)
 {
     global $db;
     $query = $db->prepare("SELECT * FROM " . TABLE_FORUM_REPLIES . " WHERE replyID=%d", $replyID);
     $reply = $db->getRow($query);
     if ($reply) {
         if ($reply['status'] == 'publish') {
             //Getting Topic
             $query = $db->prepare("SELECT * FROM " . TABLE_FORUM_TOPICS . " WHERE topicID=%d", $reply['topicID']);
             $topic = $db->getRow($query);
             //Update Replies Count For Topic
             $query = "UPDATE " . TABLE_FORUM_TOPICS . " SET `replies`=`replies` - 1 WHERE topicID=" . $reply['topicID'];
             $db->query($query);
             //Update Replies Count For Category
             $query = "UPDATE " . TABLE_FORUM_CATEGORIES . " SET `replies`=`replies` - 1 WHERE categoryID=" . $topic['categoryID'];
             $db->query($query);
             $db->query("UPDATE " . TABLE_USERS . " SET `posts_count`=`posts_count` - 1 WHERE userID=" . $reply['creatorID']);
             $db->query("UPDATE " . TABLE_USERS . " SET `posts_rating`=`posts_rating`" . ($reply['votes'] > 0 ? '-' : '+') . abs($reply['votes']) . " WHERE userID=" . $reply['creatorID']);
             //Update Stats
             BuckysUser::updateStats($topic['creatorID'], 'replies', -1);
             BuckysUser::updateStats($reply['creatorID'], 'voteUps', -1 * $reply['votes']);
         }
         //Remove Reply Votes
         $query = "DELETE FROM " . TABLE_FORUM_VOTES . " WHERE objectID=" . $reply['replyID'];
         $db->query($query);
         //Delete Frome Reports Table
         $query = "DELETE FROM " . TABLE_REPORTS . " WHERE objectType='reply' AND objectID=" . $reply['replyID'];
         $db->query($query);
         //Remove Reply
         $query = "DELETE FROM " . TABLE_FORUM_REPLIES . " WHERE replyID=" . $reply['replyID'];
         $db->query($query);
         BuckysForumTopic::updateTopicLastReplyID($reply['topicID']);
         BuckysForumCategory::updateCategoryLastTopicID($topic['categoryID']);
         return true;
     }
     return false;
 }
 /**
  * @param $userID
  * @param $commentID
  * @return bool
  */
 public static function deleteComment($userID, $commentID)
 {
     global $db;
     $query = $db->prepare("SELECT c.commentID, c.postID FROM " . TABLE_COMMENTS . " AS c LEFT JOIN " . TABLE_POSTS . " AS p ON p.postID=c.postID WHERE c.commentID=%s AND (c.commenter=%s OR p.poster=%s)", $commentID, $userID, $userID);
     $row = $db->getRow($query);
     if (!$row) {
         return false;
     } else {
         $cID = $row['commentID'];
         $postID = $row['postID'];
         $db->query('DELETE FROM ' . TABLE_COMMENTS . " WHERE commentID=" . $cID);
         //Remove Activity
         $db->query('DELETE FROM ' . TABLE_MAIN_ACTIVITIES . " WHERE actionID=" . $cID);
         //Remove From Report
         $db->query('DELETE FROM ' . TABLE_REPORTS . " WHERE objectType='comment' AND objectID=" . $cID);
         //Update comments on the posts table
         $query = $db->prepare('UPDATE ' . TABLE_POSTS . ' SET `comments`=`comments` - 1 WHERE postID=%d', $postID);
         $db->query($query);
         $postData = BuckysPost::getPostById($postID);
         //Update User Stats
         BuckysUser::updateStats($postData['poster'], 'comments', -1);
         return true;
     }
 }
 /**
  * Like Post
  *
  * @param int $userID
  * @param int $postID
  * @param $action
  * @param bool $checkToken
  * @return bool|int|null|string
  */
 public static function likePost($userID, $postID, $action, $checkToken = true)
 {
     global $db;
     $post = BuckysPost::getPostById($postID);
     if ($checkToken && !buckys_check_form_token('request')) {
         buckys_add_message(MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
         return false;
     }
     if (!$post || $post['poster'] == $userID) {
         buckys_add_message(MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
         return false;
     }
     if ($post['visibility'] == 0 && !BuckysFriend::isFriend($userID, $post['poster'])) {
         buckys_add_message(MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
         return false;
     }
     if (!BuckysUsersDailyActivity::checkUserDailyLimit($userID, 'likes')) {
         buckys_add_message(sprintf(MSG_DAILY_LIKES_LIMIT_EXCEED_ERROR, USER_DAILY_LIMIT_LIKES), MSG_TYPE_ERROR);
         return false;
     }
     //Check already like it or not
     $query = $db->prepare("SELECT likeID FROM " . TABLE_POSTS_LIKES . " WHERE userID=%s AND postID=%s", $userID, $postID);
     $likeId = $db->getVar($query);
     if ($action == 'likePost') {
         if ($likeId) {
             buckys_add_message(MSG_ALREADY_LIKED_POST, MSG_TYPE_ERROR);
             return false;
         }
         BuckysUsersDailyActivity::addLikes($userID);
         //Like This post
         $rs = $db->insertFromArray(TABLE_POSTS_LIKES, ['userID' => $userID, 'postID' => $postID]);
         //Update likes on the posts table
         $query = $db->prepare('UPDATE ' . TABLE_POSTS . ' SET `likes`=`likes` + 1 WHERE postID=%d', $postID);
         $db->query($query);
         //Add Activity
         $activityId = BuckysActivity::addActivity($userID, $postID, 'post', 'like', $rs);
         //Add Notification
         BuckysActivity::addNotification($post['poster'], $activityId, BuckysActivity::NOTIFICATION_TYPE_LIKE_POST);
         //Increase Hits
         BuckysHit::addHit($postID, $userID);
         //Update User Stats
         BuckysUser::updateStats($post['poster'], 'likes', 1);
         return $rs;
     } else {
         if ($action == 'unlikePost') {
             if (!$likeId) {
                 buckys_add_message(MSG_INVALID_REQUEST, MSG_TYPE_ERROR);
                 return false;
             }
             BuckysUsersDailyActivity::addLikes($userID);
             $query = $db->prepare("DELETE FROM " . TABLE_POSTS_LIKES . " WHERE userID=%s AND postID=%s", $userID, $postID);
             $db->query($query);
             //Update likes on the posts table
             $query = $db->prepare('UPDATE ' . TABLE_POSTS . ' SET `likes`=`likes` - 1 WHERE postID=%d', $postID);
             $db->query($query);
             //Increase Hits
             BuckysHit::removeHit($postID, $userID);
             //Update User Stats
             BuckysUser::updateStats($post['poster'], 'likes', -1);
             return true;
         }
     }
 }
 /**
  * Remove page followers when removing page
  *
  * @param mixed $pageID
  */
 public function removeAllFollowersByPageID($pageID)
 {
     global $db;
     if (!is_numeric($pageID)) {
         return;
     }
     //Getting Followers
     $query = $db->prepare("SELECT userID FROM " . TABLE_PAGES . " WHERE pageID=%d", $pageID);
     $pageCreatorId = $db->getVar($query);
     //Getting Followers
     $query = $db->prepare("SELECT count(*) FROM " . TABLE_PAGE_FOLLOWERS . " WHERE pageID=%d", $pageID);
     $followers = $db->getVar($query);
     if ($followers > 0) {
         BuckysUser::updateStats($pageCreatorId, 'pageFollowers', -1 * $followers);
     }
     $query = sprintf("DELETE FROM %s WHERE pageID=%d", TABLE_PAGE_FOLLOWERS, $pageID);
     $db->query($query);
     return;
 }
 /**
  * Cast a vote on a topic
  *
  * @param Int $userID : voterID
  * @param $topicID
  * @param Int $voteType : 1: Thumb up, -1: Thumb Down
  * @return Int|null|string
  */
 public static function voteTopic($userID, $topicID, $voteType)
 {
     global $db, $TNB_GLOBALS;
     //Check Reply ID
     $query = $db->prepare("SELECT topicID, votes, creatorID FROM " . TABLE_FORUM_TOPICS . " WHERE topicID=%d AND STATUS='publish'", $topicID);
     $topic = $db->getRow($query);
     if (!$topic) {
         return MSG_INVALID_REQUEST;
     }
     $topicID = $topic['topicID'];
     $votes = $topic['votes'];
     //Check the user already casted his vote or not
     $query = $db->prepare("SELECT voteID FROM " . TABLE_FORUM_VOTES . " WHERE objectID=%d AND voterID=%d AND objectType='topic'", $topicID, $userID);
     $voteID = $db->getVar($query);
     if ($voteID) {
         return MSG_ALREADY_CASTED_A_VOTE;
     }
     //Add Vote
     $voteID = $db->insertFromArray(TABLE_FORUM_VOTES, ['objectID' => $topicID, 'voterID' => $userID, 'objectType' => 'topic', 'voteStatus' => $voteType, 'voteDate' => date('Y-m-d H:i:s')]);
     if (!$voteID) {
         return $db->getLastError();
     }
     $votes += $voteType;
     $db->update('UPDATE ' . TABLE_FORUM_TOPICS . ' SET `votes` = ' . $votes . ' WHERE topicID=' . $topicID);
     //Update user ragings
     $db->query("UPDATE " . TABLE_USERS . " SET `posts_rating`=`posts_rating` " . ($voteType > 0 ? '+' : '-') . " 1 WHERE userID=" . $topic['creatorID']);
     if ($voteType > 0) {
         //Update User Stats
         BuckysUser::updateStats($topic['creatorID'], 'voteUps', 1);
     }
     return $votes;
 }