openrat-cms

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

commit a3bd9396810cc123dff7a7f3b3d019e2c3a8156f
parent 7dc82441b11860ae12cc248c0e0a484cc2e08440
Author: Jan Dankert <devnull@localhost>
Date:   Fri, 10 Nov 2017 22:39:59 +0100

Neu: Komponenten-Klasse statt dem bisherigen Gefrickel. Erstmal nur für die Komponente 'button' umgesetzt.

Diffstat:
themes/default/include/html/Component.class.php | 19+++++++++++++++++++
themes/default/include/html/button/Button.class.php | 38++++++++++++++++++++++++++++++++++++++
util/TemplateEngine.class.php | 234++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
3 files changed, 187 insertions(+), 104 deletions(-)

diff --git a/themes/default/include/html/Component.class.php b/themes/default/include/html/Component.class.php @@ -0,0 +1,18 @@ +<?php + +abstract class Component +{ + public function begin($attr) + { + + } + + + public 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 @@ -0,0 +1,37 @@ +<?php + +class ButtonComponent extends Component { + +public function begin( $attr ) { + + extract( $attr,EXTR_PREFIX_ALL,'attr'); + echo <<<'HTML' + <div class="invisible"> +HTML; + + if ($attr_type == 'ok') + $attr_type = 'submit'; + + if (!empty($attr_src)) + { + $attr_type = 'image'; + $attr_tmp_src = $image_dir.'icon_'.$attr_src.IMG_ICON_EXT; + } + else + { + $attr_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 } + + } + + public function end() { + echo"</div>"; + } +} + +?>+ \ No newline at end of file diff --git a/util/TemplateEngine.class.php b/util/TemplateEngine.class.php @@ -50,6 +50,8 @@ class TemplateEngine */ public function compile( $tplName = '') { + require_once( OR_THEMES_DIR."default/include/html/Component.class.".PHP_EXT ); + try { if ( empty($tplName) ) $tplName = $this->tplName; @@ -107,14 +109,14 @@ class TemplateEngine $attributes = $this->checkAttributes($tag,$attributes); if ( $type == 'open' ) - $this->copyFileContents( $tag.'/'.$tag.'-begin',$outFile,$attributes,++$depth ); + $this->copyFileContents( $tag, true, $tag.'/'.$tag.'-begin',$outFile,$attributes,++$depth ); elseif ( $type == 'complete' ) { - $this->copyFileContents( $tag.'/'.$tag.'-begin',$outFile,$attributes,++$depth ); - $this->copyFileContents( $tag.'/'.$tag.'-end' ,$outFile,array() , $depth-- ); + $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.'/'.$tag.'-end' ,$outFile,array(),$depth-- ); + $this->copyFileContents( $tag, false, $tag.'/'.$tag.'-end' ,$outFile,array(),$depth-- ); } fclose($outFile); @@ -209,116 +211,140 @@ class TemplateEngine /** - * Ein Baustein wird in die neue Vorlagedatei kopiert. + * Eine Komponente wird in die neue Vorlagedatei kopiert. */ - private function copyFileContents( $infile,$outFileHandler,$attr,$depth ) + private function copyFileContents( $tag, $begin, $infile,$outFileHandler,$attr,$depth ) { global $conf; $hash = $depth; - $inFileName = OR_THEMES_DIR.$conf['interface']['theme'].'/include/html/'.$infile.'.inc.'.PHP_EXT; - if ( !is_file($inFileName) ) - if ( count($attr)==0 ) - return; - else - // Baustein nicht vorhanden, Abbbruch. - - throw new LogicException("Compile failed, file not found: $inFileName" ); - - if ( DEVELOPMENT ) - fwrite( $outFileHandler,"<!-- Compiling $infile @ ".date('r')." -->" ); + $className = ucfirst($tag); + $classFilename = OR_THEMES_DIR.$conf['interface']['theme']."/include/html/$tag/$className.class.".PHP_EXT; - $values = array(); - foreach( $attr as $attrName=>$attrValue ) + if ( is_file($classFilename)) { - $values[] = "'".$attrName."'=>".$this->attributeValue($attrValue); - } - - // Variablen $attr_* setzen - if ( count($attr) > 0 ) - { - fwrite( $outFileHandler,'<?php '); - foreach( $attr as $attrName=>$attrValue ) - fwrite( $outFileHandler,'$a'.$hash.'_'.$attrName."=".$this->attributeValue($attrValue).';'); - fwrite( $outFileHandler,' ?>'); + 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; } - - $file = file( $inFileName ); - $ignore = false; - $linebreaks = true; - - foreach( $file as $line ) + else { - // Ignoriere Zeilen, die fuer ein nicht vorhandenes Attribut gelten. - if ( strpos($line,'#IF-ATTR')!==FALSE ) - { - $found = false; - foreach( $attr as $attrName=>$attrValue ) - { - if ( strpos($line,'#IF-ATTR '.$attrName.'#')!==FALSE ) - $found = true; - if ( strpos($line,'#IF-ATTR-VALUE '.$attrName.':'.$attrValue.'#')!==FALSE ) - $found = true; - } - if ( ! $found ) - $ignore = true; - } - // Nach einem IF-Block ertmal alles wieder anzeigen. - if ( strpos($line,'#END-IF')!==FALSE ) - { - $ignore = false; - } - - if ( strpos($line,'#ELSE')!==FALSE ) - { - $ignore = !$ignore; - } - - // Zeilenumbr�che nicht setzen. - if ( strpos($line,'#SET-LINEBREAK-OFF')!==FALSE ) - $linebreaks = false; - - // Zeilenumbr�che setzen. - if ( strpos($line,'#SET-LINEBREAK-ON')!==FALSE ) - $linebreaks = true; - - // Ignoriere Zeilen, die zu ignorieren sind (logisch). - // Dies sind Blöcke, die nur fuer ein Attribut gueltig sind, welches - // aber nicht gesetzt ist. - if ( $ignore ) - continue; - - // Ignoriere Leerzeilen - if ( strlen(trim($line)) == 0) - continue; - // Ignoriere Kommentarzeilen - if ( in_array(substr(ltrim($line),0,2),array('//','/*','<!') ) || substr(ltrim($line),0,1) == '#') - continue; - // Ignoriere Kommentarzeilen - if ( in_array(substr(rtrim($line),-3),array('-->',' */') ) ) - continue; - - if ( !$linebreaks ) - $line = rtrim($line); - - // Die Variablen "$attr_*" muessen pro Ebene eindeutig sein, daher wird an den - // Variablennamen die Tiefe angehangen. - $line = str_replace('$attr_','$a'.$hash.'_',$line); - - foreach( $attr as $attrName=>$attrValue ) - $line = str_replace('%'.$attrName.'%',$this->attributeValueOpenPHP($attrValue),$line); - - - fwrite( $outFileHandler,$line ); + + $inFileName = OR_THEMES_DIR.$conf['interface']['theme'].'/include/html/'.$infile.'.inc.'.PHP_EXT; + if ( !is_file($inFileName) ) + if ( count($attr)==0 ) + return; + else + // Baustein nicht vorhanden, Abbbruch. + throw new LogicException("Compile failed, Component $infile not found." ); + + if ( DEVELOPMENT ) + fwrite( $outFileHandler,"<!-- Compiling $infile @ ".date('r')." -->" ); + + $values = array(); + foreach( $attr as $attrName=>$attrValue ) + { + $values[] = "'".$attrName."'=>".$this->attributeValue($attrValue); + } + + // Variablen $attr_* setzen + if ( count($attr) > 0 ) + { + fwrite( $outFileHandler,'<?php '); + foreach( $attr as $attrName=>$attrValue ) + fwrite( $outFileHandler,'$a'.$hash.'_'.$attrName."=".$this->attributeValue($attrValue).';'); + fwrite( $outFileHandler,' ?>'); + } + + $file = file( $inFileName ); + $ignore = false; + $linebreaks = true; + + foreach( $file as $line ) + { + // Ignoriere Zeilen, die fuer ein nicht vorhandenes Attribut gelten. + if ( strpos($line,'#IF-ATTR')!==FALSE ) + { + $found = false; + foreach( $attr as $attrName=>$attrValue ) + { + if ( strpos($line,'#IF-ATTR '.$attrName.'#')!==FALSE ) + $found = true; + if ( strpos($line,'#IF-ATTR-VALUE '.$attrName.':'.$attrValue.'#')!==FALSE ) + $found = true; + } + if ( ! $found ) + $ignore = true; + } + // Nach einem IF-Block ertmal alles wieder anzeigen. + if ( strpos($line,'#END-IF')!==FALSE ) + { + $ignore = false; + } + + if ( strpos($line,'#ELSE')!==FALSE ) + { + $ignore = !$ignore; + } + + // Zeilenumbr�che nicht setzen. + if ( strpos($line,'#SET-LINEBREAK-OFF')!==FALSE ) + $linebreaks = false; + + // Zeilenumbr�che setzen. + if ( strpos($line,'#SET-LINEBREAK-ON')!==FALSE ) + $linebreaks = true; + + // Ignoriere Zeilen, die zu ignorieren sind (logisch). + // Dies sind Blöcke, die nur fuer ein Attribut gueltig sind, welches + // aber nicht gesetzt ist. + if ( $ignore ) + continue; + + // Ignoriere Leerzeilen + if ( strlen(trim($line)) == 0) + continue; + // Ignoriere Kommentarzeilen + if ( in_array(substr(ltrim($line),0,2),array('//','/*','<!') ) || substr(ltrim($line),0,1) == '#') + continue; + // Ignoriere Kommentarzeilen + if ( in_array(substr(rtrim($line),-3),array('-->',' */') ) ) + continue; + + if ( !$linebreaks ) + $line = rtrim($line); + + // Die Variablen "$attr_*" muessen pro Ebene eindeutig sein, daher wird an den + // Variablennamen die Tiefe angehangen. + $line = str_replace('$attr_','$a'.$hash.'_',$line); + + foreach( $attr as $attrName=>$attrValue ) + $line = str_replace('%'.$attrName.'%',$this->attributeValueOpenPHP($attrValue),$line); + + + fwrite( $outFileHandler,$line ); + } + + // Variablen $attr_* entfernen. + $unset_attr = array(); + foreach( $attr as $attrName=>$attrValue ) + $unset_attr[] = '$a'.$hash.'_'.$attrName; + + if ( count($unset_attr) > 0 ) + fwrite( $outFileHandler,'<?php unset('.implode(',',$unset_attr).') ?>'); } - - // Variablen $attr_* entfernen. - $unset_attr = array(); - foreach( $attr as $attrName=>$attrValue ) - $unset_attr[] = '$a'.$hash.'_'.$attrName; - - if ( count($unset_attr) > 0 ) - fwrite( $outFileHandler,'<?php unset('.implode(',',$unset_attr).') ?>'); }