View file vkclone-0.0.1/protected/components/FrontEndController.php

File size: 3.26Kb
<?php
/**
 * This is a base class for usual fronend-controllers
 */
class FrontEndController extends Controller {
	/**
	 * Default site frontend layout
	 */
	public $layout = 'site.views.layouts.main';
	public $baseLayout = 'site.views.layouts.mobile';
	/**
	 * Contains number of unread messages
	 */
	public $unreadMessages;
	/**
	 * Contains number of unread notifications
	 */
	public $unreadNotifications;
	/**
	 * Contains number of new friendship requests
	 */
	public $newFriendshipRequests;

	/**
	 * Filters logged users
	 */
	public function filters() {
		return CMap::mergeArray(parent::filters(), array(
			'RedirectToClosedPageIfSiteClosed',
			'LogoutDeletedUsers',
			'LogoutUsersIfAuthenticationClosed',
			'accessControl',
			'CheckUserNameIsNotEmpty',
			'UserInitialization',
			'UseWebLayoutIfComputerUser',
		));
	}

	/**
	 * Access control list.
	 * Denies access for guests.
	 */
	public function accessRules() {
		return array(
			array('deny'),
		);
	}

	/**
	 * Checks that username is filled
	 */
	public function filterCheckUserNameIsNotEmpty($filterChain) {
		// non-guest & first_name or last_name empty
		if (!Yii::app()->user->isGuest && (empty($this->webUser->first_name) || empty($this->webUser->last_name)) && strtolower($this->uniqueRoute) != 'users/default/fillname')
				$this->redirect(array('/users/default/fillName'));
		$filterChain->run();
	}

	/**
	 * Checks if logged user didn't save his name
	 */
	public function filterUserInitialization($filterChain) {
		if (!Yii::app()->user->isGuest) {
			$this->webUser->updateVisitTime();

			$this->unreadMessages = UserMessage::model()->unreadMessages($this->webUser->id)->count();
			$this->unreadNotifications = UserNotification::model()->count('`user_id` = :user_id AND `new` = "1"', array(':user_id' => $this->webUser->id));
			$this->newFriendshipRequests = UserFriend::model()->count('friend_id = :user AND approved = "0"', array(':user' => $this->webUser->id));
		}
		$filterChain->run();
	}

	/**
	 * Logouts deleted users
	 */
	public function filterLogoutDeletedUsers($filterChain) {
		// non-guest & record deleted
		if (!Yii::app()->user->isGuest && Yii::app()->user->record === null)
				Yii::app()->user->logout();
		$filterChain->run();
	}

	/**
	 * Logouts unnecessary users
	 */
	public function filterLogoutUsersIfAuthenticationClosed($filterChain) {
		// non-guest & authentication closed & non-privileged user
		if (!Yii::app()->user->isGuest && Settings::instance()->authenticationClosed && !Yii::app()->user->checkAccess('administrator'))
				Yii::app()->user->logout();
		$filterChain->run();
	}

	/**
	 * Closes access to site
	 */
	public function filterRedirectToClosedPageIfSiteClosed($filterChain) {
		$whiteList = array(
			'site/default/error',
			'site/default/closed',
			'admin/default/login',
		);
		// site closed & non-privileged user & non-allowed url
		if (Settings::instance()->siteClosed && !Yii::app()->user->checkAccess('administrator') && !in_array($this->uniqueRoute, $whiteList)) {
			$this->redirect(array('site/default/closed'));
		}
		$filterChain->run();
	}

	public function filterUseWebLayoutIfComputerUser($filterChain) {
		$theme = Yii::app()->user->getPreferredTheme();
		if (in_array($theme, array('mobile', 'web')))
			$this->baseLayout = 'site.views.layouts.'.$theme;
		$filterChain->run();
	}
}