191 lines
4.6 KiB
PHP
191 lines
4.6 KiB
PHP
|
<?php
|
||
|
App::uses('AppModel', 'Model');
|
||
|
/**
|
||
|
* MicDjPageImage Model
|
||
|
*
|
||
|
* @property PagePtr $PagePtr
|
||
|
*/
|
||
|
class DreamjobPageImage extends AppModel {
|
||
|
|
||
|
/**
|
||
|
* Use database config
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
public $useDbConfig = 'dreamjobMain';
|
||
|
|
||
|
/**
|
||
|
* Use table
|
||
|
*
|
||
|
* @var mixed False or table name
|
||
|
*/
|
||
|
public $useTable = 'mic_dj_page_image';
|
||
|
|
||
|
/**
|
||
|
* Primary key field
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
public $primaryKey = 'page_ptr_id';
|
||
|
|
||
|
/**
|
||
|
* Display field
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
public $displayField = 'page_ptr_id';
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Validation rules
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
|
public $validate = array(
|
||
|
'page_ptr_id' => array(
|
||
|
'numeric' => array(
|
||
|
'rule' => array('numeric'),
|
||
|
//'message' => 'Your custom message here',
|
||
|
//'allowEmpty' => false,
|
||
|
//'required' => false,
|
||
|
//'last' => false, // Stop validation after this rule
|
||
|
//'on' => 'create', // Limit validation to 'create' or 'update' operations
|
||
|
),
|
||
|
'multiple' => array(
|
||
|
'rule' => array('multiple'),
|
||
|
//'message' => 'Your custom message here',
|
||
|
//'allowEmpty' => false,
|
||
|
//'required' => false,
|
||
|
//'last' => false, // Stop validation after this rule
|
||
|
//'on' => 'create', // Limit validation to 'create' or 'update' operations
|
||
|
),
|
||
|
),
|
||
|
'image' => array(
|
||
|
// http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::uploadError
|
||
|
'uploadError' => array(
|
||
|
'rule' => 'uploadError',
|
||
|
'message' => 'Something went wrong with the file upload',
|
||
|
'required' => FALSE,
|
||
|
'allowEmpty' => TRUE,
|
||
|
),
|
||
|
// http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::mimeType
|
||
|
'mimeType' => array(
|
||
|
'rule' => array('mimeType', array('image/gif','image/png','image/jpg','image/jpeg')),
|
||
|
'message' => 'Invalid file, only images allowed',
|
||
|
'required' => FALSE,
|
||
|
'allowEmpty' => TRUE,
|
||
|
),
|
||
|
'processUpload' => array(
|
||
|
'rule' => 'processUpload',
|
||
|
'message' => 'Something went wrong with the file upload',
|
||
|
'required' => FALSE,
|
||
|
'allowEmpty' => TRUE,
|
||
|
'last' => TRUE,
|
||
|
)
|
||
|
)
|
||
|
);
|
||
|
|
||
|
//The Associations below have been created with all possible keys, those that are not needed can be removed
|
||
|
/**
|
||
|
* Upload Directory relative to WWW_ROOT
|
||
|
* @param string
|
||
|
*/
|
||
|
public $uploadDir = 'uploads';
|
||
|
|
||
|
/**
|
||
|
* Before Validation Callback
|
||
|
* @param array $options
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function beforeValidate($options = array()) {
|
||
|
// ignore empty file - causes issues with form validation when file is empty and optional
|
||
|
if (!empty($this->data[$this->alias]['image']['error']) && $this->data[$this->alias]['image']['error']==4 && $this->data[$this->alias]['image']['size']==0) {
|
||
|
unset($this->data[$this->alias]['image']);
|
||
|
}
|
||
|
return parent::beforeValidate($options);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Process the Upload
|
||
|
* @param array $check
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function processUpload($check=array()) {
|
||
|
// deal with uploaded file
|
||
|
if (!empty($check['image']['tmp_name'])) {
|
||
|
|
||
|
// check file is uploaded
|
||
|
if (!is_uploaded_file($check['image']['tmp_name'])) {
|
||
|
return FALSE;
|
||
|
}
|
||
|
$this->data[$this->alias]['image'] = "onUpload";
|
||
|
}
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
public function delete($id = NULL, $cascade = false){
|
||
|
$dataSource = $this->getDataSource();
|
||
|
$dataSource->begin();
|
||
|
|
||
|
$result = parent::delete($id['DreamjobPageInh']['id'],$cascade);
|
||
|
if($result){
|
||
|
$result = $this->DreamjobPageInh->delete($id['DreamjobPageInh']['id'],$cascade);
|
||
|
}
|
||
|
if($result){
|
||
|
$filename = WWW_ROOT . $this->uploadDir . DS .$id['DreamjobPageImage']['image'];
|
||
|
chmod($filename,0755);
|
||
|
$result = unlink($filename);
|
||
|
}
|
||
|
if ($result) {
|
||
|
$dataSource->commit();
|
||
|
return true;
|
||
|
} else {
|
||
|
$dataSource->rollback();
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
public function saveAndUpload($data){
|
||
|
$dataSource = $this->getDataSource();
|
||
|
$dataSource->begin();
|
||
|
$result = $this->saveAssociated($data);
|
||
|
if($result){
|
||
|
$filename = $data["DreamjobPageInh"]['user_id'] . "_-page-_" . $this->id .".". pathinfo($data[$this->alias]['image']['name'], PATHINFO_EXTENSION);
|
||
|
$filename = WWW_ROOT . $this->uploadDir . DS . $filename;
|
||
|
|
||
|
if(file_exists($filename)){
|
||
|
chmod($filename,0755); //Change the file permissions if allowed
|
||
|
unlink($filename); //remove the file
|
||
|
}
|
||
|
|
||
|
if (!move_uploaded_file($data[$this->alias]['image']['tmp_name'], $filename)) {
|
||
|
$result = false;
|
||
|
} else {
|
||
|
$result = $this->saveField('image', str_replace(DS, "/", str_replace(WWW_ROOT.$this->uploadDir.DS, "", $filename) ));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($result) {
|
||
|
$dataSource->commit();
|
||
|
return true;
|
||
|
} else {
|
||
|
$dataSource->rollback();
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
/**
|
||
|
* belongsTo associations
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
|
public $belongsTo = array(
|
||
|
'DreamjobPageInh' => array(
|
||
|
'className' => 'DreamjobPageInh',
|
||
|
'foreignKey' => 'page_ptr_id',
|
||
|
'conditions' => '',
|
||
|
'fields' => '',
|
||
|
'order' => ''
|
||
|
)
|
||
|
);
|
||
|
}
|