PageRenderTime 132ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

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

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