<?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 page(string $_page = NULL)
    {
        if (isset($_page)) {
            $page = $_page;
        } elseif (core::ifGet("p")) {
            $page = core::getGet("p");
        } else {
            if (session::accessUserByType(1)) { // Admin
                $page = DEFAUT_PAGE;
            } elseif (session::accessUserByType(3)) { // Assistance sociale
                $page = DEFAUT_PAGE_SOCIAL;
            }
        }

        if (session::accessElement($page, "page")) {
            (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 (session::accessElement($submit, "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 json()
    {
        if (core::ifGet("file")) {
            if (session::accessElement(core::getGet("file"), "json")) {
                if (file_exists(DIR_DATAS_JSON . core::getGet("file") . '.json')) {
                    header('Content-type: application/json');
                    require_once DIR_DATAS_JSON . core::getGet("file") . '.json';
                    exit();
                } else {
                    exit();
                }
            } else {
                header('HTTP/1.0 401 Unauthorized');
                exit();
            }
        }
    }

    public static function jsonData()
    {
        if (core::ifGet("jsonData")) {
            if (session::accessElement(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 currentPage($_page = NULL)
    {
        if (core::getGet("p") == $_page) {
            return " active";
        } elseif (!core::ifGet("p") and (($_SESSION["user"]["idType"] == 1 and $_page == DEFAUT_PAGE) or ($_SESSION["user"]["idType"] == 3 and $_page == DEFAUT_PAGE_SOCIAL))) {
            return " active";
        }
    }
}
