]> git.mxchange.org Git - shipsimu.git/commitdiff
Rendering of helper code improved
authorRoland Häder <roland@mxchange.org>
Fri, 8 Aug 2008 19:33:30 +0000 (19:33 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 8 Aug 2008 19:33:30 +0000 (19:33 +0000)
.gitattributes
inc/classes/exceptions/helper/class_HelperGroupAlreadyCreatedException.php [new file with mode: 0644]
inc/classes/exceptions/helper/class_HelperNoPreviousOpenedSubGroupException.php [new file with mode: 0644]
inc/classes/exceptions/helper/class_HelperSubGroupAlreadyCreatedException.php [new file with mode: 0644]
inc/classes/main/helper/class_BaseHelper.php
inc/classes/main/helper/web/forms/class_WebFormHelper.php

index e66f03a8089fbc7e5a6f7a4b9a1ca5b41d48a3e1..ece7aa951b708393103343a6be1298af5f85ca98 100644 (file)
@@ -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 (file)
index 0000000..0f14f17
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * An exception thrown when a group was added twice
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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 (file)
index 0000000..6c257c0
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * An exception thrown when no sub group was opened previously
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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: <span id=\"exception_reason\">%s</span>",
+                       $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 (file)
index 0000000..efdfff9
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * An exception thrown when a sub group was added twice
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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]
+?>
index ad0c6ca6d42179c529a133336103271039b7c6b2..c861e699ae4dcb879d8700bb013a26bb622f8597 100644 (file)
@@ -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}<br />\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}<br />\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)."<br />\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}<br />\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) {
index d167768878bed59912c5e98324eb994ace2167f8..4faaf74926bf041433e3d5ff32c81e1846841d34 100644 (file)
@@ -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."<br />\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())."<br />\n";
-               $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->getContent());
+               //* DEBUG: */ echo __METHOD__.": form=".$this->getFormName().", size=".strlen($this->renderContent())."<br />\n";
+               $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->renderContent());
        }
 }