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

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

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

				'deleteContinueUrl' => function (ForumSection $section) { return array('forum/index', 'id' => $section->forum_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.section.updateSection'),
			),
			array('allow',
				'actions' => array('confirm', 'delete'),
				'roles' => array('forums.section.deleteSection'),
			),
			array('allow',
				'actions' => array('createTopic'),
				'roles' => array('forums.topic.createTopic'),
			),
		), parent::accessRules());
	}

	/**
	 * Shows section
	 */
	public function actionIndex() {
		$this->render('index', array(
			'section' => $this->crud->model,
			'topics' => new CActiveDataProvider(Topic::model(), array('criteria' => array(
				'condition' => 'section_id = '.$this->crud->model->id,
				'order' => 'updated_at DESC',
			))),
		));
	}

	/**
	 * Add topic to section
	 */
	public function actionCreateTopic() {
		$topic = new Topic('create');
		$topic->section_id = $this->crud->model->id;
		$topic->initiator_id = $this->webUser->id;
		$post = new TopicPost('create');
		$post->author_id = $this->webUser->id;
		$postVersion = new TopicPostVersion;
		$postVersion->author_id = $this->webUser->id;
		if (isset($_POST[CHtml::modelName($topic)]) && isset($_POST[CHtml::modelName($post)])) {
			$topic->attributes = $_POST[CHtml::modelName($topic)];
			$post->attributes = $_POST[CHtml::modelName($post)];
			$transaction = Yii::app()->db->beginTransaction();
			if ($topic->validate() && $topic->save()) {
				$post->topic_id = $topic->id;
				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', 'Topic created'), 'success');
						$this->redirect(array('topic/index', 'id' => $topic->id));
					}
				}
			}
			$transaction->rollback();

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