<?php
if (!defined('IN_ASC'))
{
    die('Hacking attempt');
}



function get_menu()
{

    global $_LANG;
    global $_CFG;
    
    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('menu') ."WHERE pid = 0 AND status = 1 ORDER BY sort ASC";
    $result = $GLOBALS['db']->getAll($sql);


    foreach ($result AS $key => $val)
    {
        $menus[$key]['label'] = $val['title'];

        $sql_children = 'SELECT * FROM ' . $GLOBALS['ecs']->table('menu') ."WHERE pid = " .$val['id']. " AND status = 1 ORDER BY sort ASC";
        $c_result = $GLOBALS['db']->getAll($sql_children);
    
        if (is_array($c_result))
        {
            foreach ($c_result AS $k => $v)
            {
                $menus[$key]['children'][$k]['label']  = $v['title'];
                $menus[$key]['children'][$k]['action'] = $v['url'];
            }
        }
        else
        {
            $menus[$key]['action'] = $val;
        }

        // 如果children的子元素長度為0則刪除該組
        // if(empty($menus[$key]['children']))
        // {
        //     unset($menus[$key]);
        // }

    }
    return $menus;
}

/**
 * 取得menu
 * @return  array 
 */

function get_menu1()
{
    include_once('includes/inc_menu.php');

    include_once('includes/inc_priv.php');

    global $_LANG;
    global $_CFG;
    
    foreach ($modules AS $key => $value)
    {
        ksort($modules[$key]);
    }

    ksort($modules);

    foreach ($modules AS $key => $val)
    {
        $menus[$key]['label'] = $_LANG[$key];

        if (is_array($val))
        {
            foreach ($val AS $k => $v)
            {
                if ( isset($purview[$k]))
                {
                    if (is_array($purview[$k]))
                    {
                        $boole = false;
                        foreach ($purview[$k] as $action)
                        {
                             $boole = $boole || admin_priv($action, '', false);
                        }
                        if (!$boole)
                        {
                            continue;
                        }

                    }
                    else
                    {
                        if (! admin_priv($purview[$k], '', false))
                        {
                            continue;
                        }
                    }
                }
                if ($k == 'ucenter_setup' && $_CFG['integrate_code'] != 'ucenter')
                {
                    continue;
                }
                $menus[$key]['children'][$k]['label']  = $_LANG[$k];
                $menus[$key]['children'][$k]['action'] = $v;
            }
        }
        else
        {
            $menus[$key]['action'] = $val;
        }

        // 如果children的子元素長度為0則刪除該組
        if(empty($menus[$key]['children']))
        {
            unset($menus[$key]);
        }

    }
    return $menus;
}