File size: 1.66Kb
<?php
class PostController extends FrontEndController {
public function behaviors() {
return array(
'crud' => array(
'class' => 'application.components.CrudControllerBehavior',
'modelClass' => 'GroupTopicPost',
'updateSuccessfullAlert' => Yii::t('Groups', 'Post has been changed'),
'deleteSuccessfullAlert' => Yii::t('Groups', 'Post deleted'),
'deleteFailureAlert' => Yii::t('Groups', 'Can not delete post'),
'deleteContinueUrl' => function (GroupTopicPost $post) { return array('topic/index', 'id' => $post->topic_id); },
),
);
}
public function accessRules() {
return CMap::mergeArray(array(
array('allow',
'actions' => array('update'),
'roles' => array('groups.post.updatePost'),
),
array('allow',
'actions' => array('confirm', 'delete'),
'roles' => array('groups.post.deletePost'),
),
), parent::accessRules());
}
/**
* Updates post
*/
public function actionUpdate() {
$model = clone $this->crud->model;
$model->setScenario('update');
$model->update_datetime = new CDbExpression('NOW()');
if (isset($_POST[CHtml::modelName($model)])) {
$model->attributes = $_POST[CHtml::modelName($model)];
if ($model->text == $this->crud->model->text) {
Yii::app()->user->setAlert(Yii::t('Groups', 'Nothing changed'), 'success');
$this->redirect(array('topic/index', 'id' => $this->crud->model->topic_id));
}
if ($model->validate() && $model->save()) {
Yii::app()->user->setAlert(Yii::t('Groups', 'Post has been changed'), 'success');
$this->redirect(array('topic/index', 'id' => $this->crud->model->topic_id));
}
}
$this->render('update', array(
'model' => $model,
));
}
}