PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/comments/controllers/admin.php

http://github.com/pyrocms/pyrocms
PHP | 289 lines | 172 code | 44 blank | 73 comment | 15 complexity | 4d2d2f44aaaba85e8a9de0b2339aace9 MD5 | raw file
Possible License(s): CC0-1.0, MIT
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. *
  4. * @author Phil Sturgeon - PyroCMS Dev Team
  5. * @package PyroCMS
  6. * @subpackage Comments
  7. * @category Module
  8. */
  9. class Admin extends Admin_Controller {
  10. /**
  11. * Array that contains the validation rules
  12. * @access private
  13. * @var array
  14. */
  15. private $validation_rules = array(
  16. array(
  17. 'field' => 'name',
  18. 'label' => 'lang:comments.name_label',
  19. 'rules' => 'trim'
  20. ),
  21. array(
  22. 'field' => 'email',
  23. 'label' => 'lang:comments.email_label',
  24. 'rules' => 'trim|valid_email'
  25. ),
  26. array(
  27. 'field' => 'website',
  28. 'label' => 'lang:comments.website_label',
  29. 'rules' => 'trim'
  30. ),
  31. array(
  32. 'field' => 'comment',
  33. 'label' => 'lang:comments.send_label',
  34. 'rules' => 'trim|required'
  35. ),
  36. );
  37. /**
  38. * Constructor method
  39. * @access public
  40. * @return void
  41. */
  42. public function __construct()
  43. {
  44. parent::__construct();
  45. // Load the required libraries, models, etc
  46. $this->load->library('form_validation');
  47. $this->load->model('comments_m');
  48. $this->lang->load('comments');
  49. // Set the validation rules
  50. $this->form_validation->set_rules($this->validation_rules);
  51. }
  52. /**
  53. * Index
  54. * @access public
  55. * @return void
  56. */
  57. public function index()
  58. {
  59. // Only show is_active = 0 if we are moderating comments
  60. $base_where = array('comments.is_active' => (int) ! Settings::get('moderate_comments'));
  61. //capture active
  62. $base_where['comments.is_active'] = is_int($this->session->flashdata('is_active')) ? $this->session->flashdata('is_active') : $base_where['comments.is_active'];
  63. $base_where['comments.is_active'] = $this->input->post('f_active') ? (int) $this->input->post('f_active') : $base_where['comments.is_active'];
  64. //capture module slug
  65. $base_where = $this->input->post('module_slug') ? $base_where + array('module' => $this->input->post('module_slug')) : $base_where;
  66. // Create pagination links
  67. $total_rows = $this->comments_m->count_by($base_where);
  68. $pagination = create_pagination('admin/comments/index', $total_rows);
  69. $comments = $this->comments_m
  70. ->limit($pagination['limit'])
  71. ->order_by('comments.created_on', 'desc')
  72. ->get_many_by($base_where);
  73. $content_title = $base_where['comments.is_active'] ? lang('comments.active_title') : lang('comments.inactive_title');
  74. $this->input->is_ajax_request() && $this->template->set_layout(FALSE);
  75. $module_list = $this->comments_m->get_slugs();
  76. $this->template
  77. ->title($this->module_details['name'])
  78. ->append_metadata( js('admin/filter.js') )
  79. ->set('module_list', $module_list)
  80. ->set('content_title', $content_title)
  81. ->set('comments', process_comment_items($comments))
  82. ->set('comments_active', $base_where['comments.is_active'])
  83. ->set('pagination', $pagination);
  84. $this->input->is_ajax_request() ? $this->template->build('admin/tables/comments') : $this->template->build('admin/index');
  85. }
  86. /**
  87. * Action method, called whenever the user submits the form
  88. * @access public
  89. * @return void
  90. */
  91. public function action()
  92. {
  93. $action = strtolower($this->input->post('btnAction'));
  94. if ($action)
  95. {
  96. // Get the id('s)
  97. $id_array = $this->input->post('action_to');
  98. // Call the action we want to do
  99. if (method_exists($this, $action))
  100. {
  101. $this->{$action}($id_array);
  102. }
  103. }
  104. redirect('admin/comments');
  105. }
  106. /**
  107. * Edit an existing comment
  108. * @access public
  109. * @return void
  110. */
  111. public function edit($id = 0)
  112. {
  113. $id OR redirect('admin/comments');
  114. // Get the comment based on the ID
  115. $comment = $this->comments_m->get($id);
  116. // Validate the results
  117. if ($this->form_validation->run())
  118. {
  119. if ($comment->user_id > 0)
  120. {
  121. $commenter['user_id'] = $this->input->post('user_id');
  122. }
  123. else
  124. {
  125. $commenter['name'] = $this->input->post('name');
  126. $commenter['email'] = $this->input->post('email');
  127. }
  128. $comment = array_merge($commenter, array(
  129. 'comment' => $this->input->post('comment'),
  130. 'website' => $this->input->post('website')
  131. ));
  132. // Update the comment
  133. $this->comments_m->update($id, $comment)
  134. ? $this->session->set_flashdata('success', lang('comments.edit_success'))
  135. : $this->session->set_flashdata('error', lang('comments.edit_error'));
  136. redirect('admin/comments');
  137. }
  138. // Loop through each rule
  139. foreach ($this->validation_rules as $rule)
  140. {
  141. if ($this->input->post($rule['field']) !== FALSE)
  142. {
  143. $comment->{$rule['field']} = $this->input->post($rule['field']);
  144. }
  145. }
  146. $this->template
  147. ->title($this->module_details['name'], sprintf(lang('comments.edit_title'), $comment->id))
  148. ->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
  149. ->set('comment', $comment)
  150. ->build('admin/form', $this->data);
  151. }
  152. // Admin: Delete a comment
  153. public function delete($ids)
  154. {
  155. // Check for one
  156. $ids = ( ! is_array($ids)) ? array($ids) : $ids;
  157. // Go through the array of ids to delete
  158. $comments = array();
  159. foreach ($ids as $id)
  160. {
  161. // Get the current comment so we can grab the id too
  162. if ($comment = $this->comments_m->get($id))
  163. {
  164. $this->comments_m->delete((int) $id);
  165. // Wipe cache for this model, the content has changed
  166. $this->pyrocache->delete('comment_m');
  167. $comments[] = $comment->id;
  168. }
  169. }
  170. // Some comments have been deleted
  171. if ( ! empty($comments))
  172. {
  173. (count($comments) == 1)
  174. ? $this->session->set_flashdata('success', sprintf(lang('comments.delete_single_success'), $comments[0])) /* Only deleting one comment */
  175. : $this->session->set_flashdata('success', sprintf(lang('comments.delete_multi_success'), implode(', #', $comments ))); /* Deleting multiple comments */
  176. }
  177. // For some reason, none of them were deleted
  178. else
  179. {
  180. $this->session->set_flashdata('error', lang('comments.delete_error'));
  181. }
  182. redirect('admin/comments');
  183. }
  184. /**
  185. * Approve a comment
  186. * @access public
  187. * @param mixed $ids id or array of ids to process
  188. * @param bool $redirect optional if a redirect should be done
  189. * @return void
  190. */
  191. public function approve($id = 0, $redirect = TRUE)
  192. {
  193. $id && $this->_do_action($id, 'approve');
  194. $redirect AND redirect('admin/comments');
  195. }
  196. /**
  197. * Unapprove a comment
  198. * @access public
  199. * @param mixed $ids id or array of ids to process
  200. * @param bool $redirect optional if a redirect should be done
  201. * @return void
  202. */
  203. public function unapprove($id = 0, $redirect = TRUE)
  204. {
  205. $id && $this->_do_action($id, 'unapprove');
  206. if ($redirect)
  207. {
  208. $this->session->set_flashdata('is_active', 1);
  209. redirect('admin/comments');
  210. }
  211. }
  212. /**
  213. * Do the actual work for approve/unapprove
  214. * @access protected
  215. * @param int|array $ids id or array of ids to process
  216. * @param string $action action to take: maps to model
  217. * @return void
  218. */
  219. protected function _do_action($ids, $action)
  220. {
  221. $ids = ( ! is_array($ids)) ? array($ids) : $ids;
  222. $multiple = (count($ids) > 1) ? '_multiple' : NULL;
  223. $status = 'success';
  224. foreach ($ids as $id)
  225. {
  226. if ( ! $this->comments_m->{$action}($id))
  227. {
  228. $status = 'error';
  229. break;
  230. }
  231. if ($action == 'approve')
  232. {
  233. // add an event so third-party devs can hook on
  234. Events::trigger('comment_approved', $this->comments_m->get($id));
  235. }
  236. }
  237. $this->session->set_flashdata(array($status => lang('comments.' . $action . '_' . $status . $multiple)));
  238. }
  239. public function preview($id = 0)
  240. {
  241. $this->data->comment = $this->comments_m->get($id);
  242. $this->template->set_layout(FALSE);
  243. $this->template->build('admin/preview', $this->data);
  244. }
  245. }