/halogy/application/modules/files/controllers/admin.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 389 lines · 253 code · 62 blank · 74 comment · 48 complexity · e379b52e19eff67e8339a6f52667f07a MD5 · raw file

  1. <?php
  2. /**
  3. * Halogy
  4. *
  5. * A user friendly, modular content management system for PHP 5.0
  6. * Built on CodeIgniter - http://codeigniter.com
  7. *
  8. * @package Halogy
  9. * @author Haloweb Ltd.
  10. * @copyright Copyright (c) 2008-2011, Haloweb Ltd.
  11. * @license http://halogy.com/license
  12. * @link http://halogy.com/
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. class Admin extends Controller {
  18. // set defaults
  19. var $table = 'files'; // table to update
  20. var $includes_path = '/includes/admin'; // path to includes for header and footer
  21. var $redirect = '/admin/files/viewall'; // default redirect
  22. var $objectID = 'fileID'; // default unique ID
  23. var $permissions = array();
  24. function Admin()
  25. {
  26. parent::Controller();
  27. // check user is logged in, if not send them away from this controller
  28. if (!$this->session->userdata('session_admin'))
  29. {
  30. redirect('/admin/login/'.$this->core->encode($this->uri->uri_string()));
  31. }
  32. // get permissions and redirect if they don't have access to this module
  33. if (!$this->permission->permissions)
  34. {
  35. if (@$this->core->is_ajax())
  36. {
  37. die('<p>Sorry, you do not have permissions to do what you just tried to do. <a class="halogycms_close" href="#">Close</a>.</p>');
  38. }
  39. else
  40. {
  41. redirect('/admin/dashboard/permissions');
  42. }
  43. }
  44. if (!in_array($this->uri->segment(2), $this->permission->permissions))
  45. {
  46. if (@$this->core->is_ajax())
  47. {
  48. die('<p>Sorry, you do not have permissions to do what you just tried to do. <a class="halogycms_close" href="#">Close</a>.</p>');
  49. }
  50. else
  51. {
  52. redirect('/admin/dashboard/permissions');
  53. }
  54. }
  55. // get siteID, if available
  56. if (defined('SITEID'))
  57. {
  58. $this->siteID = SITEID;
  59. }
  60. // load libs etc
  61. $this->load->model('files_model', 'files');
  62. }
  63. function index()
  64. {
  65. redirect($this->redirect);
  66. }
  67. function viewall($folderID = '')
  68. {
  69. if (count($_FILES))
  70. {
  71. // upload file
  72. if ($oldFileName = @$_FILES['file']['name'])
  73. {
  74. $this->uploads->allowedTypes = 'pdf|doc|mp3|zip|js|swf|flv|mp4|js|css|ico|txt|xls|ppt|ttf|cff|svg|woff|eot';
  75. if ($fileData = $this->uploads->upload_file())
  76. {
  77. $this->core->set['filename'] = $fileData['file_name'];
  78. }
  79. // get file errors if there are any
  80. if ($this->uploads->errors)
  81. {
  82. $this->form_validation->set_error($this->uploads->errors);
  83. }
  84. else
  85. {
  86. // format filename
  87. $filenames = explode('.', $oldFileName);
  88. $extension = end($filenames);
  89. $filename = str_replace('.'.$extension, '', $oldFileName);
  90. // set file reference and only add to db if its unique
  91. $fileRef = url_title(trim(strtolower($filename)));
  92. if ($this->form_validation->unique($fileRef, 'files.fileRef'))
  93. {
  94. // set file ref
  95. $this->core->set['dateCreated'] = date("Y-m-d H:i:s");
  96. $this->core->set['fileRef'] = $fileRef;
  97. $this->core->set['filesize'] = $fileData['file_size'];
  98. $this->core->set['userID'] = $this->session->userdata('userID');
  99. // update
  100. if ($this->core->update('files'))
  101. {
  102. // where to redirect to
  103. redirect('/admin/files/viewall');
  104. }
  105. }
  106. else
  107. {
  108. $this->form_validation->set_error('This file has already been uploaded. Try renaming your local file.');
  109. }
  110. }
  111. }
  112. }
  113. // search
  114. if ($this->input->post('searchbox'))
  115. {
  116. $output['files'] = $this->files->search_files($this->input->post('searchbox'));
  117. }
  118. else
  119. {
  120. // set default wheres
  121. $where = array('siteID' => $this->siteID, 'deleted' => 0);
  122. // get preset selections for this dropdown
  123. if ($folderID == '' && @array_key_exists('folderID', $this->selections))
  124. {
  125. $folderID = $this->selections['folderID'];
  126. }
  127. // folderID
  128. if ($folderID != '')
  129. {
  130. // get ones uploaded by this user
  131. if ($folderID == 'me')
  132. {
  133. $where['userID'] = $this->session->userdata('userID');
  134. }
  135. // make sure that all is not selected
  136. elseif ($folderID != 'all' && $folderID != 'page' && $folderID != 'me')
  137. {
  138. $where['folderID'] = $folderID;
  139. }
  140. // set preset selections for this dropdown
  141. $this->session->set_userdata('selections', array($this->uri->segment(2) => array('folderID' => $folderID)));
  142. }
  143. // check they have permissions to see all files
  144. if (!@in_array('files_all', $this->permission->permissions))
  145. {
  146. $where['userID'] = $this->session->userdata('userID');
  147. }
  148. // grab data and display
  149. $output = $this->core->viewall($this->table, $where, NULL, 24);
  150. }
  151. // get folderID if set
  152. $output['folderID'] = $folderID;
  153. // get quota
  154. $output['quota'] = $this->site->get_quota();
  155. // get categories
  156. $output['folders'] = $this->files->get_folders();
  157. // view files
  158. $this->load->view($this->includes_path.'/header');
  159. $this->load->view('admin/viewall',$output);
  160. $this->load->view($this->includes_path.'/footer');
  161. }
  162. function edit($fileID, $redirect = '', $popup = FALSE)
  163. {
  164. // set object ID
  165. $objectID = array($this->objectID => $fileID);
  166. // get values
  167. $output['data'] = $this->core->get_values($this->table, $objectID);
  168. // handle post
  169. if (count($_POST))
  170. {
  171. // get file errors if there are any
  172. if ($this->uploads->errors)
  173. {
  174. $this->form_validation->set_error($this->uploads->errors);
  175. }
  176. else
  177. {
  178. // update
  179. if ($this->core->update('files', $objectID))
  180. {
  181. // if its not coming from ajax then just go to admin
  182. if (!$redirect && !$popup)
  183. {
  184. $redirect = '/admin/files/viewall';
  185. }
  186. // where to redirect to
  187. redirect($redirect);
  188. }
  189. }
  190. }
  191. // define view (based on popup)
  192. $view = ($popup) ? 'admin/popup' : 'admin/edit';
  193. // get categories
  194. $output['folders'] = $this->files->get_folders();
  195. // templates
  196. if (!@$this->core->is_ajax()) $this->load->view($this->includes_path.'/header');
  197. $this->load->view($view, $output);
  198. if (!@$this->core->is_ajax()) $this->load->view($this->includes_path.'/footer');
  199. }
  200. function delete($objectID)
  201. {
  202. // delete file
  203. $query = $this->db->get_where($this->table, array($this->objectID => $objectID));
  204. if ($row = $query->row_array())
  205. {
  206. $this->uploads->delete_file($row['filename']);
  207. }
  208. if ($this->core->delete($this->table, array($this->objectID => $objectID)));
  209. {
  210. // where to redirect to
  211. redirect($this->redirect);
  212. }
  213. }
  214. function browser()
  215. {
  216. // set default wheres
  217. $where = array('siteID' => $this->siteID, 'deleted' => 0);
  218. // check they have permissions to see all files
  219. if (!@in_array('files_all', $this->permission->permissions))
  220. {
  221. $where['userID'] = $this->session->userdata('userID');
  222. }
  223. // grab data and display
  224. $output = $this->core->viewall($this->table, array('folderID' => 0), 'fileRef', 999);
  225. // get folders
  226. if ($folders = $this->files->get_folders())
  227. {
  228. foreach($folders as $folder)
  229. {
  230. // grab data and display
  231. $data = $this->core->viewall($this->table, array('folderID' => $folder['folderID']), 'fileRef', 999);
  232. $output['folders'][$folder['folderID']]['folderName'] = $folder['folderName'];
  233. $output['folders'][$folder['folderID']]['files'] = $data['files'];
  234. }
  235. }
  236. $this->load->view('admin/browser',$output);
  237. }
  238. function folders()
  239. {
  240. // check permissions for this page
  241. if (!in_array('files', $this->permission->permissions))
  242. {
  243. redirect('/admin/dashboard/permissions');
  244. }
  245. // required fields
  246. $this->core->required = array('folderName' => 'Folder Name');
  247. // set date
  248. $this->core->set['dateCreated'] = date("Y-m-d H:i:s");
  249. // get values
  250. $output = $this->core->get_values('file_folders');
  251. // update
  252. if ($this->core->update('file_folders') && count($_POST))
  253. {
  254. // where to redirect to
  255. redirect('/admin/files/folders');
  256. }
  257. $output['folders'] = $this->files->get_folders();
  258. $this->load->view($this->includes_path.'/header');
  259. $this->load->view('admin/folders',$output);
  260. $this->load->view($this->includes_path.'/footer');
  261. }
  262. function edit_folder()
  263. {
  264. // check permissions for this page
  265. if (!in_array('files', $this->permission->permissions))
  266. {
  267. redirect('/admin/dashboard/permissions');
  268. }
  269. // go through post and edit each list item
  270. $listArray = $this->core->get_post();
  271. if (count($listArray))
  272. {
  273. foreach($listArray as $ID => $value)
  274. {
  275. if ($ID != '' && sizeof($value) > 0)
  276. {
  277. // set object ID
  278. $objectID = array('folderID' => $ID);
  279. $this->core->set['folderName'] = $value['folderName'];
  280. $this->core->update('file_folders', $objectID);
  281. }
  282. }
  283. }
  284. // where to redirect to
  285. redirect('/admin/files/folders');
  286. }
  287. function delete_folder($folderID)
  288. {
  289. // check permissions for this page
  290. if (!in_array('files', $this->permission->permissions))
  291. {
  292. redirect('/admin/dashboard/permissions');
  293. }
  294. // where
  295. $objectID = array('folderID' => $folderID);
  296. if ($this->core->soft_delete('file_folders', $objectID))
  297. {
  298. // set children to no parent
  299. $this->files->update_children($folderID);
  300. // where to redirect to
  301. redirect('/admin/files/folders');
  302. }
  303. }
  304. function order($field = '')
  305. {
  306. $this->core->order(key($_POST), $field);
  307. }
  308. function ac_files()
  309. {
  310. $q = strtolower($_POST["q"]);
  311. if (!$q) return;
  312. // form dropdown
  313. $results = $this->files->search_files($q);
  314. // go foreach
  315. foreach((array)$results as $row)
  316. {
  317. $items[$row['fileRef']] = $row['fileRef'];
  318. }
  319. // output
  320. $output = '';
  321. foreach ($items as $key=>$value)
  322. {
  323. $output .= "$key|$value\n";
  324. }
  325. $this->output->set_output($output);
  326. }
  327. }