<?php

class AccesoController extends CrController{

	public function actionIndex()
	{
		$this->renderPartial(array('login'));
	}

	public function actionLogin()
	{
		if (Yii::app()->user->isGuest) {
			$model=new UserLogin;

			if(isset($_POST['UserLogin']))
			{

				$model->attributes=$_POST['UserLogin'];
				// validate user input and redirect to previous page if valid
				if($model->validate()) {
					$this->lastViset();
					if (Yii::app()->user->getState('type')=="1"){
						$this->redirect(Yii::app()->controller->module->returnUrl);
					}
					if (Yii::app()->user->getState('type')=="2"){
						//$this->redirect('http://localhost/RIR/CyT/');
						//$this->redirect(Yii::app()->controller->module->returnUrl2);
						$this->redirect(Yii::app()->request->baseUrl.'/CyT/index.php/auth/login?username='.$model->username.'&password='.$model->password);
					}
				}
			}
			// display the login form
			$this->renderPartial('//site/login',array('model'=>$model));
		} else{

			if (Yii::app()->user->getState('type')=="1"){
				$this->redirect(Yii::app()->controller->module->returnUrl);
			}
			if (Yii::app()->user->getState('type')=="2"){
				//$this->redirect('http://localhost/RIR/CyT/');
				//$this->redirect(Yii::app()->controller->module->returnUrl2);
				//$this->redirect('http://localhost/RIR/CyT/index.php/auth/login?a=a&b=b&c=c');
				$this->redirect(Yii::app()->request->baseUrl.'/CyT/index.php/user/dashboard');
			}
		}
	}

	private function lastViset() {
		$lastVisit = User::model()->notsafe()->findByPk(Yii::app()->user->id);
		$lastVisit->lastvisit_at = date('Y-m-d H:i:s');
		$lastVisit->save();
	}

	public function actionLogout()
	{
		Yii::app()->user->logout();
		//$this->redirect(Yii::app()->homeUrl);
		$this->redirect('login');
	}


}


?>