This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/inc/classes/Page.class.php

123 lines
3.2 KiB
PHP
Raw Normal View History

2023-10-18 22:42:01 +02:00
<?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;
2023-10-18 19:08:38 +02:00
private $r;
2023-10-18 22:42:01 +02:00
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';
2023-10-18 19:08:38 +02:00
if($GLOBALS['redis'] !== false)
$this->r = $GLOBALS['redis'];
else
$this->r = false;
2023-10-18 22:42:01 +02:00
}
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;
2023-10-21 17:06:20 +02:00
/*$partials = [];
2023-10-18 22:42:01 +02:00
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');
2023-10-18 22:42:01 +02:00
$m = new Mustache_Engine(array(
'entity_flags' => ENT_QUOTES,
'partials_loader' => new Mustache_Loader_CascadingLoader($partials)
));
$this->variables['translate'] = function($value) { return translate($value); };
2023-10-21 17:06:20 +02:00
$m = $this->addHelpers($m);*/
2023-10-18 22:42:01 +02:00
if($this->variables['template'] && file_exists(ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template']))
2023-10-21 17:06:20 +02:00
$templatefile = ROOT . DS . 'pages' . DS . $this->_controller . DS . $this->variables['template'];
2023-10-18 22:42:01 +02:00
2023-10-21 17:06:20 +02:00
//render template by running include
ob_start();
include($templatefile);
$pagecontent = ob_get_contents();
ob_end_clean();
//$pagecontent = $m->render($template, $this->variables);
2023-10-18 22:42:01 +02:00
return $pagecontent;
}
}