From 1021cd030d57d1b50a4fa57694b7f96339e3eafd Mon Sep 17 00:00:00 2001
From: Roland Haeder <roland@mxchange.org>
Date: Sun, 5 Apr 2015 22:06:38 +0200
Subject: [PATCH] Added very basic support (unfinished) for menu rendering
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Signed-off-by: Roland Häder <roland@mxchange.org>
---
 .../interfaces/menu/class_RenderableMenu.php  |   2 +-
 inc/classes/main/menu/class_BaseMenu.php      |  19 +--
 .../template/class_BaseTemplateEngine.php     |   2 +-
 .../menu/class_MenuTemplateEngine.php         | 114 ++++++++++++++++++
 4 files changed, 128 insertions(+), 9 deletions(-)

diff --git a/inc/classes/interfaces/menu/class_RenderableMenu.php b/inc/classes/interfaces/menu/class_RenderableMenu.php
index bf0a3aba..8b0281c2 100644
--- a/inc/classes/interfaces/menu/class_RenderableMenu.php
+++ b/inc/classes/interfaces/menu/class_RenderableMenu.php
@@ -31,7 +31,7 @@ interface RenderableMenu extends FrameworkInterface {
 	function renderMenu ();
 
 	/**
-	 * Transfers the rendered menu to a given templatex engine by assigning
+	 * Transfers the rendered menu to a given template engine by assigning
 	 * the rendered content with a template variable.
 	 *
 	 * @2param	$templateInstance	An instance of a CompileableTemplate class
diff --git a/inc/classes/main/menu/class_BaseMenu.php b/inc/classes/main/menu/class_BaseMenu.php
index 38d7157e..fa32207b 100644
--- a/inc/classes/main/menu/class_BaseMenu.php
+++ b/inc/classes/main/menu/class_BaseMenu.php
@@ -52,29 +52,34 @@ class BaseMenu extends BaseFrameworkSystem {
 		// Get the 'page' from request instance
 		$page = $this->getApplicationInstance()->getRequestInstance()->getRequestElement('page');
 
+		// If page is empty, choose default
+		if (empty($page)) {
+			// Use default page as none has been specified
+			$page = $this->getConfigInstance()->getConfigEntry('default_' . $this->getApplicationInstance()->getAppShortName() . '_' . self::getResponseTypeFromSystem() . '_command');
+		} // END - if
+
 		// Load the menu template for this page
 		try {
 			$templateInstance->loadMenuTemplate($page . '_menu_entries');
 		} catch (FileIoException $e) {
-			// @TODO Should we log it here? We should, because it will be silently ignored.
+			// Log exception @TODO Maybe to intrusive?
+			self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Exception caught: ' . $e->__toString() . ', with message: ' . $e->getMessage());
 		}
 
-		// Get the prepared content
-		$menuContent = $templateInstance->getRawTemplateData();
-
-		// Render it here
-		$templateInstance->renderXmlContent($menuContent);
+		// Render template data here
+		$templateInstance->renderXmlContent();
 		//exit(__METHOD__ . ':!OK');
 	}
 
 	/**
-	 * Transfers the rendered menu to a given templatex engine by assigning
+	 * Transfers the rendered menu to a given template engine by assigning
 	 * the rendered content with a template variable.
 	 *
 	 * @param	$templateInstance	An instance of a CompileableTemplate class
 	 * @return	void
 	 */
 	public function transferContentToTemplateEngine (CompileableTemplate $templateInstance) {
+		$this->debugInstance();
 		$this->partialStub('templateInstance=' . $templateInstance->__toString());
 	}
 }
diff --git a/inc/classes/main/template/class_BaseTemplateEngine.php b/inc/classes/main/template/class_BaseTemplateEngine.php
index 11e3acc6..4f896266 100644
--- a/inc/classes/main/template/class_BaseTemplateEngine.php
+++ b/inc/classes/main/template/class_BaseTemplateEngine.php
@@ -1550,7 +1550,7 @@ class BaseTemplateEngine extends BaseFrameworkSystem {
 		// Get a XmlParser instance
 		$parserInstance = ObjectFactory::createObjectByConfiguredName('xml_parser_class', array($this));
 
-		// Check if we have XML compacting enabled
+		// Check if XML compacting is enabled
 		if ($this->isXmlCompactingEnabled()) {
 			// Yes, so get a decorator class for transparent compacting
 			$parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', array($parserInstance));
diff --git a/inc/classes/main/template/menu/class_MenuTemplateEngine.php b/inc/classes/main/template/menu/class_MenuTemplateEngine.php
index 4f1dd50d..e11548fb 100644
--- a/inc/classes/main/template/menu/class_MenuTemplateEngine.php
+++ b/inc/classes/main/template/menu/class_MenuTemplateEngine.php
@@ -57,6 +57,44 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
 		'anchor-href',
 	);
 
+	/**
+	 * Variables for a menu entry
+	 */
+	private $menuEntryVariables = array(
+		// List entry
+		'entry_id',
+		// Anchor
+		'anchor-id',
+		'anchor-text',
+		'anchor-title',
+		'anchor-href',
+	);
+
+	/**
+	 * Variables for a menu block
+	 */
+	private $menuBlockVariables = array(
+		// Title
+		'title_id',
+		'title_class',
+		'title_text',
+		// Content is taken from menuEntries
+		// Footer
+		'footer_id',
+		'footer_class',
+		'footer_text',
+	);
+
+	/**
+	 * Rendered menu entries
+	 */
+	private $menuEntries = array();
+
+	/**
+	 * Rendered menu blocks
+	 */
+	private $menuBlocks = array();
+
 	/**
 	 * Menu instance
 	 */
@@ -652,6 +690,9 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
 	private function finishEntry () {
 		// Pop the last entry
 		$this->getStackInstance()->popNamed('current_node');
+
+		// Render this menu entry
+		$this->renderMenuEntry();
 	}
 
 	/**
@@ -662,6 +703,9 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
 	private function finishBlock () {
 		// Pop the last entry
 		$this->getStackInstance()->popNamed('current_node');
+
+		// Render this menu block
+		$this->renderMenuBlock();
 	}
 
 	/**
@@ -714,6 +758,76 @@ class MenuTemplateEngine extends BaseTemplateEngine implements CompileableTempla
 		$this->getStackInstance()->popNamed('current_node');
 	}
 
+	/**
+	 * Renders this menu entry, as every block all variables got overwritten
+	 * with data from next entry.
+	 *
+	 * @todo	'anchor_href' may needs expanding with full URL
+	 * @return	void
+	 */
+	private function renderMenuEntry () {
+		// Prepare template engine
+		$templateInstance = $this->prepareTemplateInstance();
+
+		// Load menu entry template
+		$templateInstance->loadCodeTemplate('menu_entry');
+
+		// Copy all variables over to it
+		foreach ($this->menuEntryVariables as $variableName) {
+			// Copy variable
+			$variableValue = $this->readVariable($variableName);
+
+			// ... into the instance
+			$templateInstance->assignVariable($variableName, $variableValue);
+		} // END - foreach
+
+		// Compile template + variables
+		$templateInstance->compileTemplate();
+		$templateInstance->compileVariables();
+
+		// Remember it here
+		$this->menuEntries[$this->readVariable('entry_id')] = $templateInstance->getRawTemplateData();
+	}
+
+	/**
+	 * Renders this menu block, as next block all data is overwritten with
+	 * next block.
+	 *
+	 * @return	void
+	 */
+	private function renderMenuBlock () {
+		// Init block content
+		$blockContent = implode('', $this->menuEntries);
+
+		// Prepare template engine
+		$templateInstance = $this->prepareTemplateInstance();
+
+		// Load menu entry template
+		$templateInstance->loadCodeTemplate('menu_block');
+
+		// Copy all variables over to it
+		foreach ($this->menuBlockVariables as $variableName) {
+			// Copy variable
+			$variableValue = $this->readVariable($variableName);
+
+			// ... into the instance
+			$templateInstance->assignVariable($variableName, $variableValue);
+		} // END - foreach
+
+		// Assign block content
+		$templateInstance->assignVariable('block_content', $blockContent);
+
+		// Compile template + variables
+		$templateInstance->compileTemplate();
+		$templateInstance->compileVariables();
+
+		// Remember it here
+		array_push($this->menuBlocks, $templateInstance->getRawTemplateData());
+
+		// Reset rendered menu entries array
+		$this->menuEntries = array();
+	}
+
 	/**
 	 * Getter for menu cache file (FQFN)
 	 *
-- 
2.39.5