/application/modules/core/comments/controllers/admin.php

https://github.com/baltag/pyrocms · PHP · 306 lines · 227 code · 45 blank · 34 comment · 30 complexity · 126952333db2d5a49a7a68b80851f476 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Admin extends Admin_Controller
  3. {
  4. function __construct()
  5. {
  6. parent::Admin_Controller();
  7. $this->load->library('validation');
  8. $this->load->model('comments_m');
  9. $this->lang->load('comments');
  10. $this->template->set_partial('sidebar', 'admin/sidebar');
  11. }
  12. // Admin: List all comments
  13. public function index()
  14. {
  15. $this->load->helper('text');
  16. // Create pagination links
  17. $total_rows = $this->comments_m->count_by('is_active', 0);
  18. $this->data->pagination = create_pagination('admin/comments/index', $total_rows);
  19. // get all comments
  20. $this->data->comments = $this->comments_m->limit($this->data->pagination['limit'])->get_many_by('comments.is_active', 0);
  21. $this->template->build('admin/index', $this->data);
  22. }
  23. public function action()
  24. {
  25. if( $this->input->post('btnAction') )
  26. {
  27. // Get the action
  28. $id_array = $this->input->post('action_to');
  29. // Switch statement
  30. switch( strtolower( $this->input->post('btnAction') ) )
  31. {
  32. // Approve the comment
  33. case 'approve':
  34. // Loop through each ID
  35. foreach($id_array as $key => $value)
  36. {
  37. // Multiple ones ?
  38. if(count($id_array) > 1)
  39. {
  40. $this->approve($value,FALSE,TRUE);
  41. }
  42. else
  43. {
  44. $this->approve($value,FALSE);
  45. }
  46. }
  47. break;
  48. // Unapprove the comment
  49. case 'unapprove':
  50. // Loop through each ID
  51. foreach($id_array as $key => $value)
  52. {
  53. // Multiple ones ?
  54. if(count($id_array) > 1)
  55. {
  56. $this->unapprove($value,FALSE,TRUE);
  57. }
  58. else
  59. {
  60. $this->unapprove($value,FALSE);
  61. }
  62. }
  63. break;
  64. // Delete the comment
  65. case 'delete':
  66. $this->delete();
  67. break;
  68. }
  69. // Redirect
  70. redirect('admin/comments');
  71. }
  72. }
  73. public function active()
  74. {
  75. $this->load->helper('text');
  76. // Create pagination links
  77. $total_rows = $this->comments_m->count_by('is_active', 1);
  78. $this->data->pagination = create_pagination('admin/comments/active', $total_rows);
  79. // get all comments
  80. $this->data->comments = $this->comments_m->limit($this->data->pagination['limit'])->get_many_by('comments.is_active', 1);
  81. $this->template->build('admin/index', $this->data);
  82. }
  83. // Admin: Edit a comment
  84. public function edit($id = 0)
  85. {
  86. if (!$id)
  87. {
  88. redirect('admin/comments');
  89. }
  90. $rules['name'] = 'trim';
  91. $rules['email'] = 'trim|valid_email';
  92. $rules['website'] = 'trim';
  93. $rules['comment'] = 'trim|required';
  94. if(!$this->user_lib->logged_in())
  95. {
  96. $rules['name'] .= '|required';
  97. $rules['email'] .= '|required';
  98. }
  99. $this->validation->set_rules($rules);
  100. $this->validation->set_fields();
  101. $comment = $this->comments_m->get($id);
  102. // Validation Successful ------------------------------
  103. if ($this->validation->run())
  104. {
  105. if($comment->user_id > 0)
  106. {
  107. $commenter['user_id'] = $this->input->post('user_id');
  108. }
  109. else
  110. {
  111. $commenter['name'] = $this->input->post('name');
  112. $commenter['email'] = $this->input->post('email');
  113. }
  114. $comment = array_merge($commenter, array(
  115. 'comment' => $this->input->post('comment'),
  116. 'website' => $this->input->post('website'),
  117. 'module' => $this->input->post('module'),
  118. 'module_id' => $this->input->post('module_id')
  119. ));
  120. if($this->comments_m->update( $id, $comment ))
  121. {
  122. $this->session->set_flashdata( 'success', lang('comments.edit_success') );
  123. }
  124. else
  125. {
  126. $this->session->set_flashdata( 'error', lang('comments.edit_error') );
  127. }
  128. redirect('admin/comments');
  129. }
  130. // Go through all the known fields and get the post values
  131. foreach(array_keys($rules) as $field)
  132. {
  133. if($this->input->post($field))
  134. {
  135. $comment->{$field} = $this->validation->$field;
  136. }
  137. }
  138. $this->data->comment =& $comment;
  139. // Load WYSIWYG editor
  140. $this->template->append_metadata( $this->load->view('fragments/wysiwyg', $this->data, TRUE) );
  141. $this->template->build('admin/form', $this->data);
  142. }
  143. // Admin: Delete a comment
  144. public function delete($id = 0)
  145. {
  146. // Delete one
  147. $ids = ($id) ? array($id) : $this->input->post('action_to');
  148. // Go through the array of ids to delete
  149. $comments = array();
  150. foreach ($ids as $id)
  151. {
  152. // Get the current comment so we can grab the id too
  153. if($comment = $this->comments_m->get($id))
  154. {
  155. $this->comments_m->delete($id);
  156. // Wipe cache for this model, the content has changed
  157. $this->cache->delete('comment_m');
  158. $comments[] = $comment->id;
  159. }
  160. }
  161. // Some comments have been deleted
  162. if(!empty($comments))
  163. {
  164. // Only deleting one comment
  165. if(count( $comments ) == 1)
  166. {
  167. $this->session->set_flashdata( 'success', sprintf(lang('comments.delete_single_success'), $comments[0]) );
  168. }
  169. // Deleting multiple comments
  170. else
  171. {
  172. $this->session->set_flashdata( 'success', sprintf( lang('comments.delete_multi_success'), implode( ', #', $comments ) ) );
  173. }
  174. }
  175. // For some reason, none of them were deleted
  176. else
  177. {
  178. $this->session->set_flashdata( 'error', lang('comments.delete_error') );
  179. }
  180. redirect('admin/comments');
  181. }
  182. // Admin: activate a comment
  183. public function approve($id = 0, $redirect = TRUE, $multiple = FALSE)
  184. {
  185. if (!$id)
  186. {
  187. redirect('admin/comments');
  188. }
  189. if($this->comments_m->approve($id))
  190. {
  191. // Unapprove multiple comments ?
  192. if($multiple == TRUE)
  193. {
  194. $this->session->set_flashdata( array('success'=> lang('comments.approve_success_multiple')));
  195. }
  196. else
  197. {
  198. $this->session->set_flashdata( array('success'=> lang('comments.approve_success')));
  199. }
  200. }
  201. else
  202. {
  203. // Error for multiple comments ?
  204. if($multiple == TRUE)
  205. {
  206. $this->session->set_flashdata( array('error'=> lang('comments.approve_error_multiple')) );
  207. }
  208. else
  209. {
  210. $this->session->set_flashdata( array('error'=> lang('comments.approve_error')) );
  211. }
  212. }
  213. if($redirect == TRUE)
  214. {
  215. redirect('admin/comments');
  216. }
  217. }
  218. // Admin: deativate a comment
  219. public function unapprove($id = 0,$redirect = TRUE,$multiple = FALSE)
  220. {
  221. if (!$id)
  222. {
  223. redirect('admin/comments');
  224. }
  225. if($this->comments_m->unapprove($id))
  226. {
  227. // Unapprove multiple comments ?
  228. if($multiple == TRUE)
  229. {
  230. $this->session->set_flashdata( array('success'=> lang('comments.unapprove_success_multiple')) );
  231. }
  232. else
  233. {
  234. $this->session->set_flashdata( array('success'=> lang('comments.unapprove_success')) );
  235. }
  236. }
  237. else
  238. {
  239. // Error for multiple comments ?
  240. if($multiple == TRUE)
  241. {
  242. $this->session->set_flashdata( array('error'=> lang('comments.unapprove_error_multiple')) );
  243. }
  244. else
  245. {
  246. $this->session->set_flashdata( array('error'=> lang('comments.unapprove_error')) );
  247. }
  248. }
  249. if($redirect == TRUE)
  250. {
  251. redirect('admin/comments');
  252. }
  253. }
  254. public function preview($id = 0)
  255. {
  256. $this->data->comment = $this->comments_m->get($id);
  257. $this->template->set_layout(FALSE);
  258. $this->template->build('admin/preview', $this->data);
  259. }
  260. }
  261. ?>