openrat-cms

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

commit aeb38612d67b48752e61b5cb7c86da4994944dd3
parent 3fab03148ef812c59345f8d570dffd475e4f2175
Author: Jan Dankert <devnull@localhost>
Date:   Fri, 17 Nov 2017 22:54:04 +0100

Refactoring der Komponenten-Klassen: Komponenten-Attribute auch als Klassenattribute vorhalten und das Komponenten-Objekt pro Tiefe merken, damit begin() und end() auf der gleichen Instanz aufgerufen werden.

Diffstat:
dispatcher.php | 4++++
themes/default/include/html/Component.class.php | 44++++++++++++++++++++++++++++++++------------
themes/default/include/html/button/Button.class.php | 53+++++++++++++++++++++++++++++++++--------------------
themes/default/include/html/form/Form.class.php | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
util/TemplateEngine.class.php | 223++++++++++++++++++++++++++++++++++++++++++-------------------------------------
5 files changed, 280 insertions(+), 137 deletions(-)

diff --git a/dispatcher.php b/dispatcher.php @@ -175,6 +175,10 @@ try $do->actionName = $action; $do->subActionName = $subaction; + define('OR_ACTION',$action); + define('OR_METHOD',$subaction); + define('OR_ID' ,$REQ[REQ_PARAM_ID]); + $do->init(); diff --git a/themes/default/include/html/Component.class.php b/themes/default/include/html/Component.class.php @@ -1,18 +1,38 @@ <?php -abstract class Component +abstract class Component { - public function begin($attr) - { - - } - - - public function end() - { - - } -} + /** + * Gets the beginning of this component. + * @return string + */ + public function getBegin() + { + ob_start(); + $this->begin(); + $src = ob_get_contents(); + ob_end_clean(); + return $src; + } + + public function getEnd() + { + ob_start(); + $this->end(); + $src = ob_get_contents(); + ob_end_clean(); + return $src; + } + + /** + * Outputs the beginning of this component. + */ + protected function begin() + {} + + protected function end() + {} +} ?> \ No newline at end of file diff --git a/themes/default/include/html/button/Button.class.php b/themes/default/include/html/button/Button.class.php @@ -1,36 +1,49 @@ <?php -class ButtonComponent extends Component { +class ButtonComponent extends Component +{ -public function begin( $attr ) { + public $type; - extract( $attr,EXTR_PREFIX_ALL,'attr'); - echo <<<'HTML' - <div class="invisible"> -HTML; + public $src; - if ($attr_type == 'ok') - $attr_type = 'submit'; + private $tmp_src; - if (!empty($attr_src)) + protected function begin() { - $attr_type = 'image'; - $attr_tmp_src = $image_dir.'icon_'.$attr_src.IMG_ICON_EXT; + echo <<<'HTML' + <div class="invisible"> +HTML; + + if ($this->type == 'ok') + $this->type = 'submit'; + + if (! empty($this->src)) + { + $this->type = 'image'; + $this->tmp_src = $image_dir . 'icon_' . $this->src . IMG_ICON_EXT; } else { - $attr_tmp_src = ''; + $this->tmp_src = ''; } - if ( !empty($attr_type)) { - ?> - <input type="<?php echo $attr_type ?>"<?php if(isset($attr_src)) { ?> src="<?php $attr_tmp_src ?>"<?php } ?> name="<?php echo $attr_value ?>" class="%class%" title="<?php echo lang($attr_text.'_DESC') ?>" value="&nbsp;&nbsp;&nbsp;&nbsp;<?php echo langHtml($attr_text) ?>&nbsp;&nbsp;&nbsp;&nbsp;" /><?php unset($attr_src); ?> - <?php } - - } + if (! empty($this->type)) + { + ?> +<input type="<?php echo $this->type ?>" <?php if(isset($this->src)) { ?> + src="<?php $this->tmp_src ?>" <?php } ?> + name="<?php echo $this->value ?>" class="%class%" + title="<?php echo lang($this->text.'_DESC') ?>" + value="&nbsp;&nbsp;&nbsp;&nbsp;<?php echo langHtml($this->text) ?>&nbsp;&nbsp;&nbsp;&nbsp;" /><?php unset($this->src); ?> + <?php + +} + } - public function end() { - echo"</div>"; + protected function end() + { + echo "</div>"; } } diff --git a/themes/default/include/html/form/Form.class.php b/themes/default/include/html/form/Form.class.php @@ -0,0 +1,92 @@ +<?php + +class FormComponent extends Component +{ + + public $method = 'POST'; + public $name = ''; + public $action = '<?php echo OR_ACTION ?>'; + public $subaction = '<?php echo OR_METHOD ?>'; + public $id = '<?php echo OR_ID ?>'; + public $label; + public $cancel = false; + public $visible = false; + public $target = '_self'; + public $enctype = 'application/x-www-form-urlencoded'; + public $async = false; + public $autosave = false; + + private $submitFunction = 'formSubmit( $(this) ); return false;'; + + + + protected function begin() + { + if ( empty($this->label) ) + $this->label = lang('BUTTON_OK'); + + if ( empty($this->id) ) + $this->id = '<?php echo OR_ID ?>'; + + if ( $this->type == 'upload') + $this->submitFunction = ''; + + echo <<<HTML +<form name="{$this->name}" + target="{$this->target}" + action="{$this->action}" + data-method="{$this->subaction}" + data-action="{$this->action}" + data-id="{$this->id}" + method="{$this->subaction}" + enctype="{$this->enctype}" + class="{$this->action}" + data-async="{$this->async}" + data-autosave="{$this->autosave}" + onSubmit="{$this->submitFunction}"><input type="submit" class="invisible" /> + +<input type="hidden" name="<?php echo REQ_PARAM_TOKEN ?>" value="<?php echo token() ?>" /> +<input type="hidden" name="<?php echo REQ_PARAM_ACTION ?>" value="{$this->action}" /> +<input type="hidden" name="<?php echo REQ_PARAM_SUBACTION ?>" value="{$this->subaction}" /> +<input type="hidden" name="<?php echo REQ_PARAM_ID ?>" value="{$this->id}" /> + +HTML; + echo <<<'HTML' +<?php + if ( $conf['interface']['url_sessionid'] ) + echo '<input type="hidden" name="'.session_name().'" value="'.session_id().'" />'."\n"; +?> + +HTML; + } + + + + + protected function end() + { + echo <<<HTML + +<div class="bottom"> + <div class="command {$this->visible}"> + + <input type="button" class="submit ok" value="{$this->label}" onclick="$(this).closest('div.sheet').find('form').submit(); " /> + + <!-- Cancel-Button nicht anzeigen, wenn cancel==false. --> +HTML; + if ($this->cancel) { + echo <<<HTML + <input type="button" class="submit cancel" value="{lang('CANCEL')}" onclick="$('div#dialog').hide(); $('div#filler').fadeOut(500); $(this).closest('div.panel').find('ul.views > li.active').click();" /> +HTML; + } + echo <<<HTML + </div> +</div> + +</form> + +HTML; + } +} + +?>+ \ No newline at end of file diff --git a/util/TemplateEngine.class.php b/util/TemplateEngine.class.php @@ -43,95 +43,130 @@ class TemplateEngine $this->tplName = $tplName; } - /** * Wandelt eine Vorlage um - * @param filename Dateiname der Datei, die erstellt werden soll. + * + * @param + * filename Dateiname der Datei, die erstellt werden soll. */ - public function compile( $tplName = '') + public function compile($tplName = '') { - require_once( OR_THEMES_DIR."default/include/html/Component.class.".PHP_EXT ); - - try { - if ( empty($tplName) ) - $tplName = $this->tplName; - - global $conf; - $confCompiler = $conf['theme']['compiler']; - - $srcXmlFilename = 'themes/default/templates/'.$tplName.'.tpl.src.xml'; - - - if ( is_file($srcXmlFilename) ) - $srcFilename = $srcXmlFilename; - else - // Wenn Vorlage (noch) nicht existiert - throw new LogicException( "Template not found: $tplName" ); - - $filename = FileUtils::getTempDir().'/'.'or.cache.tpl.'.str_replace('/', '.',$tplName).'.tpl.'.PHP_EXT; - - // Wenn Vorlage gaendert wurde, dann Umwandlung erneut ausf�hren. - if ( $confCompiler['cache'] && is_file($filename) && filemtime($srcFilename) <= filemtime($filename)) - return; - - if ( is_file($filename) && !is_writable($filename) ) - throw new LogicException("File is read-only: $filename"); - - Logger::debug("Compile template: ".$srcFilename.' to '.$filename); - - // Vorlage und Zieldatei oeffnen - $document = $this->loadDocument( $srcFilename ); - - // Wir legen erstmal eine temporaere Datei an. - // Falls ein Fehler auftritt, ist nur die temporaere Datei defekt. - $tmpFilename = $filename.'.tmp'; - $outFile = @fopen($tmpFilename,'w'); - - if ( !is_resource($outFile) ) - throw new LogicException( "Template $tplName: Unable to open file for writing: $filename" ); - - $openCmd = array(); - $depth = 0; - - foreach( $document as $line ) - { - // Initialisieren der m�glichen Element-Inhalte - $type = ''; - $attributes = array(); - $value = ''; - $tag = ''; - - - // Setzt: $tag, $attributes, $value, $type - extract( $line ); - - if ($type == 'complete' || $type == 'open') - $attributes = $this->checkAttributes($tag,$attributes); - - if ( $type == 'open' ) - $this->copyFileContents( $tag, true, $tag.'/'.$tag.'-begin',$outFile,$attributes,++$depth ); - elseif ( $type == 'complete' ) - { - $this->copyFileContents( $tag, true, $tag.'/'.$tag.'-begin',$outFile,$attributes,++$depth ); - $this->copyFileContents( $tag, false, $tag.'/'.$tag.'-end' ,$outFile,array() , $depth-- ); - } - elseif ( $type == 'close' ) - $this->copyFileContents( $tag, false, $tag.'/'.$tag.'-end' ,$outFile,array(),$depth-- ); - } - - fclose($outFile); - - rename($tmpFilename,$filename); - - // CHMOD ausfuehren. - if ( !empty($confCompiler['chmod'])) - if ( !@chmod($filename,octdec($confCompiler['chmod'])) ) - throw new InvalidArgumentException( "Template {$this->tplName} failed to compile: CHMOD '{$confCompiler['chmod']}' failed on file {$filename}." ); - + require_once (OR_THEMES_DIR . "default/include/html/Component.class." . PHP_EXT); + + try + { + if (empty($tplName)) + $tplName = $this->tplName; + + global $conf; + $confCompiler = $conf['theme']['compiler']; + + $srcXmlFilename = 'themes/default/templates/' . $tplName . '.tpl.src.xml'; + + if (is_file($srcXmlFilename)) + $srcFilename = $srcXmlFilename; + else + // Wenn Vorlage (noch) nicht existiert + throw new LogicException("Template not found: $tplName"); + + $filename = FileUtils::getTempDir() . '/' . 'or.cache.tpl.' . str_replace('/', '.', $tplName) . '.tpl.' . PHP_EXT; + + // Wenn Vorlage gaendert wurde, dann Umwandlung erneut ausf�hren. + if ($confCompiler['cache'] && is_file($filename) && filemtime($srcFilename) <= filemtime($filename)) + return; + + if (is_file($filename) && ! is_writable($filename)) + throw new LogicException("File is read-only: $filename"); + + Logger::debug("Compile template: " . $srcFilename . ' to ' . $filename); + + // Vorlage und Zieldatei oeffnen + $document = $this->loadDocument($srcFilename); + + // Wir legen erstmal eine temporaere Datei an. + // Falls ein Fehler auftritt, ist nur die temporaere Datei defekt. + $tmpFilename = $filename . '.tmp'; + $outFile = @fopen($tmpFilename, 'w'); + + if (! is_resource($outFile)) + throw new LogicException("Template $tplName: Unable to open file for writing: $filename"); + + $openCmd = array(); + $depth = 0; + $components = array(); + + foreach ($document as $line) + { + // Initialisieren der m�glichen Element-Inhalte + $type = ''; + $attributes = array(); + $value = ''; + $tag = ''; + + // Setzt: $tag, $attributes, $value, $type + extract($line); + + if ($type == 'complete' || $type == 'open') + $attributes = $this->checkAttributes($tag, $attributes); + + if ($type == 'open' || $type == 'complete') + { + $depth ++; + + $className = ucfirst($tag); + $classFilename = OR_THEMES_DIR . $conf['interface']['theme'] . "/include/html/$tag/$className.class." . PHP_EXT; + + if (is_file($classFilename)) + { + require_once ($classFilename); + + $className .= 'Component'; + $component = new $className(); + + foreach ($attributes as $prop => $value) + { + $component->$prop = $value; + } + // $component->depth = $depth; + + $components[$depth] = $component; + fwrite($outFile, $component->getBegin()); + } + else + { + $this->copyFileContents($tag, true, $tag . '/' . $tag . '-begin', $outFile, $attributes, $depth); + } + } + + if ($type == 'close' || $type == 'complete') + { + if (isset($components[$depth])) + { + $component = $components[$depth]; + fwrite($outFile, $component->getEnd()); + unset($components[$depth]); + } + else + { + $this->copyFileContents($tag, false, $tag . '/' . $tag . '-end', $outFile, array(), $depth); + } + + $depth --; + } + } + + fclose($outFile); + + rename($tmpFilename, $filename); + + // CHMOD ausfuehren. + if (! empty($confCompiler['chmod'])) + if (! @chmod($filename, octdec($confCompiler['chmod']))) + throw new InvalidArgumentException("Template {$this->tplName} failed to compile: CHMOD '{$confCompiler['chmod']}' failed on file {$filename}."); } - catch( Exception $e) + catch (Exception $e) { - throw new LogicException("Template $tplName failed to compile", 0, $e); + throw new LogicException("Template $tplName failed to compile", 0, $e); } } @@ -221,27 +256,6 @@ class TemplateEngine $className = ucfirst($tag); $classFilename = OR_THEMES_DIR.$conf['interface']['theme']."/include/html/$tag/$className.class.".PHP_EXT; - if ( is_file($classFilename)) - { - require_once( $classFilename); - - $className .= 'Component'; - $component = new $className(); - ob_start(); - if ( $begin ) - $component->begin($attr); - else - $component->end(); - - $src = ob_get_contents(); - ob_end_clean(); - fwrite( $outFileHandler,"\n<!-- Component $tag ".($begin?'beginn':'ennd')." -->\n".$src."\n<!-- End -->\n" ); - - return; - } - else - { - $inFileName = OR_THEMES_DIR.$conf['interface']['theme'].'/include/html/'.$infile.'.inc.'.PHP_EXT; if ( !is_file($inFileName) ) if ( count($attr)==0 ) @@ -344,7 +358,6 @@ class TemplateEngine if ( count($unset_attr) > 0 ) fwrite( $outFileHandler,'<?php unset('.implode(',',$unset_attr).') ?>'); - } }