piler/webui/system/controller.php
2022-01-19 15:40:42 +01:00

120 lines
2.5 KiB
PHP

<?php
class Controller {
protected $id;
protected $data = array();
protected $children = array();
protected $layout;
protected $output;
protected $template;
public function __construct() {
$language = Registry::get('language');
$this->data = array_merge($this->data, $language->data);
}
public function __get($key) {
return Registry::get($key);
}
public function __set($key, $value) {
Registry::set($key, $value);
}
public function args($args = array()){
foreach($args as $key => $value) {
$this->data[$key] = $value;
}
}
protected function render(){
foreach ($this->children as $child) {
$file = DIR_APPLICATION . $child . ".php";
$class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $child);
if(file_exists($file)){
require_once($file);
$controller = new $class();
$controller->index();
$this->data[$controller->id] = $controller->output;
}
else {
exit("Error: Could not load controller ($file)" . $child . '!');
}
}
$this->output = $this->fetch($this->template);
if($this->layout){
$file = DIR_APPLICATION . $this->layout . '.php';
$class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $this->layout);
if(file_exists($file)){
require_once($file);
$controller = new $class();
$controller->data[$this->id] = $this->output;
$controller->index();
$this->output = $controller->output;
}
else {
exit("Error: Could not load layout ($file) " . $this->layout . '!');
}
print $this->output;
}
}
protected function fetch(){
$session = Registry::get('session');
if($session->get("theme") && preg_match("/^([a-zA-Z0-9\-\_]+)$/", $session->get("theme")) && file_exists(DIR_THEME . $session->get("theme")) ) {
$file = DIR_THEME . $session->get("theme") . '/templates/' . $this->template;
} else {
$file = DIR_THEME . THEME . '/templates/' . $this->template;
}
if(file_exists($file)){
extract($this->data);
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
else {
exit('Error: Could not load template ' . $file . '!');
}
}
}