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

Размер файла: 5.85Kb
<?php
class GroupController extends FrontEndController {
	public function accessRules() {
		return CMap::mergeArray(array(
			array('allow',
				'actions' => array('index'),
				'users' => array('@'),
			),
			array('allow',
				'actions' => array('join'),
				'expression' => '!Yii::app()->controller->model->hasMember(Yii::app()->user->record->id)',
			),
			array('allow',
				'actions' => array('leave', 'members', 'createTopic'),
				'expression' => 'Yii::app()->controller->model->checkAccess(GroupMember::MEMBER)',
			),
			array('allow',
				'actions' => array('update'),
				'expression' => 'Yii::app()->controller->crud->model->checkAccess(GroupMember::ADMINISTRATOR)',
			),
			array('allow',
				'actions' => array('update'),
				'roles' => array('groups.group.updateGroup'),
			),
			array('allow',
				'actions' => array('delete', 'confirm'),
				'expression' => 'Yii::app()->controller->crud->model->checkAccess(GroupMember::ADMINISTRATOR)',
			),
			array('allow',
				'actions' => array('delete', 'confirm'),
				'roles' => array('groups.group.deleteGroup'),
			),
			array('allow',
				'actions' => array('purgeMember'),
				'expression' => 'Yii::app()->controller->crud->model->checkAccess(GroupMember::ADMINISTRATOR)',
			),
			array('allow',
				'actions' => array('purgeMember'),
				'roles' => array('groups.member.purgeMember'),
			),
		), parent::accessRules());
	}

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

	public function behaviors() {
		return array(
			'crud' => array(
				'class' => 'application.components.CrudControllerBehavior',
				'modelClass' => 'Group',

				'updateSuccessfullAlert' => Yii::t('Groups', 'Group has been changed'),
				'deleteSuccessfullAlert' => Yii::t('Groups', 'Group deleted'),
				'deleteFailureAlert' => Yii::t('Groups', 'Can not delete group'),

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

	/**
	 * Shows group
	 */
	public function actionIndex() {
		if (!$this->crud->model->hasMember($this->webUser->id)) {
			$this->render('indexAlternative', array(
				'model' => $this->crud->model,
			));
		} else {
			$this->render('index', array(
				'model' => $this->crud->model,
				'topics' => new CActiveDataProvider(GroupTopic::model(), array(
					'criteria' => array(
						'condition' => 'group_id = :group',
						'order' => 'updated_at DESC',
						'params' => array(':group' => $this->model->id),
					),
				)),
			));
		}
	}

	public function actionJoin() {
		if ($this->crud->model->hasMember($this->webUser->id))
			$this->redirect(array('index', 'id' => $this->crud->model->id));

		$participating = new GroupMember;
		$participating->group_id = $this->crud->model->id;
		$participating->user_id = $this->webUser->id;
		$participating->role = GroupMember::MEMBER;
		if (!$participating->save())
			throw new CHttpException(500, 'Can not join group');
		Yii::app()->user->setAlert(Yii::t('Groups', 'You joined group'), 'success');
		$this->redirect(array('index', 'id' => $this->crud->model->id));
	}

	public function actionLeave() {
		if (($model = GroupMember::model()->findByAttributes(array('user_id' => $this->webUser->id, 'group_id' => $this->crud->model->id))) == null)
			$this->redirect(array('index', 'id' => $this->crud->model->id));

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

		Yii::app()->user->setAlert(Yii::t('Groups', 'You leaved group'), 'success');
		$this->redirect(array('index', 'id' => $this->crud->model->id));
	}

	public function actionMembers() {
		$this->render('members', array(
			'model' => $this->crud->model,
			'members' => new CActiveDataProvider(GroupMember::model(), array(
				'criteria' => array(
					'with' => array('group'),
					'condition' => 'group_id = :group',
					'params' => array(':group' => $this->crud->model->id),
				),
			)),
		));
	}

	public function actionCreateTopic() {
		$topic = new GroupTopic('create');
		$topic->group_id = $this->model->id;
		$topic->initiator_id = $this->webUser->id;
		$post = new GroupTopicPost('create');
		$post->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()) {
					$transaction->commit();
					Yii::app()->user->setAlert(Yii::t('Groups', 'Topic created'), 'success');
					$this->redirect(array('topic/index', 'id' => $topic->id));
				}
			}
			$transaction->rollback();

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

	public function actionPurgeMember($user) {
		if (($user = User::model()->findByPk($user)) === null)
			throw new CHttpException(404, 'Invalid User!');
		if ($user->id == $this->webUser->id)
			$this->redirect(array('members', 'id' => $this->model->id));

		if (!$this->model->hasMember($user->id))
			$this->redirect(array('members', 'id' => $this->model->id));

		$member = GroupMember::model()->findByAttributes(array('group_id' => $this->model->id, 'user_id' => $user->id));

		if (Yii::app()->user->checkAccess('groups.member.purgeMember') || ($this->model->creator_id == $this->webUser->id || ($this->model->checkAccess(GroupMember::ADMINISTRATOR) && $member->role != GroupMember::ADMINISTRATOR))) {
			if ($member->delete()) {
				$user->addNotification(Yii::t('Groups', '{user} purged you from group {group}.', array('{user}' => CHtml::encode($this->webUser->name), '{group}' => CHtml::encode($this->model->name)), null, $user->language), UserNotification::DANGER);
					Yii::app()->user->setAlert(Yii::t('Groups', 'You\'ve purged user'), 'success');
			}
		}
		$this->redirect(array('members', 'id' => $this->model->id));
	}
}