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

Размер файла: 5.03Kb
<?php
class FriendsController extends FrontEndController {
	public function accessRules() {
		return CMap::mergeArray(array(
			array('allow',
				'actions' => array('index', 'requests', 'addToFriends', 'deleteFromFriends', 'userFriends'),
				'users' => array('@'),
			),
		), parent::accessRules());
	}

	/**
	 * Resolves user
	 * @param integer|null $user User id
	 * @return User User model
	 */
	private function resolveUser($user) {
		if ($user === null || $user == Yii::app()->user->record->id) {
			$user = Yii::app()->user->record;
		} else {
			if (($user = User::model()->findByPk($user)) === null)
				throw new CHttpException(404, 'Invalid User!');
		}
		return $user;
	}

	/**
	 * Shows main friends page
	 */
	public function actionIndex() {
		$this->render('index', array(
			'user' => $this->webUser,
			'friends' => new CActiveDataProvider(UserFriend::model(), array(
				'criteria' => array(
					'scopes' => array(
						'allUserFriends' => $this->webUser->id,
					),
				),
			)),
			'incomingRequestsCount' => UserFriend::model()->count('friend_id = :user AND approved = "0"', array(':user' => $this->webUser->id)),
			'outgoingRequestsCount' => UserFriend::model()->count('user_id = :user AND approved = "0"', array(':user' => $this->webUser->id)),
		));
	}

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

		$this->render('userFriends', array(
			'user' => $user,
			'friends' => new CActiveDataProvider(UserFriend::model(), array(
				'criteria' => array(
					'scopes' => array(
						'allUserFriends' => $user->id,
					),
				),
			)),
		));
	}

	/**
	 * List of requests
	 */
	public function actionRequests($type) {
		if (!in_array($type, array('incoming', 'outgoing')))
			$this->redirect(array('index'));

		$this->render('requests', array(
			'user' => $this->webUser,
			'type' => $type,
			'requests' => new CActiveDataProvider(UserFriend::model(), array(
				'criteria' => array(
					'condition' => ($type == 'outgoing' ? 'user_id' : 'friend_id').' = :user AND approved = "0"',
					'params' => array(':user' => $this->webUser->id),
				),
			)),
		));
	}

	/**
	 * Adds user to friends
	 */
	public function actionAddToFriends($user) {
		if (($user = User::model()->findByPk($user)) === null || $this->webUser->id == $user->id)
			throw new CHttpException(404, 'Invalid User!');

		$status = UserFriend::model()->byUsers($this->webUser->id, $user->id)->find();
		if ($status !== null && $status->approved == 1)
			$this->redirect(array('/users/user/index', 'id' => $user->id));

		// send request
		if ($status === null) {
			$status = new UserFriend('add');
			$status->user_id = $this->webUser->id;
			$status->friend_id = $user->id;

			if (isset($_POST[CHtml::modelName($status)])) {
				$status->attributes = $_POST[CHtml::modelName($status)];
				if ($status->validate() && $status->save()) {
					// add notification to user
					$user->addNotification(Yii::t('Users', '{user} sent you a friendship request.', array('{user}' => $this->webUser->name), null, $user->language), UserNotification::INFO);
					Yii::app()->user->setAlert(Yii::t('Users', 'You\'ve sent a friendship request'), 'success');
					$this->redirect(array('/users/user/index', 'id' => $user->id));
				}
			}

			$this->render('addToFriends', array(
				'user' => $user,
				'model' => $status,
			));
		} else { // accept request
			$status->approved = "1";
			if ($status->save()) {
				// add notification to user
				$user->addNotification(Yii::t('Users', '{user} accepted your friendship request.', array('{user}' => $this->webUser->name), null, $user->language), UserNotification::INFO);
				Yii::app()->user->setAlert(Yii::t('Users', 'You\'ve accepted a friendship request'), 'success');
				$this->redirect(array('/users/user/index', 'id' => $user->id));
			}
		}
	}

	/**
	 * Delete user from friends
	 */
	public function actionDeleteFromFriends($user) {
		if (($user = User::model()->findByPk($user)) === null || Yii::app()->user->record->equals($user))
			throw new CHttpException(404, 'Invalid User!');

		$status = UserFriend::model()->byUsers($this->webUser->id, $user->id)->find();
		if ($status === null)
			$this->redirect(array('/users/user/index', 'id' => $user->id));

		if ($status->delete()) {
			if ($status->approved == 1) { // delete previously accepted friendship
				$user->addNotification(Yii::t('Users', '{user} deleted you from friends.', array('{user}' => $this->webUser->name), null, $user->language), UserNotification::DANGER);
				Yii::app()->user->setAlert(Yii::t('Users', 'You\'ve deleted user from friends'), 'success');
			} else { // refuse friendship request
				$user->addNotification(Yii::t('Users', '{user} refused your friendship request.', array('{user}' => $this->webUser->name), null, $user->language), UserNotification::DANGER);
				Yii::app()->user->setAlert(Yii::t('Users', 'You\'ve refused a friendship request'), 'success');
			}
			$this->redirect(array('/users/user/index', 'id' => $user->id));
		}
	}
}