<?php
/**
* This is the model class for table "{{photos}}".
*
* The followings are the available columns in table '{{photos}}':
* @property string $id
* @property string $user_id
* @property string $album_id
* @property string $uploaded_at
* @property string $name
* @property string $description
* @property string $hash
*
* The followings are the available model relations:
* @property User $user
* @property GalleryAlbum $album
*/
class GalleryPhoto extends CActiveRecord {
public $image;
/**
* @return string the associated database table name
*/
public function tableName() {
return '{{gallery_photos}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
return array(
array('user_id', 'required'),
array('user_id, album_id', 'length', 'max' => 10),
array('name', 'length', 'max' => 255),
array('hash', 'length', 'max' => 32),
array('image, description', 'safe'),
array('image', 'file', 'types' => array('png', 'gif', 'jpeg' ,'jpg'), 'on' => 'add'),
array('album_id', 'exist', 'className' => 'GalleryAlbum', 'attributeName' => 'id', 'criteria' => array('condition' => 'user_id = :user', 'params' => array(':user' => Yii::app()->user->record->id))),
array('album_id', 'default', 'value' => null),
// The following rule is used by search().
array('id, user_id, album_id, uploaded_at, description', 'safe', 'on' => 'search'),
);
}
/**
* @return array relational rules.
*/
public function relations() {
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'album' => array(self::BELONGS_TO, 'GalleryAlbum', 'album_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'user_id' => Yii::t('Gallery', 'User'),
'album_id' => Yii::t('Gallery', 'Album'),
'uploaded_at' => Yii::t('Gallery', 'Creation DateTime'),
'description' => Yii::t('Gallery', 'Description'),
'image' => Yii::t('Gallery', 'Image'),
'hash' => Yii::t('Gallery', 'Hash'),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
$criteria->compare('user_id', $this->user_id, true);
$criteria->compare('album_id', $this->album_id, true);
$criteria->compare('uploaded_at', $this->uploaded_at, true);
$criteria->compare('description', $this->description, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Photo the static model class
*/
public static function model($className=__CLASS__) {
return parent::model($className);
}
/**
* Generates hash based on image type
*/
public function getHash($type) {
return md5($this->hash.'_'.$type);
}
/**
* Returns full filename
*/
public function getFilename($type = 'original') {
$dir = Yii::app()->staticManager->generateFilename('photos', $this->id);
$filename = $dir.'/'.$type.'_'.$this->getHash($type).'.'.Yii::app()->staticManager->normalizeExtension($this->name);
return $filename;
}
/**
* Returns web path
*/
public function getWebPath($type = 'original') {
$dir = Yii::app()->staticManager->generateFileUri('photos', $this->id);
$filename = $dir.'/'.$type.'_'.$this->getHash($type).'.'.Yii::app()->staticManager->normalizeExtension($this->name);
return $filename;
}
/**
* Deletes all assigned files before delete record
*/
public function beforeDelete() {
if (parent::beforeDelete()) {
Yii::app()->gearman->client->addTask('gallery.deleteFiles', serialize(array(
$this->filename,
$this->getFilename(360),
$this->getFilename(72),
)));
Yii::app()->gearman->client->runTasks();
return true;
}
}
/**
* Generates a random hash
*/
public function beforeSave() {
if (parent::beforeSave()) {
if (empty($this->hash))
$this->hash = md5(uniqid($this->name, true));
return true;
}
return false;
}
/**
* Moves file after uploading and make previews
*/
public function afterSave() {
parent::afterSave();
// if attribute {image} is an CUploadedFile instance (means file has been uploaded)
if ($this->image instanceof CUploadedFile) {
// move file to the runtime directory and change access modes
$runtime_filename = Yii::getPathOfAlias('application.runtime').'/photo'.$this->id;
$this->image->saveAs($runtime_filename);
chmod($runtime_filename, 0666);
Yii::app()->gearman->client->addTaskBackground('gallery.saveImage', serialize(array(
'temp' => $runtime_filename,
'original' => $this->filename,
'previews' => array(
360 => $this->getFilename(360),
72 => $this->getFilename(72),
),
)));
Yii::app()->gearman->client->runTasks();
}
}
/**
* Default named scope
*/
public function defaultScope() {
return array(
'order' => 'uploaded_at DESC',
);
}
/**
* Checks if image is ready
*/
public function isReady($type = 'original') {
return file_exists($this->getFilename($type));
}
}