<?php
    use App\Helpers\globals;
	//convert stdclass to array (ga jd dipake)
	function objectToArray($d) {
        if (is_object($d)) {
            $d = get_object_vars($d);
        }
		
        if (is_array($d)) {
            return array_map(__FUNCTION__, $d);
        } else {
            return $d;
        }
    }

    //cek privilege user utk menu (rekam, ubah, hapus, export)
    function priv_menu($menu, $priv) {
    	$usr_menu = Globals::get_menu();
		$key = array_search($menu, array_column($usr_menu, 'method'));
		$has = $usr_menu[$key][$priv] == 1 ? true:false;
		return  $has;
    }

    //susun array menu biar gampang diolah jd multilevel menu
	function prepareList(array $items, $pid = 0) {
	    $output = array();

	    foreach ($items as $item) {
	        if ((int) $item['id_parent'] == $pid) {
	            if ($children = prepareList($items, $item['menu_id'])) {
	                $item['children'] = $children;
	            }
	            $output[] = $item;
	        }
	    }

	    return $output;
	}

    //kat12
	function makeNested(array $items, $pid = 0) {
        $output = array();

        foreach ($items as $item) {
            if ((int) $item['idlv1'] == $pid) {
                if ($children = makeNested($items, $item['id'])) {
                    $item['children'] = $children;
                }
                $output[] = $item;
            }
        }

        return $output;
    }

    //kat34
    function makeNestedx(array $items, $pid = 0) {
        $output = array();

        foreach ($items as $item) {
            if ((int) $item['idlv3'] == $pid) {
                if ($children = makeNestedx($items, $item['id'])) {
                    $item['children'] = $children;
                }
                $output[] = $item;
            }
        }

        return $output;
    }