commit 239b0b744e601cd2fbcb683d0949d64ae99d3324
parent 63308fcb12f88b397ea138371202fc9c5ce700ad
Author: Jan Dankert <develop@jandankert.de>
Date: Sat, 16 Nov 2019 01:35:11 +0100
New: MacroRunner for execution of macros.
Diffstat:
5 files changed, 118 insertions(+), 96 deletions(-)
diff --git a/modules/cms-core/model/Value.class.php b/modules/cms-core/model/Value.class.php
@@ -2,6 +2,7 @@
namespace cms\model;
use ArrayUtils;
use cms\publish\Publish;
+use MacroRunner;
use \ObjectNotFoundException;
use \Logger;
use \Text;
@@ -1278,104 +1279,18 @@ SQL
break;
$this->page->load();
- $className = $this->element->subtype;
- $fileName = OR_DYNAMICCLASSES_DIR.$className.'.class.php';
- if ( is_file( $fileName ) )
- {
- // Fuer den Fall, dass ein Makro mehrmals pro Vorlage auftritt
- if ( !class_exists($className) )
- require( $fileName );
-
- if ( class_exists($className) )
- {
- /** @var \Macro $macro */
- $macro = new $className;
-
- if ( method_exists( $macro,'execute' ) )
- {
- //$$macro->delOutput();
- $macro->setContextPage( $this->page );
-
- $parameters = $this->element->getDynamicParameters();
-
- array_walk_recursive($parameters, function (&$item, $key) {
- $item = \Text::resolveVariables($item, 'setting', function ($var) {
- return ArrayUtils::getSubValue($this->page->getSettings(),explode('.',$var) );
- });
- return $item;
- });
-
- foreach( $parameters as $param_name=>$param_value )
- {
- if ( $param_value && $param_value[0]=='{')
- {
- // TODO: Why this? Better use the VariableResolver for this.
- $elName = substr($param_value,1,strpos($param_value,'}')-1);
- $template = new Template($this->page->templateid);
- $elements = $template->getElementNames();
- $elementid = array_search($elName,$elements);
-
- $value = new Value();
- $value->elementid = $elementid;
- $value->element = new Element( $elementid );
- $value->element->load();
- $value->pageid = $this->page->pageid;
- $value->languageid = $this->page->languageid;
- $value->load();
-
- $param_value = $value->getRawValue();
- }
- if ( isset( $macro->$param_name ) )
- {
- Logger::trace("Setting parameter for Macro-class $className, ".$param_name.':'.$param_value );
-
- // Die Parameter der Makro-Klasse typisiert setzen.
- if ( is_int($macro->$param_name) )
- $macro->$param_name = intval($param_value);
- elseif ( is_array($macro->$param_name) )
- $macro->$param_name = (array)$param_value;
- else
- $macro->$param_name = $param_value;
-
- }
- else
- {
- if ( !$this->publisher->isPublic() )
- $inhalt .= "*WARNING*: Unknown parameter $param_name in macro $className\n";
- }
- }
-
- ob_start();
+ $macroName = $this->element->subtype;
+ $macroSettings = $this->element->getDynamicParameters();
- $macro->execute();
-
- $output = ob_get_contents();
- ob_end_clean();
- $inhalt .= $output;
- }
- else
- {
- Logger::warn('element:'.$this->element->name.', '.
- 'class:'.$className.', no method: execute()');
- if ( !$this->publisher->isPublic() )
- $inhalt = lang('ERROR_IN_ELEMENT').' (missing method: execute())';
- }
- }
- else
- {
- Logger::warn('element:'.$this->element->name.', '.
- 'class not found:'.$className);
- if ( !$this->publisher->isPublic() )
- $inhalt = lang('ERROR_IN_ELEMENT').' (class not found:'.$className.')';
- }
+ $runner = new MacroRunner();
+ try {
+ $inhalt .= $runner->executeMacro($macroName, $macroSettings,$this->page);
}
- else
- {
- Logger::warn('element:'.$this->element->name.', '.
- 'file not found:'.$fileName);
+ catch( \OpenRatException $e ) {
if ( !$this->publisher->isPublic() )
- $inhalt = lang('ERROR_IN_ELEMENT').' (file not found:'.$fileName.')';
-
+ $inhalt = lang($e->key).' ('.$e->getMessage().')'; // Inform the viewer
+ else
+ ; // Keep empty value in public mode.
}
// Wenn HTML-Ausgabe, dann Sonderzeichen in HTML �bersetzen
diff --git a/modules/cms-core/require.php b/modules/cms-core/require.php
@@ -1,6 +1,7 @@
<?php
// Require other modules
+require_once(__DIR__ . '/../cms-macros/require.php');
require_once(__DIR__ . '/../cms-publish/require.php');
require_once(__DIR__ . '/../database/require.php');
require_once(__DIR__ . '/../database-update/require.php');
diff --git a/modules/cms-macros/MacroRunner.class.php b/modules/cms-macros/MacroRunner.class.php
@@ -0,0 +1,97 @@
+<?php
+
+
+use cms\model\Element;
+use cms\model\Template;
+use cms\model\Value;
+
+class MacroRunner
+{
+ public $page;
+
+ public function executeMacro($name, $parameter, $page)
+ {
+ $this->page = $page;
+
+ $className = $name;
+ $output = '';
+
+ $fileName = OR_DYNAMICCLASSES_DIR . $name . '.class.php';
+ if (!is_file($fileName))
+ throw new OpenRatException('ERROR_IN_ELEMENT','file not found:'.$fileName);
+
+ require_once( $fileName );
+
+ if (!class_exists($className))
+ throw new OpenRatException('ERROR_IN_ELEMENT', 'class not found:' . $className);
+
+
+ /** @var \Macro $macro */
+ $macro = new $className;
+
+ if (!method_exists($macro, 'execute'))
+ throw new OpenRatException('ERROR_IN_ELEMENT',' (missing method: execute())');
+
+ $macro->setContextPage($page);
+
+ $parameters = $parameter;
+
+ array_walk_recursive($parameters, function (&$item, $key) {
+
+ $item = \Text::resolveVariables($item, 'setting', function ($var) {
+ return ArrayUtils::getSubValue($this->page->getSettings(), explode('.', $var));
+ });
+
+ $item = \Text::resolveVariables($item, 'element', function ($var) {
+ $template = new Template($this->page->templateid);
+ $elements = $template->getElementNames();
+ $elementid = array_search($var, $elements);
+
+ $value = new Value();
+ $value->publisher = $this->page->publisher;
+ $value->elementid = $elementid;
+ $value->element = new Element($elementid);
+ $value->element->load();
+ $value->pageid = $this->page->pageid;
+ $value->languageid = $this->page->languageid;
+ $value->load();
+
+ return $value->getRawValue();
+
+ });
+
+ return $item;
+ });
+
+ foreach ($parameters as $param_name => $param_value) {
+
+ if (! property_exists($macro, $param_name)) {
+
+ if (!$this->page->publisher->isPublic())
+ $output .= "*WARNING*: Unknown parameter $param_name in macro $className\n";
+ continue;
+ }
+
+ Logger::trace("Setting parameter for Macro-class $className, " . $param_name . ':' . print_r($param_value,true));
+
+ // Die Parameter der Makro-Klasse typisiert setzen.
+ if (is_int($macro->$param_name))
+ $macro->$param_name = intval($param_value);
+ elseif (is_array($macro->$param_name))
+ $macro->$param_name = (array)$param_value;
+ else
+ $macro->$param_name = $param_value;
+
+ }
+
+ ob_start();
+
+ $macro->execute();
+
+ $output .= ob_get_contents();
+
+ ob_end_clean();
+
+ return $output;
+ }
+}+
\ No newline at end of file
diff --git a/modules/cms-macros/require.php b/modules/cms-macros/require.php
@@ -0,0 +1,3 @@
+<?php
+
+require_once(__DIR__ . '/MacroRunner.class.php');
diff --git a/modules/cms-publish/PublishPreview.class.php b/modules/cms-publish/PublishPreview.class.php
@@ -62,6 +62,8 @@ class PublishPreview extends Publish
case BaseObject::TYPEID_URL:
$inhalt = \Html::url('url','show',$link->linkedObjectId,$param);
break;
+ default:
+ $inhalt = 'Unknown link type: '.$linkedObject->typeid;
}
break;
@@ -71,7 +73,10 @@ class PublishPreview extends Publish
$inhalt = $url->url;
break;
- }
+ default:
+ $inhalt = 'Unknown type: '.$to->typeid;
+
+ }
return $inhalt;