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.

129 lines
3.2 KiB

  1. <?php
  2. /**
  3. * @package uForum2
  4. * @file inc/models/PostsModel.php
  5. * @copyright 2007-2015 (c) PioDer <piotrek@pioder.pl>
  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.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 moveTopic($topic_id, $forum_id)
  71. {
  72. $query = 'UPDATE '.TOPICS_TABLE.' SET forum_id=\''.$forum_id.'\' WHERE topic_id=\''.$topic_id.'\';';
  73. $this->db->query($query);
  74. }
  75. public function addTopic($title, $content, $forum_id, $user_id)
  76. {
  77. $query = 'call add_topic(\''.$title.'\',
  78. \''.$content.'\',
  79. \''.$forum_id.'\',
  80. \''.$user_id.'\',
  81. \''.$_SERVER['HTTP_USER_AGENT'].'\',
  82. @out);';
  83. $this->db->query($query);
  84. $result = $this->db->query('select @out as topic_id');
  85. if ($result != null)
  86. return $result->fetch_assoc()['topic_id'];
  87. else
  88. return null;
  89. }
  90. public function changePost($post_id, $content)
  91. {
  92. $query = 'UPDATE '.POSTS_TABLE.' SET `content`=\''.$content.'\' WHERE `post_id`=\''.$post_id.'\'';
  93. $this->db->query($query);
  94. }
  95. public function changeTopic($topic_id, $title)
  96. {
  97. $query = 'UPDATE '.TOPICS_TABLE.' SET `title`=\''.$title.'\' WHERE `topic_id`=\''.$topic_id.'\'';
  98. $this->db->query($query);
  99. }
  100. public function addPost($topic_id, $user_id, $content)
  101. {
  102. $query = 'INSERT INTO '.POSTS_TABLE.'
  103. (post_id, topic_id, user_id, content, date, user_agent)
  104. VALUES
  105. (NULL, \''.$topic_id.'\', \''.$user_id.'\', \''.$content.'\', NOW(), \''.$_SERVER['HTTP_USER_AGENT'].'\')';
  106. $this->db->query($query);
  107. }
  108. }
  109. ?>