PageRenderTime 44ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/modules/news/controllers/admin.php

https://github.com/rotinpain/pyrocms
PHP | 335 lines | 249 code | 50 blank | 36 comment | 35 complexity | 3d645a147b2db78394f4266f811452f5 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Admin extends Admin_Controller
  3. {
  4. // Validation rules to be used for create and edit
  5. private $rules = array(
  6. 'title' => 'trim|htmlspecialchars|required|max_length[100]',
  7. 'slug' => 'trim|required|alpha_dot_dash|max_length[100]',
  8. 'category_id' => 'trim|numeric',
  9. 'intro' => 'trim|required',
  10. 'body' => 'trim|required',
  11. 'status' => 'trim|alpha',
  12. 'created_on_day' => 'trim|numeric|required',
  13. 'created_on_month' => 'trim|numeric|required',
  14. 'created_on_year' => 'trim|numeric|required',
  15. 'created_on_hour' => 'trim|numeric|required',
  16. 'created_on_minute' => 'trim|numeric|required'
  17. );
  18. function __construct()
  19. {
  20. parent::Admin_Controller();
  21. $this->load->model('news_m');
  22. $this->load->model('news_categories_m');
  23. $this->lang->load('news');
  24. $this->lang->load('categories');
  25. // Date ranges for select boxes
  26. $this->data->days = array_combine($days = range(1, 31), $days);
  27. $this->data->months = array_combine($months = range(1, 12), $months);
  28. $this->data->years = array_combine($years = range(date('Y')-2, date('Y')+2), $years);
  29. $this->data->hours = array_combine($hours = range(1, 23), $hours);
  30. $this->data->minutes = array_combine($minutes = range(1, 59), $minutes);
  31. $this->data->categories = array();
  32. if($categories = $this->news_categories_m->get_all())
  33. {
  34. foreach($categories as $category)
  35. {
  36. $this->data->categories[$category->id] = $category->title;
  37. }
  38. }
  39. $this->template->set_partial('shortcuts', 'admin/partials/shortcuts');
  40. }
  41. // Admin: List news articles
  42. function index()
  43. {
  44. // Create pagination links
  45. $total_rows = $this->news_m->count_by(array('show_future'=>TRUE, 'status' => 'all'));
  46. $this->data->pagination = create_pagination('admin/news/index', $total_rows);
  47. // Using this data, get the relevant results
  48. $this->data->news = $this->news_m->limit($this->data->pagination['limit'])->get_many_by(array(
  49. 'show_future'=>TRUE,
  50. 'status' => 'all'
  51. ));
  52. $this->template->build('admin/index', $this->data);
  53. }
  54. // Admin: Create a new article
  55. function create()
  56. {
  57. $this->load->library('validation');
  58. $this->rules['slug'] .= '|callback__check_slug';
  59. $this->validation->set_rules($this->rules);
  60. $this->validation->set_fields();
  61. // Go through all the known fields and get the post values
  62. foreach(array_keys($this->rules) as $field)
  63. {
  64. $article->$field = set_value($field);
  65. }
  66. if ($this->validation->run())
  67. {
  68. $id = $this->news_m->insert(array(
  69. 'title' => $this->input->post('title'),
  70. 'slug' => $this->input->post('slug'),
  71. 'category_id' => $this->input->post('category_id'),
  72. 'intro' => $this->input->post('intro'),
  73. 'body' => $this->input->post('body'),
  74. 'status' => $this->input->post('status'),
  75. 'created_on_hour' => $this->input->post('created_on_hour'),
  76. 'created_on_minute' => $this->input->post('created_on_minute'),
  77. 'created_on_day' => $this->input->post('created_on_day'),
  78. 'created_on_month' => $this->input->post('created_on_month'),
  79. 'created_on_year' => $this->input->post('created_on_year'),
  80. ));
  81. if (!empty($id))
  82. {
  83. $this->session->set_flashdata('success', sprintf($this->lang->line('news_article_add_success'), $this->input->post('title')));
  84. // The twitter module is here, and enabled!
  85. if($this->settings->item('twitter_news') == 1 && $this->user->twitter_access_token != NULL && $this->user->twitter_access_token_secret != NULL && $this->input->post('status') == 'live')
  86. {
  87. $url = shorten_url('news/'.$this->input->post('created_on_year').'/'.$this->input->post('created_on_month').'/'.url_title($this->input->post('title')));
  88. $this->load->library('twitter/twitter');
  89. // Try to authenticate
  90. $auth = $this->twitter->oauth($this->settings->item('twitter_consumer_key'), $this->settings->item('twitter_consumer_key_secret'), $this->user->twitter_access_token, $this->user->twitter_access_token_secret);
  91. $status_update = $this->twitter->call('statuses/update', array('status' => sprintf($this->lang->line('news_twitter_posted'), $this->input->post('title'), $url)));
  92. if(!is_array($status_update))
  93. {
  94. $this->session->set_flashdata('error', lang('news_twitter_error') . ": " . 'Unable to update Twitter status');
  95. }
  96. }
  97. // End twitter code
  98. }
  99. else
  100. {
  101. $this->session->set_flashdata('error', $this->lang->line('news_article_add_error'));
  102. }
  103. redirect('admin/news');
  104. }
  105. $this->data->article =& $article;
  106. // Load WYSIWYG editor
  107. $this->template->append_metadata( $this->load->view('fragments/wysiwyg', $this->data, TRUE) )
  108. ->append_metadata( js('news_form.js', 'news') );
  109. $this->template->build('admin/form', $this->data);
  110. }
  111. // Admin: Edit an article
  112. function edit($id = 0)
  113. {
  114. if (!$id)
  115. {
  116. redirect('admin/news');
  117. }
  118. $this->load->library('validation');
  119. $this->validation->set_rules($this->rules);
  120. $this->validation->set_fields();
  121. $article = $this->news_m->get($id);
  122. if ($this->validation->run())
  123. {
  124. $result = $this->news_m->update($id, array(
  125. 'title' => $this->input->post('title'),
  126. 'slug' => $this->input->post('slug'),
  127. 'category_id' => $this->input->post('category_id'),
  128. 'intro' => $this->input->post('intro'),
  129. 'body' => $this->input->post('body'),
  130. 'status' => $this->input->post('status'),
  131. 'created_on_hour' => $this->input->post('created_on_hour'),
  132. 'created_on_minute' => $this->input->post('created_on_minute'),
  133. 'created_on_day' => $this->input->post('created_on_day'),
  134. 'created_on_month' => $this->input->post('created_on_month'),
  135. 'created_on_year' => $this->input->post('created_on_year'),
  136. ));
  137. if ($result)
  138. {
  139. $this->session->set_flashdata(array('success'=> sprintf($this->lang->line('news_edit_success'), $this->input->post('title'))));
  140. // The twitter module is here, and enabled!
  141. if($this->settings->item('twitter_news') == 1 && ($article->status != 'live' && $this->input->post('status') == 'live'))
  142. {
  143. $url = shorten_url('news/'.$this->input->post('created_on_year').'/'.str_pad($this->input->post('created_on_month'), 2, '0', STR_PAD_LEFT).'/'.url_title($this->input->post('title')));
  144. $this->load->model('twitter/twitter_m');
  145. if(!$this->twitter_m->update(sprintf($this->lang->line('news_twitter_posted'), $this->input->post('title'), $url)))
  146. {
  147. $this->session->set_flashdata('error', lang('news_twitter_error') . ": " . $this->twitter->last_error['error']);
  148. }
  149. }
  150. // End twitter code
  151. }
  152. else
  153. {
  154. $this->session->set_flashdata(array('error'=> $this->lang->line('news_edit_error')));
  155. }
  156. redirect('admin/news');
  157. }
  158. // Go through all the known fields and get the post values
  159. foreach(array_keys($this->rules) as $field)
  160. {
  161. if(isset($_POST[$field])) $article->$field = $this->validation->$field;
  162. }
  163. $this->data->article =& $article;
  164. // Load WYSIWYG editor
  165. $this->template->append_metadata( $this->load->view('fragments/wysiwyg', $this->data, TRUE) );
  166. $this->template->build('admin/form', $this->data);
  167. }
  168. function preview($id = 0)
  169. {
  170. $this->data->article = $this->news_m->get($id);
  171. $this->template->set_layout('admin/basic_layout');
  172. $this->template->build('admin/preview', $this->data);
  173. }
  174. // Admin: Different actions
  175. function action()
  176. {
  177. switch($this->input->post('btnAction'))
  178. {
  179. case 'publish':
  180. $this->publish();
  181. break;
  182. case 'delete':
  183. $this->delete();
  184. break;
  185. default:
  186. redirect('admin/news');
  187. break;
  188. }
  189. }
  190. // Admin: Publish an article
  191. function publish($id = 0)
  192. {
  193. // Publish one
  194. $ids = ($id) ? array($id) : $this->input->post('action_to');
  195. if(!empty($ids))
  196. {
  197. // Go through the array of slugs to publish
  198. $article_titles = array();
  199. foreach ($ids as $id)
  200. {
  201. // Get the current page so we can grab the id too
  202. if($article = $this->news_m->get($id) )
  203. {
  204. $this->news_m->publish($id);
  205. // Wipe cache for this model, the content has changed
  206. $this->cache->delete('news_m');
  207. $article_titles[] = $article->title;
  208. }
  209. }
  210. }
  211. // Some articles have been published
  212. if(!empty($article_titles))
  213. {
  214. // Only publishing one article
  215. if( count($article_titles) == 1 )
  216. {
  217. $this->session->set_flashdata('success', sprintf($this->lang->line('news_publish_success'), $article_titles[0]));
  218. }
  219. // Publishing multiple articles
  220. else
  221. {
  222. $this->session->set_flashdata('success', sprintf($this->lang->line('news_mass_publish_success'), implode('", "', $article_titles)));
  223. }
  224. }
  225. // For some reason, none of them were published
  226. else
  227. {
  228. $this->session->set_flashdata('notice', $this->lang->line('news_publish_error'));
  229. }
  230. redirect('admin/news');
  231. }
  232. // Admin: Delete an article
  233. function delete($id = 0)
  234. {
  235. // Delete one
  236. $ids = ($id) ? array($id) : $this->input->post('action_to');
  237. // Go through the array of slugs to delete
  238. if(!empty($ids))
  239. {
  240. $article_titles = array();
  241. foreach ($ids as $id)
  242. {
  243. // Get the current page so we can grab the id too
  244. if($article = $this->news_m->get($id) )
  245. {
  246. $this->news_m->delete($id);
  247. // Wipe cache for this model, the content has changed
  248. $this->cache->delete('news_m');
  249. $article_titles[] = $article->title;
  250. }
  251. }
  252. }
  253. // Some pages have been deleted
  254. if(!empty($article_titles))
  255. {
  256. // Only deleting one page
  257. if( count($article_titles) == 1 )
  258. {
  259. $this->session->set_flashdata('success', sprintf($this->lang->line('news_delete_success'), $article_titles[0]));
  260. }
  261. // Deleting multiple pages
  262. else
  263. {
  264. $this->session->set_flashdata('success', sprintf($this->lang->line('news_mass_delete_success'), implode('", "', $article_titles)));
  265. }
  266. }
  267. // For some reason, none of them were deleted
  268. else
  269. {
  270. $this->session->set_flashdata('notice', lang('news_delete_error'));
  271. }
  272. redirect('admin/news');
  273. }
  274. // Callback: from create()
  275. function _check_slug($slug = '')
  276. {
  277. if(!$this->news_m->check_slug($slug))
  278. {
  279. $this->validation->set_message('_check_slug', lang('news_already_exist_error'));
  280. return FALSE;
  281. }
  282. return TRUE;
  283. }
  284. }
  285. ?>