CommentsRenderer.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. class CommentsRenderer {
  3. const PAGE_SIZE = 100;
  4. private $database;
  5. public function __construct() {
  6. $this->database = new Database();
  7. }
  8. public function getComments($postId, $parentCommentId, $page = 0) {
  9. $user = new User();
  10. $notificationScraper = new NotificationScraper();
  11. $user->populate();
  12. $userId = $user->getId();
  13. $sql = sprintf("SELECT c.comment_id, c.content, c.timestamp, c.url_name, c.is_edited, ca.parent_comment_id, cp.post_id, pt.topic_id, u.user_id, u.username, u.display_name
  14. FROM comments c
  15. JOIN comment_comment_associations ca ON c.comment_id = ca.comment_id
  16. JOIN comment_post_associations cp ON c.comment_id = cp.comment_id
  17. JOIN comment_user_associations cu ON c.comment_id = cu.comment_id
  18. JOIN post_topic_associations pt ON cp.post_id = pt.post_id
  19. JOIN users u ON u.user_id = cu.user_id
  20. WHERE ca.parent_comment_id = '%s'
  21. AND cp.post_id = '%s'
  22. ORDER BY timestamp DESC LIMIT %s, %s", $parentCommentId, $postId, $page * CommentsRenderer::PAGE_SIZE, CommentsRenderer::PAGE_SIZE);
  23. $comments = $this->database->query($sql);
  24. foreach ($comments as $index => $commentData) {
  25. $sql = sprintf("SELECT count(*) as replies FROM comment_comment_associations WHERE parent_comment_id = '%s'", $commentData['comment_id']);
  26. $countData = $this->database->query($sql);
  27. $comments[$index]['replies'] = $countData[0]['replies'];
  28. $comments[$index]['content'] = htmlspecialchars_decode($commentData['content']);
  29. $comments[$index]['content'] = $notificationScraper->replaceNotificationsWithLinks($comments[$index]['content']);
  30. $sql = sprintf("SELECT * FROM files f
  31. JOIN comment_file_associations cfa ON f.file_id = cfa.file_id
  32. WHERE cfa.comment_id = '%s'",$commentData['comment_id']);
  33. $fileData = $this->database->query($sql);
  34. foreach($fileData as $fileindex => $file) {
  35. $fileComponents = explode('.', $file['name']);
  36. $fileData[$fileindex]['icon'] = $fileComponents[count($fileComponents)-1];
  37. }
  38. $comments[$index]['fileData'] = $fileData;
  39. }
  40. $view = new View();
  41. $viewData = array(
  42. 'comments' => $comments,
  43. 'user' => $user
  44. );
  45. if ($userId) {
  46. $viewData['userId'] = $userId;
  47. }
  48. return $view->render('commentsView.inc', $viewData);
  49. }
  50. public function getReplies($parentCommentId, $page = 0) {
  51. $user = new User();
  52. $notificationScraper = new NotificationScraper();
  53. $user->populate();
  54. $userId = $user->getId();
  55. $sql = sprintf("SELECT c.comment_id, c.content, c.timestamp, c.url_name, c.is_edited, ca.parent_comment_id, cp.post_id, pt.topic_id, u.user_id, u.username, u.display_name
  56. FROM comments c
  57. JOIN comment_comment_associations ca ON c.comment_id = ca.comment_id
  58. JOIN comment_post_associations cp ON c.comment_id = cp.comment_id
  59. JOIN comment_user_associations cu ON c.comment_id = cu.comment_id
  60. JOIN post_topic_associations pt ON cp.post_id = pt.post_id
  61. JOIN users u ON u.user_id = cu.user_id
  62. WHERE ca.parent_comment_id = '%s'
  63. ORDER BY timestamp DESC LIMIT %s, %s", $parentCommentId, $page * CommentsRenderer::PAGE_SIZE, CommentsRenderer::PAGE_SIZE);
  64. $comments = $this->database->query($sql);
  65. foreach ($comments as $index => $commentData) {
  66. $sql = sprintf("SELECT count(*) as replies FROM comment_comment_associations WHERE parent_comment_id = '%s'", $commentData['comment_id']);
  67. $countData = $this->database->query($sql);
  68. $comments[$index]['replies'] = $countData[0]['replies'];
  69. $comments[$index]['content'] = htmlspecialchars_decode($commentData['content']);
  70. $comments[$index]['content'] = $notificationScraper->replaceNotificationsWithLinks($comments[$index]['content']);
  71. $sql = sprintf("SELECT * FROM files f
  72. JOIN comment_file_associations cfa ON f.file_id = cfa.file_id
  73. WHERE cfa.comment_id = '%s'",$commentData['comment_id']);
  74. $fileData = $this->database->query($sql);
  75. foreach($fileData as $fileindex => $file) {
  76. $fileComponents = explode('.', $file['name']);
  77. $fileData[$fileindex]['icon'] = $fileComponents[count($fileComponents)-1];
  78. }
  79. $comments[$index]['fileData'] = $fileData;
  80. }
  81. $view = new View();
  82. $viewData = array(
  83. 'user' => $user,
  84. 'comments' => $comments
  85. );
  86. if ($userId) {
  87. $viewData['userId'] = $userId;
  88. }
  89. return $view->render('commentsView.inc', $viewData);
  90. }
  91. public function getTopLevelComments($postId, $page = 0) {
  92. return $this->getComments($postId, 0, $page);
  93. }
  94. }