X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fhelper%2Fclass_BaseHelper.php;h=b4e893f4bcee531260a179f4cf0b54fde375cbcc;hb=12dbc1af8f0bc2981711b17c7c955f270c440b35;hp=02a99e3aa16714b9eff08701e0eb95a01a2512c2;hpb=12a993738a1d1bea29a886e06478beb145c275e5;p=hub.git diff --git a/inc/classes/main/helper/class_BaseHelper.php b/inc/classes/main/helper/class_BaseHelper.php index 02a99e3aa..b4e893f4b 100644 --- a/inc/classes/main/helper/class_BaseHelper.php +++ b/inc/classes/main/helper/class_BaseHelper.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -23,9 +23,9 @@ */ class BaseHelper extends BaseFrameworkSystem { /** - * Template engine instance + * Instance to the class which provides field values */ - private $templateInstance = null; + private $valueInstance = null; /** * Rendered content created by the helper class @@ -33,7 +33,42 @@ class BaseHelper extends BaseFrameworkSystem { private $content = ""; /** - * Private constructor + * Array with groups + */ + private $groups = array(); + + /** + * Array with sub group + */ + private $subGroups = array(); + + /** + * Previously opened group + */ + private $previousGroupId = ""; + + /** + * Previously opened sub group + */ + private $previousSubGroupId = ""; + + /** + * Total counter for groups and sub groups + */ + private $totalCounter = 0; + + // Exception constants + const EXCEPTION_XML_PARSER_ERROR = 0x1e0; + const EXCEPTION_XML_NODE_UNKNOWN = 0x1e1; + const EXCEPTION_XML_NODE_MISMATCH = 0x1e2; + const EXCEPTION_GROUP_NOT_OPENED = 0x1e3; + const EXCEPTION_GROUP_ALREADY_FOUND = 0x1e4; + const EXCEPTION_SUB_GROUP_ALREADY_FOUND = 0x1e5; + const EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED = 0x1e6; + const EXCEPTION_NO_PREVIOUS_GROUP_OPENED = 0x1e7; + + /** + * Protected constructor * * @param $className Real name of the class * @return void @@ -48,32 +83,63 @@ class BaseHelper extends BaseFrameworkSystem { } /** - * Setter for template engine instances + * Adds content directly + * + * @param $newContent New content to add + * @return void + */ + protected final function addContent ($newContent) { + $this->content .= (string) trim($newContent)."\n"; + } + + /** + * Add header content to the helper * - * @param $templateInstance An instance of a template engine class + * @param $content Content to to the base * @return void */ - protected final function setTemplateInstance (CompileableTemplate $templateInstance) { - $this->templateInstance = $templateInstance; + protected function addHeaderContent ($content) { + // Add the header content + $this->groups['header']['content'] = (string) trim($content); } /** - * Getter for template engine instances + * Add footer content to the helper * - * @return $templateInstance An instance of a template engine class + * @param $content Content to to the base + * @return void */ - protected final function getTemplateInstance () { - return $this->templateInstance; + protected function addFooterContent ($content) { + // Add the footer content + $this->groups['footer']['content'] = (string) trim($content); } /** - * Add content + * Adds content to the previously opened group or sub group. If a sub group + * was found it will be taken. If no group/sub group is opened at the moment + * the code will be passed to addContent(). * * @param $newContent New content to add * @return void */ - protected final function addContent ($newContent) { - $this->content .= (string) trim($newContent)."\r\n"; + protected final function addContentToPreviousGroup ($newContent) { + // Check for sub/group + if ($this->ifSubGroupOpenedPreviously()) { + // Get sub group id + $subGroupId = $this->getPreviousSubGroupId(); + + // Add the content + $this->subGroups[$subGroupId]['content'] .= $newContent; + } elseif ($this->ifGroupOpenedPreviously()) { + // Get group id + $groupId = $this->getPreviousGroupId(); + + // Add the content + $this->groups[$groupId]['content'] .= $newContent; + } else { + // Add it directly + $this->addContent($newContent); + } } /** @@ -84,6 +150,353 @@ class BaseHelper extends BaseFrameworkSystem { protected final function getContent () { return $this->content; } + + /** + * Assigns a field from the value instance with a template variable + * + * @param $fieldName Name of the field to assign + * @return void + */ + public function assignField ($fieldName) { + // Get the value from value instance + $fieldValue = $this->getValueField($fieldName); + + // Assign it with a template variable + $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $fieldValue); + } + + /** + * Assigns a field from the value instance with a template variable but + * parses its content through a given filter method of the value instance + * + * @param $fieldName Name of the field to assign + * @param $filterMethod Method name to call of the value instance + * @return void + * @todo Rewrite this method using a helper class for filtering data + */ + public function assignFieldWithFilter ($fieldName, $filterMethod) { + // Get the value + $fieldValue = $this->getValueField($fieldName); + + // Now filter it through the value through the filter method + $filteredValue = call_user_func_array(array($this, 'doFilter' . $this->convertToClassName($filterMethod)), array($fieldValue)); + + // Assign it with a template variable + $this->getTemplateInstance()->assignVariable('block_' . $fieldName, $filteredValue); + } + + /** + * Pre-fetches field default values from the given registry key instance into this class + * + * @param $registryKey Registry key which holds an object with values + * @param $extraKey Extra value instance key used if registryKey is null + * @return void + * @throws NullPointerException If recovery of requested value instance failed + */ + public function prefetchValueInstance ($registryKey, $extraKey = null) { + // Get the required instance + $this->valueInstance = Registry::getRegistry()->getInstance($registryKey); + + // Is the value instance valid? + if (is_null($this->valueInstance)) { + // Try to create it "from scratch", by first init extra instance + $extraInstance = null; + + // Shall we get an extra instance? + if (!is_null($extraKey)) { + // Get the extra instance. + $extraInstance = Registry::getRegistry()->getInstance($extraKey); + } // END - if + + // Get the requested instance + try { + $this->valueInstance = ObjectFactory::createObjectByConfiguredName($registryKey . '_class', array($extraInstance)); + + } catch (FrameworkException $e) { + // Okay, nothing found so throw a null pointer exception here + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } + } // END - if + } + + /** + * Opens a helper group with given group id and content or throws an + * exception if that group is already found regardless if it is open or + * closed. + * + * @param $groupId Group id to open + * @param $content Initial content to add to the group + * @param $tag HTML tag used to open this group + * @return void + * @throws HelperGroupAlreadyCreatedException If the group was already created before + */ + protected function openGroupByIdContent ($groupId, $content, $tag) { + //* DEBUG: */ echo "OPEN:groupId={$groupId},content=
".htmlentities($content)."
\n"; + // Is the group already there? + if (isset($this->groups[$groupId])) { + // Then throw an exception here + throw new HelperGroupAlreadyCreatedException(array($this, $groupId), self::EXCEPTION_GROUP_ALREADY_FOUND); + } // END - if + + // Count one up + $this->totalCounter++; + + // Add the group to the stack + $this->groups[$this->totalCounter] = $groupId; + $this->groups[$groupId]['opened'] = true; + $this->groups[$groupId]['content'] = sprintf("%s\n", $groupId, strlen($content), $tag, $content); + $this->groups[$groupId]['tag'] = $tag; + + // Mark this group as previously opened + $this->setPreviousGroupId($groupId); + } + + /** + * Closes the previously opened group by added given content to it or + * throws an exception if no previous group was opened + * + * @param $content Content for previously opened group, or empty to use tag of opener + * @return void + * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found + */ + public function closePreviousGroupByContent ($content = "") { + // Check if any sub group was opened before + if ($this->ifSubGroupOpenedPreviously()) { + // Close it automatically + $this->closePreviousSubGroupByContent(); + } // END - if + + // Check if any group was opened before + if ($this->ifGroupOpenedPreviously() === false) { + // Then throw an exception + throw new HelperNoPreviousOpenedGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED); + } // END - if + + // Get previous group + $groupId = $this->getPreviousGroupId(); + + // Is the content empty? + if ((empty($content)) && (!empty($this->groups[$groupId]['tag']))) { + // Get it from opener + $content = sprintf("", $groupId, $this->groups[$groupId]['tag']); + } // END - if + + // Add content to it and mark it as closed + $this->groups[$groupId]['content'] .= sprintf("%s\n", $groupId, strlen($content), $this->groups[$groupId]['tag'], $content); + $this->groups[$groupId]['opened'] = false; + + // Mark previous group as closed + $this->setPreviousGroupId(""); + //* DEBUG: */ echo "CLOSE:groupId={$groupId}
\n"; + } + + /** + * Opens a helper sub group with given group id and content or throws an + * exception if that sub group is already found regardless if it is open or + * closed. + * + * @param $subGroupId Sub group id to open + * @param $content Initial content to add to the sub group + * @param $tag HTML tag used to open this group + * @return void + * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before + */ + protected function openSubGroupByIdContent ($subGroupId, $content, $tag) { + //* DEBUG: */ echo "OPEN:subGroupId={$subGroupId},content=".htmlentities($content)."
\n"; + // Is the group already there? + if (isset($this->subGroups[$subGroupId])) { + // Then throw an exception here + throw new HelperSubGroupAlreadyCreatedException(array($this, $subGroupId), self::EXCEPTION_SUB_GROUP_ALREADY_FOUND); + } // END - if + + // Count one up + $this->totalCounter++; + + // Add the group to the stack + $this->subGroups[$this->totalCounter] = $subGroupId; + $this->subGroups[$subGroupId]['opened'] = true; + $this->subGroups[$subGroupId]['content'] = sprintf("%s\n", $subGroupId, strlen($content), $tag, $content); + $this->subGroups[$subGroupId]['tag'] = $tag; + + // Mark this group as previously opened + $this->setPreviousSubGroupId($subGroupId); + } + + /** + * Closes the previously opened sub group by added given content to it or + * throws an exception if no previous sub group was opened + * + * @param $content Content for previously opened sub group, or leave empty to use div/span of openener + * @return void + * @throws HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found + */ + public function closePreviousSubGroupByContent ($content = "") { + // Check if any sub group was opened before + if ($this->ifSubGroupOpenedPreviously() === false) { + // Then throw an exception + throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED); + } // END - if + + // Get previous sub group + $subGroupId = $this->getPreviousSubGroupId(); + + // Is the content empty? + if ((empty($content)) && (!empty($this->subGroups[$subGroupId]['tag']))) { + // Get it from opener + $content = sprintf("", $subGroupId, $this->subGroups[$subGroupId]['tag']); + } // END - if + + // Add content to it and mark it as closed + $this->subGroups[$subGroupId]['content'] .= sprintf("%s\n", $subGroupId, strlen($content), $this->subGroups[$subGroupId]['tag'], $content); + $this->subGroups[$subGroupId]['opened'] = false; + + // Mark previous sub group as closed + $this->setPreviousSubGroupId(""); + //* DEBUG: */ echo "CLOSE:subGroupId={$subGroupId}
\n"; + } + + /** + * Renders all group and sub group in their order + * + * @return $content Rendered HTML content + */ + public function renderContent () { + // Initialize content + $content = ""; + + // Is header content there? + if (isset($this->groups['header'])) { + // Then add it + $content .= $this->groups['header']['content']."\n"; + } // END - if + + // Initiate content + $content .= $this->getContent(); + + // Now "walk" through all groups and sub-groups + for ($idx = 1; $idx <= $this->totalCounter; $idx++) { + // Is this a group and is it closed? + if ((isset($this->groups[$idx])) && ($this->groups[$this->groups[$idx]]['opened'] === false)) { + // Then add it's content + $groupContent = trim($this->groups[$this->groups[$idx]]['content']); + //* DEBUG: */ echo "group={$this->groups[$idx]},content=
".htmlentities($groupContent)."

\n"; + $content .= $groupContent; + } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) { + // Then add it's content + $subGroupContent = $this->subGroups[$this->subGroups[$idx]]['content']; + //* DEBUG: */ echo "subgroup={$this->subGroups[$idx]},content=
".htmlentities($subGroupContent)."

\n"; + $content .= trim($subGroupContent); + } else { + // Something went wrong + $this->debugInstance(__METHOD__."(): Something unexpected happened here."); + } + } // END - for + + // Is footer content there? + if (isset($this->groups['footer'])) { + // Then add it + $content .= $this->groups['footer']['content']."\n"; + } // END - if + + // Return it + //* DEBUG: */ echo "content=
".htmlentities($content)."
(".strlen($content).")
\n"; + return $content; + } + + /** + * Checks wether the specified group is opened + * + * @param $groupId Id of group to check + * @return $isOpened Wether the specified group is open + */ + protected function ifGroupIsOpened ($groupId) { + // Is the group open? + $isOpened = ((isset($this->groups[$groupId])) && ($this->groups[$groupId]['opened'] === true)); + + // Return status + return $isOpened; + } + + /** + * Getter for direct field values + * + * @param $fieldName Name of the field we shall fetch + * @return $fieldValue Value from field + */ + public function getValueField ($fieldName) { + // Get the field value + $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName)); + + // Return it + return $fieldValue; + } + + /** + * Getter for value instance + * + * @return $valueInstance Instance of the class holding our values + */ + public final function getValueInstance () { + return $this->valueInstance; + } + + /** + * Check wether a group was opened previously + * + * @return $groupOpened Wether any group was opened before + */ + protected final function ifGroupOpenedPreviously () { + $groupOpened = (!empty($this->previousGroupId)); + return $groupOpened; + } + + /** + * Check wether a group was opened previously + * + * @return $subGroupOpened Wether any group was opened before + */ + protected final function ifSubGroupOpenedPreviously () { + $subGroupOpened = (!empty($this->previousSubGroupId)); + return $subGroupOpened; + } + + /** + * Getter for previous group id + * + * @return $previousGroupId Id of previously opened group + */ + protected final function getPreviousGroupId () { + return $this->previousGroupId; + } + + /** + * Setter for previous group id + * + * @param $previousGroupId Id of previously opened group + * @return void + */ + protected final function setPreviousGroupId ($previousGroupId) { + $this->previousGroupId = (string) $previousGroupId; + } + + /** + * Getter for previous sub group id + * + * @return $previousSubGroupId Id of previously opened sub group + */ + protected final function getPreviousSubGroupId () { + return $this->previousSubGroupId; + } + + /** + * Setter for previous sub group id + * + * @param $previousSubGroupId Id of previously opened sub group + * @return void + */ + protected final function setPreviousSubGroupId ($previousSubGroupId) { + $this->previousSubGroupId = (string) $previousSubGroupId; + } } // [EOF]