PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/system/cms/modules/comments/models/comments_m.php

http://github.com/pyrocms/pyrocms
PHP | 237 lines | 141 code | 26 blank | 70 comment | 10 complexity | ccf542adfc11d96a5dc0df1de0d5c0f6 MD5 | raw file
Possible License(s): CC0-1.0, MIT
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * Comments model
  4. *
  5. * @package PyroCMS
  6. * @subpackage Comments Module
  7. * @category Models
  8. * @author Phil Sturgeon - PyroCMS Dev Team
  9. */
  10. class Comments_m extends MY_Model
  11. {
  12. /**
  13. * Get a comment based on the ID
  14. * @access public
  15. * @param int $id The ID of the comment
  16. * @return array
  17. */
  18. public function get($id)
  19. {
  20. $this->db->select('c.*')
  21. ->select('IF(c.user_id > 0, m.display_name, c.name) as name', false)
  22. ->select('IF(c.user_id > 0, u.email, c.email) as email', false)
  23. ->from('comments c')
  24. ->join('users u', 'c.user_id = u.id', 'left')
  25. ->join('profiles m', 'm.user_id = u.id', 'left')
  26. // If there is a comment user id, make sure the user still exists
  27. ->where('IF(c.user_id > 0, c.user_id = u.id, 1)')
  28. ->where('c.id', $id);
  29. return $this->db->get()->row();
  30. }
  31. /**
  32. * Get recent comments
  33. *
  34. * @access public
  35. * @param int $limit The amount of comments to get
  36. * @return array
  37. */
  38. public function get_recent($limit = 10)
  39. {
  40. $this->_get_all_setup();
  41. $this->db->order_by('c.created_on', 'desc');
  42. if ($limit > 0)
  43. {
  44. $this->db->limit($limit);
  45. }
  46. return $this->get_all();
  47. }
  48. /**
  49. * Get something based on a module item
  50. *
  51. * @access public
  52. * @param string $module The name of the module
  53. * @param int $ref_id The ID of the module
  54. * @param int $is_active Is the module active?
  55. * @return array
  56. */
  57. public function get_by_module_item($module, $ref_id, $is_active = 1)
  58. {
  59. $this->_get_all_setup();
  60. $this->db
  61. ->where('c.module', $module)
  62. ->where('c.module_id', $ref_id)
  63. ->where('c.is_active', $is_active)
  64. ->order_by('c.created_on', $this->settings->comment_order);
  65. return $this->get_all();
  66. }
  67. /**
  68. * Get all comments
  69. *
  70. * @access public
  71. * @return array
  72. */
  73. public function get_all()
  74. {
  75. return parent::get_all();
  76. }
  77. /**
  78. * Insert a new comment
  79. *
  80. * @access public
  81. * @param array $input The data to insert
  82. * @return void
  83. */
  84. public function insert($input)
  85. {
  86. $this->load->helper('date');
  87. return parent::insert(array(
  88. 'user_id' => isset($input['user_id']) ? $input['user_id'] : 0,
  89. 'is_active' => ! empty($input['is_active']) ? 1 : 0,
  90. 'name' => isset($input['name']) ? ucwords(strtolower(strip_tags($input['name']))) : '',
  91. 'email' => isset($input['email']) ? strtolower($input['email']) : '',
  92. 'website' => isset($input['website']) ? prep_url(strip_tags($input['website'])) : '',
  93. 'comment' => htmlspecialchars($input['comment'], NULL, FALSE),
  94. 'parsed' => parse_markdown(htmlspecialchars($input['comment'], NULL, FALSE)),
  95. 'module' => $input['module'],
  96. 'module_id' => $input['module_id'],
  97. 'created_on' => now(),
  98. 'ip_address' => $this->input->ip_address()
  99. ));
  100. }
  101. /**
  102. * Update an existing comment
  103. *
  104. * @access public
  105. * @param int $id The ID of the comment to update
  106. * @param array $input The array containing the data to update
  107. * @return void
  108. */
  109. public function update($id, $input)
  110. {
  111. $this->load->helper('date');
  112. return parent::update($id, array(
  113. 'name' => isset($input['name']) ? ucwords(strtolower(strip_tags($input['name']))) : '',
  114. 'email' => isset($input['email']) ? strtolower($input['email']) : '',
  115. 'website' => isset($input['website']) ? prep_url(strip_tags($input['website'])) : '',
  116. 'comment' => htmlspecialchars($input['comment'], NULL, FALSE),
  117. 'parsed' => parse_markdown(htmlspecialchars($input['comment'], NULL, FALSE)),
  118. ));
  119. }
  120. /**
  121. * Approve a comment
  122. *
  123. * @access public
  124. * @param int $id The ID of the comment to approve
  125. * @return mixed
  126. */
  127. public function approve($id)
  128. {
  129. return parent::update($id, array('is_active' => 1));
  130. }
  131. /**
  132. * Unapprove a comment
  133. *
  134. * @access public
  135. * @param int $id The ID of the comment to unapprove
  136. * @return mixed
  137. */
  138. public function unapprove($id)
  139. {
  140. return parent::update($id, array('is_active' => 0));
  141. }
  142. public function get_slugs()
  143. {
  144. $this->db->select('comments.module, modules.name')
  145. ->distinct()
  146. ->join('modules', 'comments.module = modules.slug', 'left');
  147. $slugs = parent::get_all();
  148. $options = array();
  149. if ( ! empty($slugs))
  150. {
  151. foreach($slugs as $slug)
  152. {
  153. if ( ! $slug->name && ($pos = strpos($slug->module, '-')) !== FALSE)
  154. {
  155. $slug->ori_module = $slug->module;
  156. $slug->module = substr($slug->module, 0, $pos);
  157. }
  158. if ( ! $slug->name && $module = $this->module_m->get_by('slug', plural($slug->module)))
  159. {
  160. $slug->name = $module->name;
  161. }
  162. //get the module name
  163. if ($slug->name AND $module_names = unserialize($slug->name))
  164. {
  165. if (array_key_exists(CURRENT_LANGUAGE, $module_names))
  166. {
  167. $slug->name = $module_names[CURRENT_LANGUAGE];
  168. }
  169. else
  170. {
  171. $slug->name = $module_names['en'];
  172. }
  173. if (isset($slug->ori_module))
  174. {
  175. $options[$slug->ori_module] = $slug->name . " ($slug->ori_module)";
  176. }
  177. else
  178. {
  179. $options[$slug->module] = $slug->name;
  180. }
  181. }
  182. else
  183. {
  184. if (isset($slug->ori_module))
  185. {
  186. $options[$slug->ori_module] = $slug->ori_module;
  187. }
  188. else
  189. {
  190. $options[$slug->module] = $slug->module;
  191. }
  192. }
  193. }
  194. }
  195. asort($options);
  196. return $options;
  197. }
  198. /**
  199. * Setting up the query for the get* functions
  200. */
  201. private function _get_all_setup()
  202. {
  203. $this->_table = NULL;
  204. $this->db->select('c.*');
  205. $this->db->from('comments c');
  206. $this->db->select('IF(c.user_id > 0, m.display_name, c.name) as name', false);
  207. $this->db->select('IF(c.user_id > 0, u.email, c.email) as email', false);
  208. $this->db->join('users u', 'c.user_id = u.id', 'left');
  209. $this->db->join('profiles m', 'm.user_id = u.id', 'left');
  210. }
  211. }