Created
January 17, 2012 10:02
-
-
Save mpmont/1626035 to your computer and use it in GitHub Desktop.
File Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Files extends MY_Controller { | |
function __construct() { | |
parent::__construct(); | |
//load dependecies | |
$this->load->model('admin/files_model'); | |
} | |
// LIST FILES | |
public function index( $offset = 0 ){ | |
//load dependencies | |
$this->load->library('pagination'); | |
//Pagination | |
$perpage = 30; | |
$config = array( | |
'base_url' => base_url() . 'index.php/admin/files/index/', | |
'num_links' => 4, | |
'per_page' => $perpage, | |
'uri_segment' => 4, | |
'first_link' => $this->lang->line('first'), | |
'last_link' => $this->lang->line('last'), | |
'next_link' => $this->lang->line('next').' >', | |
'prev_link' => '< '.$this->lang->line('previous'), | |
'anchor_class' => "class='number' " | |
); | |
if ($this->input->post('filter')) { | |
$config['total_rows'] = $this->files_model->GetFiles( | |
array( | |
'count' => true, | |
'filestatus' => 'active', | |
'filetype' => $this->input->post('filter') | |
) | |
); | |
} | |
else{ | |
$config['total_rows'] = $this->files_model->GetFiles( | |
array( | |
'count' => true, | |
'filestatus' => 'active' | |
) | |
); | |
} | |
$this->pagination->initialize($config); | |
//Data for the view | |
if ($this->input->post('filter')) { | |
$data['records'] = $this->files_model->GetFiles( | |
array( | |
'limit' => $perpage, | |
'offset' => $offset, | |
'sortBy' => 'idfile', | |
'sortDirection' => 'DESC', | |
'filetype' => $this->input->post('filter') | |
) | |
); | |
$data['filter'] = $this->input->post('filter'); | |
} | |
elseif ($this->input->post('order')) { | |
$order = $this->input->post('order'); | |
switch ($order) { | |
case 'date-down': | |
$data['records'] = $this->files_model->GetFiles( | |
array( | |
'limit' => $perpage, | |
'offset' => $offset, | |
'sortBy' => 'idfile', | |
'sortDirection' => 'ASC' | |
) | |
); | |
$data['order'] = $this->input->post('order'); | |
break; | |
case 'date-up': | |
$data['records'] = $this->files_model->GetFiles( | |
array( | |
'limit' => $perpage, | |
'offset' => $offset, | |
'sortBy' => 'idfile', | |
'sortDirection' => 'DESC' | |
) | |
); | |
$data['order'] = $this->input->post('order'); | |
break; | |
case 'alpha-down': | |
$data['records'] = $this->files_model->GetFiles( | |
array( | |
'limit' => $perpage, | |
'offset' => $offset, | |
'sortBy' => 'filename', | |
'sortDirection' => 'ASC') | |
); | |
$data['order'] = $this->input->post('order'); | |
break; | |
case 'alpha-up': | |
$data['records'] = $this->files_model->GetFiles( | |
array( | |
'limit' => $perpage, | |
'offset' => $offset, | |
'sortBy' => 'filename', | |
'sortDirection' => 'DESC' | |
) | |
); | |
$data['order'] = $this->input->post('order'); | |
break; | |
default: | |
$data['records'] = $this->files_model->GetFiles( | |
array( | |
'limit' => $perpage, | |
'offset' => $offset, | |
'sortBy' => 'idfile', | |
'sortDirection' => 'ASC') | |
); | |
$data['order'] = ''; | |
break; | |
} | |
} | |
else{ | |
$data['records'] = $this->files_model->GetFiles( | |
array( | |
'limit' => $perpage, | |
'offset' => $offset, | |
'sortBy' => 'idfile', | |
'sortDirection' => 'DESC' | |
) | |
); | |
} | |
$data['menu_actions'] = $this->_permissions(); | |
$data['pagination'] = $this->pagination->create_links(); | |
$data['main_content'] = 'admin/files_view'; | |
$data['action'] = 'list'; | |
$data['current_page'] = 'files'; | |
$this->load->view('admin/template/template', $data); | |
} | |
// ADD FILE | |
public function add(){ | |
//load dependencies | |
$this->load->library('form_validation'); | |
$new_file = $this->files_model->AddFile($_POST); | |
if ( $new_file ) { | |
$this->session->set_flashdata('flashConfirm', $this->lang->line('file_created')); | |
redirect('admin/files'); | |
} | |
else { | |
$this->session->set_flashdata('flashError', $this->lang->line('file_nocreated')); | |
redirect('admin/files'); | |
} | |
} | |
// EDIT FILE | |
public function edit($idfile) { | |
$data['file'] = $this->files_model->GetFiles( | |
array( | |
'idfile' => $idfile | |
) | |
); | |
$data['main_content'] = 'admin/files_view'; | |
$data['action'] = 'edit'; | |
$data['current_page'] = 'files'; | |
$this->load->view('admin/template/template', $data); | |
} | |
// DELETE FILE | |
public function delete($idfile){ | |
//Load Dependencies | |
$this->load->model('admin/articles_model'); | |
$data['file'] = $this->files_model->GetFiles( | |
array( | |
'idfile' => $idfile | |
) | |
); | |
if(!$data['file']){ | |
$this->session->set_flashdata('flashError', $this->lang->line('nofile')); | |
redirect('admin/files'); | |
} | |
else{ | |
$article = $this->articles_model->GetArticles( | |
array( | |
'articlestatus' => 'active', | |
'articlestatus' => 'inactive', | |
'articleimage' => $data['file']->filename | |
) | |
); | |
if($article){ | |
$this->session->set_flashdata('flashError', $this->lang->line('ass_article')); | |
redirect('admin/files'); | |
} | |
} | |
$this->files_model->UpdateFile( | |
array( | |
'idfile' => $idfile, | |
'filestatus' => 'deleted' | |
) | |
); | |
$this->session->set_flashdata('flashConfirm', $this->lang->line('deleted')); | |
redirect('admin/files'); | |
} | |
// Check user permissions for the icons while listing the items. | |
function _permissions(){ | |
$active_menus = $this->session->userdata('active_menus'); | |
$user_permissions = $this->session->userdata('actions'); | |
$current_menu = $this->uri->segment(2); | |
$menus = array( | |
'articles' => '1', | |
'category' => '2', | |
'menus' => '3', | |
'users' => '4', | |
'files' => '5', | |
'stats' => '6' | |
); | |
return $user_permissions[$menus[$current_menu]]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE IF NOT EXISTS `files` ( | |
`idfile` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`filename` varchar(255) CHARACTER SET utf8 DEFAULT NULL, | |
`thumb_100` varchar(255) CHARACTER SET utf8 NOT NULL, | |
`thumb_250` varchar(255) CHARACTER SET utf8 NOT NULL, | |
`thumb_500` varchar(255) CHARACTER SET utf8 NOT NULL, | |
`thumb_800` varchar(255) CHARACTER SET utf8 NOT NULL, | |
`filetype` enum('.pdf','.jpg','.jpeg','.png','.gif','.rar','.zip','.txt') CHARACTER SET utf8 DEFAULT NULL, | |
`filegroup` enum('image','bin') CHARACTER SET utf8 NOT NULL, | |
`filestatus` enum('active','inactive','deleted') CHARACTER SET utf8 DEFAULT NULL, | |
PRIMARY KEY (`idfile`) | |
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
/* | |
* Files_Model | |
* | |
* @package Files | |
* | |
*/ | |
class Files_model extends CI_Model{ | |
/** Utility Methods **/ | |
function _required($required, $data) { | |
foreach ($required as $field) | |
if(!isset($data[$field])) return false; | |
return true; | |
} | |
function _default($defaults, $options) { | |
return array_merge($defaults, $options); | |
} | |
/** Files Methods **/ | |
/* | |
* AddFile | |
* | |
* Option: Values | |
* -------------- | |
* filename => required | |
* filetype => required | |
* filestatus | |
* | |
* @param array $options | |
* @result int insert_id() | |
*/ | |
function AddFile($options = array()) { | |
$this->load->library('image_lib'); | |
$file = $_FILES['userfile']; | |
if (!empty($file)) { | |
//config upload | |
$upload_config = array( | |
'allowed_types' => 'jpg|jpeg|gif|png|pdf|zip|rar|txt|bmp', | |
'upload_path' => './public/images/articles/', | |
'max_size' => 2500, | |
'remove_spaces' => TRUE | |
); | |
//load dependecies | |
$this->load->library('upload', $upload_config); | |
$this->upload->do_upload('userfile'); | |
$image_data = $this->upload->data(); | |
if ( ! $this->upload->data()){ | |
echo $this->upload->display_errors(); | |
} | |
else{ | |
$this->load->helper('thumb'); | |
$thumb_100 = create_thumb($image_data, '100'); | |
$thumb_250 = create_thumb($image_data, '250'); | |
$thumb_500 = create_thumb($image_data, '500'); | |
$thumb_800 = create_thumb($image_data, '800'); | |
if( !empty($thumb_100) ){ | |
$options['thumb_100'] = $thumb_100['thumb_name']; | |
} | |
if( !empty($thumb_250) ){ | |
$options['thumb_250'] = $thumb_250['thumb_name']; | |
} | |
if( !empty($thumb_500) ){ | |
$options['thumb_500'] = $thumb_500['thumb_name']; | |
} | |
if( !empty($thumb_800) ){ | |
$options['thumb_800'] = $thumb_800['thumb_name']; | |
} | |
$options['filename'] = $image_data['file_name']; | |
$options['filetype'] = $image_data['file_ext']; | |
if ($options['filename'] == NULL || $options['filetype'] == NULL) return false; | |
} | |
} | |
else{ | |
unset($_FILES['userfile']); | |
} | |
if($image_data['file_ext'] == '.jpg' || $image_data['file_ext'] == '.jpeg' | |
|| $image_data['file_ext'] == '.png' || $image_data['file_ext'] == '.gif' | |
|| $image_data['file_ext'] == '.bmp'){ | |
$options['filegroup'] = 'image'; | |
} | |
else{ | |
$options['filegroup'] = 'bin'; | |
} | |
//required Values | |
if(!$this->_required( | |
array('filename', 'filetype'), | |
$options | |
)) return false; | |
$options = $this->_default(array('filestatus' => 'active'), $options); | |
$this->db->insert('files', $options); | |
return $this->db->insert_id(); | |
} | |
/* | |
* UpdateFile | |
* | |
* Option: Values | |
* -------------- | |
* idfile => required | |
* menustatus | |
* | |
* @param array $options | |
* @result int affected_rows() | |
*/ | |
function UpdateFile($options = array()) { | |
//required Values | |
if(!$this->_required( | |
array('idfile'), | |
$options | |
)) return false; | |
if (isset($options['filestatus'])) | |
$this->db->set('filestatus', $options['filestatus']); | |
$this->db->where('idfile', $options['idfile']); | |
$this->db->update('files'); | |
return $this->db->affected_rows(); | |
} | |
/* | |
* GetFiles | |
* | |
* Option: Values | |
* -------------- | |
* idfile | |
* filename | |
* filestatus | |
* limit limit the returned records | |
* offset bypass this many records | |
* sortBy sort by this column | |
* sortDirection (ASC, DESC) | |
* | |
* | |
* Returned Object (array of) | |
* -------------------------- | |
* idfile | |
* filename | |
* filetype | |
* filestatus | |
* | |
* @param array $options | |
* @result array of objects | |
* | |
*/ | |
function GetFiles($options = array()) { | |
// QUALIFICATION | |
if (isset($options['idfile'])) | |
$this->db->where('idfile', $options['idfile']); | |
if (isset($options['filename'])) | |
$this->db->where('filename', $options['filename']); | |
if (isset($options['filetype'])) | |
$this->db->where('filetype', $options['filetype']); | |
if (isset($options['filegroup'])) | |
$this->db->where('filegroup', $options['filegroup']); | |
if (isset($options['filestatus'])) | |
$this->db->where('filestatus', $options['filestatus']); | |
//so you don't get any deleted values | |
if(!isset($options['filestatus'])) $this->db->where('filestatus !=', 'deleted'); | |
// LIMIT OFFSET | |
if (isset($options['limit']) && isset($options['offset'])) | |
$this->db->limit($options['limit'], $options['offset']); | |
elseif (isset($options['limit'])) | |
$this->db->limit($options['limit']); | |
// SORT | |
if (isset($options['sortBy']) && isset($options['sortDirection'])) | |
$this->db->order_by($options['sortBy'], $options['sortDirection']); | |
$query = $this->db->get('files'); | |
if(isset($options['count'])) return $query->num_rows(); | |
if (isset($options['idfile'])) | |
return $query->row(0); | |
return $query->result(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment