PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/marcoscoelho/pyrocms
PHP | 445 lines | 289 code | 67 blank | 89 comment | 34 complexity | 74e5e1635cd0d03f9bfc2cf0943217a6 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * Admin controller for the themes module
  4. *
  5. * @author PyroCMS Dev Team
  6. * @package PyroCMS
  7. * @subpackage Themes module
  8. * @category Modules
  9. */
  10. class Admin extends Admin_Controller
  11. {
  12. /**
  13. * The current active section
  14. * @access protected
  15. * @var string
  16. */
  17. protected $section = 'themes';
  18. /**
  19. * Validation array
  20. * @access private
  21. * @var array
  22. */
  23. private $validation_rules = array();
  24. /**
  25. * Constructor method
  26. *
  27. * @access public
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. // Call the parent's constructor
  33. parent::__construct();
  34. $this->load->model('theme_m');
  35. $this->lang->load('themes');
  36. $this->load->library('form_validation');
  37. $this->template
  38. ->append_metadata(css('themes.css', 'themes'))
  39. ->append_metadata(js('admin.js', 'themes'));
  40. }
  41. /**
  42. * List all themes
  43. *
  44. * @access public
  45. * @return void
  46. */
  47. public function index()
  48. {
  49. $themes = $this->theme_m->get_all();
  50. $data = array();
  51. foreach ($themes as $theme)
  52. {
  53. if ( ! isset($theme->type) OR $theme->type != 'admin')
  54. {
  55. if ($theme->slug == $this->settings->default_theme)
  56. {
  57. $theme->is_default = TRUE;
  58. }
  59. $data['themes'][] = $theme;
  60. }
  61. }
  62. // Render the view
  63. $this->template
  64. ->title($this->module_details['name'])
  65. ->build('admin/index', $data);
  66. }
  67. /**
  68. * Save the option settings
  69. *
  70. * @param string $slug The theme slug
  71. * @access public
  72. * @return void
  73. */
  74. public function options($slug = '')
  75. {
  76. if ($this->input->post('btnAction') == 're-index')
  77. {
  78. $this->theme_m->delete_options($this->input->post('slug'));
  79. // now re-index all themes that don't have saved options
  80. if ($this->theme_m->get_all())
  81. {
  82. // Success...
  83. $this->session->set_flashdata('success', lang('themes.re-index_success'));
  84. redirect('admin/themes/options/'.$slug);
  85. }
  86. }
  87. $all_options = $this->theme_m->get_options_by(array('theme'=> $slug));
  88. $options_array = array();
  89. if ($all_options)
  90. {
  91. // Create dynamic validation rules
  92. foreach($all_options as $option)
  93. {
  94. $this->validation_rules[] = array(
  95. 'field' => $option->slug . (in_array($option->type, array('select-multiple', 'checkbox')) ? '[]' : ''),
  96. 'label' => $option->title,
  97. 'rules' => 'trim' . ($option->is_required ? '|required' : '') . '|max_length[255]'
  98. );
  99. $options_array[$option->slug] = $option->value;
  100. }
  101. // Set the validation rules
  102. $this->form_validation->set_rules($this->validation_rules);
  103. // Got valid data?
  104. if ($this->form_validation->run())
  105. {
  106. // Loop through again now we know it worked
  107. foreach($options_array as $option_slug => $stored_value)
  108. {
  109. $input_value = $this->input->post($option_slug, FALSE);
  110. if (is_array($input_value))
  111. {
  112. $input_value = implode(',', $input_value);
  113. }
  114. // Dont update if its the same value
  115. if ($input_value !== $stored_value)
  116. {
  117. $this->theme_m->update_options($option_slug, array('value' => $input_value));
  118. }
  119. }
  120. $this->session->set_flashdata('success', lang('themes.save_success'));
  121. redirect('admin/themes/options/'.$slug);
  122. }
  123. }
  124. $data->slug = $slug;
  125. $data->options_array = $all_options;
  126. $data->controller = &$this;
  127. $this->template->build('admin/options', $data);
  128. }
  129. /**
  130. * Set the default theme to theme X
  131. *
  132. * @access public
  133. * @return void
  134. */
  135. public function set_default()
  136. {
  137. // Store the theme name
  138. $theme = $this->input->post('theme');
  139. // Set the theme
  140. if ($this->theme_m->set_default($this->input->post()))
  141. {
  142. $this->session->set_flashdata('success', sprintf(lang('themes.set_default_success'), $theme));
  143. }
  144. else
  145. {
  146. $this->session->set_flashdata('error', sprintf( lang('themes.set_default_error'), $theme));
  147. }
  148. if ($this->input->post('method') == 'admin_themes')
  149. {
  150. redirect('admin/themes/admin_themes');
  151. }
  152. redirect('admin/themes');
  153. }
  154. /**
  155. * Upload a theme to the server
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. public function upload()
  161. {
  162. if ( ! $this->settings->addons_upload)
  163. {
  164. show_error('Uploading add-ons has been disabled for this site. Please contact your administrator');
  165. }
  166. if($this->input->post('btnAction') == 'upload')
  167. {
  168. $config['upload_path'] = FCPATH.UPLOAD_PATH;
  169. $config['allowed_types'] = 'zip';
  170. $config['max_size'] = '2048';
  171. $config['overwrite'] = TRUE;
  172. $this->load->library('upload', $config);
  173. if ($this->upload->do_upload())
  174. {
  175. $upload_data = $this->upload->data();
  176. // Check if we already have a dir with same name
  177. if($this->template->theme_exists($upload_data['raw_name']))
  178. {
  179. $this->session->set_flashdata('error', lang('themes.already_exists_error'));
  180. }
  181. else
  182. {
  183. // Now try to unzip
  184. $this->load->library('unzip');
  185. // TODO: Work out a better security plan, adding .php back for now (2.0)
  186. $this->unzip->allow(array('php', 'xml', 'html', 'css', 'js', 'png', 'gif', 'jpeg', 'jpg', 'swf', 'ico', 'txt', 'eot', 'svg', 'ttf', 'woff'));
  187. // Try and extract
  188. $this->unzip->extract($upload_data['full_path'], ADDONPATH . 'themes/' )
  189. ? $this->session->set_flashdata('success', lang('themes.upload_success'))
  190. : $this->session->set_flashdata('error', $this->unzip->error_string());
  191. }
  192. // Delete uploaded file
  193. @unlink($upload_data['full_path']);
  194. }
  195. else
  196. {
  197. $this->session->set_flashdata('error', $this->upload->display_errors());
  198. }
  199. redirect('admin/themes');
  200. }
  201. $this->template
  202. ->set_layout('modal')
  203. ->title($this->module_details['name'], lang('themes.upload_title'))
  204. ->build('admin/upload', $this->data);
  205. }
  206. /**
  207. * Delete an existing theme
  208. *
  209. * @access public
  210. * @param string $theme_name The name of the theme to delete
  211. * @return void
  212. */
  213. public function delete($theme_name = '')
  214. {
  215. $this->load->helper('file');
  216. $name_array = $theme_name ? array($theme_name) : $this->input->post('action_to');
  217. // Delete multiple
  218. if ( ! empty($name_array))
  219. {
  220. $deleted = 0;
  221. $to_delete = 0;
  222. foreach ($name_array as $theme_name)
  223. {
  224. $theme_name = urldecode($theme_name);
  225. $to_delete++;
  226. if($this->settings->default_theme == $theme_name)
  227. {
  228. $this->session->set_flashdata('error', lang('themes.default_delete_error'));
  229. }
  230. else
  231. {
  232. $theme_dir = ADDONPATH.'themes/'.$theme_name;
  233. if( is_really_writable($theme_dir) )
  234. {
  235. delete_files($theme_dir, TRUE);
  236. if(@rmdir($theme_dir))
  237. {
  238. $deleted++;
  239. }
  240. }
  241. else
  242. {
  243. $this->session->set_flashdata('error', sprintf(lang('themes.delete_error'), $theme_dir) );
  244. }
  245. }
  246. }
  247. if( $deleted == $to_delete)
  248. {
  249. $this->session->set_flashdata('success', sprintf(lang('themes.mass_delete_success'), $deleted, $to_delete) );
  250. }
  251. }
  252. else
  253. {
  254. $this->session->set_flashdata('error', lang('themes.delete_select_error'));
  255. }
  256. redirect('admin/themes');
  257. }
  258. /**
  259. * Form Control
  260. *
  261. * Returns the form control for the theme option
  262. *
  263. * @param object $option
  264. * @return string
  265. */
  266. public function form_control(&$option)
  267. {
  268. if ($option->options)
  269. {
  270. if (substr($option->options, 0, 5) == 'func:')
  271. {
  272. if (is_callable($func = substr($option->options, 5)))
  273. {
  274. $option->options = call_user_func($func);
  275. }
  276. else
  277. {
  278. $option->options = array('=' . lang('global:select-none'));
  279. }
  280. }
  281. if (is_string($option->options))
  282. {
  283. $option->options = explode('|', $option->options);
  284. }
  285. }
  286. switch ($option->type)
  287. {
  288. default:
  289. case 'text':
  290. $form_control = form_input(array(
  291. 'id' => $option->slug,
  292. 'name' => $option->slug,
  293. 'value' => $option->value,
  294. 'class' => 'text width-20'
  295. ));
  296. break;
  297. case 'textarea':
  298. $form_control = form_textarea(array(
  299. 'id' => $option->slug,
  300. 'name' => $option->slug,
  301. 'value' => $option->value,
  302. 'class' => 'width-20'
  303. ));
  304. break;
  305. case 'password':
  306. $form_control = form_password(array(
  307. 'id' => $option->slug,
  308. 'name' => $option->slug,
  309. 'value' => $option->value,
  310. 'class' => 'text width-20',
  311. 'autocomplete' => 'off',
  312. ));
  313. break;
  314. case 'select':
  315. $form_control = form_dropdown($option->slug, $this->_format_options($option->options), $option->value, 'class="width-20"');
  316. break;
  317. case 'select-multiple':
  318. $options = $this->_format_options($option->options);
  319. $size = sizeof($options) > 10 ? ' size="10"' : '';
  320. $form_control = form_multiselect($option->slug . '[]', $options, explode(',', $option->value), 'class="width-20"' . $size);
  321. break;
  322. case 'checkbox':
  323. $form_control = '';
  324. $stored_values = is_string($option->value) ? explode(',', $option->value) : $option->value;
  325. foreach ($this->_format_options($option->options) as $value => $label)
  326. {
  327. if (is_array($stored_values))
  328. {
  329. $checked = in_array($value, $stored_values);
  330. }
  331. else
  332. {
  333. $checked = FALSE;
  334. }
  335. $form_control .= '<label>';
  336. $form_control .= '' . form_checkbox(array(
  337. 'id' => $option->slug . '_' . $value,
  338. 'name' => $option->slug . '[]',
  339. 'checked' => $checked,
  340. 'value' => $value
  341. ));
  342. $form_control .= ' ' . $label . '</label>';
  343. }
  344. break;
  345. case 'radio':
  346. $form_control = '';
  347. foreach ($this->_format_options($option->options) as $value => $label)
  348. {
  349. $form_control .= '' . form_radio(array(
  350. 'id' => $option->slug,
  351. 'name' => $option->slug,
  352. 'checked' => $option->value == $value,
  353. 'value' => $value
  354. )) . ' ' . $label . '';
  355. }
  356. break;
  357. }
  358. return $form_control;
  359. }
  360. /**
  361. * Format Options
  362. *
  363. * Formats the options for a theme option into an associative array.
  364. *
  365. * @param array $options
  366. * @return array
  367. */
  368. private function _format_options($options = array())
  369. {
  370. $select_array = array();
  371. foreach ($options as $option)
  372. {
  373. list($value, $name) = explode('=', $option);
  374. $select_array[$value] = $name;
  375. }
  376. return $select_array;
  377. }
  378. }