<?php


/**
 * Page is the Controller Class
 * which should be extended by any page
 *
 * @author Christian Haschek
 */
class Page
{
    protected $_controller;
    protected $_action;
    protected $_template;
    public $variables;
    public $params;
    public $render;
    public $menu_text;
    public $menu_image;
    public $menu_priority;
    public $submenu;

    function __construct($controller, $action, $r = 1, $params = [])
    {
        $this->_controller = $controller;
        $this->_action = $action;
        $this->render = $r;
        $this->submenu = array();
        $this->setMenu();
        $this->params = $params;
        if(is_array($GLOBALS['vars']) && is_array($this->params))
            $this->params = array_merge($this->params, $GLOBALS['vars']);
        $this->menu_image = '/css/imgs/empty.png';
    }

    function setMenu()
    {
        $this->menu_text = '';
        $this->menu_priority = 1;
    }

    function redirect($url)
    {
        header("Location: $url");
        exit();
    }

    /**
     * override this function to check if a user can use this object
     * @return true -> user will be able to access
     * @return false -> user will not be able to access and this page won't
     * be shown in the menu
     *  
     */
    public function maySeeThisPage()
    {
        return true;
    }

    function set($name, $value)
    {
        $this->variables[$name] = $value;
    }

    function get($name)
    {
        return $this->variables[$name];
    }

    function __destruct()
    {
        //		if($this->render)
        //			$this->_template->render();
    }

    function addHelpers($m)
    {
        $m->addHelper('case', [
            'lower' => function($value) { return strtolower((string) $value); },
            'upper' => function($value) { return strtoupper((string) $value); }
        ]);
        $m->addHelper('!!', function($value) { return $value . '!!'; });

        return $m;
    }

    function renderPagecontent()
    {
        $controller_dir = ROOT . DS . 'pages' . DS . $this->_controller . DS;
        $partials = [];
        if(is_dir($controller_dir.'partials'))
            $partials[] = new Mustache_Loader_FilesystemLoader($controller_dir.'partials');
        $partials[] = new Mustache_Loader_FilesystemLoader(ROOT.DS.'..'.DS.'templates'.DS.'partials');
        $m = new Mustache_Engine(array(
            'entity_flags' => ENT_QUOTES,
            'partials_loader' => new Mustache_Loader_CascadingLoader($partials)
        ));

        $this->variables['translate'] = function($value) { return translate($value); };

        $m = $this->addHelpers($m);

        if($this->variables['template'] && file_exists(ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template']))
            $template = file_get_contents(ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template']);
        else
            $template = file_get_contents(ROOT . DS . 'views' . DS . 'defaultcontainer.mustache');

        
        $pagecontent = $m->render($template, $this->variables);

        return $pagecontent;
    }
}