TagModel.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. class TagModel {
  3. const PAGE_SIZE = 50;
  4. public function geAllTagsForUser($userId, $page = 0) {
  5. $sql = sprintf("SELECT ut.tag_id, ut.user_id, ut.is_seen, ut.tagged_on, ct.comment_id, pt.post_id FROM user_tags ut
  6. LEFT JOIN comment_tags ct ON ct.tag_id = ut.tag_id
  7. LEFT JOIN post_tags pt ON pt.tag_id = ut.tag_id
  8. WHERE ut.user_id = '%s'
  9. ORDER BY ut.tagged_on DESC
  10. LIMIT %s, %s", $userId, $page * TagModel::PAGE_SIZE, TagModel::PAGE_SIZE);
  11. $database = new Database();
  12. $allTagsData = $database->query($sql);
  13. $postModel = new PostModel();
  14. $commentModel = new CommentModel();
  15. foreach($allTagsData as $index => $tagData) {
  16. if($tagData['comment_id']) {
  17. $commentData = $commentModel->getComment($tagData['comment_id']);
  18. $allTagsData[$index]['commentData'] = $commentData;
  19. } else if($tagData['post_id']) {
  20. $postData = $postModel->getPost($tagData['post_id']);
  21. $allTagsData[$index]['postData'] = $postData;
  22. }
  23. }
  24. return $allTagsData;
  25. }
  26. public function getUnseenTagsForUser($userId, $page = 0) {
  27. $sql = sprintf("SELECT ut.tag_id, ut.user_id, ut.is_seen, ut.tagged_on, ct.comment_id, pt.post_id FROM user_tags ut
  28. LEFT JOIN comment_tags ct ON ct.tag_id = ut.tag_id
  29. LEFT JOIN post_tags pt ON pt.tag_id = ut.tag_id
  30. WHERE ut.user_id = '%s'
  31. AND ut.is_seen = 0
  32. ORDER BY ut.tagged_on DESC
  33. LIMIT %s, %s", $userId, $page * TagModel::PAGE_SIZE, TagModel::PAGE_SIZE);
  34. $database = new Database();
  35. $allTagsData = $database->query($sql);
  36. $postModel = new PostModel();
  37. $commentModel = new CommentModel();
  38. foreach($allTagsData as $index => $tagData) {
  39. if($tagData['comment_id']) {
  40. $commentData = $commentModel->getComment($tagData['comment_id']);
  41. $allTagsData[$index]['commentData'] = $commentData;
  42. } else if($tagData['post_id']) {
  43. $postData = $postModel->getPost($tagData['post_id']);
  44. $allTagsData[$index]['postData'] = $postData;
  45. }
  46. }
  47. return $allTagsData;
  48. }
  49. public function markTagDataRead(array $tagDataRead) {
  50. $tagIds = array();
  51. foreach($tagDataRead as $index => $tagData) {
  52. if($tagData['is_seen'] == 0) {
  53. $tagIds[] = $tagData['tag_id'];
  54. }
  55. }
  56. if(count($tagIds) > 0) {
  57. $sql = sprintf("UPDATE user_tags SET is_seen = 1 WHERE tag_id IN (%s)", join(",", $tagIds));
  58. $database = new Database();
  59. $database->write($sql);
  60. }
  61. }
  62. }