View file vkclone-0.0.1/protected/modules/forums/controllers/TopicController.php

File size: 2.43Kb
<?php
class TopicController extends FrontEndController {
	public function behaviors() {
		return array(
			'crud' => array(
				'class' => 'application.components.CrudControllerBehavior',
				'modelClass' => 'Topic',

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

				'deleteContinueUrl' => function (Topic $topic) { return array('section/index', 'id' => $topic->section_id); },
			),
		);
	}

	public function filters() {
		return CMap::mergeArray(parent::filters(), array(
			'postOnly + delete',
		));
	}

	public function accessRules() {
		return CMap::mergeArray(array(
			array('allow',
				'actions' => array('index'),
				'users' => array('@'),
			),
			array('allow',
				'actions' => array('update'),
				'roles' => array('forums.topic.updateTopic'),
			),
			array('allow',
				'actions' => array('confirm', 'delete'),
				'roles' => array('forums.topic.deleteTopic'),
			),
			array('allow',
				'actions' => array('createPost'),
				'roles' => array('forums.post.createPost'),
			),
		), parent::accessRules());
	}

	/**
	 * Shows topic
	 */
	public function actionIndex() {
		$this->render('index', array(
			'topic' => $this->crud->model,
			'posts' => new CActiveDataProvider(TopicPost::model(), array('criteria' => array(
				'condition' => 'topic_id = '.$this->crud->model->id,
				'order' => 'created_at',
			))),
		));
	}

	/**
	 * Add post to topic
	 */
	public function actionCreatePost() {
		$post = new TopicPost('create');
		$post->topic_id = $this->crud->model->id;
		$post->author_id = $this->webUser->id;
		$postVersion = new TopicPostVersion;
		$postVersion->author_id = $this->webUser->id;
		if (isset($_POST[CHtml::modelName($post)])) {
			$post->attributes = $_POST[CHtml::modelName($post)];
			$transaction = Yii::app()->db->beginTransaction();

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

		}
		$this->render('createPost', array(
			'topic' => $this->crud->model,
			'model' => $post,
		));
	}
}