/application/modules/forums/models/discussions_m.php

https://github.com/ChrisBaines/DoveForums · PHP · 282 lines · 191 code · 41 blank · 50 comment · 18 complexity · c578ef925c26dbc3b8dfe940f258488d MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. *
  4. * NOTICE OF LICENSE
  5. *
  6. * Licensed under the Open Software License version 3.0
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0) that is
  9. * bundled with this package in the files license.txt / license.rst. It is
  10. * also available through the world wide web at this URL:
  11. * http://opensource.org/licenses/OSL-3.0
  12. * If you did not receive a copy of the license and are unable to obtain it
  13. * through the world wide web, please send an email to
  14. * licensing@ellislab.com so we can send you a copy immediately.
  15. *
  16. * @package Dove Forums
  17. * @copyright Copyright (c) 2012 - Christopher Baines
  18. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  19. * @link http://www.doveforums.com
  20. * @since Version 2.0.0
  21. * @author Christopher Baines
  22. *
  23. */
  24. class discussions_m extends CI_Model {
  25. public function get_discussions($limit=NULL, $offset=NULL, $filter=NULL)
  26. {
  27. // Set hook.
  28. $this->dove_core->trigger_events('pre_get_all_discussions');
  29. // Select.
  30. $this->db->select('discussions.discussion_id, discussions.category_id, discussions.name as discussion_name,
  31. discussions.permalink as discussion_permalink, discussions.answered, discussions.created_by,
  32. discussions.created_date, discussions.created_ip, discussions.last_comment_by,
  33. discussions.last_comment_date, discussions.last_comment_ip, discussions.likes, discussions.announcement,
  34. discussions.closed, categories.name as category_name, categories.permalink as category_permalink,
  35. categories.description as category_description, users.username as created_by_username,
  36. users.email as created_by_email')
  37. ->join('categories', 'categories.id = discussions.category_id')
  38. ->join('users', 'users.id = discussions.created_by')
  39. ->order_by('announcement', 'desc')
  40. ->order_by('discussion_id', 'desc')
  41. ->order_by('answered', 'desc')
  42. ->limit($limit, $offset);
  43. // Options.
  44. if ( isset($filter) )
  45. {
  46. if ( strtolower($filter) == 'unanswered_discussions')
  47. {
  48. $this->db->where('answered', 0);
  49. }
  50. elseif ( strtolower($filter) == 'my_discussions')
  51. {
  52. $this->db->where('created_by', $this->session->userdata('user_id'));
  53. }
  54. }
  55. // Query.
  56. $query = $this->db->get($this->tables['discussions']);
  57. // Results.
  58. if($query->num_rows() > 0)
  59. {
  60. return $query->result();
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67. public function get_category_discussions($category_id, $limit=NULL, $offset=NULL)
  68. {
  69. // Set hook.
  70. $this->dove_core->trigger_events('pre_get_category_discussions');
  71. // Select.
  72. $query = $this->db->select('discussions.discussion_id, discussions.category_id, discussions.name as discussion_name,
  73. discussions.permalink as discussion_permalink, discussions.answered, discussions.created_by,
  74. discussions.created_date, discussions.created_ip, discussions.last_comment_by,
  75. discussions.last_comment_date, discussions.last_comment_ip, discussions.likes, discussions.announcement,
  76. discussions.closed, categories.name as category_name, categories.permalink as category_permalink,
  77. categories.description as category_description, users.username as created_by_username,
  78. users.email as created_by_email')
  79. ->join('categories', 'categories.id = discussions.category_id')
  80. ->join('users', 'users.id = discussions.created_by')
  81. ->order_by('announcement', 'desc')
  82. ->order_by('discussion_id', 'desc')
  83. ->order_by('answered', 'desc')
  84. ->limit($limit, $offset)
  85. ->where('category_id', $category_id)
  86. ->get($this->tables['discussions']);
  87. // Results.
  88. if($query->num_rows() > 0)
  89. {
  90. return $query->result();
  91. }
  92. else
  93. {
  94. return false;
  95. }
  96. }
  97. public function count_all_discussions()
  98. {
  99. // Query
  100. $query = $this->db->select('*')
  101. ->get($this->tables['discussions']);
  102. // Result
  103. return ( $query->num_rows() > 0 ? $query->num_rows() : 0 );
  104. }
  105. public function count_category_discussions($category_permalink)
  106. {
  107. if(!is_string($category_permalink))
  108. {
  109. return NULL;
  110. }
  111. // Get category ID.
  112. $category_id = $this->categories->get_id_by_category_permalink($category_permalink);
  113. // Query
  114. $query = $this->db->select('*')
  115. ->where('category_id', $category_id)
  116. ->get($this->tables['discussions']);
  117. // Result
  118. return ( $query->num_rows() > 0 ? $query->num_rows() : 0 );
  119. }
  120. public function count_unanswered_discussions()
  121. {
  122. // Query
  123. $query = $this->db->select('*')
  124. ->where('answered', '0')
  125. ->get($this->tables['discussions']);
  126. // Result
  127. return ( $query->num_rows() > 0 ? $query->num_rows() : 0 );
  128. }
  129. public function count_user_discussions($user_id)
  130. {
  131. if(!is_int($user_id))
  132. {
  133. return NULL;
  134. }
  135. // Query.
  136. $query = $this->db->select('*')
  137. ->where('created_by', $user_id)
  138. ->get($this->tables['discussions']);
  139. // Result.
  140. return ( $query->num_rows() > 0 ? $query->num_rows() : 0 );
  141. }
  142. public function add_discussion($discussion_data, $comment_data)
  143. {
  144. if(!is_array($discussion_data) || !is_array($comment_data))
  145. {
  146. return NULL;
  147. }
  148. // Trans start.
  149. $this->db->trans_start();
  150. // Insert.
  151. $this->db->insert($this->tables['discussions'], $discussion_data);
  152. $insert_id = $this->db->insert_id();
  153. // Add insert id to comments array.
  154. $comment_data['discussion_id'] = $insert_id;
  155. $this->db->insert($this->tables['comments'], $comment_data);
  156. // Trans end.
  157. $this->db->trans_complete();
  158. if ($this->db->trans_status() === FALSE)
  159. {
  160. $this->dove_core->set_error('create_discussion_failed.');
  161. $this->db->trans_rollback();
  162. return FALSE;
  163. }
  164. else
  165. {
  166. $this->dove_core->set_message('create_discussion_successful');
  167. $this->db->trans_commit();
  168. return TRUE;
  169. }
  170. }
  171. public function get_id_from_permalink($permalink)
  172. {
  173. if(!is_string($permalink))
  174. {
  175. return NULL;
  176. }
  177. // Query.
  178. $query = $this->db->select('discussion_id')
  179. ->limit(1)
  180. ->where('permalink', $permalink)
  181. ->get($this->tables['discussions']);
  182. // Result.
  183. return ( $query->num_rows() > 0 ? $query->result() : 0 );
  184. }
  185. public function delete($discussion_id)
  186. {
  187. if(!is_int($discussion_id))
  188. {
  189. return NULL;
  190. }
  191. // Delete.
  192. $delete = $this->dove_core->delete(array('discussion_id' => $discussion_id), $this->tables['discussions']);
  193. if ( $delete === TRUE )
  194. {
  195. $delete = $this->dove_core->delete(array('discussion_id' => $discussion_id), $this->tables['comments']);
  196. if ( $delete === TRUE )
  197. {
  198. $this->dove_core->set_message('remove_discussion_success');
  199. return TRUE;
  200. }
  201. else
  202. {
  203. $this->dove_core->set_error('remove_discussion_failed');
  204. return FALSE;
  205. }
  206. }
  207. else
  208. {
  209. $this->dove_core->set_error('remove_discussion_failed');
  210. return FALSE;
  211. }
  212. }
  213. public function get_discussion_id_from_permalink($discussion_permalink)
  214. {
  215. if(!is_string($discussion_permalink))
  216. {
  217. return NULL;
  218. }
  219. // Query.
  220. $query = $this->db->select('discussion_id')
  221. ->limit(1)
  222. ->where('permalink', $discussion_permalink)
  223. ->get($this->tables['discussions']);
  224. // Result.
  225. return ( $query->num_rows() > 0 ? $query->result() : false );
  226. }
  227. public function get_discussion_name_from_permalink($discussion_permalink)
  228. {
  229. if(!is_string($discussion_permalink))
  230. {
  231. return NULL;
  232. }
  233. // Query.
  234. $query = $this->db->select('name')
  235. ->limit(1)
  236. ->where('permalink', $discussion_permalink)
  237. ->get('discussions');
  238. // Result.
  239. return ( $query->num_rows() > 0 ? $query->row('name') : false );
  240. }
  241. }