PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/kadoshmt/Pyro-Deals
PHP | 535 lines | 347 code | 69 blank | 119 comment | 34 complexity | 5397543c713ceec569fb93f0be7c42ff MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. *
  4. * @package PyroCMS
  5. * @subpackage Categories
  6. * @category Module
  7. */
  8. class Admin extends Admin_Controller {
  9. /**
  10. * The id of post
  11. * @access protected
  12. * @var int
  13. */
  14. protected $id = 0;
  15. /**
  16. * Array that contains the validation rules
  17. * @access protected
  18. * @var array
  19. */
  20. protected $validation_rules = array(
  21. array(
  22. 'field' => 'title',
  23. 'label' => 'lang:blog_title_label',
  24. 'rules' => 'trim|htmlspecialchars|required|max_length[100]|callback__check_title'
  25. ),
  26. array(
  27. 'field' => 'slug',
  28. 'label' => 'lang:blog_slug_label',
  29. 'rules' => 'trim|required|alpha_dot_dash|max_length[100]|callback__check_slug'
  30. ),
  31. array(
  32. 'field' => 'category_id',
  33. 'label' => 'lang:blog_category_label',
  34. 'rules' => 'trim|numeric'
  35. ),
  36. array(
  37. 'field' => 'intro',
  38. 'label' => 'lang:blog_intro_label',
  39. 'rules' => 'trim|required'
  40. ),
  41. array(
  42. 'field' => 'body',
  43. 'label' => 'lang:blog_content_label',
  44. 'rules' => 'trim|required'
  45. ),
  46. array(
  47. 'field' => 'status',
  48. 'label' => 'lang:blog_status_label',
  49. 'rules' => 'trim|alpha'
  50. ),
  51. array(
  52. 'field' => 'created_on',
  53. 'label' => 'lang:blog_date_label',
  54. 'rules' => 'trim|required'
  55. ),
  56. array(
  57. 'field' => 'created_on_hour',
  58. 'label' => 'lang:blog_created_hour',
  59. 'rules' => 'trim|numeric|required'
  60. ),
  61. array(
  62. 'field' => 'created_on_minute',
  63. 'label' => 'lang:blog_created_minute',
  64. 'rules' => 'trim|numeric|required'
  65. ),
  66. array(
  67. 'field' => 'comments_enabled',
  68. 'label' => 'lang:blog_comments_enabled_label',
  69. 'rules' => 'trim|numeric'
  70. )
  71. );
  72. /**
  73. * The constructor
  74. * @access public
  75. * @return void
  76. */
  77. public function __construct()
  78. {
  79. parent::Admin_Controller();
  80. $this->load->model('blog_m');
  81. $this->load->model('blog_categories_m');
  82. $this->lang->load('blog');
  83. $this->lang->load('categories');
  84. // Date ranges for select boxes
  85. $this->data->hours = array_combine($hours = range(0, 23), $hours);
  86. $this->data->minutes = array_combine($minutes = range(0, 59), $minutes);
  87. $this->data->categories = array();
  88. if ($categories = $this->blog_categories_m->order_by('title')->get_all())
  89. {
  90. foreach ($categories as $category)
  91. {
  92. $this->data->categories[$category->id] = $category->title;
  93. }
  94. }
  95. $this->template
  96. ->append_metadata( css('blog.css', 'blog') )
  97. ->set_partial('shortcuts', 'admin/partials/shortcuts');
  98. }
  99. /**
  100. * Show all created blog posts
  101. * @access public
  102. * @return void
  103. */
  104. public function index()
  105. {
  106. //set the base/default where clause
  107. $base_where = array('show_future' => TRUE, 'status' => 'all');
  108. //add post values to base_where if f_module is posted
  109. $base_where = $this->input->post('f_category') ? $base_where + array('category' => $this->input->post('f_category')) : $base_where;
  110. $base_where['status'] = $this->input->post('f_status') ? $this->input->post('f_status') : $base_where['status'];
  111. $base_where = $this->input->post('f_keywords') ? $base_where + array('keywords' => $this->input->post('f_keywords')) : $base_where;
  112. // Create pagination links
  113. $total_rows = $this->blog_m->count_by($base_where);
  114. $pagination = create_pagination('admin/blog/index', $total_rows);
  115. // Using this data, get the relevant results
  116. $blog = $this->blog_m->limit($pagination['limit'])->get_many_by($base_where);
  117. foreach ($blog as &$post)
  118. {
  119. $post->author = $this->ion_auth->get_user($post->author_id);
  120. }
  121. //do we need to unset the layout because the request is ajax?
  122. $this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
  123. $this->template
  124. ->title($this->module_details['name'])
  125. ->set_partial('filters', 'admin/partials/filters')
  126. ->append_metadata(js('admin/filter.js'))
  127. ->set('pagination', $pagination)
  128. ->set('blog', $blog)
  129. ->build('admin/index', $this->data);
  130. }
  131. /**
  132. * Create new post
  133. * @access public
  134. * @return void
  135. */
  136. public function create()
  137. {
  138. $this->load->library('form_validation');
  139. $this->form_validation->set_rules($this->validation_rules);
  140. if ($this->input->post('created_on'))
  141. {
  142. $created_on = strtotime(sprintf('%s %s:%s', $this->input->post('created_on'), $this->input->post('created_on_hour'), $this->input->post('created_on_minute')));
  143. }
  144. else
  145. {
  146. $created_on = now();
  147. }
  148. if ($this->form_validation->run())
  149. {
  150. // They are trying to put this live
  151. if ($this->input->post('status') == 'live')
  152. {
  153. role_or_die('blog', 'put_live');
  154. }
  155. $id = $this->blog_m->insert(array(
  156. 'title' => $this->input->post('title'),
  157. 'slug' => $this->input->post('slug'),
  158. 'category_id' => $this->input->post('category_id'),
  159. 'intro' => $this->input->post('intro'),
  160. 'body' => $this->input->post('body'),
  161. 'status' => $this->input->post('status'),
  162. 'created_on' => $created_on,
  163. 'comments_enabled' => $this->input->post('comments_enabled'),
  164. 'author_id' => $this->user->id
  165. ));
  166. if ($id)
  167. {
  168. $this->pyrocache->delete_all('blog_m');
  169. $this->session->set_flashdata('success', sprintf($this->lang->line('blog_post_add_success'), $this->input->post('title')));
  170. }
  171. else
  172. {
  173. $this->session->set_flashdata('error', $this->lang->line('blog_post_add_error'));
  174. }
  175. // Redirect back to the form or main page
  176. $this->input->post('btnAction') == 'save_exit' ? redirect('admin/blog') : redirect('admin/blog/edit/' . $id);
  177. }
  178. else
  179. {
  180. // Go through all the known fields and get the post values
  181. foreach ($this->validation_rules as $key => $field)
  182. {
  183. $post->$field['field'] = set_value($field['field']);
  184. }
  185. $post->created_on = $created_on;
  186. }
  187. $this->template
  188. ->title($this->module_details['name'], lang('blog_create_title'))
  189. ->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
  190. ->append_metadata(js('blog_form.js', 'blog'))
  191. ->set('post', $post)
  192. ->build('admin/form');
  193. }
  194. /**
  195. * Edit blog post
  196. * @access public
  197. * @param int $id the ID of the blog post to edit
  198. * @return void
  199. */
  200. public function edit($id = 0)
  201. {
  202. $id OR redirect('admin/blog');
  203. $this->load->library('form_validation');
  204. $this->form_validation->set_rules($this->validation_rules);
  205. $post = $this->blog_m->get($id);
  206. $post->author = $this->ion_auth->get_user($post->author_id);
  207. // If we have a useful date, use it
  208. if ($this->input->post('created_on'))
  209. {
  210. $created_on = strtotime(sprintf('%s %s:%s', $this->input->post('created_on'), $this->input->post('created_on_hour'), $this->input->post('created_on_minute')));
  211. }
  212. else
  213. {
  214. $created_on = $post->created_on;
  215. }
  216. $this->id = $post->id;
  217. if ($this->form_validation->run())
  218. {
  219. // They are trying to put this live
  220. if ($post->status != 'live' and $this->input->post('status') == 'live')
  221. {
  222. role_or_die('blog', 'put_live');
  223. }
  224. $author_id = empty($post->author) ? $this->user->id : $post->author_id;
  225. $result = $this->blog_m->update($id, array(
  226. 'title' => $this->input->post('title'),
  227. 'slug' => $this->input->post('slug'),
  228. 'category_id' => $this->input->post('category_id'),
  229. 'intro' => $this->input->post('intro'),
  230. 'body' => $this->input->post('body'),
  231. 'status' => $this->input->post('status'),
  232. 'created_on' => $created_on,
  233. 'comments_enabled' => $this->input->post('comments_enabled'),
  234. 'author_id' => $author_id
  235. ));
  236. if ($result)
  237. {
  238. $this->session->set_flashdata(array('success' => sprintf($this->lang->line('blog_edit_success'), $this->input->post('title'))));
  239. // The twitter module is here, and enabled!
  240. // if ($this->settings->item('twitter_blog') == 1 && ($post->status != 'live' && $this->input->post('status') == 'live'))
  241. // {
  242. // $url = shorten_url('blog/'.$date[2].'/'.str_pad($date[1], 2, '0', STR_PAD_LEFT).'/'.url_title($this->input->post('title')));
  243. // $this->load->model('twitter/twitter_m');
  244. // if ( ! $this->twitter_m->update(sprintf($this->lang->line('blog_twitter_posted'), $this->input->post('title'), $url)))
  245. // {
  246. // $this->session->set_flashdata('error', lang('blog_twitter_error') . ": " . $this->twitter->last_error['error']);
  247. // }
  248. // }
  249. }
  250. else
  251. {
  252. $this->session->set_flashdata(array('error' => $this->lang->line('blog_edit_error')));
  253. }
  254. // Redirect back to the form or main page
  255. $this->input->post('btnAction') == 'save_exit' ? redirect('admin/blog') : redirect('admin/blog/edit/' . $id);
  256. }
  257. // Go through all the known fields and get the post values
  258. foreach (array_keys($this->validation_rules) as $field)
  259. {
  260. if (isset($_POST[$field]))
  261. {
  262. $post->$field = $this->form_validation->$field;
  263. }
  264. }
  265. $post->created_on = $created_on;
  266. // Load WYSIWYG editor
  267. $this->template
  268. ->title($this->module_details['name'], sprintf(lang('blog_edit_title'), $post->title))
  269. ->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
  270. ->append_metadata(js('blog_form.js', 'blog'))
  271. ->set('post', $post)
  272. ->build('admin/form');
  273. }
  274. /**
  275. * Preview blog post
  276. * @access public
  277. * @param int $id the ID of the blog post to preview
  278. * @return void
  279. */
  280. public function preview($id = 0)
  281. {
  282. $post = $this->blog_m->get($id);
  283. $this->template
  284. ->set_layout('modal', 'admin')
  285. ->set('post', $post)
  286. ->build('admin/preview');
  287. }
  288. /**
  289. * Helper method to determine what to do with selected items from form post
  290. * @access public
  291. * @return void
  292. */
  293. public function action()
  294. {
  295. switch ($this->input->post('btnAction'))
  296. {
  297. case 'publish':
  298. role_or_die('blog', 'put_live');
  299. $this->publish();
  300. break;
  301. case 'delete':
  302. role_or_die('blog', 'delete_live');
  303. $this->delete();
  304. break;
  305. default:
  306. redirect('admin/blog');
  307. break;
  308. }
  309. }
  310. /**
  311. * Publish blog post
  312. * @access public
  313. * @param int $id the ID of the blog post to make public
  314. * @return void
  315. */
  316. public function publish($id = 0)
  317. {
  318. role_or_die('blog', 'put_live');
  319. // Publish one
  320. $ids = ($id) ? array($id) : $this->input->post('action_to');
  321. if ( ! empty($ids))
  322. {
  323. // Go through the array of slugs to publish
  324. $post_titles = array();
  325. foreach ($ids as $id)
  326. {
  327. // Get the current page so we can grab the id too
  328. if ($post = $this->blog_m->get($id))
  329. {
  330. $this->blog_m->publish($id);
  331. // Wipe cache for this model, the content has changed
  332. $this->pyrocache->delete('blog_m');
  333. $post_titles[] = $post->title;
  334. }
  335. }
  336. }
  337. // Some posts have been published
  338. if ( ! empty($post_titles))
  339. {
  340. // Only publishing one post
  341. if (count($post_titles) == 1)
  342. {
  343. $this->session->set_flashdata('success', sprintf($this->lang->line('blog_publish_success'), $post_titles[0]));
  344. }
  345. // Publishing multiple posts
  346. else
  347. {
  348. $this->session->set_flashdata('success', sprintf($this->lang->line('blog_mass_publish_success'), implode('", "', $post_titles)));
  349. }
  350. }
  351. // For some reason, none of them were published
  352. else
  353. {
  354. $this->session->set_flashdata('notice', $this->lang->line('blog_publish_error'));
  355. }
  356. redirect('admin/blog');
  357. }
  358. /**
  359. * Delete blog post
  360. * @access public
  361. * @param int $id the ID of the blog post to delete
  362. * @return void
  363. */
  364. public function delete($id = 0)
  365. {
  366. // Delete one
  367. $ids = ($id) ? array($id) : $this->input->post('action_to');
  368. // Go through the array of slugs to delete
  369. if ( ! empty($ids))
  370. {
  371. $post_titles = array();
  372. foreach ($ids as $id)
  373. {
  374. // Get the current page so we can grab the id too
  375. if ($post = $this->blog_m->get($id))
  376. {
  377. $this->blog_m->delete($id);
  378. // Wipe cache for this model, the content has changed
  379. $this->pyrocache->delete('blog_m');
  380. $post_titles[] = $post->title;
  381. }
  382. }
  383. }
  384. // Some pages have been deleted
  385. if ( ! empty($post_titles))
  386. {
  387. // Only deleting one page
  388. if (count($post_titles) == 1)
  389. {
  390. $this->session->set_flashdata('success', sprintf($this->lang->line('blog_delete_success'), $post_titles[0]));
  391. }
  392. // Deleting multiple pages
  393. else
  394. {
  395. $this->session->set_flashdata('success', sprintf($this->lang->line('blog_mass_delete_success'), implode('", "', $post_titles)));
  396. }
  397. }
  398. // For some reason, none of them were deleted
  399. else
  400. {
  401. $this->session->set_flashdata('notice', lang('blog_delete_error'));
  402. }
  403. redirect('admin/blog');
  404. }
  405. /**
  406. * Callback method that checks the title of an post
  407. * @access public
  408. * @param string title The Title to check
  409. * @return bool
  410. */
  411. public function _check_title($title = '')
  412. {
  413. if ( ! $this->blog_m->check_exists('title', $title, $this->id))
  414. {
  415. $this->form_validation->set_message('_check_title', sprintf(lang('blog_already_exist_error'), lang('blog_title_label')));
  416. return FALSE;
  417. }
  418. return TRUE;
  419. }
  420. /**
  421. * Callback method that checks the slug of an post
  422. * @access public
  423. * @param string slug The Slug to check
  424. * @return bool
  425. */
  426. public function _check_slug($slug = '')
  427. {
  428. if ( ! $this->blog_m->check_exists('slug', $slug, $this->id))
  429. {
  430. $this->form_validation->set_message('_check_slug', sprintf(lang('blog_already_exist_error'), lang('blog_slug_label')));
  431. return FALSE;
  432. }
  433. return TRUE;
  434. }
  435. /**
  436. * method to fetch filtered results for blog list
  437. * @access public
  438. * @return void
  439. */
  440. public function ajax_filter()
  441. {
  442. $category = $this->input->post('f_category');
  443. $status = $this->input->post('f_status');
  444. $keywords = $this->input->post('f_keywords');
  445. $post_data = array();
  446. if ($status == 'live' OR $status == 'draft')
  447. {
  448. $post_data['status'] = $status;
  449. }
  450. if ($category != 0)
  451. {
  452. $post_data['category_id'] = $category;
  453. }
  454. //keywords, lets explode them out if they exist
  455. if ($keywords)
  456. {
  457. $post_data['keywords'] = $keywords;
  458. }
  459. $results = $this->blog_m->search($post_data);
  460. //set the layout to false and load the view
  461. $this->template
  462. ->set_layout(FALSE)
  463. ->set('blog', $results)
  464. ->build('admin/index');
  465. }
  466. }