287 lines
7.2 KiB
PHP
287 lines
7.2 KiB
PHP
<?php
|
|
|
|
class mfLayout {
|
|
|
|
private $log;
|
|
private $tvars = array();
|
|
private $template = "cli";
|
|
private $package = "default";
|
|
private $inline;
|
|
private $returnValue;
|
|
protected static $instance;
|
|
|
|
public function __construct() {
|
|
$this->log = mfLoghandler::singleton();
|
|
$this->inline = new stdClass();
|
|
|
|
if (!defined("LAYOUT_DEFAULTPACKAGE")) {
|
|
$this->package = "default";
|
|
} else {
|
|
$this->package = LAYOUT_DEFAULTPACKAGE;
|
|
}
|
|
|
|
if (method_exists($this, "init")) {
|
|
$this->init();
|
|
}
|
|
}
|
|
|
|
public static function singleton($param = false) {
|
|
if (!isset(self::$instance)) {
|
|
$c = __CLASS__;
|
|
self::$instance = new $c($param);
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function set($name, $value) {
|
|
$this->tvars[$name] = $value;
|
|
}
|
|
|
|
public function get($name) {
|
|
if(array_key_exists($name, $this->tvars)) {
|
|
return $this->tvars[$name];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function setPackage($package) {
|
|
$this->package = $package;
|
|
}
|
|
|
|
public function setTemplate($template) {
|
|
$this->template = $template;
|
|
}
|
|
|
|
private function setReturnValue($value) {
|
|
$this->returnValue = $value;
|
|
}
|
|
|
|
public function getReturnedValue() {
|
|
return $this->returnValue;
|
|
}
|
|
|
|
private function getTplPath() {
|
|
return VIEWDIR . "/" . $this->package . "/" . $this->template . ".php";
|
|
}
|
|
|
|
public function templatePathExists() {
|
|
return file_exists($this->getTplPath());
|
|
}
|
|
|
|
public function render() {
|
|
$this->defaultLayoutvariables();
|
|
|
|
$tpl_path = $this->getTplPath();
|
|
if(!is_file($tpl_path)) {
|
|
$tpl_path = VIEWDIR . "/default/" . $this->template . ".php";
|
|
}
|
|
|
|
foreach ($this->tvars as $name => $value) {
|
|
if ($name === "this")
|
|
continue;
|
|
$$name = $value;
|
|
}
|
|
|
|
ob_end_clean();
|
|
ob_start();
|
|
include($tpl_path);
|
|
return ob_get_clean();
|
|
}
|
|
|
|
public function display() {
|
|
echo $this->render();
|
|
}
|
|
|
|
public function renderPDF($filename = false, $extraPdfArgs = false) {
|
|
$html = $this->render();
|
|
|
|
if (!$filename)
|
|
$filename = date('U') . "-" . rand(1000, 9999) . ".pdf";
|
|
|
|
$wk = new mfWkhtmltopdf($html);
|
|
|
|
$pdfargs = "";
|
|
if ($extraPdfArgs) {
|
|
$pdfargs .= " $extraPdfArgs";
|
|
}
|
|
|
|
$filename = $wk->generate($filename, $pdfargs);
|
|
|
|
if (!$filename) {
|
|
throw new Exception("Error generating PDF document.");
|
|
}
|
|
|
|
$file = PDFOUTPUTPATH . "/$filename";
|
|
|
|
return $file;
|
|
}
|
|
|
|
public function displayPDF($filename = false, $extraPdfArgs = false) {
|
|
$filepath = $this->renderPDF($filename, $extraPdfArgs);
|
|
|
|
if (!$filename && strpos($filepath, "/") !== false) {
|
|
$path_parts = explode("/", $filepath);
|
|
$filename = end($path_parts);
|
|
}
|
|
|
|
header('Content-Type: application/octet-stream');
|
|
header('Content-disposition: attachment; filename=' . $filename);
|
|
header('Content-Transfer-Encoding: binary');
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
header('Content-Type: ' . mime_content_type($filepath));
|
|
header("Content-Length: " . filesize($filepath));
|
|
|
|
readfile($filepath);
|
|
exit;
|
|
}
|
|
|
|
public function setFlash($msg, $type = "info") {
|
|
// info, warning, error
|
|
if (!$msg) {
|
|
return true;
|
|
}
|
|
|
|
$_SESSION[MFAPPNAME . '_mfNotify'] = array("type" => $type, "message" => $msg);
|
|
|
|
return true;
|
|
}
|
|
|
|
private function defaultLayoutvariables() {
|
|
$this->set('mfLayoutPackage', $this->package);
|
|
|
|
if (isset($_SESSION[MFAPPNAME . '_mfNotify'])) {
|
|
$this->set("mfNotify", $_SESSION[MFAPPNAME . '_mfNotify']);
|
|
unset($_SESSION[MFAPPNAME . '_mfNotify']);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Begin inline functions
|
|
*/
|
|
|
|
public static function strtrim($string, $chars) {
|
|
if (strlen($string) <= $chars)
|
|
return $string;
|
|
return substr($string, 0, $chars) . "...";
|
|
}
|
|
|
|
// produces url for generating links
|
|
public static function getUrl($mod, $action = null, $param = null) {
|
|
if (!$mod) {
|
|
return "";
|
|
}
|
|
|
|
|
|
if (MFUSEFANCYURLS) {
|
|
// use fancy urls
|
|
$url = MFFANCYBASEURL;
|
|
if ($mod) {
|
|
$url .= "/$mod";
|
|
if ($action) {
|
|
$url .= "/$action";
|
|
}
|
|
}
|
|
$url = preg_replace('#//#', '/', $url);
|
|
} else {
|
|
// no fancy urls
|
|
$url = "?action=$mod";
|
|
if ($action) {
|
|
$url .= "_$action";
|
|
}
|
|
}
|
|
if (is_array($param) && count($param)) {
|
|
$url .= (MFUSEFANCYURLS) ? "/?" : "&";
|
|
$param_qs = http_build_query($param);
|
|
$url .= "$param_qs";
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
|
|
public static function getResourcePath() {
|
|
if (MFUSEFANCYURLS) {
|
|
$path = MFFANCYBASEURL;
|
|
if (substr($path, -1, 1) != "/") {
|
|
$path .= "/";
|
|
}
|
|
} else {
|
|
$path = "";
|
|
}
|
|
return $path;
|
|
}
|
|
|
|
public function cycle() {
|
|
$args = func_get_args();
|
|
|
|
if (is_array($this->inline->cycle) && count($this->inline->cycle)) {
|
|
if (array_diff($this->inline->cycle, $args)) { // if different, start new cycle
|
|
$this->inline->cycle = $args;
|
|
$this->inline->cyclecount = 0;
|
|
}
|
|
} else {
|
|
$this->inline->cycle = $args;
|
|
$this->inline->cyclecount = 0;
|
|
}
|
|
|
|
if ($this->inline->cyclecount >= count($this->inline->cycle)) {
|
|
$this->inline->cyclecount = 0;
|
|
}
|
|
|
|
return $this->inline->cycle[$this->inline->cyclecount++];
|
|
}
|
|
|
|
}
|
|
|
|
|
|
function mfLayoutInclude($filename, $folder = "") {
|
|
global $mfLayoutPackage;
|
|
|
|
if($folder) {
|
|
$folder = $folder."/";
|
|
}
|
|
|
|
$inc_path = VIEWDIR . "/" . LAYOUT_DEFAULTPACKAGE . "/" . $folder . $filename;
|
|
if(!file_exists($inc_path)) {
|
|
$inc_path = VIEWDIR . "/default/" . $folder . $filename;
|
|
}
|
|
|
|
include $inc_path;
|
|
}
|
|
|
|
/*
|
|
* global variable $lang to avoid loading it every time it's accessed
|
|
*/
|
|
if(!isset($lang)) {
|
|
$lang = [];
|
|
}
|
|
|
|
$last_translation_failed = false;
|
|
|
|
function __($_string, $prefix = null) {
|
|
global $lang;
|
|
global $last_translation_failed;
|
|
$last_translation_failed = false;
|
|
|
|
$string = str_replace(["Ä","Ö","Ü","ß","ä","ö","ü","ß"], ["ae","oe","ue","ss","ae","oe","ue", "ss"], strtolower($_string));
|
|
|
|
if(!$lang) {
|
|
//mfLoghandler::singleton()->debug("Loading language file for __() function");
|
|
include(BASEDIR . "/lang/de.php");
|
|
}
|
|
|
|
if($prefix) {
|
|
$string = "$prefix.$string";
|
|
}
|
|
|
|
if (array_key_exists($string, $lang['de'])) {
|
|
return $lang['de'][$string];
|
|
}
|
|
$last_translation_failed = true;
|
|
return $string;
|
|
}
|
|
|
|
function __last_translation_failed() {
|
|
global $last_translation_failed;
|
|
return $last_translation_failed;
|
|
}
|