<?php
	
	class login_model extends CI_Model
	{
     function __construct()
     {
          // Call the Model constructor
          parent::__construct();
          $this->load->database();
     }

	// Read data using username and password
	public function login($data) {
	
		$username = $this->db->escape($data['username']);
	    $sec_pass = $this->db->escape($data['sec_pass']);
	    
		$condition = "(email=".$username.") AND secure_password = ".$sec_pass." and status_id='1' and (user_type_id='1' or user_type_id='4')";
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where($condition);
		$this->db->limit(1);
		$query = $this->db->get();
		
		$return_val = $query->result();
		//print_r($this->db->last_query());exit();
		foreach($return_val as $return_vals){
          //$root_id = $return_vals->root_id;
          $user_id = $return_vals->id;
		}
		
		if(isset($user_id))
		{
		$_SESSION['user_id'] = $user_id;
		}
	
		if ($query->num_rows() == 1) {
			return true;
		} else {
			return false;
		}
	}
	


	// Read data from database to show data in admin page
	public function read_user_information($username) {
		$condition = "username =" . "'" . $username . "' ";
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where($condition);
		$this->db->limit(1);
		$query = $this->db->get();
		
		if ($query->num_rows() == 1) {
			return $query->result();
		} else {
			return false;
		}
	}
	
	// Read data from database to show data in admin page
	public function read_user_information_login($username,$sec_pass) {
	    
		$condition = "(email='".$username."') AND secure_password = '".$sec_pass."'  ";
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where($condition);
		$this->db->limit(1);
		$query = $this->db->get();
		
		if ($query->num_rows() == 1) {
			return $query->result();
		} else {
			return false;
		}
	}
	
		public function read_user_info_byemail($email) {
		$condition = "email =" . "'" . $email . "'";
		$this->db->select('*');
		$this->db->from('users');
		$this->db->where($condition);
		$this->db->limit(1);
		return $query = $this->db->get();
		
		//return $query->num_rows();
	}

}
?>