openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit d57b904272be9629bbdc56bb4d1d9b15a543d044
parent 7953c61015cdbbf4cb57f6ab9cbfcea9d42d3a52
Author: Jan Dankert <devnull@localhost>
Date:   Wed, 15 Aug 2018 22:40:32 +0200

Die TemplateEngine kann nun auch Includes in den XML-Quellen verarbeiten.

Diffstat:
modules/cms-ui/UI.class.php | 102++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
modules/cms-ui/themes/default/html/views/include/timestamps.inc.xml | 47+++++++++++++++++++++++++++++++++++++++++++++++
modules/cms-ui/themes/default/html/views/page/info.php | 114++++++++++++++++++++++++++++++++++++++++---------------------------------------
modules/cms-ui/themes/default/html/views/page/info.tpl.src.xml | 45+--------------------------------------------
modules/template-engine/engine/TemplateEngine.class.php | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
5 files changed, 264 insertions(+), 160 deletions(-)

diff --git a/modules/cms-ui/UI.class.php b/modules/cms-ui/UI.class.php @@ -22,13 +22,60 @@ define('CMS_UI_REQ_PARAM_EMBED', 'embed'); class UI { + /** + * Shows the complete UI. + */ public static function execute() { - try { + try + { + define('COOKIE_PATH',dirname($_SERVER['SCRIPT_NAME'])); + + // Everything is UTF-8. + header('Content-Type: text/html; charset=UTF-8'); + + // Sending the Content-Security-Policy. + self::setContentSecurityPolicy(); if(empty($_REQUEST[CMS_UI_REQ_PARAM_EMBED])) { - $action = 'index'; - $subaction = 'show'; + + $isPost = $_SERVER['REQUEST_METHOD'] == 'POST'; + + if($isPost) + { + $action = $_REQUEST[CMS_UI_REQ_PARAM_ACTION]; + $subaction = $_REQUEST[CMS_UI_REQ_PARAM_SUBACTION]; + + $dispatcher = new Dispatcher(); + + $dispatcher->action = $action; + if(!defined('OR_ACTION')) + define('OR_ACTION', $action); + + $dispatcher->subaction = $subaction; + if(!defined('OR_METHOD')) + define('OR_METHOD', $subaction); + + // Embedded Actions are ALWAYS Queries (means: GET). + $dispatcher->isAction = true; + $dispatcher->isEmbedded = true; + + $data = $dispatcher->doAction(); + + //$data['notices']; + + // POST-Action has ended, now we want to show the UI. + $action = 'index'; + $subaction = 'show'; + UI::executeEmbeddedAction($action,$subaction); + } + else + { + $action = 'index'; + $subaction = 'show'; + UI::executeAction($action,$subaction); + } + } else { @@ -42,14 +89,9 @@ class UI else { $subaction = 'show'; } + UI::executeAction($action,$subaction); } - header('Content-Type: text/html; charset=UTF-8'); - - self::setContentSecurityPolicy(); - - UI::executeAction($action,$subaction); - } catch (BadMethodCallException $e) { // Action-Method does not exist. Http::noContent(); @@ -66,6 +108,10 @@ class UI } } + /** + * Shows a UI fragment. + * This can only be executed after a UI::execute()-call. + */ public static function executeEmbedded($action, $subaction) { try { @@ -89,7 +135,7 @@ class UI } } - private static function executeAction( $action, $subaction ) + private static function executeAction( $action, $subaction,$isPost=false ) { $dispatcher = new Dispatcher(); @@ -103,6 +149,7 @@ class UI $data = $dispatcher->doAction(); + $dispatcher->isAction = $isPost; // The action is able to change its method and action name. $subaction = $dispatcher->subaction; @@ -113,7 +160,7 @@ class UI UI::outputTemplate($tplName,$data['output']); } - private static function executeEmbeddedAction( $action, $subaction ) + private static function executeEmbeddedAction( $action, $subaction,$isPost=false ) { $dispatcher = new Dispatcher(); @@ -126,7 +173,7 @@ class UI define('OR_METHOD', $subaction); // Embedded Actions are ALWAYS Queries (means: GET). - $dispatcher->isAction = false; + $dispatcher->isAction = $isPost; $dispatcher->isEmbedded = true; $data = $dispatcher->callActionMethod(); @@ -148,35 +195,10 @@ class UI */ private static function outputTemplate($templateName, $outputData) { - $templateFile = __DIR__.'/themes/default/html/views/' . $templateName . '.php'; - - // In development mode, we are compiling every template on the fly. - if (DEVELOPMENT) { - $srcFile = __DIR__.'/themes/default/html/views/' . $templateName . '.tpl.src.xml'; - - // Compile the template. - // From a XML source file we are generating a PHP file. - try - { - $te = new TemplateEngine(); - $te->compile($srcFile, $templateFile); - unset($te); - } catch (Exception $e) { - throw new DomainException("Compilation failed for Template '$templateName'.", 0, $e); - } - #header("X-CMS-Template-File: " . $templateFile); - } - - // Spätestens jetzt muss das Template vorhanden sein. - if (!is_file($templateFile)) - throw new LogicException("Template file '$templateFile' was not found."); - - // Übertragen der Ausgabe-Variablen in den aktuellen Kontext - // - extract($outputData); + $templateFile = __DIR__.'/themes/default/html/views/' . $templateName . '.tpl.src.xml'; - // Einbinden des Templates - require_once($templateFile); + $engine = new TemplateEngine(); + $engine->executeTemplate( $templateFile, $outputData ); } diff --git a/modules/cms-ui/themes/default/html/views/include/timestamps.inc.xml b/modules/cms-ui/themes/default/html/views/include/timestamps.inc.xml @@ -0,0 +1,47 @@ +<output xmlns="http://www.openrat.de/template" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.openrat.de/template ../template.xsd"> + <group title="message:prop_userinfo"> + <part class="line"> + <part class="label"> + <label for="create_date"> + <text text="global_created"></text> + </label> + </part> + <part class="input"> + <image icon="el_date"></image> + <date date="var:create_date"></date> + <newline></newline> + <image icon="user"></image> + <user user="var:create_user"></user> + </part> + </part> + <part class="line"> + <part class="label"> + <label for="lastchange_date"> + <text text="global_lastchange"></text> + </label> + </part> + <part class="input"> + <image icon="el_date"></image> + <date date="var:lastchange_date"></date> + <newline></newline> + <image icon="user"></image> + <user user="var:lastchange_user"></user> + </part> + </part> + <part class="line"> + <part class="label"> + <label for="published_date"> + <text text="global_published"></text> + </label> + </part> + <part class="input"> + <image icon="el_date"></image> + <date date="var:published_date"></date> + <newline></newline> + <image icon="user"></image> + <user user="var:published_user"></user> + </part> + </part> + </group> +</output> diff --git a/modules/cms-ui/themes/default/html/views/page/info.php b/modules/cms-ui/themes/default/html/views/page/info.php @@ -98,67 +98,69 @@ </div> </div> </div></fieldset> - <fieldset class="<?php echo '1'?" open":" closed" ?><?php echo '1'?" show":"" ?>"><legend><div class="arrow arrow-right on-closed"></div><div class="arrow arrow-down on-open"></div><?php echo lang('prop_userinfo') ?></legend><div> - <div class="line"> - <div class="label"> - <label for="<?php echo REQUEST_ID ?>_create_date" class="label"> - <span class="text"><?php echo nl2br(encodeHtml(htmlentities(lang('global_created')))); ?></span> + + <fieldset class="<?php echo '1'?" open":" closed" ?><?php echo '1'?" show":"" ?>"><legend><div class="arrow arrow-right on-closed"></div><div class="arrow arrow-down on-open"></div><?php echo lang('prop_userinfo') ?></legend><div> + <div class="line"> + <div class="label"> + <label for="<?php echo REQUEST_ID ?>_create_date" class="label"> + <span class="text"><?php echo nl2br(encodeHtml(htmlentities(lang('global_created')))); ?></span> + + </label> + </div> + <div class="input"> + <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/el_date.png" /> - </label> - </div> - <div class="input"> - <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/el_date.png" /> - - <?php include_once( 'modules/template-engine/components/html/date/component-date.php') ?><?php component_date($create_date) ?> - - <br/> - - <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/user.png" /> - - <?php include_once( 'modules/template-engine/components/html/user/component-user.php') ?><?php component_user($create_user) ?> - - </div> - </div> - <div class="line"> - <div class="label"> - <label for="<?php echo REQUEST_ID ?>_lastchange_date" class="label"> - <span class="text"><?php echo nl2br(encodeHtml(htmlentities(lang('global_lastchange')))); ?></span> + <?php include_once( 'modules/template-engine/components/html/date/component-date.php') ?><?php component_date($create_date) ?> - </label> - </div> - <div class="input"> - <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/el_date.png" /> - - <?php include_once( 'modules/template-engine/components/html/date/component-date.php') ?><?php component_date($lastchange_date) ?> - - <br/> - - <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/user.png" /> - - <?php include_once( 'modules/template-engine/components/html/user/component-user.php') ?><?php component_user($lastchange_user) ?> - + <br/> + + <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/user.png" /> + + <?php include_once( 'modules/template-engine/components/html/user/component-user.php') ?><?php component_user($create_user) ?> + + </div> </div> - </div> - <div class="line"> - <div class="label"> - <label for="<?php echo REQUEST_ID ?>_published_date" class="label"> - <span class="text"><?php echo nl2br(encodeHtml(htmlentities(lang('global_published')))); ?></span> + <div class="line"> + <div class="label"> + <label for="<?php echo REQUEST_ID ?>_lastchange_date" class="label"> + <span class="text"><?php echo nl2br(encodeHtml(htmlentities(lang('global_lastchange')))); ?></span> + + </label> + </div> + <div class="input"> + <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/el_date.png" /> - </label> + <?php include_once( 'modules/template-engine/components/html/date/component-date.php') ?><?php component_date($lastchange_date) ?> + + <br/> + + <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/user.png" /> + + <?php include_once( 'modules/template-engine/components/html/user/component-user.php') ?><?php component_user($lastchange_user) ?> + + </div> </div> - <div class="input"> - <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/el_date.png" /> - - <?php include_once( 'modules/template-engine/components/html/date/component-date.php') ?><?php component_date($published_date) ?> - - <br/> - - <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/user.png" /> - - <?php include_once( 'modules/template-engine/components/html/user/component-user.php') ?><?php component_user($published_user) ?> - + <div class="line"> + <div class="label"> + <label for="<?php echo REQUEST_ID ?>_published_date" class="label"> + <span class="text"><?php echo nl2br(encodeHtml(htmlentities(lang('global_published')))); ?></span> + + </label> + </div> + <div class="input"> + <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/el_date.png" /> + + <?php include_once( 'modules/template-engine/components/html/date/component-date.php') ?><?php component_date($published_date) ?> + + <br/> + + <img class="" title="" src="./modules/cms-ui/themes/default/images/icon/user.png" /> + + <?php include_once( 'modules/template-engine/components/html/user/component-user.php') ?><?php component_user($published_user) ?> + + </div> </div> - </div> - </div></fieldset> + </div></fieldset> + <div class="bottom"><div class="command "><input type="submit" class="submit ok" value="OK" /></div></div></form> \ No newline at end of file diff --git a/modules/cms-ui/themes/default/html/views/page/info.tpl.src.xml b/modules/cms-ui/themes/default/html/views/page/info.tpl.src.xml @@ -79,49 +79,6 @@ </part> </part> </group> - <group title="message:prop_userinfo"> - <part class="line"> - <part class="label"> - <label for="create_date"> - <text text="global_created"></text> - </label> - </part> - <part class="input"> - <image icon="el_date"></image> - <date date="var:create_date"></date> - <newline></newline> - <image icon="user"></image> - <user user="var:create_user"></user> - </part> - </part> - <part class="line"> - <part class="label"> - <label for="lastchange_date"> - <text text="global_lastchange"></text> - </label> - </part> - <part class="input"> - <image icon="el_date"></image> - <date date="var:lastchange_date"></date> - <newline></newline> - <image icon="user"></image> - <user user="var:lastchange_user"></user> - </part> - </part> - <part class="line"> - <part class="label"> - <label for="published_date"> - <text text="global_published"></text> - </label> - </part> - <part class="input"> - <image icon="el_date"></image> - <date date="var:published_date"></date> - <newline></newline> - <image icon="user"></image> - <user user="var:published_user"></user> - </part> - </part> - </group> +<include file="timestamps" /> </form> </output> \ No newline at end of file diff --git a/modules/template-engine/engine/TemplateEngine.class.php b/modules/template-engine/engine/TemplateEngine.class.php @@ -2,6 +2,9 @@ namespace template_engine; +use DomainException; +use Exception; +use LogicException; use \template_engine\components\Component; /** @@ -19,37 +22,26 @@ class TemplateEngine public $config = array(); /** - * Name Template. - * - * @var String - */ - private $tplFileName; - - /** * Erzeugt einen Templateparser. - * - * @param String $tplName Name des Templates, das umgewandelt werden soll. */ - public function TemplateEngine( $tplFileName='' ) + public function __construct() { - $this->tplFileName = $tplFileName; } /** - * Wandelt eine Vorlage um - * - * @param - * filename Dateiname der Datei, die erstellt werden soll. + * Compile the template. + * From a XML source file we are generating a PHP file. + * + * @param $srcXmlFilename string Filename of template source + * @param $tplOutName string Filename of template code */ public function compile($srcXmlFilename = '',$tplOutName = '') { + // Imports the base class of all component types. require_once (dirname(__FILE__).'/../components/'.$this->renderType.'/Component.class.' . PHP_EXT); try { - if (empty($srcFilename)) - $srcFilename = $this->tplFileName; - $confCompiler = $this->config; if (is_file($srcXmlFilename)) @@ -78,6 +70,8 @@ class TemplateEngine $openCmd = array(); $depth = 0; $components = array(); + + $document = $this->parseIncludes( $document, dirname($srcXmlFilename).'/../include' ); foreach ($document as $element) { @@ -86,7 +80,7 @@ class TemplateEngine $attributes = array(); $value = ''; $tag = ''; - + // Setzt: $tag, $attributes, $value, $type extract($element); @@ -139,7 +133,7 @@ class TemplateEngine // CHMOD ausfuehren. if (! empty($confCompiler['chmod'])) if (! @chmod($filename, octdec($confCompiler['chmod']))) - throw new \InvalidArgumentException("Template {$this->tplFileName} failed to compile: CHMOD '{$confCompiler['chmod']}' failed on file {$filename}."); + throw new \InvalidArgumentException("Template {$srcXmlFilename} failed to compile: CHMOD '{$confCompiler['chmod']}' failed on file {$filename}."); } catch (\Exception $e) { @@ -162,6 +156,9 @@ class TemplateEngine */ private function loadXmlDocument( $filename ) { + if (!is_file($filename)) + throw new LogicException("File '$filename' was not found.'"); + $index = array(); $vals = array(); $p = xml_parser_create(); @@ -172,6 +169,85 @@ class TemplateEngine return $vals; } + + + /** + * Führt das gewünschte Template aus und schreibt das Ergebnis auf die Standardausgabe. + * + * In Development-Mode the template is compiled. + * + * @param $srcFile string Quelldateiname des Templates (im XML-Format) + * @param $outputData array Ausgabedaten + */ + public function executeTemplate($srcFile, $outputData) + { + // Converting filename: '/path/file.src.xml' => '/path/file.php'. + $templateFile = dirname( $srcFile ).'/'.substr( basename($srcFile),0,strpos( basename($srcFile),'.')).'.php'; + + // In development mode, we are compiling every template on the fly. + if (DEVELOPMENT) { + + // Compile the template. + // From a XML source file we are generating a PHP file. + try + { + $this->compile($srcFile, $templateFile); + unset($te); + } catch (Exception $e) { + throw new DomainException("Compilation failed for Template '$srcFile'.", 0, $e); + } + #header("X-CMS-Template-File: " . $templateFile); + } + + // Spätestens jetzt muss das Template vorhanden sein. + if (!is_file($templateFile)) + throw new LogicException("Template file '$templateFile' was not found."); + + // Übertragen der Ausgabe-Variablen in den aktuellen Kontext + // + extract($outputData); + + // Einbinden des Templates + require_once($templateFile); + + } + + + /** + * @param $document array + * @param $includePath + * @return array + */ + private function parseIncludes($document, $includePath ) + { + $newDocument = array(); + + foreach ($document as $element) { + // Initialisieren der m�glichen Element-Inhalte + $type = ''; + $attributes = array(); + $value = ''; + $tag = ''; + + // Setzt: $tag, $attributes, $value, $type + extract($element); + + if ($tag == 'include') { + if ($type == 'complete' || $type == 'open') { + + $includeDocument = $this->loadDocument($includePath . '/' . $attributes['file'] . '.inc.xml'); + $newDocument = array_merge($newDocument,$includeDocument); + + } + } + else + { + $newDocument[] = $element; + } + } + + return $newDocument; + } } ?> \ No newline at end of file