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

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

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

				'deleteContinueUrl' => array('default/index'),
			),
		);
	}

	public function accessRules() {
		return CMap::mergeArray(array(
			array('allow',
				'actions' => array('index'),
				'users' => array('@'),
			),
			array('allow',
				'actions' => array('moveUp', 'moveDown'),
				'roles' => array('forums.forum.orderForum'),
			),
			array('allow',
				'actions' => array('update'),
				'roles' => array('forums.forum.updateForum'),
			),
			array('allow',
				'actions' => array('confirm', 'delete'),
				'roles' => array('forums.forum.deleteForum'),
			),
			array('allow',
				'actions' => array('createSection'),
				'roles' => array('forums.section.createSection'),
			),
		), parent::accessRules());
	}

	/**
	 * Shows forum
	 */
	public function actionIndex() {
		$model = new ForumSection('create');
		$model->forum_id = $this->crud->model->id;
		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			if ($model->validate()) {
				if ($model->save()) {
					Yii::app()->user->setAlert(Yii::t('Forums', 'Section added'), 'success');
					$this->refresh();
				}
			}
		}
		$this->render('index', array(
			'forum' => $this->crud->model,
			'sections' => new CActiveDataProvider(ForumSection::model(), array('criteria' => array(
				'condition' => 'forum_id = '.$this->crud->model->id,
				'order' => 'total_topics DESC',
			)), array('pagination' => false)),
			'model' => $model,
		));
	}

	/**
	 * Moves forum up
	 */
	public function actionMoveUp() {
		if ($this->crud->model->moveUp())
			Yii::app()->user->setAlert(Yii::t('Forums', 'Forum moved up'), 'success');
		$this->redirect(array('default/index'));
	}

	/**
	 * Moves forum down
	 */
	public function actionMoveDown() {
		if ($this->crud->model->moveDown())
			Yii::app()->user->setAlert(Yii::t('Forums', 'Forum moved down'), 'success');
		$this->redirect(array('default/index'));
	}

	/**
	 * Deletes forum
	 */
	public function actionDelete() {
		if (count($this->crud->model->sections) > 0) {
			Yii::app()->user->setAlert(Yii::t('Forums', 'You can not delete non-empty forum!'), 'danger');
			$this->redirect(array('confirm', 'id' => $this->crud->model->id));
		}

		if ($this->crud->model->delete()) {
			Yii::app()->user->setAlert(Yii::t('Forums', 'Forum {forum} deleted', array('{forum}' => CHtml::encode($this->crud->model->name))), 'success');
			$this->redirect(array('default/index'));
		} else {
			Yii::app()->user->setAlert(Yii::t('Forums', 'Can not delete forum'), 'danger');
			$this->redirect(array('confirm', 'id' => $this->crud->model->id));
		}
	}

	/**
	 * Add section to forum
	 */
	public function actionCreateSection() {
		$model = new ForumSection('create');
		$model->forum_id = $this->crud->model->id;
		if (isset($_POST[CHtml::modelName($model)])) {
			$model->attributes = $_POST[CHtml::modelName($model)];
			if ($model->validate()) {
				if ($model->save()) {
					Yii::app()->user->setAlert(Yii::t('Forums', 'Section created'), 'success');
					$this->redirect(array('index', 'id' => $this->crud->model->id));
				}
			}
		}
		$this->render('createSection', array(
			'forum' => $this->crud->model,
			'model' => $model,
		));
	}
}