Просмотр файла vkclone-0.0.1/protected/modules/forums/controllers/PostController.php

Размер файла: 3.24Kb
<?php
class PostController extends FrontEndController {
	/**
	 * Contains version
	 * @var TopicPostVersion
	 */
	public $version;

	public function behaviors() {
		return array(
			'crud' => array(
				'class' => 'application.components.CrudControllerBehavior',
				'modelClass' => 'TopicPost',

				'updateSuccessfullAlert' => Yii::t('Forums', 'Post has been changed'),
				'deleteSuccessfullAlert' => Yii::t('Forums', 'Post deleted'),
				'deleteFailureAlert' => Yii::t('Forums', 'Can not delete post'),

				'deleteContinueUrl' => function (TopicPost $post) { return array('topic/index', 'id' => $post->topic_id); },
			),
		);
	}


	public function filters() {
		return CMap::mergeArray(parent::filters(), array(
			array('ext.RetrieveRecordFilter.RetrieveRecordFilter + historyView',
				'param' => 'version_id',
				'attribute' => 'version',
				'model' => 'TopicPostVersion',
				'message' => 'Invalid Version!'
			),
			'postOnly + delete',
		));
	}

	public function accessRules() {
		return CMap::mergeArray(array(
			array('allow',
				'actions' => array('update'),
				'roles' => array('forums.post.updatePost', 'author' => array('author' => $this->crud->model->author_id)),
			),
			array('allow',
				'actions' => array('confirm', 'delete'),
				'roles' => array('forums.post.deletePost'),
			),
			array('allow',
				'actions' => array('history', 'historyView'),
				'roles' => array('forums.post.seeHistory'/*, 'author' => array('author' => $this->crud->model->author_id)*/),
			),
		), parent::accessRules());
	}

	/**
	 * Updates post
	 */
	public function actionUpdate() {
		$model = clone $this->crud->model;
		$model->setScenario('update');
		$model->update_datetime = new CDbExpression('NOW()');
		$postVersion = new TopicPostVersion;
		$postVersion->author_id = $this->webUser->id;
		$postVersion->post_id = $this->crud->model->id;

		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			$transaction = Yii::app()->db->beginTransaction();

			if ($model->text == $this->crud->model->text) {
				Yii::app()->user->setAlert(Yii::t('Forums', 'Nothing changed'), 'success');
				$this->redirect(array('topic/index', 'id' => $this->crud->model->topic_id));
			}

			if ($model->validate() && $model->save()) {
				$postVersion->diff = xdiff_string_diff($this->crud->model->text, $model->text);
				if ($postVersion->validate() && $postVersion->save()) {
					$transaction->commit();
					Yii::app()->user->setAlert(Yii::t('Forums', 'Post has been changed'), 'success');
					$this->redirect(array('topic/index', 'id' => $this->crud->model->topic_id));
				}
			}
			$transaction->rollback();
		}

		$this->render('edit', array(
			'model' => $model,
		));
	}

	/**
	 * Shows history of post
	 */
	public function actionHistory() {
		$this->render('history', array(
			'post' => $this->crud->model,
			'versions' => new CActiveDataProvider(TopicPostVersion::model(), array('criteria' => array(
				'condition' => 'post_id = :post_id',
				'order' => 'created_at, id',
				'params' => array(':post_id' => $this->crud->model->id),
			)))
		));
	}

	/**
	 * Shows version of post
	 */
	public function actionHistoryView() {
		$this->render('../version/index', array(
			'post' => $this->crud->model,
			'version' => $this->version,
		));
	}
}