Advertisement
Guest User

Untitled

a guest
Nov 16th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.15 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class comments extends db_connect
  5. {
  6.  
  7. private $requestFrom = 0;
  8. private $language = 'en';
  9.  
  10. public function __construct($dbo = NULL)
  11. {
  12. parent::__construct($dbo);
  13. }
  14.  
  15. public function allCommentsCount()
  16. {
  17. $stmt = $this->db->prepare("SELECT max(id) FROM comments");
  18. $stmt->execute();
  19.  
  20. return $number_of_rows = $stmt->fetchColumn();
  21. }
  22.  
  23. public function count($postId)
  24. {
  25. $stmt = $this->db->prepare("SELECT count(*) FROM comments WHERE postId = (:postId) AND removeAt = 0");
  26. $stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
  27. $stmt->execute();
  28.  
  29. return $number_of_rows = $stmt->fetchColumn();
  30. }
  31.  
  32. public function create($postId, $text, $notifyId = 0)
  33. {
  34. $result = array("error" => true,
  35. "error_code" => ERROR_UNKNOWN);
  36.  
  37. if (strlen($text) == 0) {
  38.  
  39. return $result;
  40. }
  41.  
  42. $post = new post($this->db);
  43.  
  44. $postInfo = $post->info($postId);
  45.  
  46. unset($post);
  47.  
  48. $currentTime = time();
  49. $ip_addr = helper::ip_addr();
  50. $u_agent = helper::u_agent();
  51.  
  52. $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)");
  53. $stmt->bindParam(":fromUserId", $this->requestFrom, PDO::PARAM_INT);
  54. $stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
  55. $stmt->bindParam(":comment", $text, PDO::PARAM_STR);
  56. $stmt->bindParam(":createAt", $currentTime, PDO::PARAM_INT);
  57. $stmt->bindParam(":notifyId", $notifyId, PDO::PARAM_INT);
  58. $stmt->bindParam(":ip_addr", $ip_addr, PDO::PARAM_STR);
  59. $stmt->bindParam(":u_agent", $u_agent, PDO::PARAM_STR);
  60.  
  61. if ($stmt->execute()) {
  62.  
  63. $result = array("error" => false,
  64. "error_code" => ERROR_SUCCESS,
  65. "commentId" => $this->db->lastInsertId(),
  66. "comment" => $this->info($this->db->lastInsertId()));
  67.  
  68. if ($this->requestFrom != $postInfo['fromUserId']) {
  69.  
  70. $gcm = new gcm($this->db, $postInfo['fromUserId']);
  71. $gcm->setData(GCM_NOTIFY_COMMENT, "You have a new comment.", $postId);
  72. $gcm->send();
  73. }
  74. }
  75.  
  76. return $result;
  77. }
  78.  
  79. public function remove($commentId)
  80. {
  81. $result = array("error" => true,
  82. "error_code" => ERROR_UNKNOWN);
  83.  
  84. $commentInfo = $this->info($commentId);
  85.  
  86. if ($commentInfo['error'] === true) {
  87.  
  88. return $result;
  89. }
  90.  
  91. // if ($commentInfo['fromUserId'] != $this->requestFrom) {
  92. //
  93. // return $result;
  94. // }
  95.  
  96. $currentTime = time();
  97.  
  98. $stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE id = (:commentId)");
  99. $stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
  100. $stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
  101.  
  102. if ($stmt->execute()) {
  103.  
  104. $result = array("error" => false,
  105. "error_code" => ERROR_SUCCESS);
  106. }
  107.  
  108. return $result;
  109. }
  110.  
  111. public function removeAll($postId) {
  112.  
  113. $currentTime = time();
  114.  
  115. $stmt = $this->db->prepare("UPDATE comments SET removeAt = (:removeAt) WHERE postId = (:postId)");
  116. $stmt->bindParam(":postId", $postId, PDO::PARAM_INT);
  117. $stmt->bindParam(":removeAt", $currentTime, PDO::PARAM_INT);
  118. }
  119.  
  120. public function info($commentId)
  121. {
  122. $result = array("error" => true,
  123. "error_code" => ERROR_UNKNOWN);
  124.  
  125. $stmt = $this->db->prepare("SELECT * FROM comments WHERE id = (:commentId) LIMIT 1");
  126. $stmt->bindParam(":commentId", $commentId, PDO::PARAM_INT);
  127.  
  128. if ($stmt->execute()) {
  129.  
  130. if ($stmt->rowCount() > 0) {
  131.  
  132. $row = $stmt->fetch();
  133.  
  134. $time = new language($this->db, $this->language);
  135.  
  136. $profile = new profile($this->db, $row['fromUserId']);
  137. $fromUserId = $profile->get();
  138. unset($profile);
  139.  
  140. $lowPhotoUrl = "/img/profile_default_photo.png";
  141.  
  142. if (strlen($fromUserId['lowPhotoUrl']) != 0) {
  143.  
  144. $lowPhotoUrl = $fromUserId['lowPhotoUrl'];
  145. }
  146.  
  147. $post = new post($this->db);
  148. $post->setRequestFrom($this->getRequestFrom());
  149.  
  150. $postInfo = $post->info($row['postId']);
  151.  
  152. $result = array("error" => false,
  153. "error_code" => ERROR_SUCCESS,
  154. "id" => $row['id'],
  155. "fromUserId" => $row['fromUserId'],
  156. "fromUserState" => $fromUserId['state'],
  157. "fromUserUsername" => $fromUserId['username'],
  158. "fromUserFullname" => $fromUserId['fullname'],
  159. "fromUserPhotoUrl" => $lowPhotoUrl,
  160. "postId" => $row['postId'],
  161. "postFromUserId" => $postInfo['fromUserId'],
  162. "comment" => htmlspecialchars_decode(stripslashes($row['comment'])),
  163. "createAt" => $row['createAt'],
  164. "notifyId" => $row['notifyId'],
  165. "timeAgo" => $time->timeAgo($row['createAt']));
  166. }
  167. }
  168.  
  169. return $result;
  170. }
  171.  
  172. public function get($postId, $commentId = 0)
  173. {
  174. if ($commentId == 0) {
  175.  
  176. $commentId = $this->allCommentsCount() + 1;
  177. }
  178.  
  179. $comments = array("error" => false,
  180. "error_code" => ERROR_SUCCESS,
  181. "commentId" => $commentId,
  182. "postId" => $postId,
  183. "comments" => array());
  184.  
  185. $stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 0,38");
  186. $stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
  187. $stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
  188.  
  189. if ($stmt->execute()) {
  190.  
  191. while ($row = $stmt->fetch()) {
  192.  
  193. $commentInfo = $this->info($row['id']);
  194.  
  195. array_push($comments['comments'], $commentInfo);
  196.  
  197. $comments['commentId'] = $commentInfo['id'];
  198.  
  199. unset($commentInfo);
  200. }
  201. }
  202.  
  203. return $comments;
  204. }
  205.  
  206. public function getPreview($postId)
  207. {
  208. $commentId = $this->allCommentsCount() + 1;
  209.  
  210. $comments = array("error" => false,
  211. "error_code" => ERROR_SUCCESS,
  212. "commentId" => $commentId,
  213. "postId" => $postId,
  214. "count" => $this->count($postId),
  215. "comments" => array());
  216.  
  217. $stmt = $this->db->prepare("SELECT id FROM comments WHERE postId = (:postId) AND id < (:commentId) AND removeAt = 0 ORDER BY id ASC LIMIT 3");
  218. $stmt->bindParam(':postId', $postId, PDO::PARAM_INT);
  219. $stmt->bindParam(':commentId', $commentId, PDO::PARAM_INT);
  220.  
  221. if ($stmt->execute()) {
  222.  
  223. while ($row = $stmt->fetch()) {
  224.  
  225. $commentInfo = $this->info($row['id']);
  226.  
  227. array_push($comments['comments'], $commentInfo);
  228.  
  229. $comments['commentId'] = $commentInfo['id'];
  230.  
  231. unset($commentInfo);
  232. }
  233. }
  234.  
  235. return $comments;
  236. }
  237.  
  238. public function setLanguage($language)
  239. {
  240. $this->language = $language;
  241. }
  242.  
  243. public function getLanguage()
  244. {
  245. return $this->language;
  246. }
  247.  
  248. public function setRequestFrom($requestFrom)
  249. {
  250. $this->requestFrom = $requestFrom;
  251. }
  252.  
  253. public function getRequestFrom()
  254. {
  255. return $this->requestFrom;
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement