<?php

class UserController extends Controller
{
	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return CMap::mergeArray(parent::filters(),array(
			'accessControl', // perform access control for CRUD operations
		));
	}
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}	

	/**
	 * Displays a particular model.
	 */
	public function actionView()
	{
		/*$model = $this->loadModel();
		$this->render('view',array(
			'model'=>$model,
		));*/
        if (Yii::app()->user->isGuest) {
            //redirect to login if guest
            $this->redirect(Yii::app()->controller->module->loginUrl);
        } else {
            if (Yii::app()->getModule('user')->isAdmin()) {
                $model = $this->loadModel();
                $this->render('view',array(
                    'model'=>$model,
                ));
            } else {
                throw new CHttpException(403, "You are not authorized to perform that.");
            }
        }
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
        if (Yii::app()->user->isGuest) {
            //redirect to login if guest
            $this->redirect(Yii::app()->controller->module->loginUrl);
        } else {
            if (Yii::app()->getModule('user')->isAdmin()) {
                $dataProvider=new CActiveDataProvider('User', array(
                    'criteria'=>array(
                        'condition'=>'status>'.User::STATUS_BANNED,
                    ),
                    'pagination'=>array(
                        'pageSize'=>Yii::app()->controller->module->user_page_size,
                    ),
                ));

                $this->redirect('/user/admin');
                /*$this->render('index',array(
                    'dataProvider'=>$dataProvider,
                ));*/
            } else {
                throw new CHttpException(403, "You are not authorized to perform that.");
            }
        }
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=User::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
	 */
	public function loadUser($id=null)
	{
		if($this->_model===null)
		{
			if($id!==null || isset($_GET['id']))
				$this->_model=User::model()->findbyPk($id!==null ? $id : $_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
}
