<?php
class PhotoController extends FrontEndController {
public function behaviors() {
return array(
'crud' => array(
'class' => 'application.components.CrudControllerBehavior',
'modelClass' => 'GalleryPhoto',
'updateSuccessfullAlert' => Yii::t('Gallery', 'Photo description saved'),
'deleteSuccessfullAlert' => Yii::t('Gallery', 'Photo deleted'),
'deleteFailureAlert' => Yii::t('Gallery', 'Can not delete photo'),
'deleteContinueUrl' => array('default/myPhotos'),
),
);
}
public function accessRules() {
return CMap::mergeArray(array(
array('allow',
'actions' => array('index'),
'users' => array('@'),
),
array('allow',
'actions' => array('avatar'),
'roles' => array('author' => array('author' => $this->crud->model->user->id)),
),
array('allow',
'actions' => array('update'),
'roles' => array('gallery.photo.updatePhoto', 'author' => array('author' => $this->crud->model->user->id)),
),
array('allow',
'actions' => array('confirm', 'delete'),
'roles' => array('gallery.photo.deletePhoto', 'author' => array('author' => $this->crud->model->user->id)),
),
), parent::accessRules());
}
public function filters() {
return CMap::mergeArray(parent::filters(), array(
'postOnly + delete',
));
}
// /**
// * Renders form to confirm deletion
// */
// public function actionConfirm() {
// $this->render('confirm', array(
// 'photo' => $this->crud->model,
// ));
// }
// /**
// * Deletes photo
// */
// public function actionDelete() {
// if ($this->crud->model->delete()) {
// Yii::app()->user->setAlert(Yii::t('Gallery', 'Photo {name} deleted', array('{name}' => CHtml::encode($this->crud->model->name))), 'success');
// $this->redirect(array('album/view', 'id' => $this->crud->model->album->id));
// } else {
// Yii::app()->user->setAlert(Yii::t('Gallery', 'Can not delete photo'), 'danger');
// }
// }
/**
* Sets photo as a user's avatar
*/
public function actionAvatar() {
Yii::app()->user->record->profile->avatar_id = $this->crud->model->id;
Yii::app()->user->record->profile->save();
Yii::app()->user->setAlert(Yii::t('Gallery', 'Photo has been setted as your avatar'), 'success');
$this->redirect(array('index', 'id' => $this->crud->model->id));
}
// /**
// * Shows form to edit photo description
// */
// public function actionUpdate() {
// $model = $this->crud->model;
// $model->setScenario('edit');
// if (isset($_POST[CHtml::modelName($model)])) {
// $model->attributes = $_POST[CHtml::modelName($model)];
// if ($model->save()) {
// Yii::app()->user->setAlert(Yii::t('Gallery', 'Photo description saved'), 'success');
// $this->redirect(array('index', 'id' => $this->crud->model->id));
// }
// }
// $this->render('edit', array(
// 'model' => $model,
// ));
// }
}