piler/webui/system/controller.php

118 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2012-02-08 23:14:28 +01:00
<?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()){
while(list($key, $value) = each($args)) $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(){
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
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;
2012-02-08 23:14:28 +01:00
} else {
$file = DIR_THEME . THEME . '/templates/' . $this->template;
}
if(file_exists($file)){
extract($this->data);
ob_start();
2012-02-08 23:14:28 +01:00
include($file);
2012-02-08 23:14:28 +01:00
$content = ob_get_contents();
ob_end_clean();
return $content;
}
else {
exit('Error: Could not load template ' . $file . '!');
}
}
}