PostRenderer.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. class PostRenderer {
  3. const PAGE_SIZE = 20;
  4. private $database;
  5. public function __construct() {
  6. $this->database = new Database();
  7. }
  8. public function getPostsInUserTopics($page = 0) {
  9. $user = new User();
  10. $user->populate();
  11. $userId = $user->getId();
  12. if ($userId) {
  13. $view = new View();
  14. $sql = sprintf("SELECT p.post_id, u.user_id, p.timestamp, p.name as post_name, p.url_name as post_url_name, p.content, u.username, u.display_name as user_display_name, t.topic_id, t.display_name as topic_display_name
  15. FROM posts p
  16. JOIN post_topic_associations a ON p.post_id = a.post_id
  17. JOIN users u ON p.poster_user_id = u.user_id
  18. JOIN topics t on t.topic_id = a.topic_id
  19. WHERE a.topic_id IN (SELECT topic_id FROM user_topic_subscription WHERE user_id = '%s')
  20. OR a.topic_id IN (SELECT subtopic_id FROM topic_topic_associations WHERE topic_id IN (SELECT topic_id FROM user_topic_subscription WHERE user_id = '%s'))
  21. ORDER BY timestamp DESC LIMIT %s, %s", $userId, $userId, $page * PostRenderer::PAGE_SIZE, PostRenderer::PAGE_SIZE);
  22. $posts = $this->database->query($sql);
  23. $posts = $this->addCommentCount($posts);
  24. $posts = $this->addPostDifferent($posts, $userId);
  25. $postViewData = array(
  26. "posts" => $posts
  27. );
  28. return $view->render("postView.inc", $postViewData);
  29. }
  30. return $this->getPostsInDefaultTopics($page);
  31. }
  32. public function getPostsInDefaultTopics($page = 0) {
  33. $view = new View();
  34. $sql = sprintf("SELECT p.post_id, u.user_id, p.timestamp, p.name as post_name, p.url_name as post_url_name, p.content, u.username, u.display_name as user_display_name, t.topic_id, t.display_name as topic_display_name
  35. FROM posts p
  36. JOIN post_topic_associations a ON p.post_id = a.post_id
  37. JOIN users u ON p.poster_user_id = u.user_id
  38. JOIN topics t on t.topic_id = a.topic_id
  39. WHERE a.topic_id IN (SELECT topic_id FROM default_topics)
  40. OR a.topic_id IN (SELECT subtopic_id FROM topic_topic_associations WHERE topic_id IN (SELECT topic_id FROM default_topics))
  41. ORDER BY timestamp DESC LIMIT %s, %s", $page * PostRenderer::PAGE_SIZE, PostRenderer::PAGE_SIZE);
  42. $posts = $this->database->query($sql);
  43. $posts = $this->addCommentCount($posts);
  44. $postViewData = array(
  45. "posts" => $posts
  46. );
  47. return $view->render("postView.inc", $postViewData);
  48. }
  49. private function addCommentCount($posts) {
  50. foreach ($posts as $index => $post) {
  51. $sql = sprintf("SELECT count(*) AS comment_count FROM comment_post_associations WHERE post_id = '%s'", $post['post_id']);
  52. $countResult = $this->database->query($sql);
  53. $count = $countResult[0]['comment_count'];
  54. $posts[$index]['comment_count'] = $count;
  55. }
  56. return $posts;
  57. }
  58. private function addPostDifferent($posts, $userId) {
  59. foreach ($posts as $index => $post) {
  60. $sql = sprintf("SELECT 1 FROM post_last_updated plu JOIN user_post_viewed upv ON plu.post_id = upv.post_id WHERE plu.post_id = %s AND upv.user_id = %s AND plu.last_updated > upv.last_viewed", $post['post_id'], $userId);
  61. $timeData = $this->database->query($sql);
  62. $newContent = false;
  63. if (count($timeData) > 0) {
  64. $newContent = true;
  65. }
  66. $posts[$index]['new_content'] = $newContent;
  67. }
  68. return $posts;
  69. }
  70. public function getPostsInTopic($topicId, $page = 0) {
  71. $view = new View();
  72. $sql = sprintf("SELECT p.post_id, u.user_id, p.timestamp, p.name as post_name, p.url_name as post_url_name, p.content, t.topic_id, u.username, u.display_name as user_display_name, t.display_name as topic_display_name, t.url_name as topic_url_name
  73. FROM posts p
  74. JOIN post_topic_associations a ON p.post_id = a.post_id
  75. JOIN users u ON p.poster_user_id = u.user_id
  76. JOIN topics t ON a.topic_id = t.topic_id
  77. WHERE a.topic_id = '%s'
  78. OR a.topic_id IN (SELECT subtopic_id FROM topic_topic_associations WHERE topic_id = '%s')
  79. ORDER BY timestamp DESC LIMIT %s, %s", $topicId, $topicId, $page * PostRenderer::PAGE_SIZE, PostRenderer::PAGE_SIZE);
  80. $posts = $this->database->query($sql);
  81. $posts = $this->addCommentCount($posts);
  82. $user = new User();
  83. $user->populate();
  84. $userId = $user->getId();
  85. if ($userId) {
  86. $posts = $this->addPostDifferent($posts, $userId);
  87. }
  88. $postViewData = array(
  89. "posts" => $posts
  90. );
  91. return $view->render("postView.inc", $postViewData);
  92. }
  93. public function getPostsByUser($userId, $page = 0) {
  94. $view = new View();
  95. $sql = sprintf("SELECT p.post_id, u.user_id, p.timestamp, p.name as post_name, p.url_name as post_url_name, p.content, u.username, u.display_name as user_display_name, t.topic_id, t.display_name as topic_display_name
  96. FROM posts p
  97. JOIN post_topic_associations a ON p.post_id = a.post_id
  98. JOIN users u ON p.poster_user_id = u.user_id
  99. JOIN topics t on t.topic_id = a.topic_id
  100. WHERE p.poster_user_id = '%s'
  101. ORDER BY timestamp DESC LIMIT %s, %s", $userId, $page * PostRenderer::PAGE_SIZE, PostRenderer::PAGE_SIZE);
  102. $posts = $this->database->query($sql);
  103. $posts = $this->addCommentCount($posts);
  104. $post = $this->addPostDifferent($posts, $userId);
  105. $postViewData = array(
  106. "posts" => $posts
  107. );
  108. return $view->render("postView.inc", $postViewData);
  109. }
  110. public function getPostedTaggedIn($userId, $page = 0) {
  111. $view = new View();
  112. $sql = sprintf("SELECT p.post_id, u.user_id, p.timestamp, p.name as post_name, p.url_name as post_url_name, p.content, u.username, u.display_name as user_display_name, t.topic_id, t.display_name as topic_display_name
  113. FROM posts p
  114. JOIN post_topic_associations a ON p.post_id = a.post_id
  115. JOIN post_tags pt ON pt.post_id = p.post_id
  116. JOIN user_tags ut ON ut.tag_id = pt.tag_id
  117. JOIN users u ON ut.user_id = u.user_id
  118. JOIN topics t on t.topic_id = a.topic_id
  119. WHERE ut.user_id = '%s'
  120. ORDER BY ut.tagged_on DESC LIMIT %s, %s", $userId, $page * PostRenderer::PAGE_SIZE, PostRenderer::PAGE_SIZE);
  121. $posts = $this->database->query($sql);
  122. $posts = $this->addCommentCount($posts);
  123. $post = $this->addPostDifferent($posts, $userId);
  124. $postViewData = array(
  125. "posts" => $posts
  126. );
  127. return $view->render("postView.inc", $postViewData);
  128. }
  129. }