<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Auth extends Controller /*_Website*/ {

	public function action_index()
	{
		$this->auto_render = FALSE;
		
		#If user already signed-in
		if(Auth::instance()->logged_in()!= 0){
			$this->request->response = View::factory("loggedin");	
		}
		//$this->request->response = View::factory("login");
		echo View::factory("login");
	}
	public function action_login()
	{
		#If there is a post and $_POST is not empty
		if ($_POST)
		{
			#Instantiate a new user
			$user = ORM::factory('user');
 
			#Check Auth
			$status = $user->login($_POST);
 
			#If the post data validates using the rules setup in the user model
			if (!$status)
				session::instance()->set("errors",$_POST->errors('login'));
			Request::instance()->redirect("");
			//gen::re_back();
		}
		else{
			//$this->action_index();
			Request::instance()->redirect("auth");
		}
		//$this->request->response = View::factory("login");
	}

	function action_register()
	{
	        if(Auth::instance()->get_user()){
			Request::instance()->redirect("main/act");
		}
            $universities = ORM::factory("university")->find_all();
            $universities_arr = array();
            foreach($universities as $university)
            {
                $universities_arr [$university->id] = $university->univname;
            }
            $departments = ORM::factory("department")->find_all();
            $departments_arr = array();
            foreach($departments as $department)
            {
                $departments_arr [$department->id] = $department->departname;
            }		
		#If user already signed-in
		if(Auth::instance()->logged_in()!= 0){
			#redirect to the user account
			
			//gen::re_back();
			//gen::re_back();
		Request::instance()->redirect('');
		}

		#Load the view
		//$content = $this->request->response = View::factory('register');
 
		#If there is a post and $_POST is not empty
		if ($_POST)
		{
			#Instantiate a new user
			$user = ORM::factory('user');
			#Load the validation rules, filters etc...
			$post = $user->validate_create($_POST);
			$user->university_id = $_POST["university"];
			$user->department_id = $_POST["department"];
			//throw new Exception($_POST["university"]);
			#If the post data validates using the rules setup in the user model
			if ($post->check())
			{
				#Affects the sanitized vars to the user object
				$user->values($post);
				#create the account
				$user->save();
 
				#Add the login role to the user
				$login_role = new Model_Role(array('name' =>'login'));
				$user->add('roles',$login_role);
 
				#sign the user in
				Auth::instance()->login($post['username'], $post['password']);

				#redirect to the user account
				gen::re_back();
			}
			else
			{
                #Get errors for display in view
				$content->errors = $post->errors('register');
			}
		}
		$data = array( "universities_arr" =>$universities_arr,"departments_arr"=>$departments_arr);
										    
		$this->request->response = View::factory('register', $data);
		//$this->request->response = View::factory("register",);
	}
	
	public function action_logout()
	{
		if(Auth::instance()->logged_in()!= 0){
			#Sign out the user
			Auth::instance()->logout();
		}
 
		#redirect to the user account and then the signin page if logout worked as expected
		Request::instance()->redirect(''); //changed
		//gen::re_back();
	}
}
