Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class comments extends db_connect
- {
- private $requestFrom = 0;
- private $language = 'en';
- public function __construct($dbo = NULL)
- {
- parent::__construct($dbo);
- }
- public function allCommentsCount()
- {
- $stmt = $this->db->prepare("SELECT max(id) FROM comments");
- $stmt->execute();
- return $number_of_rows = $stmt->fetchColumn();
- }
- public function count($postId)
- {
- $stmt = $this->db->prepare("SELECT count(*) FROM comments WHERE postId = (:postId) AND removeAt = 0");
- $stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
- $stmt->execute();
- return $number_of_rows = $stmt->fetchColumn();
- }
- public function create($postId, $text, $notifyId = 0)
- {
- $result = array("error" => true,
- "error_code" => ERROR_UNKNOWN);
- if (strlen($text) == 0) {
- return $result;
- }
- $post = new post($this->db);
- $postInfo = $post->info($postId);
- unset($post);
- $currentTime = time();
- $ip_addr = helper::ip_addr();
- $u_agent = helper::u_agent();
- $stmt = $this->db->prepare("INSERT INTO comments (fromUserId, postId, comment, createAt, notifyId, ip_addr, u_agent) value (:fromUserId, :postId, :comment, :createAt, :notifyId, :ip_addr, :u_agent)");
- $stmt->bindParam(":fromUserId", $this->requestFrom, PDO::PARAM_INT);
- $stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
- $stmt->bindParam(":comment", $text, PDO::PARAM_STR);
- $stmt->bindParam(":createAt", $currentTime, PDO::PARAM_INT);
- $stmt->bindParam(":notifyId", $notifyId, PDO::PARAM_INT);
- $stmt->bindParam(":ip_addr", $ip_addr, PDO::PARAM_STR);
- $stmt->bindParam(":u_agent", $u_agent, PDO::PARAM_STR);
- if ($stmt->execute()) {
- $result = array("error" => false,
- "error_code" => ERROR_SUCCESS,
- "commentId" => $this->db->lastInsertId(),
- "comment" => $this->info($this->db->lastInsertId()));
- if ($this->requestFrom != $postInfo['fromUserId']) {
- $gcm = new gcm($this->db, $postInfo['fromUserId']);
- $gcm->setData(GCM_NOTIFY_COMMENT, "You have a new comment.", $postId);
- $gcm->send();
- }
- }
- return $result;
- }
- public function remove($commentId)
- {
- $result = array("error" => true,
- "error_code" => ERROR_UNKNOWN);
- $commentInfo = $this->info($commentId);
- if ($commentInfo['error'] === true) {
- return $result;
- }
- // if ($commentInfo['fromUserId'] != $this->requestFrom) {
- //
- // return $result;
- // }
- $currentTime = time();
- $stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE id = (:commentId)");
- $stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
- $stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
- if ($stmt->execute()) {
- $result = array("error" => false,
- "error_code" => ERROR_SUCCESS);
- }
- return $result;
- }
- public function removeAll($postId) {
- $currentTime = time();
- $stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE postId = (:postId)");
- $stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
- $stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
- }
- public function info($commentId)
- {
- $result = array("error" => true,
- "error_code" => ERROR_UNKNOWN);
- $stmt = $this->db->prepare("SELECT * FROM comments WHERE id = (:commentId) LIMIT 1");
- $stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
- if ($stmt->execute()) {
- if ($stmt->rowCount() > 0) {
- $row = $stmt->fetch();
- $time = new language($this->db, $this->language);
- $profile = new profile($this->db, $row['fromUserId']);
- $fromUserId = $profile->get();
- unset($profile);
- $lowPhotoUrl = "/img/profile_default_photo.png";
- if (strlen($fromUserId['lowPhotoUrl']) != 0) {
- $lowPhotoUrl = $fromUserId['lowPhotoUrl'];
- }
- $post = new post($this->db);
- $post->setRequestFrom($this->getRequestFrom());
- $postInfo = $post->info($row['postId']);
- $result = array("error" => false,
- "error_code" => ERROR_SUCCESS,
- "id" => $row['id'],
- "fromUserId" => $row['fromUserId'],
- "fromUserState" => $fromUserId['state'],
- "fromUserUsername" => $fromUserId['username'],
- "fromUserFullname" => $fromUserId['fullname'],
- "fromUserPhotoUrl" => $lowPhotoUrl,
- "postId" => $row['postId'],
- "postFromUserId" => $postInfo['fromUserId'],
- "comment" => htmlspecialchars_decode(stripslashes($row['comment'])),
- "createAt" => $row['createAt'],
- "notifyId" => $row['notifyId'],
- "timeAgo" => $time->timeAgo($row['createAt']));
- }
- }
- return $result;
- }
- public function get($postId, $commentId = 0)
- {
- if ($commentId == 0) {
- $commentId = $this->allCommentsCount() + 1;
- }
- $comments = array("error" => false,
- "error_code" => ERROR_SUCCESS,
- "commentId" => $commentId,
- "postId" => $postId,
- "comments" => array());
- $stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 0,38");
- $stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
- $stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
- if ($stmt->execute()) {
- while ($row = $stmt->fetch()) {
- $commentInfo = $this->info($row['id']);
- array_push($comments['comments'], $commentInfo);
- $comments['commentId'] = $commentInfo['id'];
- unset($commentInfo);
- }
- }
- return $comments;
- }
- public function getPreview($postId)
- {
- $commentId = $this->allCommentsCount() + 1;
- $comments = array("error" => false,
- "error_code" => ERROR_SUCCESS,
- "commentId" => $commentId,
- "postId" => $postId,
- "count" => $this->count($postId),
- "comments" => array());
- $stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 3");
- $stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
- $stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
- if ($stmt->execute()) {
- while ($row = $stmt->fetch()) {
- $commentInfo = $this->info($row['id']);
- array_push($comments['comments'], $commentInfo);
- $comments['commentId'] = $commentInfo['id'];
- unset($commentInfo);
- }
- }
- return $comments;
- }
- public function setLanguage($language)
- {
- $this->language = $language;
- }
- public function getLanguage()
- {
- return $this->language;
- }
- public function setRequestFrom($requestFrom)
- {
- $this->requestFrom = $requestFrom;
- }
- public function getRequestFrom()
- {
- return $this->requestFrom;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement