<?php

class module {

    public $_loc_vars    = [];
    public $_get_vars    = [ 'act' ];
    public $_post_vars   = [ 'act'];
    public $_sys_no_load = false;
    public $is_ajax      = false;
    public $templBlade   = "";

    public $templ;

    public function __construct($loc_data = []) {

        global $cat;


        if (isset($loc_data['in_cat'])) {
            $in_cats = explode(',', $loc_data['in_cat']);
            if (!in_array($cat->cat, $in_cats)) {
                $this->_sys_no_load = true;
            }
        }

        if (isset($loc_data['in_path'])) {
            $in_path = explode(',', $loc_data['in_path']);
            if (!array_intersect($in_path, $cat->path)) {
                $this->_sys_no_load = true;
            }
        }


        if (isset($loc_data['not_in_cat'])) {
            $in_cats = explode(',', $loc_data['not_in_cat']);
            if (in_array($cat->cat, $in_cats)) {
                $this->_sys_no_load = true;
            }
        }
        //pr($cat->path);
        if (isset($loc_data['not_in_path'])) {
            $in_path = explode(',', $loc_data['not_in_path']);
            if (array_intersect($in_path, $cat->path)) {
                $this->_sys_no_load = true;
            }
        }

        /* init vars */
        foreach ($this->_get_vars as $k) {
            if (isset($_GET[$k])) {
                if (is_array($_GET[$k])) {
                    $this->$k = $_GET[$k];
                } else {
                    $this->$k = addslashes($_GET[$k]);
                }
            }
        }
        foreach ($this->_post_vars as $k) {
            if (isset($_POST[$k])) {
                if (is_array($_POST[$k])) {
                    $this->$k = $_POST[$k];
                } else {
                    $this->$k = addslashes($_POST[$k]);
                }
            }
        }

        foreach ($this->_loc_vars as $k) {
            if (isset($loc_data[$k])) {
                if (is_array($loc_data[$k])) {
                    $this->$k = $loc_data[$k];
                } else {
                    $this->$k = $loc_data[$k];
                }
            }
        }


        if (!$this->_init()) {
            cdie();
        }

    }

    public function _init() {
        return true;
    }

    public function render($templ = '', $ajax = false) {
        $this->is_ajax = $ajax;

        if ($templ == 'body') {
            if ($ajax) {
                if (method_exists($this, '_ajax_' . $this->act)) {
                    $render = '_ajax_' . $this->act;
                } else {
                    $render = '_on_'  . $this->act;
                }
            } else {
                $render = '_on_' . $this->act;
            }
        } else {
            if (method_exists($this, '_'.$templ.'_')) {
                $render = '_'.$templ.'_';
            } else {
                $render = '_on_';
            }
        }

        if (method_exists($this, $render)) {
            return $this->$render();
        } else {
            if (!$this->url_parse()) {
                return $this->die404();
            } else {
                return $this->url_parse();
            }
        }
    }

    public function url_parse() {
        return false;
    }

    public function die404() {
        global $cat;
        $cat->addTitle('Страница не найдена');
        Header("HTTP/1.0 404 Not Found", true, 404);
        $html = '
            <div class="container text-center mt-5 width-fixed-300">
                <h1>Ошибка 404</h1>
                <div class="alert alert-warning mt-4">Страница не найдена<br> или у Вас нет доступа к ней</div>
                <img src="data:image/png;base64,[encoded data, 32488 characters]">
            </div>
        ';

        return $html;
    }

    /**
     * Получаем шаблон для Блейда
     * @param string $templName имя шаблона
     * @return false|string
     */
    public function getBladeTempl($templName = "") {
        $baseTempl = $this->templBlade;
        if (substr($baseTempl, -1) == '.') {
            $baseTempl = substr($baseTempl, 0, -1);
        }
        if ($templName == "") {
            return $baseTempl;
        }
        return  $baseTempl . '.' . $templName;
    }

}

?>