<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Home Class
 * 
 * @author dzungnv02
 *
 */

class Home extends MX_Controller 
{
	const TOKENPW = 'violet';
	
	public function __construct() 
	{
		parent::__construct();
		$this->load->helper('cookie');
	}

	public function index() 
	{
		$user_info = $this->session->userdata('userInfo');
		if ($user_info)
		{
			redirect('/frontend/lecture');
		}else{
			$cookie_name = "siteCookie";
			if(isset($_COOKIE[$cookie_name])){
				$arr_user = parse_str($_COOKIE[$cookie_name]);
				$user_info = array( 'username' => $arr_user['username'], "logined_in" => TRUE);
				$this->session->set_userdata('userInfo', $user_info);
				redirect('/frontend/lecture');
			}else{
				$this->load->view ( 'home');
			}
		}
	}
	
	public function signin() 
	{
		$username = $this->input->post('username', TRUE);
		$password = $this->input->post('password', TRUE);
		$autologin = ($this->input->post('remember_me') == 'on') ? 1 : 0;
		$token = md5($username.self::TOKENPW);
		
		$this->load->model(array('user_model'));
		$data = $this->user_model->checkLogin('violet', $token, $username, $password);
		
		if ($data == null)
		{
			$this->session->set_flashdata('login_error', TRUE);
			redirect('/frontend/home');
		}else{
			$arr_users = explode("&", $data);
			$str_status = "";
			$str_fullname = "";
			$fullname = "";
			for ($i=0; $i<count($arr_users); $i++)
			{
				if (strpos($arr_users[$i], 'status=') !== false)
				{
					$str_status = $arr_users[$i];
				}
				if (strpos($arr_users[$i], 'fullname=') !== false)
				{
					$str_fullname = $arr_users[$i];
				}
			}
			$arr_status = explode("=", $str_status);
			if ($str_fullname != ""){
				$arr_fullname = explode("=", $str_fullname);
				$fullname = $arr_fullname[1];
			}
			$status = (int)$arr_status[1];
			switch($status)
			{
				case 0:		
					if($autologin == 1){
						$cookie_name = 'siteCookie';
						//$cookie_time = 7200;
						$cookie_time = 3600*24*30;
						setcookie($cookie_name, 'username='.$username, time() + $cookie_time);
					}
					$userdata = array('username' => $username, "logined_in" => TRUE);
					$this->session->set_userdata('userInfo', $userdata);
					redirect('/frontend/lecture');
					break;
				case 1:
					$this->session->set_flashdata('login_error', TRUE);
					redirect('/frontend/home');
					break;
				case 2:
				case 3:
					$this->session->set_flashdata('login_error', TRUE);
					redirect('/frontend/home');
					break;
				case 4:
					redirect('/frontend/home');
					break;
				case 10:
					$this->session->set_flashdata('login_error_token', TRUE);
					redirect('/frontend/home');
					break;
				default:
					break;
			}
		}
	}
	
	public function signout() 
	{
		$this->session->sess_destroy();
		//unset($this->session->userdata); 
		$cookie_name = "siteCookie";
		setcookie($cookie_name, 'username='."", time() - 3600);
		redirect('/frontend/home');
	}

}

/* End of file home.php */
/* Location: ./application/modules/frontend/controllers/home.php */