A new, object-oriented, better vesion of μForum
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.1 KiB

  1. <?php
  2. require_once('./inc/model.class.php');
  3. class PostsModel extends Model
  4. {
  5. private $topic_info = null;
  6. private $post_info = null;
  7. public function getTopic($topic_id)
  8. {
  9. if ($this->topic_info == null)
  10. {
  11. $query = '
  12. SELECT
  13. t.topic_id as topic_id, t.title as topic_title, t.locked as topic_locked, t.forum_id as forum_id, f.name as forum_name, f.locked as forum_locked, pc.post_count as post_count
  14. FROM '.TOPICS_TABLE.' t
  15. LEFT JOIN '.FORUMS_TABLE.' f ON f.forum_id = t.forum_id
  16. LEFT JOIN '.TOPICS_PC_VIEW.' pc ON pc.topic_id = t.topic_id
  17. WHERE t.topic_id=\''.$topic_id.'\'';
  18. $out = $this->select_query($query);
  19. if (count($out) > 0)
  20. $this->topic_info = $out[0];
  21. }
  22. return $this->topic_info;
  23. }
  24. public function getPosts($topic_id)
  25. {
  26. $out = $this->select (POSTS_VIEW, '*', 'topic_id=\''.$topic_id.'\'', 'post_id ASC');
  27. if ($out != null)
  28. return $out;
  29. else
  30. return null;
  31. }
  32. public function getPost($post_id)
  33. {
  34. $out = $this->select (POSTS_VIEW, '*', 'post_id=\''.$post_id.'\'');
  35. if (count($out) > 0)
  36. return $out[0];
  37. else
  38. return null;
  39. }
  40. public function getFirstPost($topic_id)
  41. {
  42. $out = $this->select (POSTS_VIEW, 'post_id', 'topic_id=\''.$topic_id.'\'', 'post_id ASC', 1);
  43. if (count($out) > 0)
  44. return $out[0];
  45. else
  46. return null;
  47. }
  48. //mod options
  49. public function deletePost($post_id)
  50. {
  51. $query = 'DELETE FROM '.POSTS_TABLE.' WHERE post_id=\''.$post_id.'\';';
  52. $this->db->query($query);
  53. }
  54. public function deleteTopic($topic_id)
  55. {
  56. $query = 'call delete_topic(\''.$topic_id.'\');';
  57. $this->db->query($query);
  58. }
  59. public function lockTopic($topic_id, $locked = true)
  60. {
  61. $query = 'UPDATE '.TOPICS_TABLE.' SET locked=\''.$locked.'\' WHERE topic_id=\''.$topic_id.'\';';
  62. $this->db->query($query);
  63. }
  64. public function moveTopic($topic_id, $forum_id)
  65. {
  66. $query = 'UPDATE '.TOPICS_TABLE.' SET forum_id=\''.$forum_id.'\' WHERE topic_id=\''.$topic_id.'\';';
  67. $this->db->query($query);
  68. }
  69. public function addTopic($title, $content, $forum_id, $user_id)
  70. {
  71. $query = 'call add_topic(\''.$title.'\',
  72. \''.$content.'\',
  73. \''.$forum_id.'\',
  74. \''.$user_id.'\',
  75. \''.$_SERVER['HTTP_USER_AGENT'].'\',
  76. @out);';
  77. $this->db->query($query);
  78. $result = $this->db->query('select @out as topic_id');
  79. if ($result != null)
  80. return $result->fetch_assoc()['topic_id'];
  81. else
  82. return null;
  83. }
  84. public function changePost($post_id, $content)
  85. {
  86. $query = 'UPDATE '.POSTS_TABLE.' SET `content`=\''.$content.'\' WHERE `post_id`=\''.$post_id.'\'';
  87. $this->db->query($query);
  88. }
  89. public function changeTopic($topic_id, $title)
  90. {
  91. $query = 'UPDATE '.TOPICS_TABLE.' SET `title`=\''.$title.'\' WHERE `topic_id`=\''.$topic_id.'\'';
  92. $this->db->query($query);
  93. }
  94. public function addPost($topic_id, $user_id, $content)
  95. {
  96. $query = 'INSERT INTO '.POSTS_TABLE.'
  97. (post_id, topic_id, user_id, content, date, user_agent)
  98. VALUES
  99. (NULL, \''.$topic_id.'\', \''.$user_id.'\', \''.$content.'\', NOW(), \''.$_SERVER['HTTP_USER_AGENT'].'\')';
  100. $this->db->query($query);
  101. }
  102. }
  103. ?>