PageRenderTime 35ms CodeModel.GetById 13ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

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