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

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

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