<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class userguideTopic extends CI_Model{
	
	function menu(){
/*		if(!$this->session->userdata('logged_in')){
			$this->session->set_userdata('first_uri', uri_string());
			redirect('login');
		}
			$this->load->model('systemgeneral/systemgeneral_model');
			if ($this->systemgeneral_model->complete_setup_bycompanyID() && $this->uri->segment(2) != 'generalSetup' ) {
			 	redirect('systemgeneral/generalSetup', 'refresh');
			} */
		$this->lang->load("ug_topic",$this->session->userdata('language'));
		$this->load->library('menus');
		$this->load->model('userguideTopic_model');
//		$this->load->model('Useraccess');
		$controller = $this->uri->segment(1);
		$function = $this->uri->segment(2);
		$act = $controller;
		if($function != '' && $function != 'index'){ $act = $act . '/' . $function; };
		$active = $this->userGuideTopic_model->sysMenu_active($act);
//		echo $act;
		$sub_par = "";
		$sub_par2 = "";
		if($active){
			foreach($active as $aktif){
					$sub_par = $aktif->parents;
			}
				if($sub_par != 0){
					$active2 = $this->userGuideTopic_model->sysMenu_sub2($sub_par);
					foreach($active2 as $aktif2){
						$sub_par2 = $aktif2->parents;
					}
				}else{
					$sub2 = NULL;
				}
		}
		$result = $this->userGuideTopic_model->sysMenu_all();
		if($result)   {
			$menu_array = array();
			$parent = 0;
			$line = '';
			foreach($result as $row) {
				$close = '';
				if($act == $row->urls){
					$urlss = "active";
					$line = $close . '<li class="'. $urlss .'"><a href="'. base_url() . $row->urls . '" class="icon"><span class="'.$row->icons.'"></span>' . $this->lang->line($row->name) . '</a></li>';
					$this->menus->add_includes($line);
				} else {
					if($row->submenu == 1){
					  $query = $this->Useraccess->useraccess_id_module($row->module);
					  if($query){
						if($row->fldid == $sub_par || $row->fldid == $sub_par2) {
							$urlss = "openable open";
						} else {
							$urlss = "openable";
						}
						$line = '<li class="'. $urlss .'"><a href="'. base_url() . $row->urls . '"  class="icon"><span class="'.$row->icons.'"></span>' . $this->lang->line($row->name) . '</a><ul>';
						$this->menus->add_includes($line);
						$sub = $this->userGuideTopic_model->sysMenu_sub($row->fldid);
						foreach($sub as $row1) {
							if($act == $row1->urls){
								$urlss = "active";
								$line = $close . '<li class="'. $urlss .'"><a href="'. base_url() . $row1->urls . '" class="icon"><span class="'.$row1->icons.'"></span>' . $this->lang->line($row1->name) . '</a></li>';						
								$this->session->set_userdata('menuactive', $this->lang->line($row1->name));
								$this->menus->add_includes($line);							
							} else {
								if($row1->submenu == 1){
					  			  if($row1->module == '10' || $row1 == '9'){
								  	$query1 = $this->Useraccess->useraccess_id_module($row1->module);
								  }else{
								  	$query1 = $this->Useraccess->useraccess_id_module2($row1->module, $row1->name);
								  }
					  			  if($query1){
									if($row1->fldid == $sub_par || $row1->fldid == $sub_par2) {
										$urlss = "openable open";
									} else {
										$urlss = "openable";
									}
									$line = '<li class="'. $urlss .'"><a href="'. base_url() . $row1->urls . '" class="icon"><span class="'.$row1->icons.'"></span>' . $this->lang->line($row1->name) . '</a><ul>';
									$this->menus->add_includes($line);
									$sub2 = $this->userGuideTopic_model->sysMenu_sub($row1->fldid);
									foreach($sub2 as $row2) {
										if($act == $row2->urls){
											$urlss = "active";
											$line = $close . '<li class="'. $urlss .'"><a href="'. base_url() . $row2->urls . '" class="icon"><span class="'.$row2->icons.'"></span>' . $this->lang->line($row2->name) . '</a></li>';						
											$this->session->set_userdata('menuactive', $this->lang->line($row2->name));
											$this->menus->add_includes($line);							
										} else {
											$urlss = '';
											$line = '<li class="'. $urlss .'"><a href="'. base_url() . $row2->urls . '" class="icon"><span class="'.$row2->icons.'"></span>' . $this->lang->line($row2->name) . '</a></li>';
											$this->menus->add_includes($line);
										}
									}
									$line = '</ul>';
									$this->menus->add_includes($line);
								  }
								}else{
									$urlss = '';
									$line = '<li class="'. $urlss .'"><a href="'. base_url() . $row1->urls . '" class="icon"><span class="'.$row1->icons.'"></span>' . $this->lang->line($row1->name) . '</a></li>';
									$this->menus->add_includes($line);
								}
							}
						}
						$line = '</ul>';
						$this->menus->add_includes($line);							
					  }
					} else {						
						$urlss = '';
						$line = '<li class="'. $urlss .'"><a href="'. base_url() . $row->urls . '" class="icon"><span class="'.$row->icons.'"></span>' . $this->lang->line($row->name) . '</a></li>';
						$this->menus->add_includes($line);
					}
				}
			}
		}
	}
	public function menu_access_all(){
		$this->lang->load("menu",$this->session->userdata('language'));
		$module = $this->input->post('module');
		$this->db->select('*');
		$this->db->where('ID', $this->input->post('module'));
		$this->db->from('module');
		$query = $this->db->get();
		$menuCode = $query->result();
		$this->db->flush_cache();
		switch ($this->input->post('detailsAcccess')){
			case '1':
				if($module == 9 OR $module == 10){
					$code = $menuCode[0]->moduleName;
				}else{
					$code = $menuCode[0]->moduleName . 'TRANS';
				}
			break;
			case '2':
				if($module == 9 OR $module == 10){
					$code = $menuCode[0]->moduleName;
				}else{
					$code = $menuCode[0]->moduleName . 'RPT';
				}
			break;
			case '3':
				if($module == 9 OR $module == 10){
					$code = $menuCode[0]->moduleName;
				}else{
					$code = $menuCode[0]->moduleName . 'SETUP';
				}
			break;
		}
		$this->db->select('a.code as acode');
		$this->db->select('b.*');
		$this->db->select('c.accessRight');
		$this->db->where('a.code', $code);
		$this->db->from('_sys_menu a');
		$this->db->join('_sys_menu b', 'b.parents = a.fldid', 'left outer');
		$this->db->join('user_module_access_details c', 'c.menuCode = b.code AND c.userID = ' . $this->input->post('fldid') . ' AND c.companyID = ' . $this->input->post('companyID'), 'left outer');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			$test = $query->result();
			$resultDetails = array();
			foreach($test as $row){
				$rowDet = array();
				$rowDet['fldid'] = $row->fldid;
				$rowDet['code'] = $row->code;
				$rowDet['name'] = $this->lang->line($row->name);
				$rowDet['accessRight'] = $row->accessRight;
				$rowDet['type'] = $this->input->post('detailsAcccess');
				$resultDetails[] = $rowDet;
			}
			return $resultDetails;
		}else{
			return FALSE;
		}
	}
}
/* End of file Menu.php */
/* Location: ./application/models/Menu.php */
/* Created By : Yahaya Abdollah */
