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

/system/cms/modules/files/controllers/admin_folders.php

https://github.com/Doap/pyrocms
PHP | 540 lines | 385 code | 92 blank | 63 comment | 32 complexity | 9ecc7e70b0744ccf09ed71cfdf3b4855 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * PyroCMS
  4. *
  5. * An open source CMS based on CodeIgniter
  6. *
  7. * @package PyroCMS
  8. * @author PyroCMS Dev Team
  9. * @license Apache License v2.0
  10. * @link http://pyrocms.com
  11. * @since Version 1.0-dev
  12. * @filesource
  13. */
  14. /**
  15. * PyroCMS Files Admin Controller
  16. *
  17. * Provides an admin for the files module.
  18. *
  19. * @author Dan Horrigan <dan@dhorrigan.com>
  20. * @author Eric Barnes <eric@pyrocms.com>
  21. * @package PyroCMS
  22. * @subpackage Files
  23. */
  24. class Admin_folders extends Admin_Controller {
  25. /**
  26. * Formatted array of all folders.
  27. */
  28. private $_folders = array();
  29. private $_validation_rules = array(
  30. array(
  31. 'field' => 'name',
  32. 'label' => 'lang:file_folders.name_label',
  33. 'rules' => 'required'
  34. ),
  35. array(
  36. 'field' => 'slug',
  37. 'label' => 'lang:file_folders.slug_label',
  38. 'rules' => 'required'
  39. ),
  40. array(
  41. 'field' => 'parent_id',
  42. 'label' => 'lang:file_folders.parent_label',
  43. 'rules' => 'required'
  44. )
  45. );
  46. // ------------------------------------------------------------------------
  47. public function __construct()
  48. {
  49. parent::__construct();
  50. $this->load->models(array('file_m', 'file_folders_m'));
  51. $this->lang->load('files');
  52. $this->config->load('files');
  53. $this->load->library('form_validation');
  54. $this->form_validation->set_rules($this->_validation_rules);
  55. $this->_folders = $this->file_folders_m->get_folders();
  56. // Array for select
  57. $this->data->folders_tree = array();
  58. foreach ($this->_folders as $folder)
  59. {
  60. $this->data->folders_tree[$folder->id] = repeater('&raquo; ', $folder->depth) . $folder->name;
  61. }
  62. $this->template
  63. ->set_partial('shortcuts', 'admin/partials/shortcuts')
  64. ->set_partial('nav', 'admin/partials/nav', array(
  65. 'file_folders' => $this->_folders,
  66. 'current_id' => 0
  67. ));
  68. }
  69. // ------------------------------------------------------------------------
  70. public function index()
  71. {
  72. $this->load->library('table');
  73. $data->file_folders = $this->_folders;
  74. if ($this->input->is_ajax_request())
  75. {
  76. $content = $this->load->view('admin/folders/index', $data, TRUE);
  77. $navigation = $this->load->view('admin/partials/nav', array(
  78. 'file_folders' => $this->_folders,
  79. 'current_id' => 0
  80. ), TRUE);
  81. return $this->template->build_json(array(
  82. 'status' => 'success',
  83. 'content' => $content,
  84. 'navigation'=> $navigation,
  85. ));
  86. }
  87. $this->template
  88. ->title($this->module_details['name'], lang('file_folders.manage_title'))
  89. ->build('admin/folders/index', $data);
  90. }
  91. // ------------------------------------------------------------------------
  92. /**
  93. * Show the folders contents
  94. */
  95. public function contents($id = 0, $filter = '')
  96. {
  97. if ($id)
  98. {
  99. $folder = $this->file_folders_m->get($id);
  100. }
  101. elseif ($path = $this->input->get('path'))
  102. {
  103. $folder = $this->file_folders_m->get_by_path($path);
  104. $filter = $this->input->get('filter');
  105. }
  106. if ( ! (isset($folder) && $folder))
  107. {
  108. if ($this->input->is_ajax_request())
  109. {
  110. $status = 'error';
  111. $message = lang('file_folders.not_exists');
  112. $data = array();
  113. $data['messages'][$status] = $message;
  114. $message = $this->load->view('admin/partials/notices', $data, TRUE);
  115. return $this->template->build_json(array(
  116. 'status' => $status,
  117. 'message' => $message
  118. ));
  119. }
  120. show_error(lang('file_folders.not_exists'));
  121. return;
  122. }
  123. elseif ( ! isset($folder->root_id))
  124. {
  125. $folder->root_id = $this->_folders[$folder->id]->root_id;
  126. $folder->virtual_path = $this->_folders[$folder->id]->virtual_path;
  127. }
  128. $this->load->library('table');
  129. // Make a breadcrumb trail
  130. $this->data->crumbs = $this->file_folders_m->breadcrumb($folder->id);
  131. // Get a list of all child folders
  132. $sub_folders = $this->file_folders_m->folder_tree($folder->root_id);
  133. // Array for select
  134. $this->data->sub_folders = array();
  135. foreach ($sub_folders as $sub_folder)
  136. {
  137. $this->data->sub_folders[$sub_folder->virtual_path] = repeater('&raquo; ', $sub_folder->depth) . $sub_folder->name;
  138. }
  139. $root_folder = $this->_folders[$folder->root_id];
  140. // Set a default label
  141. $this->data->sub_folders = $this->data->sub_folders
  142. ? array($root_folder->virtual_path => lang('files.dropdown_root')) + $this->data->sub_folders
  143. : array($root_folder->virtual_path => lang('files.dropdown_no_subfolders'));
  144. // Get the selected information.
  145. $this->data->folder = $folder;
  146. $this->data->selected_filter = $filter;
  147. // Avaliable type filters
  148. $this->data->types = array();
  149. $this->db
  150. ->select('type as letter')
  151. ->group_by('type');
  152. $types = $this->file_m->get_many_by('folder_id', $folder->id);
  153. foreach ($types as $type)
  154. {
  155. $this->data->types[$type->letter] = lang('files.type_' . $type->letter);
  156. }
  157. asort($this->data->types);
  158. // Get all files
  159. in_array($filter, array('a', 'v', 'd', 'i', 'o')) && $this->db->where('type', $filter);
  160. $this->data->files = $this->file_m
  161. ->order_by('date_added', 'DESC')
  162. ->get_many_by('folder_id', $folder->id);
  163. // Response ajax
  164. if ($this->input->is_ajax_request())
  165. {
  166. $content = $this->load->view('admin/folders/contents', $this->data, TRUE);
  167. $navigation = $this->load->view('admin/partials/nav', array(
  168. 'file_folders' => $this->_folders,
  169. 'current_id' => $folder->root_id
  170. ), TRUE);
  171. return $this->template->build_json(array(
  172. 'status' => 'success',
  173. 'content' => $content,
  174. 'navigation'=> $navigation,
  175. ));
  176. }
  177. $this->template
  178. ->title($this->module_details['name'], $folder->name)
  179. ->append_metadata( css('files.css', 'files') )
  180. ->build('admin/folders/contents', $this->data);
  181. }
  182. // ------------------------------------------------------------------------
  183. public function create()
  184. {
  185. if ($this->form_validation->run())
  186. {
  187. $name = $this->input->post('name');
  188. if ( count($this->file_folders_m->get_by('name', $name)) > 0)
  189. {
  190. $message = sprintf(lang('file_folders.duplicate_error'), $name);
  191. $status = 'error';
  192. }
  193. else
  194. {
  195. if ($this->file_folders_m->insert(array(
  196. 'name' => $name,
  197. 'slug' => $this->input->post('slug'),
  198. 'parent_id' => $this->input->post('parent_id'),
  199. 'date_added' => now()
  200. )))
  201. {
  202. $message = sprintf(lang('file_folders.create_success'), $name);
  203. $status = 'success';
  204. }
  205. else
  206. {
  207. $message = sprintf(lang('file_folders.create_error'), $name);
  208. $status = 'error';
  209. }
  210. }
  211. // If request is ajax return json data, otherwise do normal stuff
  212. if ($this->input->is_ajax_request())
  213. {
  214. $data = array();
  215. $data['messages'][$status] = $message;
  216. $message = $this->load->view('admin/partials/notices', $data, TRUE);
  217. return print ( json_encode((object) array(
  218. 'status' => $status,
  219. 'message' => $message
  220. )) );
  221. }
  222. // Redirect
  223. $this->session->set_flashdata($status, $message);
  224. redirect('admin/files/folders' . ($status === 'error' OR $this->input->post('btnAction') !== 'save_exit' ? '/edit': ''));
  225. }
  226. elseif (validation_errors())
  227. {
  228. // if request is ajax return json data, otherwise do normal stuff
  229. if ($this->input->is_ajax_request())
  230. {
  231. $message = $this->load->view('admin/partials/notices', array(), TRUE);
  232. return $this->template->build_json(array(
  233. 'status' => 'error',
  234. 'message' => $message
  235. ));
  236. }
  237. }
  238. foreach ($this->_validation_rules as $rules)
  239. {
  240. $folder->{$rules['field']} = set_value($rules['field']);
  241. }
  242. $this->data->folder = $folder;
  243. $this->input->is_ajax_request() && $this->template->set_layout(FALSE);
  244. $this->template
  245. ->title($this->module_details['name'], lang('file_folders.create_title'))
  246. ->build('admin/folders/form', $this->data);
  247. }
  248. // ------------------------------------------------------------------------
  249. public function edit($id = 0)
  250. {
  251. $folder = $this->file_folders_m->get($id);
  252. if ( ! $folder)
  253. {
  254. if ($this->input->is_ajax_request())
  255. {
  256. $status = 'error';
  257. $message = lang('file_folders.not_exists');
  258. $data = array();
  259. $data['messages'][$status] = $message;
  260. $message = $this->load->view('admin/partials/notices', $data, TRUE);
  261. return $this->template->build_json(array(
  262. 'status' => $status,
  263. 'message' => $message
  264. ));
  265. }
  266. redirect('files/folders');
  267. }
  268. if ($this->form_validation->run())
  269. {
  270. $name = $this->input->post('name');
  271. if ($this->file_folders_m->update($id, array(
  272. 'name' => $name,
  273. 'slug' => $this->input->post('slug'),
  274. 'parent_id' => $this->input->post('parent_id')
  275. )))
  276. {
  277. $message = sprintf(lang('file_folders.create_success'), $name);
  278. $status = 'success';
  279. }
  280. else
  281. {
  282. $message = sprintf(lang('files.folders.error'), $name);
  283. $status = 'error';
  284. }
  285. // If request is ajax return json data, otherwise do normal stuff
  286. if ($this->input->is_ajax_request())
  287. {
  288. $data = array();
  289. $data['messages'][$status] = $message;
  290. $message = $this->load->view('admin/partials/notices', $data, TRUE);
  291. return print ( json_encode((object) array(
  292. 'status' => $status,
  293. 'message' => $message,
  294. 'title' => sprintf(lang('file_folders.edit_title'), $name)
  295. )) );
  296. }
  297. // Redirect
  298. $this->session->set_flashdata($status, $message);
  299. redirect('admin/files/folders' . ($status === 'error' ? '/edit': ''));
  300. }
  301. elseif (validation_errors())
  302. {
  303. // if request is ajax return json data, otherwise do normal stuff
  304. if ($this->input->is_ajax_request())
  305. {
  306. $message = $this->load->view('admin/partials/notices', array(), TRUE);
  307. return $this->template->build_json(array(
  308. 'status' => 'error',
  309. 'message' => $message
  310. ));
  311. }
  312. }
  313. foreach ($this->_validation_rules as $rules)
  314. {
  315. $this->input->post($rules['field']) && $folder->{$rules['field']} = set_value($rules['field']);
  316. }
  317. $this->data->folder = $folder;
  318. $this->input->is_ajax_request() && $this->template->set_layout(FALSE);
  319. $this->template
  320. ->title($this->module_details['name'], sprintf(lang('file_folders.edit_title'), $folder->name))
  321. ->build('admin/folders/form', $this->data);
  322. }
  323. // ------------------------------------------------------------------------
  324. public function delete($id = 0)
  325. {
  326. $ids = $id
  327. ? is_array($id)
  328. ? $id
  329. : array($id)
  330. : (array) $this->input->post('action_to');
  331. // Do deletion
  332. if ($this->input->post('confirm_delete') === 'yes')
  333. {
  334. $total = sizeof($ids);
  335. $deleted = array();
  336. // Try do deletion
  337. foreach ($ids as $id)
  338. {
  339. // Get the row to use a value.. as title, name
  340. if ($folder = $this->file_folders_m->get($id))
  341. {
  342. // Make deletion retrieving an status and store an value to display in the messages
  343. $deleted[($this->file_folders_m->delete($id) ? 'success': 'error')][] = $folder->name;
  344. }
  345. }
  346. // Set status messages
  347. foreach ($deleted as $status => &$values)
  348. {
  349. // Mass deletion
  350. if (($status_total = sizeof($values)) > 1)
  351. {
  352. $last_value = array_pop($values);
  353. $first_values = implode(', ', $values);
  354. // Success / Error message
  355. $values = sprintf(lang('file_folders.delete_mass_' . $status), $status_total, $total, $first_values, $last_value);
  356. }
  357. // Single deletion
  358. else
  359. {
  360. // Success / Error messages
  361. $values = sprintf(lang('file_folders.delete_' . $status), $values[0]);
  362. }
  363. }
  364. // He arrived here but it was not done nothing, certainly valid ids not were selected
  365. if ( ! $deleted)
  366. {
  367. $status = 'error';
  368. $deleted = array('error' => lang('file_folders.no_select_error'));
  369. }
  370. else
  371. {
  372. $status = array_key_exists('error', $deleted) ? 'error': 'success';
  373. }
  374. if ($this->input->is_ajax_request())
  375. {
  376. $data = array();
  377. $data['messages'] = $deleted;
  378. $message = $this->load->view('admin/partials/notices', $data, TRUE);
  379. return $this->template->build_json(array(
  380. 'status' => $status,
  381. 'message' => $message,
  382. ));
  383. }
  384. foreach ($deleted as $status => $message)
  385. {
  386. $this->session->set_flashdata($status, $message);
  387. }
  388. redirect('admin/files');
  389. }
  390. $data->file_folders = array();
  391. foreach ($ids as $id)
  392. {
  393. isset($this->_folders[$id]) && $data->file_folders[$id] = $this->_folders[$id];
  394. }
  395. if ($this->input->is_ajax_request())
  396. {
  397. $this->template->set_layout(FALSE);
  398. if ( ! empty($data->file_folders))
  399. {
  400. $status = 'success';
  401. $html = $this->load->view('admin/folders/confirm', $data, TRUE);
  402. }
  403. else
  404. {
  405. $status = 'error';
  406. $html = lang('file_folders.no_select_error');
  407. $data = array();
  408. $data['messages'][$status] = $html;
  409. $html = $this->load->view('admin/partials/notices', $data, TRUE);
  410. }
  411. return $this->template->build_json(array(
  412. 'status' => $status,
  413. 'html' => $html
  414. ));
  415. }
  416. $this->template
  417. ->title($this->module_details['name'], lang('file_folders.delete_title'))
  418. ->build('admin/folders/confirm', $data);
  419. }
  420. // ------------------------------------------------------------------------
  421. public function upload()
  422. {
  423. $this->template
  424. ->title($this->module['name'],lang('files.upload_title'))
  425. ->build('admin/upload', $this->data);
  426. }
  427. // ------------------------------------------------------------------------
  428. public function action()
  429. {
  430. $action = strtolower($this->input->post('btnAction'));
  431. if ($action)
  432. {
  433. // Get the id('s)
  434. $id_array = $this->input->post('action_to');
  435. // Call the action we want to do
  436. if (method_exists($this, $action))
  437. {
  438. return $this->{$action}($id_array);
  439. }
  440. }
  441. redirect('admin/files');
  442. }
  443. public function html_dropdown($id = 0)
  444. {
  445. $this->data->folder = $id && isset($this->_folders[$id]) ? $this->_folders[$id] : array();
  446. return $this->load->view('admin/folders/html_dropdown', $this->data);
  447. }
  448. }