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.

136 lines
3.4 KiB

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