<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function menu_list( $id = null)
{
	$ci =& get_instance();
	$ci->db->join('t_role_module b','b.f_module_id=a.f_module_id');
	$menu = $ci->db->get_where('t_module a',array('a.f_module_active'=>1,'b.f_role_id'=>$id));

	$result = array();
	$result1 = array();
	$result2 = array();
	if($menu->num_rows()>0)
	{
		foreach($menu->result_array() as $k => $v):
			$result1[$v['f_module_level']][$v['f_module_parent']][$v['f_module_id']] = array(
				'code' => $v['f_module_class'],'name' => $v['f_module_name'],
				'icon' => $v['f_module_icon'],'parent' => $v['f_module_parent'],
				'urut' => $v['f_module_urut'],
			);
		endforeach;

		foreach($menu->result_array() as $k => $v):
			$result2[$v['f_module_level']][$v['f_module_parent']][] = $v['f_module_class'];
		endforeach;
		
		$result['r1'] = $result1;
		$result['r2'] = $result2;
	}
	
	//echo "<pre>";echo print_r($result1)."</pre><br/>";
	$html = "";
	foreach(array_sort($result1[0][0],'urut',SORT_ASC) as $a => $b):
		$href = ($b['code'] != '') ? site_url($b['code']): "javascript:void(0)";
		$html .="<li class=\"treeview ".menu($result2[1][$a],1)."\"><a href=\"".$href."\"><i class=\"".$b['icon']."\"></i><span>".$b['name']."</span>";
		// Cek level 1
		if(is_array($result1[1][$a]) == 1)
		{
			$html .="<i class=\"fa fa-angle-left pull-right\"></i></a><ul class=\"treeview-menu\">";
			foreach(array_sort($result1[1][$a],'urut',SORT_ASC) as $c => $d):
				$href1 = ($d['code']) ? site_url($d['code']): "javascript:void(0)";
				if(is_array($result1[2][$c]) == 1)
				{
					$active_in = menu($result2[1][$c],1);
				}
				else
				{
					$uri2 = $ci->uri->segment(1).'/'.$ci->uri->segment(2);
					$cari = strpos($d['code'],'/');
					if($cari)
					{
						if($uri2 == $d['code'])	 {	$active_in = 'active';	}
						else $active_in = '';
					}
					else
					{
						if($ci->uri->segment(1) == $d['code'])	 {	$active_in = 'active';	}
						else $active_in = '';
					}
					
				}
				
				$html .="<li class=\"treeview ".$active_in."\"><a href=\"".$href1."\"><i class=\"".$d['icon']."\"></i><span>".$d['name']."</span>";
				// Cek Level 2
				if(is_array($result1[2][$c]) == 1)
				{
					$html .="<i class=\"fa fa-angle-left pull-right\"></i></a><ul class=\"treeview-menu\">";
					foreach(array_sort($result1[2][$c],'urut',SORT_ASC) as $e => $f):
						$href2 = ($f['code']) ? site_url($f['code']): "javascript:void(0)";
						if(is_array($result1[3][$c]) == 1)
						{
							$active_in1 = menu($result2[1][$e],1);
						}
						else
						{
							if($ci->uri->segment(1) == $f['code']) $active_in1 = 'active';
							else $active_in1 = '';
						}
						
						$html .="<li class=\"treeview ".$active_in1."\"><a href=\"".$href2."\"><i class=\"".$f['icon']."\"></i><span>".$f['name']."</span>";
						// Cek Level 3
						if(is_array($result1[3][$e]) == 1)
						{
							$html .="<i class=\"fa fa-angle-left pull-right\"></i></a><ul class=\"treeview-menu\">";
							foreach(array_sort($result1[3][$e],'urut',SORT_ASC) as $g => $h):
								if($ci->uri->segment(1) == $h['code']) $active_in2 = 'active';
								else $active_in2 = '';
								$href3 = ($h['code']) ? site_url($h['code']): "javascript:void(0)";
								$html .="<li class=\"treeview ".$active_in2."\"><a href=\"".$href3."\"><i class=\"".$h['icon']."\"></i><span>".$h['name']."</span></a></li>";
							endforeach;
							$html .="</ul>";
						}
						else
						{
							$html .= "</a>";
						}
						$html .="</li>";
					endforeach;
					$html .="</ul>";
				}
				else
				{
					$html .= "</a>";
				}
				$html .="</li>";
			endforeach;
			$html .="</ul>";
		}
		else
		{
			$html .= "</a>";
		}
		$html .="</li>";
	endforeach;	
	
	return $html;
}

function menu($d = array(),$u)
{
	$ci =& get_instance();
	if(in_array($ci->uri->segment($u),$d, true)) return 'active';
}

function submenu($d = null,$u)
{
	$ci =& get_instance();
	if($ci->uri->segment($u) == $d) return 'class="active"';
}

function menu1($d = array(),$u,$uri1)
{
	$ci =& get_instance();
	if(in_array($ci->uri->segment($u),$d, true)  && $ci->uri->segment(1) == $uri1) return 'active';
}

function submenu1($d = null,$u,$uri1)
{
	$ci =& get_instance();
	if($ci->uri->segment($u) == $d && $ci->uri->segment(1) == $uri1) return 'class="active"';
}

function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

function my_array_merge($array1, $array2) {
    $result = Array();
    foreach($array1 as $key => $value) {
        $result[$key] = array_merge($value, $array2[$key]);
    }
    return $result;
}

function seo_title($s) {
    $c = array (' ');
    $d = array ('-','/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+');

    $s = str_replace($d, '', $s); // Hilangkan karakter yang telah disebutkan di array $d
    
    $s = strtolower(str_replace($c, '-', $s)); // Ganti spasi dengan tanda - dan ubah hurufnya menjadi kecil semua
    return $s;
}

function tgl_indo($tgl){
	$tanggal = substr($tgl,8,2);
	$bulan = getBulan(substr($tgl,5,2));
	$tahun = substr($tgl,0,4);
	return $tanggal.' '.$bulan.' '.$tahun;
}

function tgl_indo2($tgl){
	$bulan = getBulan(substr($tgl,5,2));
	$tahun = substr($tgl,0,4);
	return $bulan.' '.$tahun;
}

function getBulan($bln){
	switch ($bln){
		case 1: 
			return "Januari";
			break;
		case 2:
			return "Februari";
			break;
		case 3:
			return "Maret";
			break;
		case 4:
			return "April";
			break;
		case 5:
			return "Mei";
			break;
		case 6:
			return "Juni";
			break;
		case 7:
			return "Juli";
			break;
		case 8:
			return "Agustus";
			break;
		case 9:
			return "September";
			break;
		case 10:
			return "Oktober";
			break;
		case 11:
			return "November";
			break;
		case 12:
			return "Desember";
			break;
	}
}

function tahun_list($kosong = 0, $dari = -10, $sampai = 1)
{
	$CI =& get_instance();
	$CI->lang->load('calendar');

	if($kosong) $result[0] = 'Semua tahun';
	
	$y = date("Y");
	for($i = $dari; $i <= $sampai; $i++)
	{
		$result[$y + $i] = $y + $i;
	}

	return $result;
}

function bulan_list($kosong = 0)
{
	$CI =& get_instance();
	$CI->lang->load('calendar');

	if($kosong) $result[0] = 'Semua bulan';
	$result['1']	= $CI->lang->line('cal_january');
	$result['2']	= $CI->lang->line('cal_february');
	$result['3']	= $CI->lang->line('cal_march');
	$result['4']	= $CI->lang->line('cal_april');
	$result['5']	= $CI->lang->line('cal_may');
	$result['6']	= $CI->lang->line('cal_june');
	$result['7']	= $CI->lang->line('cal_july');
	$result['8']	= $CI->lang->line('cal_august');
	$result['9']	= $CI->lang->line('cal_september');
	$result['10']	= $CI->lang->line('cal_october');
	$result['11']	= $CI->lang->line('cal_november');
	$result['12']	= $CI->lang->line('cal_december');
	
	return $result;
}

function format_tanggal_order($val)
{
	$tahun	= substr($val,0,4);
	$bulan	= substr($val,5,2);
	$tgl	= substr($val,8,2);
	$combine = $tgl."/".$bulan."/".$tahun;
	return $combine; 
}

function formatMoney($number, $fractional=TRUE) { 
	if ($fractional) { 
		$number = sprintf('%.2f', $number); 
	} 
	while (true) { 
		$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number); 
		if ($replaced != $number) { 
			$number = $replaced; 
		} else { 
			break; 
		} 
	} 
	return $number; 
}

function kesiapan($x)
{
	if($x >= 20 && $x <= 23)
	{
		$hasil = "Tidak Siap";
	}
	else if($x >= 24 && $x <= 27)
	{
		$hasil = "Kurang Siap";
	}
	else if($x >= 28 && $x <= 31)
	{
		$hasil = "Belum Cukup Siap";
	}
	else if($x >= 32 && $x <= 35)
	{
		$hasil = "Siap";
	}
	else if($x >= 36 && $x <= 40)
	{
		$hasil = "Sangat Siap";
	}
	else
	{
		$hasil = "Sangat Tidak Siap";
	}
	
	return $hasil;
}

function kecemasan($x)
{
	if($x >= 20 && $x <= 23)
	{
		$hasil = "Agak Panik";
	}
	else if($x >= 24 && $x <= 27)
	{
		$hasil = "Kecemasan Sedang";
	}
	else if($x >= 28 && $x <= 31)
	{
		$hasil = "Cukup Cemas";
	}
	else if($x >= 32 && $x <= 40)
	{
		$hasil = "Kecemasan Ringan";
	}
	else
	{
		$hasil = "Panik";
	}
	
	return $hasil;
}

function tipe_pensiun($x)
{
	if($x == 2)
	{
		$hasil = 'Istirahat';
	}
	else if($x == 3)
	{
		$hasil = 'Aktif';
	}
	else if($x == 4)
	{
		$hasil = 'Terencana';
	}
	else
	{
		$hasil = 'Berlibur';
	}
	return $hasil;
}

function umur($tgl_lahir,$delimiter='-') {
    
    list($tahun,$bulan,$hari) = explode($delimiter, $tgl_lahir);
    
    $selisih_hari = date('d') - $hari;
    $selisih_bulan = date('m') - $bulan;
    $selisih_tahun = date('Y') - $tahun;
    
    if ($selisih_hari < 0 || $selisih_bulan < 0) {
        $selisih_tahun--;
    }
    
    return $selisih_tahun;
}
