<?php

class HmsAdmin_Model extends CI_Model
{
	/*############################################ START Of Coding By Nandini ############################################*/
	public function __construct()
	{
		$this->load->database();
	}
	
	/**************************************** START of Admin Roles - Nandini ************************************/
	public function get_hms_admin_roles($where='', $sqlQry='', $limit=FALSE, $offset=FALSE)		
	{			
		if($limit)			
			$this->db->limit($limit, $offset);
		
		if($where!='')
			$this->db->where($where);
		
		$this->db->order_by('hms_admin_roles.id', 'DESC');						
		$query = $this->db->get('hms_admin_roles');

		if($sqlQry!='')
			$query = $this->db->query($sqlQry);
		
		return $query->result_array();
	}
	
	public function check_role_exists($id, $rname)
	{
		$rId = ($this->input->post('id') !== NULL) ? $this->input->post('id'): $id;
		$rName = ($this->input->post('role_name') !== NULL) ? $this->input->post('role_name'): $rname;
		
		$query = $this->db->get_where('hms_admin_roles', array('role_name' => $rName));		
		if(!empty($query->row_array()))
		{
			if($rId == 0)
				return false;											//Duplicate role_name
			else
			{
				if($query->row(0)->id != $rId)
					return false;										//Duplicate role_name			
			}
		}
		return true;													//No duplicate
	}
	
	function add_edit_admin_role($rId=0, $data)
	{
		if($rId == 0)
		{
			$this->db->insert('hms_admin_roles', $data);
			if($this->db->affected_rows() > 0){
				$insertid = $this->db->insert_id();
				return  $insertid;
			}
			else{
				return 0;
			}
		}
		else
		{
			$this->db->where('id', $rId);
			$this->db->update('hms_admin_roles', $data);
			if($this->db->affected_rows() > 0)
				return true;
			else
			{
				if ($this->db->trans_status() === FALSE)
					return 0;
				
				return true;				
			}
		}
	}
	
	public function delete_admin_role($idVal)
	{
		$delSqlQry = "DELETE FROM hms_admin_roles WHERE id = $idVal";
		$this->db->query($delSqlQry);
		return true;
	}
	/**************************************** ENDDD of Admin Roles - Nandini ************************************/
	
	
	/**************************************** START of Admin Menu - Nandini ************************************/
	public function get_admin_menu_details($where='', $sqlQry='', $limit=FALSE, $offset=FALSE)		
	{
		if ($limit){				
			$this->db->limit($limit, $offset);
		}
		if($where!='')
			$this->db->where($where);
		
		$this->db->order_by('hms_admin_menu.id', 'DESC');						
		$query = $this->db->get('hms_admin_menu');

		if($sqlQry!='')
			$query = $this->db->query($sqlQry);
		
		return $query->result_array();
	}
	
	function recursive_menue($parentId = 0)
	{
		$this->db->where('parent_id', $parentId);
		//$this->db->where('is_active', 'Y');
		$this->db->order_by('order_number', 'ASC');
		$query = $this->db->get('hms_admin_menu');				
		$result = $query->result_array();
		
		$adminMenu = array();
		if(!empty($result))
		{
			foreach($result as $res)
			{
				array_push($adminMenu, $res);					
			}
		}
		return $adminMenu;			
	}
	
	function get_admin_menu($parentId = 0)
	{
		$zeroMenu = $this->recursive_menue(0);
		$adminMenu = array();
		if(!empty($zeroMenu))
		{
			foreach($zeroMenu as $zero)
			{
				$adminMenu[$zero['id']] = $zero;

				$oneMenu = $this->recursive_menue($zero['id']);
				if(!empty($oneMenu))
				{
					foreach($oneMenu as $one)
					{
						$adminMenu[$zero['id']][$zero['id']][$one['id']] = $one;							
						
						$twoMenu = $this->recursive_menue($one['id']);
						if(!empty($twoMenu))
						{
							foreach($twoMenu as $two)
							{
								$adminMenu[$zero['id']][$zero['id']][$one['id']][$one['id']][$two['id']] = $two;							
							}						
						}
					}
				}					
			}
		}			
		//echo '<pre>';print_r($adminMenu);
		return $adminMenu;
	}
	
	public function get_hms_admin_role_permissions($where='', $sqlQry='', $limit=FALSE, $offset=FALSE)		
	{			
		if($limit)			
			$this->db->limit($limit, $offset);
		
		if($where!='')
			$this->db->where($where);
		
		$this->db->order_by('hms_admin_role_permissions.id', 'DESC');						
		$query = $this->db->get('hms_admin_role_permissions');

		if($sqlQry!='')
			$query = $this->db->query($sqlQry);
		
		return $query->result_array();
	}
	
	function add_edit_admin_role_permissions($rpId=0, $data)
	{
		if($rpId == 0)
		{
			$this->db->insert('hms_admin_role_permissions', $data);
			if($this->db->affected_rows() > 0){
				$insertid = $this->db->insert_id();
				return  $insertid;
			}
			else{
				return 0;
			}
		}
		else
		{
			$this->db->where('id', $rpId);
			$this->db->update('hms_admin_role_permissions', $data);
			if($this->db->affected_rows() > 0)
				return true;
			else
			{
				if ($this->db->trans_status() === FALSE)
					return 0;
				
				return true;				
			}
		}
	}
	/**************************************** END of Admin Menu - Nandini ************************************/	
	
	public function check_emailid_exists($id, $email)
	{
		$uId = ($this->input->post('id') !== NULL) ? $this->input->post('id'): $id;
		$eId = ($this->input->post('email') !== NULL) ? $this->input->post('email'): $email;
		
		$query = $this->db->get_where('hms_admin', array('email' => $eId));		
		if(!empty($query->row_array()))
		{
			if($uId == 0)
				return false;											//Duplicate email id
			else
			{
				if($query->row(0)->id != $uId)
					return false;										//Duplicate email id				
			}
		}
		return true;													//No duplicate
	}
	
	function add_edit_admin_user($uId=0, $data)
	{
		if($uId == 0)
		{
			$this->db->insert('hms_admin', $data);
			if($this->db->affected_rows() > 0){
				$insertid = $this->db->insert_id();
				return  $insertid;
			}
			else{
				return 0;
			}
		}
		else
		{
			$this->db->where('id', $uId);
			$this->db->update('hms_admin', $data);
			if($this->db->affected_rows() > 0)
				return true;
			else
			{
				if ($this->db->trans_status() === FALSE)
					return 0;
				
				return true;				
			}
		}
	}
	
	public function delete_admin_user($idVal)
	{
		$delSqlQry = "DELETE FROM hms_admin WHERE id = $idVal";
		$this->db->query($delSqlQry);
		return true;
	}
	
	public function get_admin_details($where='', $sqlQry='', $limit=FALSE, $offset=FALSE)		
	{
		if ($limit){				
			$this->db->limit($limit, $offset);
		}
		if($where!='')
			$this->db->where($where);
		
		$this->db->order_by('hms_admin.id', 'DESC');						
		$query = $this->db->get('hms_admin');

		if($sqlQry!='')
			$query = $this->db->query($sqlQry);
		
		return $query->result_array();
	}
	
	public function check_is_emailid_valid($email)
	{
		$query = $this->db->query("SELECT id FROM hms_admin WHERE email='$email'");    
		if($row = $query->row()){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	/*----------------------------- START of Admin User Login Functionality -----------------------------*/	
	public function check_login_credentials($email, $encrypt_password)
	{		
		$where = 'email="'.$email.'" AND password="'.$encrypt_password.'"';
		$this->db->where($where);
		$result = $this->db->get('hms_admin');

		if ($result->num_rows() == 1) {
			return true;
		}else{
			return false;
		}
	}
	/*----------------------------- ENDDD of Admin User Login Functionality -----------------------------*/	
	
	/*----------------------------- START of Admin User Password Functionality -----------------------------*/	
	public function reset_admin_password($uId, $new_password)
	{
		$data = array(
			'password' => md5($new_password)
		);
		$this->db->where('id', $uId);
		return $this->db->update('hms_admin', $data);
	}
	/*----------------------------- ENDDD of Admin User Password Functionality -----------------------------*/
	/*############################################ ENDDD Of Coding By Nandini ############################################*/

	// ================== Blocks Start - Afras khan - 02-12-2021 ==================
	
	function add_edit_table($table,$id, $data, $update_where = '')
	{
		if ($id == 0) {
			$this->db->insert($table, $data);
			if ($this->db->affected_rows() > 0)
				return  $insert_id = $this->db->insert_id();
			else
				return 0;
		} else {

			if($update_where != '')
				$this->db->where($update_where);
			else
				$this->db->where('id', $id);

			$this->db->update($table, $data);
			if ($this->db->trans_status() === FALSE)
				return 0;

			return true;
		}
	}

	function update_block_floors($block_id, $data) {

		$this->db->trans_start();
		$this->db->where('block_id', $block_id);
		$this->db->update_batch('hms_floors', $data,'id');
		$this->db->trans_complete();

		if ($this->db->trans_status() === FALSE)
			return  0;
		else
			return  true;
	}

	function insert_batch_data($table, $data) {
		$this->db->trans_start();
		$this->db->insert_batch($table, $data);
		$this->db->trans_complete();

		if ($this->db->trans_status() === FALSE)
			return  0;
		else
			return  true;

	}

	function insert_data($table, $data) {
		$this->db->trans_start();
		$this->db->insert($table, $data);
		$this->db->trans_complete();

		if ($this->db->trans_status() === FALSE)
			return  0;
		else
			return  true;

	}

	public function get_table_info($table_name, $where = '', $sqlQry = '', $order = 'DESC', $limit = FALSE, $offset = FALSE)
	{

		if ($limit)
			$this->db->limit($limit, $offset);

		if ($where != '')
			$this->db->where($where);

		$this->db->order_by($table_name . '.id', $order);
		$query = $this->db->get($table_name);

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->result_array();
	}

	public function get_table_info_row($table_name, $where = '', $sqlQry = '', $order = 'DESC', $limit = FALSE, $offset = FALSE)
	{

		if ($limit)
			$this->db->limit($limit, $offset);

		if ($where != '')
			$this->db->where($where);

		$this->db->order_by($table_name . '.id', $order);
		$query = $this->db->get($table_name);

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->row_array();
	}

	public function deleteTable($table, $id='', $where='')
	{
		if($where != '')
			$this->db->delete($table, $where);
		else
			$this->db->delete($table, array('id' => $id));


		if ($this->db->affected_rows()) {
			return true;
		} else {
			return false;
		}
	}

	public function check_value_exists($table, $where, $id)
	{

		$query = $this->db->get_where($table, $where);

		if (!empty($query->row_array())) {
			if ($id == 0)
				return false;
			else {
				if ($query->row(0)->id != $id)
					return false;
			}
		}
		return 1;
	}

	// ================== Blocks End - Afras khan - 02-12-2021 ==================

	// ================== Floors - Afras khan - 03-12-2021 ==================

	function update_floor_rooms($floor_id, $data) {

		$this->db->trans_start();
		$this->db->where('room_floor_id', $floor_id);
		$this->db->update_batch('hms_rooms', $data,'id');
		$this->db->trans_complete();

		if ($this->db->trans_status() === FALSE)
			return  0;
		else
			return  true;
	}

	// ================== Floors End - Afras khan - 03-12-2021 ==================
}

?>