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

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

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

				'deleteContinueUrl' => function (GroupTopic $topic) { return array('group/index', 'id' => $topic->group_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'),
				'expression' => 'Yii::app()->controller->crud->model->group->checkAccess(GroupMember::ADMINISTRATOR)',
			),
			array('allow',
				'actions' => array('update'),
				'roles' => array('groups.topic.updateTopic'),
			),
			array('allow',
				'actions' => array('delete', 'confirm'),
				'expression' => 'Yii::app()->controller->crud->model->group->checkAccess(GroupMember::ADMINISTRATOR)',
			),
			array('allow',
				'actions' => array('confirm', 'delete'),
				'roles' => array('groups.topic.deleteTopic'),
			),
			array('allow',
				'actions' => array('createPost'),
				'expression' => 'Yii::app()->controller->model->group->checkAccess(GroupMember::MEMBER)',
			),
		), parent::accessRules());
	}

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

	/**
	 * Add post to topic
	 */
	public function actionCreatePost() {
		$post = new GroupTopicPost('create');
		$post->topic_id = $this->crud->model->id;
		$post->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()) {
				$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,
		));
	}
}