<?php
function getTemplate($content = '')
{
    global $menus;
    $requesturl = $_SERVER['REQUEST_URI'];
    $requesturl = explode("/", $requesturl);
    if ($content == '') {
        if (isset($requesturl[1]) && $requesturl[1] != '') {
            $val = count($requesturl);
            $searched = $requesturl[$val - 1];
            if (strpos($searched, '?') > 0) {
                $searched = str_split($searched, strpos($searched, '?'));
                $searched = $searched[0];
            }
            foreach ($menus as $mainkey => $mainmenu) {
                if (isset($mainmenu['submenu']) && $val > 2) {
                    foreach ($mainmenu['submenu'] as $subkey => $submenu) {
                        if (isset($submenu['submenu']) && $val > 3) {
                            foreach ($submenu['submenu'] as $controlkey => $controlmenu) {
                                if ($searched == $controlkey)
                                    return TEMPLATEPATH . $submenu['template'] . '/' . $controlmenu['template'] . '.php';
                            }
                        } else if ($val == 3) {
                            if ($searched == $subkey)
                                return TEMPLATEPATH . $submenu['template'] . '.php';
                        }
                    }
                } else if ($val == 2) {
                    if ($searched == $mainkey)
                        return TEMPLATEPATH . $mainmenu['template'] . '.php';
                }
            }
            //print_r($requesturl[$val-1]);
        } else {
            return TEMPLATEPATH . $menus['index']['template'] . '.php';
        }
    } else {
        return TEMPLATEPATH . $content . '.php';
    }
}
function getContent($content = '')
{
    global $menus;
    $requesturl = $_SERVER['REQUEST_URI'];
    $requesturl = explode("/", $requesturl);
    foreach($requesturl as $key=>$url){
        if(strpos($url,"?")>0){
            $requesturl[$key] = substr($url,0,strpos($url,"?"));
        }
    }
    if ($content == '') {
        if (isset($requesturl[1]) && $requesturl[1] != '') {
            if (isset($requesturl[2]) && $requesturl[2] != '') {
                if (isset($requesturl[3]) && $requesturl[3] != '') {
                    $result = [$menus[$requesturl[1]]];
                    $result[1] = $menus[$requesturl[1]]['submenu'][$requesturl[2]];
                    $result[2] = $menus[$requesturl[1]]['submenu'][$requesturl[2]]['submenu'][stripRoute($requesturl[3])];
                    unset($result[0]['submenu'], $result[1]['submenu'], $result[3]['submenu']);
                    return $result;
                } else {
                    $result = [$menus[$requesturl[1]]];
                    $result[1] = $menus[$requesturl[1]]['submenu'][$requesturl[2]];
                    unset($result[0]['submenu'], $result[1]['submenu']);
                    return $result;
                }
            } else {
                $result = [$menus[$requesturl[1]]];
                unset($result[0]['submenu']);
                return $result;
            }
        } else {
            $result = [$menus['index']];
            unset($result[0]['submenu']);
            return $result;
        }
    } else {
        $content;
    }
}
function stripRoute($searched)
{
    if (strpos($searched, '?') > 0) {
        $searched = str_split($searched, strpos($searched, '?'));
        $searched = $searched[0];
    }
    return $searched;
}
