<?php

class get
{
    public static function environnement()
    {
        if ($_SERVER['HTTP_HOST'] == DOMAIN_CMS) {
            return "cms.";
        } elseif ($_SERVER['HTTP_HOST'] == DOMAIN_EVENTS or $_SERVER['HTTP_HOST'] == DOMAIN_CONTROL) {
            return "events.";
        }
    }

    public static function getDefautPage(){
        return HOME_TYPE_USER[session::getType()]["home"];
    }
    
    public static function isDefautMenu(array $_menu){
        return (core::ifGet("p") == FALSE AND in_array(self::getDefautPage(), $_menu)) ? TRUE : FALSE;
    }

    public static function page(string $_page = NULL)
    {
        if (isset($_page)) {
            $page = $_page;
        } elseif (core::ifGet("p")) {
            $page = core::getGet("p");
        } else {
            $page = self::getDefautPage();
        }
        if (access::check($page, "page") OR in_array($page, WHITE_ACCESS)) { 
            (file_exists(DIR_PHP_VIEWS_PAGE . self::environnement() . $page . '.php')) ?
                require_once DIR_PHP_VIEWS_PAGE . self::environnement() . $page . '.php' : alert::recError("Page introuvable : " . $page);
        } else { 
            alert::recError("La page que vous tentez de charger ne vous est pas disponible.");
        }
    }

    public static function submit()
    {

        if (core::ifPost("from")) {
            $submit = core::getPost("from");
        } elseif (core::ifGet("from")) {
            $submit = core::getGet("from");
        } else {
            alert::recError("Submit introuvable #1");
            header("Location: /");
            exit();
        }

        if (access::ifAccesss($submit)) {
            if (file_exists(DIR_PHP_SUBMIT . self::environnement() . $submit . '.php')) {
                require_once DIR_PHP_SUBMIT . self::environnement() . $submit . '.php';
            } else {
                alert::recError("Submit introuvable #2");
                header("Location: /");
                exit();
            }
        }
    }

    public static function javascript(string $_string){
        if (file_exists(DIR_PHP_JAVASCRIPT . self::environnement() . $_string . '.php')) {
            include DIR_PHP_JAVASCRIPT . self::environnement() . $_string . '.php';
        }
    }

    public static function json()
    {
        if (core::ifGet("file")) {
            if (access::check(core::getGet("file"), "json")) {
                // Exception 
                    if(core::getGet("file") == "documents" AND access::ifLimitAccessException("salaire")){ 
                        $file = "documents-limited";
                    } else {
                        $file = core::getGet("file");
                    }
                // Exception
                if (file_exists(DIR_DATAS_JSON . $file . '.json')) {
                    header('Content-type: application/json');
                    require_once DIR_DATAS_JSON . $file . '.json';
                    exit();
                } else {
                    echo json_encode("['No found']");
                    exit();
                }
            } else {
                header('HTTP/1.0 401 Unauthorized');
                exit();
            }
        }
    }

    public static function jsonData()
    {
        if (core::ifGet("jsonData")) {
            if (access::check(core::getGet("jsonData"), "json") == TRUE) {
                if (file_exists(DIR_DATAS_JSONDATA . self::environnement() . core::getGet("jsonData") . '.php')) {
                    require_once DIR_DATAS_JSONDATA . self::environnement() . core::getGet("jsonData") . '.php';
                    exit();
                } else {
                    exit();
                }
            } else {
                header('HTTP/1.0 401 Unauthorized');
                exit();
            }
        }
    }

    public static function jsonDateDataExcel()
    {

        db::query("SELECT dateData FROM " . DB_T_EXCEL . " ORDER BY id DESC LIMIT 0, 1");
        $data = db::single();
        return (isset($data["dateData"])) ? $data["dateData"] : NULL;
    }

    public static function jsonDateDataExcelProweb()
    {
        db::query("SELECT dateData FROM " . DB_T_EXCEL_PROWEB . " ORDER BY id DESC LIMIT 0, 1");
        $data = db::single();
        return (isset($data["dateData"])) ? $data["dateData"] : NULL;
    }

    public static function splitIdPage($_page = NULL)
    {
        $return = array(
            "page" => NULL,
            "id" => NULL,
        );

        $split = explode("-", $_page);
        foreach ($split as $val) {
            if (is_numeric($val)) {
                $return["id"] = $val;
            } else {
                $return["page"] .= $val . "-";
            }
        }

        $return["page"] = substr($return["page"], 0, -1);
        return $return;
    }

    public static function currentPage($_page = NULL)
    {
        if($_page != NULL){
            $page = self::splitIdPage($_page);
        } else {
            $page = NULL;
        }

        if (core::getGet("id") == $page["id"] AND core::getGet("p") == $page["page"]) { 
            return " active"; 
        } elseif (core::getGet("p") == $_page) { 
            return " active";
        } elseif (core::getGet("p") == $_page) { 
            return " active";
        } elseif (self::getDefautPage() == $_page AND core::getGet("p") == $_page) { 
            return " active";
        } elseif (self::getDefautPage() == $_page AND core::getGet("p") == "") { 
            return " active";
        }
    }

}
