From 70885e2e9d1d62009b1cccd962f5f013e93744b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 8 Aug 2008 19:33:30 +0000 Subject: [PATCH] Rendering of helper code improved --- .gitattributes | 3 + ...ass_HelperGroupAlreadyCreatedException.php | 46 +++++ ...elperNoPreviousOpenedSubGroupException.php | 46 +++++ ..._HelperSubGroupAlreadyCreatedException.php | 46 +++++ inc/classes/main/helper/class_BaseHelper.php | 185 ++++++++++++++++-- .../helper/web/forms/class_WebFormHelper.php | 21 +- 6 files changed, 319 insertions(+), 28 deletions(-) create mode 100644 inc/classes/exceptions/helper/class_HelperGroupAlreadyCreatedException.php create mode 100644 inc/classes/exceptions/helper/class_HelperNoPreviousOpenedSubGroupException.php create mode 100644 inc/classes/exceptions/helper/class_HelperSubGroupAlreadyCreatedException.php diff --git a/.gitattributes b/.gitattributes index e66f03a..ece7aa9 100644 --- a/.gitattributes +++ b/.gitattributes @@ -281,6 +281,9 @@ inc/classes/exceptions/helper/.htaccess -text inc/classes/exceptions/helper/class_FormClosedException.php -text inc/classes/exceptions/helper/class_FormGroupClosedException.php -text inc/classes/exceptions/helper/class_FormOpenedException.php -text +inc/classes/exceptions/helper/class_HelperGroupAlreadyCreatedException.php -text +inc/classes/exceptions/helper/class_HelperNoPreviousOpenedSubGroupException.php -text +inc/classes/exceptions/helper/class_HelperSubGroupAlreadyCreatedException.php -text inc/classes/exceptions/helper/class_InvalidFormNameException.php -text inc/classes/exceptions/helper/class_NoGroupOpenedException.php -text inc/classes/exceptions/helper/class_UserInstanceMissingException.php -text diff --git a/inc/classes/exceptions/helper/class_HelperGroupAlreadyCreatedException.php b/inc/classes/exceptions/helper/class_HelperGroupAlreadyCreatedException.php new file mode 100644 index 0000000..0f14f17 --- /dev/null +++ b/inc/classes/exceptions/helper/class_HelperGroupAlreadyCreatedException.php @@ -0,0 +1,46 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class HelperGroupAlreadyCreatedException extends FrameworkException { + /** + * The constructor + * + * @param $msgArray Array containing exception data + * @param $code Code number for the exception + * @return void + */ + public function __construct (array $msgArray, $code) { + // Add a message around the missing class + $message = sprintf("[%s:%d] Group %s already opened before.", + $msgArray[0]->__toString(), + $this->getLine(), + $msgArray[1] + ); + + // Call parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/exceptions/helper/class_HelperNoPreviousOpenedSubGroupException.php b/inc/classes/exceptions/helper/class_HelperNoPreviousOpenedSubGroupException.php new file mode 100644 index 0000000..6c257c0 --- /dev/null +++ b/inc/classes/exceptions/helper/class_HelperNoPreviousOpenedSubGroupException.php @@ -0,0 +1,46 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class HelperNoPreviousOpenedSubGroupException extends FrameworkException { + /** + * The constructor + * + * @param $msgArray Array containing exception data + * @param $code Code number for the exception + * @return void + */ + public function __construct (array $msgArray, $code) { + // Add a message around the missing class + $message = sprintf("[%s:%d] Sub group not opened before. Raw text follows: %s", + $msgArray[0]->__toString(), + $this->getLine(), + $msgArray[1] + ); + + // Call parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/exceptions/helper/class_HelperSubGroupAlreadyCreatedException.php b/inc/classes/exceptions/helper/class_HelperSubGroupAlreadyCreatedException.php new file mode 100644 index 0000000..efdfff9 --- /dev/null +++ b/inc/classes/exceptions/helper/class_HelperSubGroupAlreadyCreatedException.php @@ -0,0 +1,46 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class HelperSubGroupAlreadyCreatedException extends FrameworkException { + /** + * The constructor + * + * @param $msgArray Array containing exception data + * @param $code Code number for the exception + * @return void + */ + public function __construct (array $msgArray, $code) { + // Add a message around the missing class + $message = sprintf("[%s:%d] Sub group %s already opened before.", + $msgArray[0]->__toString(), + $this->getLine(), + $msgArray[1] + ); + + // Call parent constructor + parent::__construct($message, $code); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/helper/class_BaseHelper.php b/inc/classes/main/helper/class_BaseHelper.php index ad0c6ca..c861e69 100644 --- a/inc/classes/main/helper/class_BaseHelper.php +++ b/inc/classes/main/helper/class_BaseHelper.php @@ -33,24 +33,14 @@ class BaseHelper extends BaseFrameworkSystem { private $content = ""; /** - * Wether the group is opened or not + * Array with groups */ - private $groupOpened = false; + private $groups = array(); /** - * Wether the sub group is opened or not + * Array with sub group */ - private $subGroupOpened = false; - - /** - * Name of the group - */ - private $groupName = ""; - - /** - * Name of the sub group - */ - private $subGroupId = ""; + private $subGroups = array(); /** * Previously opened group @@ -62,11 +52,20 @@ class BaseHelper extends BaseFrameworkSystem { */ 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_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 @@ -148,7 +147,7 @@ class BaseHelper extends BaseFrameworkSystem { // Get the required instance $this->valueInstance = Registry::getRegistry()->getInstance($registryKey); - // Is the instance valid? + // Is the value instance valid? if (is_null($this->valueInstance)) { // Try to create it "from scratch", by first init extra instance $extraInstance = null; @@ -170,6 +169,150 @@ class BaseHelper extends BaseFrameworkSystem { } // 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 + * @return void + * @throws HelperGroupAlreadyCreatedException If the group was already created before + */ + protected function openGroupByIdContent ($groupId, $content) { + //* DEBUG: */ echo "OPEN:groupId={$groupId}
\n"; + // Is the group already there? + if (isset($this->groups[$groupId])) { + // Then throw an exception here + $this->debugBackTrace(); + 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'] = $content."\n"; + + // 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 grouop + * @return void + * @throws HelperNoPreviousOpenedGroupException If no previously opened group was found + */ + public function closePreviousGroupByContent ($content) { + // Check if any group was opened before + if (!$this->ifGroupOpenedPreviously()) { + // 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(); + + // Add content to it and mark it as closed + $this->groups[$groupId]['content'] .= $content."\n"; + $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 + * @return void + * @throws HelperSubGroupAlreadyCreatedException If the sub group was already created before + */ + protected function openSubGroupByIdContent ($subGroupId, $content) { + //* 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'] = $content."\n"; + + // 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 grouop + * @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()) { + // 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(); + + // Add content to it and mark it as closed + $this->subGroups[$subGroupId]['content'] .= $content."\n"; + $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 () { + // 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 + $content .= $this->groups[$this->groups[$idx]]['content']; + } elseif ((isset($this->subGroups[$idx])) && ($this->subGroups[$this->subGroups[$idx]]['opened'] === false)) { + // Then add it's content + $content .= $this->subGroups[$this->subGroups[$idx]]['content']; + } else { + // Something went wrong + die("GROUP/SUB GROUP ERROR: {$idx}"); + } + + } // END - for + + // Return it + return $content; + } + /** * Getter for direct field values * @@ -235,7 +378,7 @@ class BaseHelper extends BaseFrameworkSystem { /** * Getter for previous sub group id * - * @return $previousSubGroupId Id of previously opened sub group + * @return $previousSubGroupId Id of previously opened sub group */ protected final function getPreviousSubGroupId () { return $this->previousSubGroupId; @@ -244,7 +387,7 @@ class BaseHelper extends BaseFrameworkSystem { /** * Setter for previous sub group id * - * @param $previousSubGroupId Id of previously opened sub group + * @param $previousSubGroupId Id of previously opened sub group * @return void */ protected final function setPreviousSubGroupId ($previousSubGroupId) { diff --git a/inc/classes/main/helper/web/forms/class_WebFormHelper.php b/inc/classes/main/helper/web/forms/class_WebFormHelper.php index d167768..4faaf74 100644 --- a/inc/classes/main/helper/web/forms/class_WebFormHelper.php +++ b/inc/classes/main/helper/web/forms/class_WebFormHelper.php @@ -417,11 +417,15 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate { $this->addFormSubGroup(); } // END - if + // Get previous group id + $prevGroupId = $this->getPreviousGroupId(); + // Switch the state $this->closePreviousGroupByContent($content); // All call it again if the group name is not empty - if (!empty($groupId)) { + if ((!empty($groupId)) && ($groupId != $prevGroupId)) { + //* DEBUG: */ echo $groupId."/".$prevGroupId."
\n"; $this->addFormGroup($groupId, $groupText); } // END - if } @@ -451,11 +455,11 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate { throw new EmptyVariableException(array($this, 'subGroupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); } elseif (empty($subGroupId)) { // Close the last opened - $subGroupId = $this->getSubGroupId(); + $subGroupId = $this->getPreviousSubGroupId(); } // Same sub group to open? - if ((!$this->ifSubGroupOpenedPreviously()) && ($subGroupId == $this->getSubGroupId())) { + if ((!$this->ifSubGroupOpenedPreviously()) && ($subGroupId == $this->getPreviousSubGroupId())) { // Abort here silently return false; } // END - if @@ -482,11 +486,14 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate { // Switch the state and remeber the name $this->openSubGroupByIdContent($subGroupId, $content); } else { + // Get previous sub group id + $prevSubGroupId = $this->getPreviousSubGroupId(); + // Switch the state $this->closePreviousSubGroupByContent($content); // All call it again if sub group name is not empty - if (!empty($subGroupId)) { + if ((!empty($subGroupId)) && ($subGroupId != $prevSubGroupId)) { $this->addFormSubGroup($subGroupId, $subGroupText); } // END - if } @@ -599,7 +606,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate { $captchaInstance->renderCode(); // Get the content and add it to the helper - $this->addContent($captchaInstance->getContent()); + $this->addContent($captchaInstance->renderContent()); } /** @@ -664,8 +671,8 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate { } // Send content to template engine - //* DEBUG: */ echo __METHOD__.": form=".$this->getFormName().", size=".strlen($this->getContent())."
\n"; - $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->getContent()); + //* DEBUG: */ echo __METHOD__.": form=".$this->getFormName().", size=".strlen($this->renderContent())."
\n"; + $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->renderContent()); } } -- 2.30.2