<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//the model will mainly handle general data like menu, pages etc
class Data_handler extends CI_Model {

    function __construct()
    {
       parent::__construct();
    }
	
	 private function get_relevant_parent_id($user_type)
	{
		//SELECT parent_id FROM menu_permissions Where user_type=$user_type and visibility_status=1;
		$this->db->select('parent_id');
		$this->db->where('user_type',$user_type);
		$this->db->where('visibility_status',1);
		$this->db->from('menu_permissions');
		$query = $this->db->get();
		
		
		$parent_id = $query->result_array();
		
		return $parent_id;
			
	}

	function get_menu($user_type)
	{
		//select * from menu where id={$parent_id} order by position.
		$parent_id = $this->get_relevant_parent_id($user_type);
		$parentIDs ='';
		$countAllItems = count($parent_id);
		for($i=0;$i<$countAllItems;$i++)
		{
			if($i == $countAllItems-1)
			{
				$parentIDs .= $parent_id[$i]['parent_id'];
			}
			else
			{
				$parentIDs .= $parent_id[$i]['parent_id'].',';
			}
		}
		$this->db->select('*');
		$this->db->from('menu');
		//$this->db->where('id',$parent_id[$i]['parent_id']);
		$this->db->where('id IN('.$parentIDs.')');
		$this->db->order_by('position','asc');
		$query=$this->db->get();
		if($query->num_rows()>0)
		{
			return $query->result_array();
		}
		else return NULL;

	}
	
	//function to fetch the allowed submenu items
 	private function get_relevant_submenu($parent_id,$user_type)
	{
		//$query = "SELECT submenu_id FROM menu_permissions WHERE user_type =$user_type";
		
		$this->db->select('submenu_id');
		$this->db->where('parent_id',$parent_id);
		$this->db->where('user_type',$user_type);
		$this->db->from('menu_permissions');
		$query = $this->db->get();
		
		$submenu_array = $query->result_array();
		return $submenu_array;
	}
	
	//select * from sub_menu where parent_id={$parent_id} order by position
	function get_submenu($parent_id,$user_type)
	{
		$submenu_array = $this->get_relevant_submenu($parent_id,$user_type);
		
		$this->db->select('*');
		$this->db->from('sub_menu');
		//$this->db->where_in('id',$submenu_array[0]);
		$this->db->where('parent_id',$parent_id);
		$this->db->where('id IN('.$submenu_array[0]['submenu_id'].')');
		$this->db->order_by('position');
		
		$query=$this->db->get();
		//echo $this->db->last_query();		
//		exit;
		if($query->num_rows()>0)
		{
			return $query->result_array();

		}
		else return NULL;
		
	}

}
?>