<?
/**
* A built-in model to handle files and uploads.
*
* You should not directly use this model unless you are developing extensions.
*
* @package Model
* @subpackage BuiltinModels
* @todo Can this be created on-the-fly like many-to-many field tables?
*/
class File extends zajModel {
///////////////////////////////////////////////////////////////
// !Model design
///////////////////////////////////////////////////////////////
public static function __model(){
// define custom database fields
$fields->parent = zajDb::text();
$fields->name = zajDb::name();
$fields->mime = zajDb::text();
$fields->size = zajDb::integer();
$fields->description = zajDb::textbox();
// do not modify the line below!
$fields = parent::__model(__CLASS__, $fields); return $fields;
}
///////////////////////////////////////////////////////////////
// !Construction and other required methods
///////////////////////////////////////////////////////////////
public function __construct($id = ""){ parent::__construct($id, __CLASS__); }
public static function __callStatic($name, $arguments){ array_unshift($arguments, __CLASS__); return call_user_func_array(array('parent', $name), $arguments); }
///////////////////////////////////////////////////////////////
// !Model methods
///////////////////////////////////////////////////////////////
public function download($force_download=true){
// generate path
$this->zajlib->load->library('file');
$file_path = $this->zajlib->file->get_id_path($this->zajlib->basepath."data/File", $this->id, true);
// pass file thru to user
header('Content-Type: '.$this->data->mime);
header('Content-Length: '.filesize($file_path));
if($force_download) header('Content-Disposition: attachment; filename="'.$this->data->name.'"');
else header('Content-Disposition: inline; filename="'.$this->data->name.'"');
ob_clean();
flush();
readfile($file_path);
// now exit
exit;
}
public function upload($uploadbox){
// process data given by upload box
// TODO: finish this
}
public function setfile($tmpname){
// generate new path
$this->zajlib->load->library('file');
$new_path = $this->zajlib->file->get_id_path($this->zajlib->basepath."data/File", $this->id, true);
// move tmpname to new location
move_uploaded_file($tmpname, $new_path);
// now set restrictive permissions
chmod($new_path, 0644);
// now set and save me
// TODO: add mime-type detection here!
// $this->set('mime',$mimetype);
$this->set('size',filesize($new_path));
$this->save();
}
public function delete($complete = false){
// remove photo files
if($complete){
// generate path
$this->zajlib->load->library('file');
$file_path = $this->zajlib->file->get_id_path($this->zajlib->basepath."data/File", $this->id, true);
// delete file
@unlink($file_path);
}
// call parent
parent::delete($complete);
}
///////////////////////////////////////////////////////////////
// !Static methods
///////////////////////////////////////////////////////////////
}
?>