<?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 $menu_classes;
    public $submenu;

    private $r;

    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';

        if($GLOBALS['redis'] !== false)
            $this->r = $GLOBALS['redis'];
        else
            $this->r = false;
    }

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

    function addSubmenuItem($text, $action='', $icon = '', $classes = '')
    {
        $active = $GLOBALS['url'][0];
        if (!$active) $active = 'index';


        if ($active == $action)
            $act = 1;
        else $act = 0;

        $this->submenu[] = array('text' => $text, 'action' => $action, 'active' => $act, 'icon' => $icon, 'classes' => $classes);
    }

    function setSubmenu()
    {
        //run addsubmenuitem here
    }

    function getSubmenu($active = 0)
    {
        $prepare = array();
        if (is_array($this->submenu))
            foreach ($this->submenu as $key => $var)
                $prepare[] = array('text' => $var['text'], 'base' => strtolower($this->_controller), 'action' => $var['action'], 'active' => $var['active']);
        return $prepare;
    }

    function redirect($url)
    {
        //header("Location: $url");
        header("HX-Redirect: ". $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;

        //when the template path starts with a / it is relative to the root of the project
        if($this->variables['template'][0]=='/')
            $this->variables['template'] = '../../'.$this->variables['template'];
        if($this->variables['template'] && file_exists(ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template']))
            $templatefile = ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template'];
        
        
        //render template by running include
        ob_start();
        if(is_array($this->variables))
            extract($this->variables);
        if($templatefile!= '')
            include($templatefile);
        $pagecontent = ob_get_contents();
        ob_end_clean();


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

        return $pagecontent;
    }
}