Created
April 4, 2013 10:39
-
-
Save sandeepone/5309410 to your computer and use it in GitHub Desktop.
A sample media model for Gleez CMS
This file contains 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 defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Core File class for handling media. | |
* | |
* This is the API for handling file media, extend this for handling content types. | |
* | |
* Note: by design, this class does not do any permission checking. | |
* | |
* @package Gleez | |
* @category Media | |
* @author Sandeep Sangamreddi - Gleez | |
* @copyright (c) 2012 Gleez Technologies | |
* @license http://gleezcms.org/license | |
*/ | |
class Gleez_Media extends Model { | |
protected $_table_columns = array( | |
'id' => array( 'type' => 'int' ), | |
'uid' => array( 'type' => 'int' ), | |
'title' => array( 'type' => 'string' ), | |
'name' => array( 'type' => 'string' ), | |
'path' => array( 'type' => 'string' ), | |
'desc' => array( 'type' => 'string' ), | |
'type' => array( 'type' => 'string' ), | |
'size' => array( 'type' => 'int' ), | |
'status' => array( 'type' => 'int' ), | |
'model' => array( 'type' => 'string' ), | |
'mid' => array( 'type' => 'int' ), | |
'created' => array( 'type' => 'int' ), | |
'updated' => array( 'type' => 'int' ), | |
'version' => array( 'type' => 'int' ), | |
'default' => array( 'type' => 'int' ), | |
'entity' => array( 'type' => 'int' ) | |
); | |
/** | |
* Auto fill create and update columns | |
*/ | |
protected $_created_column = array('column' => 'created', 'format' => TRUE); | |
protected $_updated_column = array('column' => 'updated', 'format' => TRUE); | |
/** | |
* Table name | |
* @var array | |
*/ | |
protected $_object = array(); | |
/** | |
* Table name | |
* @var string | |
*/ | |
protected $_table_name = 'media'; | |
/** | |
* Database Object | |
* @var Database | |
*/ | |
protected $_db = NULL; | |
/** | |
* @var User User id of the currently logged in user | |
*/ | |
protected $user; | |
/** | |
* @var array the File objects | |
*/ | |
protected $_files = array(); | |
/** | |
* @var array the allowed file types | |
*/ | |
protected $_allowedtype = array('jpg', 'png', 'gif'); | |
/** | |
* @var string the File size | |
*/ | |
protected $_allowedsize = '10M'; | |
/** | |
* @var string the File storage path | |
*/ | |
protected $_file_path; | |
/** | |
* @var string the Thumbnail storage path | |
*/ | |
protected $_thumb_path; | |
/** | |
* @var string the URI of the file | |
*/ | |
protected $_file_url; | |
/** | |
* @var string the Thumbnail URI of the file | |
*/ | |
protected $_thumb_url; | |
/** | |
* @var boolean ajax request check | |
*/ | |
protected $_ajax = FALSE; | |
/** | |
* Creates a new media object | |
* | |
* $file = new Media(); | |
* | |
* If $id parameter is set, loads the file object from the database. | |
*/ | |
public function __construct($id = NULL) | |
{ | |
$this->user = (int) User::active_user()->id; | |
//set default urls | |
$this->_file_path = APPPATH . 'media/uploads'; | |
$this->_thumb_path = DOCROOT . 'media/imagecache/resize/80x80/uploads'; | |
// create directories | |
if( !is_dir($this->_file_path) ) mkdir($this->_file_path, 0777, TRUE); | |
if( !is_dir($this->_thumb_path) ) mkdir($this->_thumb_path, 0777, TRUE); | |
// check ajax request | |
$this->_ajax = ( Request::current()->is_ajax() OR isset($_POST['script']) ); | |
//set default urls | |
$this->_file_url = URL::site('media/uploads/'); | |
$this->_thumb_url = URL::site('media/imagecache/resize/80x80/uploads/'); | |
if ( ! is_object($this->_db)) | |
{ | |
// Get database instance | |
//$this->_db = Database::instance(); | |
} | |
} | |
/** | |
* Finds multiple database rows and returns an iterator of the rows found. | |
* | |
* @return Media array | |
*/ | |
public function find_all($mid = 0, $model = 'post') | |
{ | |
$files = DB::select()->from($this->_table_name)->where('model', '=', $model)->where('mid', '=', $mid)->where('entity', '=', Entity::$id)->execute($this->_db); | |
$info = array(); | |
if( $files ) | |
{ | |
foreach($files as $file) | |
{ | |
$info[] = array( 'name' => $file['name'], | |
'title'=> $file['title'], | |
'size' => (int) $file['size'], | |
'type' => $file['type'], | |
'mid' => (int) $file['mid'], | |
'url' => $file['path'], | |
'thumb_url' => "{$this->_thumb_url}/{$file['name']}", | |
'delete_url' => '/upload/delete/'.$file['id'], | |
'delete_type' => 'DELETE', | |
'id' => (int) $file['id'], | |
'mdefault' => (int) $file['default'] //IE bug dont send default, rename it to mdefault | |
); | |
} | |
} | |
return $info; | |
} | |
/** | |
* Create or update the media object | |
* | |
* @param $_FILES array of file data in the form $_FILES['files'] | |
* | |
* @return array Media | |
* @throws Validation_Exception when an invalid name/file is given | |
* @throws Kohana_Exception when an error occurs saving the file | |
*/ | |
public function save($_files) | |
{ | |
$upload = isset($_files['files']) ? $_files['files'] : null; | |
$files = array(); | |
Module::event('media_presave', $this, $files); | |
if( $upload AND is_array($upload['tmp_name']) ) | |
{ | |
foreach($upload['tmp_name'] as $index => $value) | |
{ | |
$file = array( 'name' => isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index], | |
'type' => isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index], | |
'tmp_name' => $upload['tmp_name'][$index], | |
'error' => $upload['error'][$index], | |
'size' => isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index], | |
); | |
$files[] = $this->upload( array('files' => $file) ); | |
} | |
} | |
else if( $upload AND is_array($upload) ) | |
{ | |
$files[] = $this->upload( array('files' => $upload) ); | |
} | |
Module::event('media_save', $this, $files); | |
if( $this->_ajax ) return $files; | |
return $this; | |
} | |
/** | |
* Deletes a single record or multiple records, ignoring relationships. | |
* | |
* @return Bool | |
*/ | |
public function delete($id) | |
{ | |
$file = DB::select()->from($this->_table_name)->where('id', '=', $id)->where('entity', '=', Entity::$id)->execute($this->_db)->current(); | |
if( $file AND isset($file['name']) ) | |
{ | |
$file_path = $this->_file_path.'/'.$file['name']; | |
$success = is_file($file_path) AND unlink($file_path); | |
if ($success) | |
{ | |
DB::delete($this->_table_name)->where('id', '=', $id)->execute($this->_db); | |
$path = $this->_thumb_path.'/'.$file['name']; | |
is_file($path) AND unlink($path); | |
} | |
} | |
return $success; | |
} | |
/** | |
* Handles setting of column | |
* | |
* @param string $column Column name | |
* @param mixed $value Column value | |
* @return void | |
*/ | |
public function set($column, $value) | |
{ | |
if (array_key_exists($column, $this->_table_columns)) | |
{ | |
$this->_object[$column] = $value; | |
} | |
else | |
{ | |
throw new Kohana_Exception('The :property: property does not exist in the :class: class', | |
array(':property:' => $column, ':class:' => get_class($this))); | |
} | |
return $this; | |
} | |
/** | |
* Set or get the allowed types for the file | |
* | |
* @param array $type The allowed file types | |
* @return Media | |
* @return void | |
*/ | |
public function type( array $type = NULL) | |
{ | |
if ($type === NULL) | |
{ | |
// Act as a getter | |
return $this->_allowedtype; | |
} | |
// Act as a setter | |
$this->_allowedtype = $type; | |
return $this; | |
} | |
/** | |
* Set or get the size of the file | |
* | |
* @param string $size Allowed file size with units('10B', '10M') | |
* @return Media | |
* @return void | |
*/ | |
public function size($size = NULL) | |
{ | |
if ($size === NULL) | |
{ | |
// Act as a getter | |
return $this->_allowedsize; | |
} | |
// Act as a setter | |
$this->_allowedsize = $size; | |
return $this; | |
} | |
/** | |
* Set or get the path for the file | |
* | |
* @param string $path Path to store the file | |
* @return Media | |
* @return void | |
*/ | |
public function file_path($path = NULL) | |
{ | |
if ($path === NULL) | |
{ | |
// Act as a getter | |
return $this->_file_path; | |
} | |
// Act as a setter | |
$this->_file_path = $path; | |
if( !is_dir($this->_file_path) ) mkdir($this->_file_path, 0777, TRUE); | |
return $this; | |
} | |
/** | |
* Set or get the thumbnail path for the file | |
* | |
* @param string $path Thumbnail Path to store the file | |
* @return Media | |
* @return void | |
*/ | |
public function thumb_path($path = NULL) | |
{ | |
if ($path === NULL) | |
{ | |
// Act as a getter | |
return $this->_thumb_path; | |
} | |
// Act as a setter | |
$this->_thumb_path = $path; | |
if( !is_dir($this->_thumb_path) ) mkdir($this->_thumb_path, 0777, TRUE); | |
return $this; | |
} | |
/** | |
* Set or get the url for the file | |
* | |
* @param string $url URL of the file | |
* @return Media | |
* @return void | |
*/ | |
public function file_url($url = NULL) | |
{ | |
if ($url === NULL) | |
{ | |
// Act as a getter | |
return $this->_file_url; | |
} | |
// Act as a setter | |
$this->_file_url = $url; | |
return $this; | |
} | |
/** | |
* Set or get the thumbnail url for the file | |
* | |
* @param string $url Thumbnail Url of the file | |
* @return Media | |
* @return void | |
*/ | |
public function thumb_url($url = NULL) | |
{ | |
if ($url === NULL) | |
{ | |
// Act as a getter | |
return $this->_thumb_url; | |
} | |
// Act as a setter | |
$this->_thumb_url = $url; | |
return $this; | |
} | |
/** | |
* Validate and Upload a file | |
* | |
* @throws Validation_Exception when an invalid name/file is given | |
* @throws Kohana_Exception when an error occurs saving the file | |
* @param array Array of file data | |
*/ | |
protected function upload($file) | |
{ | |
// In order to make upload validation to work we need to add temp array | |
//$f['file'] = $file; | |
Module::event('media_prevalid', $this, $file); | |
$data = Validation::factory($file) | |
->rule('files', 'Upload::not_empty') | |
->rule('files', 'Upload::valid') | |
->rule('files', 'Upload::size', array(':value', $this->_allowedsize)) | |
->rule('files', 'Upload::type', array(':value', $this->_allowedtype)); | |
if ( $data->check() ) | |
{ | |
try | |
{ | |
$ext = strtolower(pathinfo($file['files']['name'], PATHINFO_EXTENSION)); | |
$filename = UTF8::strtolower(uniqid().Text::random('alnum', 10)).'.'.$ext; | |
if( $filepath = Upload::save($file['files'], $filename, $this->_file_path, "0777") ) | |
{ | |
$file['files']['url'] = "$this->_file_url/$filename"; | |
$file['files']['filename'] = $filename; | |
/** Create thumbnail only for images. | |
* @todo move to pathinfo for handling type specefic thumbs or icons | |
*/ | |
if( Upload::type($file['files'], array('jpg','png','gif')) ) | |
{ | |
//create thumb for performance with fileupload, respecting media/imagecache | |
Image::factory($filepath)->resize(80, 80)->save($this->_thumb_path . '/'.$filename); | |
$file['files']['thumb_url'] = "$this->_thumb_url/$filename"; | |
} | |
$_file = $this->_upload_file($file['files']); | |
$this->_files[] = $_file; | |
return $_file; | |
} | |
} | |
catch (Exception $e) | |
{ | |
Kohana::$log->add(LOG::ERROR, 'Exception encountered uploading file to '.$e->getMessage()); | |
if( $this->_ajax ) return array('error' => $e->getMessage() ); // for json response | |
throw new Kohana_Exception('Unable to upload file :name', array(':name' => $this)); | |
} | |
} | |
$error = $data->errors('models/upload'); | |
if( $this->_ajax ) return array('error' => $error['files'] ); | |
return $error; | |
} | |
private function _upload_file($file) | |
{ | |
$data = array( | |
'title' => isset($file['title']) ? $file['title'] : $file['name'], | |
'name' => $file['filename'], | |
'size' => $file['size'], | |
'path' => $file['url'], | |
'type' => $file['type'], | |
'uid' => $this->user, | |
'default' => (bool) isset($file['default']) ? $file['default'] : 0, | |
'status' => (bool) isset($file['status']) ? $file['status'] : 1, | |
'created' => time(), | |
'model' => isset($this->_object['model']) ? $this->_object['model'] : 'post', | |
'mid' => (int) isset($this->_object['mid']) ? $this->_object['mid'] : 0, | |
'entity' => Entity::$id, | |
); | |
$result = $this->_upload_save($data); | |
return $data + array('id' => $result[0], 'url' => $file['url'], 'thumb_url' => isset($file['thumb_url']) ? $file['thumb_url'] : null); | |
} | |
private function _upload_save($data) | |
{ | |
try | |
{ | |
return DB::insert($this->_table_name) | |
->columns(array_keys($data)) | |
->values(array_values($data)) | |
->execute($this->_db); | |
} | |
catch(Exception $e) | |
{ | |
Kohana::$log->add(LOG::ERROR, 'Error: :error saving media!', array(':error' => $e->getMessage()) ); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment