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

Размер файла: 3.6Kb
<?php
class PublicController extends FrontEndController {
	public function behaviors() {
		return array(
			'crud' => array(
				'class' => 'application.components.CrudControllerBehavior',
				'modelClass' => 'CPublic',

				'updateSuccessfullAlert' => Yii::t('Publics', 'Public has been changed'),
				'deleteSuccessfullAlert' => Yii::t('Publics', 'Public deleted'),
				'deleteFailureAlert' => Yii::t('Publics', 'Can not delete public'),

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

	public function accessRules() {
		return CMap::mergeArray(parent::accessRules(), array(
			// edit/delete public
			array('allow',
				'actions' => array('update', 'confirm', 'delete'),
				'expression' => 'Yii::app()->controller->crud->model->checkAccess(PublicReader::ADMINISTRATOR);',
			),
			// add post
			array('allow',
				'actions' => array('writePost'),
				'expression' => 'Yii::app()->controller->crud->model->checkAccess(PublicReader::WRITER);',
			),
			array('deny',
				'actions' => array('create', 'update', 'confirm', 'delete', 'writePost'),
			),
		));
	}

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

	/**
	 * Subscribes currently logged user to public
	 */
	public function actionSubscribe() {
		if (PublicReader::model()->findByAttributes(array('user_id' => $this->webUser->id, 'public_id' => $this->crud->model->id)) !== null)
			$this->redirect(array('index', 'id' => $this->crud->model->id));

		$reading = new PublicReader;
		$reading->public_id = $this->crud->model->id;
		$reading->user_id = $this->webUser->id;
		$reading->role = PublicReader::READER;
		if (!$reading->save())
			throw new CHttpException(500, 'Can not subscribe user');
		Yii::app()->user->setAlert(Yii::t('Publics', 'You subscribed to public'), 'success');
		$this->redirect(array('index', 'id' => $this->crud->model->id));
	}

	/**
	 * Unsubscribes currently logged user from public
	 */
	public function actionUnsubscribe() {
		if (($model = PublicReader::model()->findByAttributes(array('user_id' => $this->webUser->id, 'public_id' => $this->crud->model->id))) == null)
			$this->redirect(array('index', 'id' => $this->crud->model->id));

		if (!$model->delete())
			throw new CHttpException(500, 'Can not unsubscribe user');

		Yii::app()->user->setAlert(Yii::t('Publics', 'You unsubscribed from public'), 'success');
		$this->redirect(array('index', 'id' => $this->crud->model->id));
	}

	/**
	 * Saves post
	 */
	public function actionWritePost() {
		$post = new PublicPost('create');
		$post->public_id = $this->crud->model->id;
		$post->author_id = $this->webUser->id;
		if (isset($_POST[CHtml::modelName($post)])) {
			$post->attributes = $_POST[CHtml::modelName($post)];
			if ($post->validate() && $post->save()) {
				Yii::app()->user->setAlert(Yii::t('Publics', 'Post created'), 'success');
				$this->redirect(array('index', 'id' => $this->crud->model->id));
			}
		}
		$this->render('writePost', array(
			'public' => $this->crud->model,
			'model' => $post,
		));
	}

	/**
	 * Shows public's readers
	 */
	public function actionReaders() {
		$this->render('readers', array(
			'model' => $this->crud->model,
			'readers' => new CActiveDataProvider(PublicReader::model(), array(
				'criteria' => array(
					'condition' => 'public_id = :public',
					'params' => array(':public' => $this->crud->model->id),
				),
			)),
		));
	}
}