2016-10-06 15:57:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-01-13 12:45:12 +00:00
|
|
|
* Class Template for setup
|
2016-10-06 15:57:01 +00:00
|
|
|
*
|
2017-01-13 12:45:12 +00:00
|
|
|
* @package ConLite
|
|
|
|
* @subpackage Setup
|
2019-07-03 11:58:28 +00:00
|
|
|
* @version $Rev$
|
2017-01-13 12:45:12 +00:00
|
|
|
* @author Ortwin Pinke <o.pinke@conlite.org>
|
|
|
|
* @copyright (c) 2017, conlite.org
|
2016-10-06 15:57:01 +00:00
|
|
|
*
|
2019-07-03 11:58:28 +00:00
|
|
|
* $Id$:
|
2017-01-13 12:45:12 +00:00
|
|
|
*/
|
|
|
|
/**
|
2016-10-06 15:57:01 +00:00
|
|
|
* @package ContenidoBackendArea
|
|
|
|
* @version 1.2
|
|
|
|
* @author Jan Lengowski <Jan.Lengowski@4fb.de>
|
|
|
|
* @copyright four for business AG <www.4fb.de>
|
|
|
|
* @license http://www.contenido.org/license/LIZENZ.txt
|
|
|
|
* @link http://www.4fb.de
|
|
|
|
* @link http://www.contenido.org
|
|
|
|
*/
|
2017-01-13 12:45:12 +00:00
|
|
|
if (!defined('CON_FRAMEWORK')) {
|
|
|
|
die('Illegal call');
|
2016-10-06 15:57:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
class Template {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Needles (static)
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $needles = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replacements (static)
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $replacements = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dyn_Needles (dynamic)
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $Dyn_needles = array();
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Dyn_Replacements (dynamic)
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $Dyn_replacements = array();
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Database instance
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
var $db;
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Template cache
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $tplcache;
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Template name cache
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $tplnamecache;
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Dynamic counter
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
var $dyn_cnt = 0;
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Tags array (for dynamic blocks);
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $tags = array('static' => '{%s}', 'start' => '<!-- BEGIN:BLOCK -->', 'end' => '<!-- END:BLOCK -->');
|
2022-12-20 18:36:01 +00:00
|
|
|
|
|
|
|
protected $_sDomain;
|
|
|
|
protected $_encoding;
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Constructor function
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($tags = false) {
|
|
|
|
$this->tplcache = Array();
|
|
|
|
$this->tplnamecache = Array();
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
if (is_array($tags)) {
|
|
|
|
$this->tags = $tags;
|
|
|
|
}
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
$this->setEncoding("");
|
|
|
|
$this->setDomain("conlite");
|
|
|
|
}
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
// end function
|
2016-10-06 15:57:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* setDomain
|
|
|
|
*
|
|
|
|
* Sets the gettext domain to use for translations in a template
|
|
|
|
*
|
2017-01-13 12:45:12 +00:00
|
|
|
* @param $sDomain string Sets the domain to use for template translations
|
2016-10-06 15:57:01 +00:00
|
|
|
* @return none
|
2017-01-13 12:45:12 +00:00
|
|
|
*/
|
|
|
|
public function setDomain($sDomain) {
|
|
|
|
$this->_sDomain = $sDomain;
|
2016-10-06 15:57:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Set Templates placeholders and values
|
|
|
|
*
|
|
|
|
* With this method you can replace the placeholders
|
|
|
|
* in the static templates with dynamic data.
|
|
|
|
*
|
|
|
|
* @param $which String 's' for Static or else dynamic
|
|
|
|
* @param $needle String Placeholder
|
|
|
|
* @param $replacement String Replacement String
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-01-11 18:41:20 +00:00
|
|
|
public function set($which, $needle, $replacement) {
|
|
|
|
$which = empty($which)?'s':$which;
|
2017-01-13 12:45:12 +00:00
|
|
|
if ($which == 's') { // static
|
|
|
|
$this->needles[] = sprintf($this->tags['static'], $needle);
|
|
|
|
$this->replacements[] = $replacement;
|
|
|
|
} else { // dynamic
|
|
|
|
$this->Dyn_needles[$this->dyn_cnt][] = sprintf($this->tags['static'], $needle);
|
|
|
|
$this->Dyn_replacements[$this->dyn_cnt][] = $replacement;
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 15:57:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets an encoding for the template's head block.
|
|
|
|
*
|
|
|
|
* @param $encoding string Encoding to set
|
2017-01-13 12:45:12 +00:00
|
|
|
*/
|
|
|
|
public function setEncoding($encoding) {
|
|
|
|
$this->_encoding = $encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate internal counter by one
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function next() {
|
|
|
|
$this->dyn_cnt++;
|
2016-10-06 15:57:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Reset template data
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function reset() {
|
|
|
|
$this->dyn_cnt = 0;
|
|
|
|
$this->needles = array();
|
|
|
|
$this->replacements = array();
|
|
|
|
$this->Dyn_needles = array();
|
|
|
|
$this->Dyn_replacements = array();
|
|
|
|
}
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
/**
|
|
|
|
* Generate the template and
|
|
|
|
* print/return it. (do translations sequentially to save memory!!!)
|
|
|
|
*
|
|
|
|
* @param $template string/file Template
|
|
|
|
* @param $return bool Return or print template
|
|
|
|
* @param $note bool Echo "Generated by ... " Comment
|
|
|
|
*
|
|
|
|
* @return string complete Template string
|
|
|
|
*/
|
|
|
|
public function generate($template, $return = 0, $note = 1) {
|
|
|
|
global $cfg;
|
|
|
|
$this->set("s", "TPL_ACT_YEAR", date("Y"));
|
|
|
|
//check if the template is a file or a string
|
|
|
|
if (!@ file_exists($template))
|
|
|
|
$content = & $template; //template is a string (it is a reference to save memory!!!)
|
|
|
|
else
|
|
|
|
$content = implode("", file($template)); //template is a file
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
$content = (($note) ? "<!-- Generated by ConLite " . $cfg['version'] . "-->\n" : "") . $content;
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
$pieces = array();
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
//if content has dynamic blocks
|
|
|
|
if (preg_match("/^.*" . preg_quote($this->tags['start'], "/") . ".*?" . preg_quote($this->tags['end'], "/") . ".*$/s", $content)) {
|
|
|
|
//split everything into an array
|
|
|
|
preg_match_all("/^(.*)" . preg_quote($this->tags['start'], "/") . "(.*?)" . preg_quote($this->tags['end'], "/") . "(.*)$/s", $content, $pieces);
|
|
|
|
//safe memory
|
|
|
|
array_shift($pieces);
|
|
|
|
$content = "";
|
|
|
|
//now combine pieces together
|
|
|
|
//start block
|
|
|
|
$pieces[0][0] = str_replace($this->needles, $this->replacements, $pieces[0][0]);
|
|
|
|
$this->replacei18n($pieces[0][0], "i18n");
|
|
|
|
$this->replacei18n($pieces[0][0], "trans");
|
|
|
|
$content .= $pieces[0][0];
|
|
|
|
unset($pieces[0][0]);
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
//generate dynamic blocks
|
|
|
|
for ($a = 0; $a < $this->dyn_cnt; $a ++) {
|
|
|
|
$temp = str_replace($this->Dyn_needles[$a], $this->Dyn_replacements[$a], $pieces[1][0]);
|
|
|
|
$this->replacei18n($temp, "i18n");
|
|
|
|
$this->replacei18n($temp, "trans");
|
|
|
|
$content .= $temp;
|
|
|
|
}
|
|
|
|
unset($temp);
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
//end block
|
|
|
|
$pieces[2][0] = str_replace($this->needles, $this->replacements, $pieces[2][0]);
|
|
|
|
$this->replacei18n($pieces[2][0], "i18n");
|
|
|
|
$this->replacei18n($pieces[2][0], "trans");
|
|
|
|
$content .= $pieces[2][0];
|
|
|
|
unset($pieces[2][0]);
|
|
|
|
} else {
|
|
|
|
$content = str_replace($this->needles, $this->replacements, $content);
|
|
|
|
$this->replacei18n($content, "i18n");
|
|
|
|
$this->replacei18n($content, "trans");
|
|
|
|
}
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
if ($this->_encoding != "") {
|
|
|
|
$content = str_replace("</head>", '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->_encoding . '">' . "\n" . '</head>', $content);
|
2016-10-06 15:57:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
if ($return)
|
|
|
|
return $content;
|
|
|
|
else
|
|
|
|
echo $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
# end function
|
|
|
|
|
|
|
|
/**
|
|
|
|
* replacei18n()
|
|
|
|
*
|
|
|
|
* Replaces a named function with the translated variant
|
|
|
|
*
|
|
|
|
* @param $template string Contents of the template to translate (it is reference to save memory!!!)
|
|
|
|
* @param $functionName string Name of the translation function (e.g. i18n)
|
|
|
|
*/
|
|
|
|
public function replacei18n(& $template, $functionName) {
|
|
|
|
$matches = array();
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
//if template contains functionName + parameter store all matches
|
|
|
|
preg_match_all("/" . preg_quote($functionName, "/") . "\\(([\\\"\\'])(.*?)\\1\\)/s", $template, $matches);
|
|
|
|
$matches = array_values(array_unique($matches[2]));
|
|
|
|
for ($a = 0; $a < count($matches); $a ++) {
|
|
|
|
$template = preg_replace("/" . preg_quote($functionName, "/") . "\\([\\\"\\']" . preg_quote($matches[$a], "/") . "[\\\"\\']\\)/s", i18n($matches[$a], $this->_sDomain), $template);
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 15:57:01 +00:00
|
|
|
|
2017-01-13 12:45:12 +00:00
|
|
|
}
|
2016-10-06 15:57:01 +00:00
|
|
|
?>
|