<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}
class Build_menu {
	function __construct() {
		$this->CI = &get_instance();
	}
	function accept_data($value) {
		foreach ($value as $key => $val) {
			$data[$val] = $this->CI->input->post($val, TRUE);
			if (!is_array($data[$val])) {
				$data[$val] = strip_image_tags($data[$val]);
				$data[$val] = quotes_to_entities($data[$val]);
				$data[$val] = encode_php_tags($data[$val]);
				$data[$val] = trim($data[$val]);
			}
		}
		return $data;
	}
	function build($group_id, $smodul) {
		$q = "SELECT id AS menu_id, menu_name, menu_icons, module, parent_id
				  FROM tb_menu
				  WHERE active='on'
				  AND parent_id = '0'
				  ORDER BY ORDERING ASC";
		// die($kolok_skpd);
		$rs = $this->CI->db->query($q)->result();
		$html_out = '';
		foreach ($rs as $r) {
			$ltuh = $this->cekAkses($group_id, $r->menu_id);
			$lihat = 0;
			$tambah = 0;
			$ubah = 0;
			$hapus = 0;
			if ($ltuh != "") {
				$ltuh2 = json_decode($ltuh);
				// print_r($ltuh2->lihat);die;
				$lihat = $ltuh2->lihat;
				$tambah = $ltuh2->tambah;
				$ubah = $ltuh2->ubah;
				$hapus = $ltuh2->hapus;
			}
			$qSub = " SELECT A.id AS menu_id, A.menu_name, A.parent_id, A.module
					  FROM tb_menu A
					  WHERE active='on'
					  AND parent_id = '" . $r->menu_id . "'";
			$rSub = $this->CI->db->query($qSub)->result();
			if ($lihat == 1) {
				$class_aktif = '';
				if ($rSub) {
					$class_aktif = $this->cekAktif($group_id, $r->menu_id, $smodul);
					$html_out .= '<li class="' . $class_aktif . ' treeview">
									  <a href="#">
									    <i class="fa ' . $r->menu_icons . '"></i> <span> ' . $r->menu_name . '</span>
									    <span class="pull-right-container">
									      <i class="fa fa-angle-left pull-right"></i>
									    </span>
									  </a>';
					$html_out .= $this->getChilds($r->menu_id, $group_id, $smodul);
				} else {
					if ($r->module == $smodul) {
						$class_aktif = 'active';
					}
					$html_out .= '<li class="' . $class_aktif . '"><a href="' . site_url($r->module) . '"><i class="fa ' . $r->menu_icons . '"></i> ' . $r->menu_name . '</a>';
				}
				$html_out .= '</li>';
			}
		}
		echo $html_out;
	}
	function getChilds($menu_id, $group_id, $smodul) {
		$q = "SELECT id AS menu_id, menu_name, menu_icons, module, parent_id
					  FROM tb_menu
					  WHERE active='on'
					  AND parent_id = '$menu_id'";
		$rs = $this->CI->db->query($q)->result();
		$html_out = '';
		if ($rs) {
			$html_out .= '<ul class="treeview-menu">';
			foreach ($rs as $r) {
				$ltuh = $this->cekAkses($group_id, $r->menu_id);
				$lihat = 0;
				$tambah = 0;
				$ubah = 0;
				$hapus = 0;
				if ($ltuh != "") {
					$ltuh2 = json_decode($ltuh);
					// print_r($ltuh2->lihat);die;
					$lihat = $ltuh2->lihat;
					$tambah = $ltuh2->tambah;
					$ubah = $ltuh2->ubah;
					$hapus = $ltuh2->hapus;
				}
				if ($lihat == 1) {
					$qSub = "SELECT id AS menu_id, menu_name, parent_id, module
							  FROM tb_menu
							  WHERE active='on'
							  AND parent_id = '" . $r->menu_id . "'";
					$rSub = $this->CI->db->query($qSub)->result();
					$class_aktif = '';
					if ($rSub) {
						$class_aktif = $this->cekAktif($group_id, $r->menu_id, $smodul);
						$html_out .= '<li class="' . $class_aktif . ' treeview">
									  <a href="#">
									    <i class="fa ' . $r->menu_icons . '"></i> <span> ' . $r->menu_name . '</span>
									    <span class="pull-right-container">
									      <i class="fa fa-angle-left pull-right"></i>
									    </span>
									  </a>';
						$html_out .= $this->getChilds($r->menu_id, $group_id, $smodul);
					} else {
						if ($r->module == $smodul) {
							$class_aktif = 'active';
						}
						$html_out .= '<li class="' . $class_aktif . '"><a href="' . site_url($r->module) . '"><i class="fa ' . $r->menu_icons . '"></i> ' . $r->menu_name . '</a>';
					}
					$html_out .= '</li>';
				}
			}
			$html_out .= '</ul>';
		}
		return $html_out;
	}
	function cekAkses($group_id, $menu_id) {
		$q = "SELECT access_data
			  FROM tb_groups_access
			  WHERE group_id='$group_id'
			  AND menu_id = '$menu_id'";
		// echo $q;
		$rs = $this->CI->db->query($q)->result();
		if ($rs) {
			return $rs[0]->access_data;
		}
	}
	function cekAktif($group_id, $menu_id, $smodul) {
		$class_aktif = '';
		$qSub = "SELECT id AS menu_id, menu_name, parent_id, module
				  FROM tb_menu
				  WHERE active='on'
				  AND parent_id = '" . $menu_id . "'";
		$rsSub = $this->CI->db->query($qSub)->result();
		if ($rsSub) {
			foreach ($rsSub as $rsub) {
				if ($rsub->module == $smodul) {
					$class_aktif = 'active';
					break;
				} else {
					$class_aktif = $this->cekAktif($group_id, $rsub->menu_id, $smodul);
				}
			}
		}
		return $class_aktif;
	}
} 