]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/helper/web/forms/class_WebFormHelper.php
Payment introduced, minor rewrites
[shipsimu.git] / inc / classes / main / helper / web / forms / class_WebFormHelper.php
index 1fc561eebbcc366d25d0d0f7116c9cd5d8273ef8..a188251b5cd84215b1e2c94e55a4ef4beb77a3c1 100644 (file)
@@ -33,26 +33,6 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         */
        private $formName = "";
 
-       /**
-        * Wether the group is opened or not
-        */
-       private $groupOpened = false;
-
-       /**
-        * Wether the sub group is opened or not
-        */
-       private $subGroupOpened = false;
-
-       /**
-        * Name of the group
-        */
-       private $groupName = "";
-
-       /**
-        * Name of the sub group
-        */
-       private $subGroupName = "";
-
        /**
         * Wether form tag is enabled (default: true)
         */
@@ -72,12 +52,6 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
-
-               // Set part description
-               $this->setObjectDescription("Helper class for HTML forms");
-
-               // Create unique ID number
-               $this->generateUniqueId();
        }
 
        /**
@@ -87,7 +61,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         * @param       $formName                       Name of the form
         * @param       $formId                         Value for "id" attribute (default: $formName)
         * @param       $withForm                       Wether include the form tag
-        * @return      $helperInstance         A preparedf instance of this class
+        * @return      $helperInstance         A preparedf instance of this helper
         */
        public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false, $withForm = true) {
                // Get new instance
@@ -104,6 +78,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
 
                // Set form name
                $helperInstance->setFormName($formName);
+
                // A form-less field may say "false" here...
                if ($withForm === true) {
                        // Create the form
@@ -157,22 +132,25 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
 
                        // Open the form and remeber the form name
                        $this->formOpened = true;
+
+                       // Add it to the content
+                       $this->addHeaderContent($formContent);
                } else {
                        // Add the hidden field required to identify safely this form
                        $this->addInputHiddenField('form', $this->getFormName());
 
                        // Is a group open?
-                       if ($this->groupOpened === true) {
+                       if ($this->ifGroupOpenedPreviously()) {
                                // Then automatically close it here
                                $this->addFormGroup();
                        } // END - if
 
                        // Simply close it
                        $this->formOpened = false;
-               }
 
-               // Add it to the content
-               $this->addContent($formContent);
+                       // Add it to the content
+                       $this->addFooterContent($formContent);
+               }
        }
 
        /**
@@ -199,7 +177,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it maybe with a "li" tag
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
@@ -210,7 +188,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         */
        public function addInputTextFieldWithDefault ($fieldName) {
                // Get the value from instance
-               $fieldValue = $this->getField($fieldName);
+               $fieldValue = $this->getValueField($fieldName);
                //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
 
                // Add the text field
@@ -221,8 +199,8 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         * Add a password input tag to the form or throw an exception if it is not
         * yet opened. The field's name will be set as id.
         *
-        * @param       $fieldName                      Input field name
-        * @param       $fieldValue                     Input default value (default: empty)
+        * @param       $fieldName              Input field name
+        * @param       $fieldValue             Input default value (default: empty)
         * @return      void
         * @throws      FormClosedException             If the form is not yet opened
         */
@@ -241,15 +219,15 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
         * Add a hidden input tag to the form or throw an exception if it is not
         * yet opened. The field's name will be set as id.
         *
-        * @param       $fieldName                      Input field name
-        * @param       $fieldValue                     Input default value (default: empty)
+        * @param       $fieldName              Input field name
+        * @param       $fieldValue             Input default value (default: empty)
         * @return      void
         * @throws      FormClosedException             If the form is not yet opened
         */
@@ -267,7 +245,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
@@ -278,7 +256,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         */
        public function addInputHiddenFieldWithDefault ($fieldName) {
                // Get the value from instance
-               $fieldValue = $this->getField($fieldName);
+               $fieldValue = $this->getValueField($fieldName);
                //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
 
                // Add the text field
@@ -305,8 +283,8 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         * Add a checkbox input tag to the form or throw an exception if it is not
         * yet opened. The field's name will be set as id.
         *
-        * @param       $fieldName                      Input field name
-        * @param       $fieldChecked           Wether the field is checked (defaut: checked)
+        * @param       $fieldName              Input field name
+        * @param       $fieldChecked   Wether the field is checked (defaut: checked)
         * @return      void
         * @throws      FormClosedException             If the form is not yet opened
         */
@@ -329,7 +307,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
@@ -354,14 +332,14 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
         * Add a reset input tag to the form or throw an exception if it is not
         * yet opened. The field's name will be set as id.
         *
-        * @param       $buttonText                     Text displayed on the button
+        * @param       $buttonText             Text displayed on the button
         * @return      void
         * @throws      FormClosedException             If the form is not yet opened
         */
@@ -380,36 +358,36 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
         * Add a form group or close an already opened and open a new one
         *
-        * @param       $groupName      Name of the group or last opened if empty
+        * @param       $groupId        Name of the group or last opened if empty
         * @param       $groupText      Text including HTML to show above this group
         * @return      void
         * @throws      FormClosedException             If no form has been opened before
-        * @throws      EmptyVariableException  If $groupName is not set
+        * @throws      EmptyVariableException  If $groupId is not set
         */
-       public function addFormGroup ($groupName = "", $groupText = "") {
+       public function addFormGroup ($groupId = "", $groupText = "") {
                // Is a form opened?
                if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw exception here
-                       throw new FormClosedException(array($this, $groupName), self::EXCEPTION_CLOSED_FORM);
+                       throw new FormClosedException(array($this, $groupId), self::EXCEPTION_CLOSED_FORM);
                } // END - if
 
                // At least the group name should be set
-               if ((empty($groupName)) && ($this->groupOpened === false)) {
+               if ((empty($groupId)) && (!$this->ifGroupOpenedPreviously())) {
                        // Throw exception here
-                       throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (empty($groupName)) {
+                       throw new EmptyVariableException(array($this, 'groupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } elseif (empty($groupId)) {
                        // Close the last opened
-                       $groupName = $this->groupName;
+                       $groupId = $this->getPreviousGroupId();
                }
 
                // Same group to open?
-               if (($this->groupOpened === false) && ($groupName == $this->groupName)) {
+               if ((!$this->ifGroupOpenedPreviously()) && ($groupId == $this->getPreviousGroupId())) {
                        // Abort here silently
                        return false;
                } // END - if
@@ -418,7 +396,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                $content = "    </div>\n</div><!-- Group - CLOSE //-->";
 
                // Is this group opened?
-               if ($this->groupOpened === false) {
+               if (!$this->ifGroupOpenedPreviously()) {
                        // Begin the div/span blocks
                        $content = sprintf("<!-- Group %s - OPEN //-->
 <div class=\"group_box\" id=\"%s_group_box\">
@@ -426,35 +404,32 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                %s
        </span>
        <div class=\"group_field\" id=\"%s_group_field\">",
-                               $groupName,
-                               $groupName,
-                               $groupName,
+                               $groupId,
+                               $groupId,
+                               $groupId,
                                $groupText,
-                               $groupName
+                               $groupId
                        );
 
-                       // Add the content
-                       $this->addContent($content);
-
                        // Switch the state
-                       $this->groupName = $groupName;
-                       $this->groupOpened = true;
+                       $this->openGroupByIdContent($groupId, $content);
                } else {
                        // Is a sub group opened?
-                       if ($this->subGroupOpened === true) {
+                       if ($this->ifSubGroupOpenedPreviously()) {
                                // Close it here
                                $this->addFormSubGroup();
                        } // END - if
 
-                       // Add the content
-                       $this->addContent($content);
+                       // Get previous group id
+                       $prevGroupId = $this->getPreviousGroupId();
 
                        // Switch the state
-                       $this->groupOpened = false;
+                       $this->closePreviousGroupByContent($content);
 
                        // All call it again if the group name is not empty
-                       if (!empty($groupName)) {
-                               $this->addFormGroup($groupName, $groupText);
+                       if ((!empty($groupId)) && ($groupId != $prevGroupId)) {
+                               //* DEBUG: */ echo $groupId."/".$prevGroupId."<br />\n";
+                               $this->addFormGroup($groupId, $groupText);
                        } // END - if
                }
        }
@@ -464,30 +439,30 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
         * throws an exception if no group has been opened before or if the sub
         * group name is empty.
         *
-        * @param       $subGroupName   Name of the group or last opened if empty
+        * @param       $subGroupId             Name of the group or last opened if empty
         * @param       $subGroupText   Text including HTML to show above this group
         * @return      void
-        * @throws      FormGroupClosedException        If no group has been opened before
-        * @throws      EmptyVariableException          If $subGroupName is not set
+        * @throws      FormFormClosedException         If no group has been opened before
+        * @throws      EmptyVariableException          If $subGroupId is not set
         */
-       public function addFormSubGroup ($subGroupName = "", $subGroupText = "") {
+       public function addFormSubGroup ($subGroupId = "", $subGroupText = "") {
                // Is a group opened?
-               if ($this->groupOpened === false) {
+               if (!$this->ifGroupOpenedPreviously()) {
                        // Throw exception here
-                       throw new FormGroupClosedException(array($this, $subGroupName), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
+                       throw new FormFormClosedException(array($this, $subGroupId), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
                } // END - if
 
                // At least the sub group name should be set
-               if ((empty($subGroupName)) && ($this->subGroupOpened === false)) {
+               if ((empty($subGroupId)) && (!$this->ifSubGroupOpenedPreviously())) {
                        // Throw exception here
-                       throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (empty($subGroupName)) {
+                       throw new EmptyVariableException(array($this, 'subGroupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } elseif (empty($subGroupId)) {
                        // Close the last opened
-                       $subGroupName = $this->subGroupName;
+                       $subGroupId = $this->getPreviousSubGroupId();
                }
 
                // Same sub group to open?
-               if (($this->subGroupOpened === false) && ($subGroupName == $this->subGroupName)) {
+               if ((!$this->ifSubGroupOpenedPreviously()) && ($subGroupId == $this->getPreviousSubGroupId())) {
                        // Abort here silently
                        return false;
                } // END - if
@@ -496,7 +471,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                $content = "    </div>\n</div><!-- Sub group- CLOSE //-->";
 
                // Is this group opened?
-               if ($this->subGroupOpened === false) {
+               if (!$this->ifSubGroupOpenedPreviously()) {
                        // Begin the span block
                        $content = sprintf("<!-- Sub group %s - OPEN //-->
 <div class=\"subgroup_box\" id=\"%s_subgroup_box\">
@@ -504,29 +479,25 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                %s
        </span>
        <div class=\"subgroup_field\" id=\"%s_subgroup_field\">",
-                               $subGroupName,
-                               $subGroupName,
-                               $subGroupName,
+                               $subGroupId,
+                               $subGroupId,
+                               $subGroupId,
                                $subGroupText,
-                               $subGroupName
+                               $subGroupId
                        );
 
-                       // Add the content
-                       $this->addContent($content);
-
                        // Switch the state and remeber the name
-                       $this->subGroupOpened = true;
-                       $this->subGroupName = $subGroupName;
+                       $this->openSubGroupByIdContent($subGroupId, $content);
                } else {
-                       // Add the content
-                       $this->addContent($content);
+                       // Get previous sub group id
+                       $prevSubGroupId = $this->getPreviousSubGroupId();
 
                        // Switch the state
-                       $this->subGroupOpened = false;
+                       $this->closePreviousSubGroupByContent($content);
 
                        // All call it again if sub group name is not empty
-                       if (!empty($subGroupName)) {
-                               $this->addFormSubGroup($subGroupName, $subGroupText);
+                       if ((!empty($subGroupId)) && ($subGroupId != $prevSubGroupId)) {
+                               $this->addFormSubGroup($subGroupId, $subGroupText);
                        } // END - if
                }
        }
@@ -549,7 +520,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
 
                // Set the block type
                $block = "div";
-               if ($this->groupOpened === true) $block = "span";
+               if ($this->ifGroupOpenedPreviously()) $block = "span";
 
                // Generate the content
                $inputContent = sprintf("       <%s id=\"%s_text\">
@@ -562,7 +533,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
@@ -582,7 +553,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                } // END - if
 
                // Is a group open?
-               if ($this->groupOpened === true) {
+               if ($this->ifGroupOpenedPreviously()) {
                        // Then automatically close it here
                        $this->addFormGroup();
                } // END - if
@@ -596,27 +567,109 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                );
 
                // And add it
-               $this->addContent($inputContent);
+               $this->addContentToPreviousGroup($inputContent);
        }
 
        /**
-        * Checks wether the registration requires a valid email address
+        * Adds a selection box as a sub group to the form. Do not box this into
+        * another sub group. Sub-sub groups are not (yet) supported.
         *
-        * @return      $required       Wether the email address is required
+        * @param       $selectId               Id of the selection box
+        * @param       $firstEntry             Content to be added as first non-selectable entry
+        * @return      void
+        * @throws      FormClosedException             If the form is not yet opened
         */
-       public function ifRegisterRequiresEmailVerification () {
-               $required = ($this->getConfigInstance()->readConfig('register_requires_email') === "Y");
-               return $required;
+       public function addInputSelectField ($selectId, $firstEntry) {
+               // Is the form group opened?
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
+                       // Throw an exception
+                       throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
+               } // END - if
+
+               // Shall we close or open the sub group?
+               if ((!$this->ifSubGroupOpenedPreviously()) && ($this->getPreviousSubGroupId() !== $selectId)) {
+                       // Initialize first entry (which might be non-selectable if content is provided
+                       if (!empty($firstEntry)) {
+                               // Add selection around it
+                               $firstEntry = sprintf("<option value=\"invalid\" disabled=\"disabled\">%s</option>\n",
+                                       $firstEntry
+                               );
+                       } // END - if
+
+                       // Construct the opening select tag
+                       $content = sprintf("<select class=\"select_box\" id=\"%s_%s\" name=\"%s\">\n%s",
+                               $this->getFormName(),
+                               $selectId,
+                               $selectId,
+                               $firstEntry
+                       );
+
+                       // Open the sub group
+                       $this->openSubGroupByIdContent($selectId, $content);
+               } elseif ($this->getPreviousSubGroupId() != $selectId) {
+                       // Something went wrong!
+                       $this->debugInstance();
+               } else {
+                       // Close the sub group
+                       $this->closePreviousSubGroupByContent("</select>");
+               }
        }
 
        /**
-        * Checks wether profile data shall be asked
+        * Adds a non-selectable sub option to a previously added selection box.
+        * This method does *not* validate if there is already a sub option added
+        * with the same name. We need to finish this here!
         *
-        * @return      $required       Wether profile shall be asked
+        * @param       $subName        Name of the sub action
+        * @param       $subValue       Value of the sub action
+        * @return      void
+        * @throws      HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
+        * @todo        Add checking if sub option is already added
         */
-       public function ifRegisterIncludesProfile () {
-               $required = ($this->getConfigInstance()->readConfig('register_includes_profile') === "Y");
-               return $required;
+       public function addSelectSubOption ($subName, $subValue) {
+               // Is there a sub group (shall be a selection box!)
+               if (!$this->ifSubGroupOpenedPreviously()) {
+                       // Then throw an exception here
+                       throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
+               } // END - if
+
+               // Render the content
+               $content = sprintf("<option value=\"invalid\" class=\"suboption suboption_%s\" disabled=\"disabled\">%s</option>\n",
+                       $subName,
+                       $subValue
+               );
+
+               // Add the content to the previously opened sub group
+               $this->addContentToPreviousGroup($content);
+       }
+
+       /**
+        * Adds a selectable option to a previously added selection box. This method
+        * does *not* validate if there is already a sub option added with the same
+        * name. We need to finish this here!
+        *
+        * @param       $optionName     Name of the sub action
+        * @param       $optionValue    Value of the sub action
+        * @return      void
+        * @throws      HelperNoPreviousOpenedSubGroupException If no previously opened sub group was found
+        * @todo        Add checking if sub option is already added
+        */
+       public function addSelectOption ($optionName, $optionValue) {
+               // Is there a sub group (shall be a selection box!)
+               if (!$this->ifSubGroupOpenedPreviously()) {
+                       // Then throw an exception here
+                       throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
+               } // END - if
+
+               // Render the content
+               $content = sprintf("<option value=\"%s\" class=\"option option_%s\">%s</option>\n",
+                       $optionName,
+                       $optionName,
+                       $optionValue
+               );
+
+               // Add the content to the previously opened sub group
+               $this->addContentToPreviousGroup($content);
        }
 
        /**
@@ -629,7 +682,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                $extraInstance = Registry::getRegistry()->getInstance('extra');
 
                // Get a configured instance
-               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName()."_captcha", array($this, $extraInstance));
+               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName().'_captcha', array($this, $extraInstance));
 
                // Initiate the CAPTCHA
                $captchaInstance->initiateCaptcha();
@@ -638,7 +691,7 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                $captchaInstance->renderCode();
 
                // Get the content and add it to the helper
-               $this->addContent($captchaInstance->getContent());
+               $this->addContentToPreviousGroup($captchaInstance->renderContent());
        }
 
        /**
@@ -670,6 +723,26 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                return $this->formName;
        }
 
+       /**
+        * Checks wether the registration requires a valid email address
+        *
+        * @return      $required       Wether the email address is required
+        */
+       public function ifRegisterRequiresEmailVerification () {
+               $required = ($this->getConfigInstance()->readConfig('register_requires_email') === "Y");
+               return $required;
+       }
+
+       /**
+        * Checks wether profile data shall be asked
+        *
+        * @return      $required       Wether profile data shall be asked
+        */
+       public function ifRegisterIncludesProfile () {
+               $required = ($this->getConfigInstance()->readConfig('register_includes_profile') === "Y");
+               return $required;
+       }
+
        /**
         * Checks wether this form is secured by a CAPTCHA
         *
@@ -693,18 +766,18 @@ class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
                        // Close the form automatically
                        $this->addFormTag();
                } elseif ($this->formEnabled === false) {
-                       if ($this->subGroupOpened === true) {
+                       if ($this->ifSubGroupOpenedPreviously()) {
                                // Close sub group
                                $this->addFormSubGroup();
-                       } elseif ($this->groupOpened === true) {
+                       } elseif ($this->ifGroupOpenedPreviously()) {
                                // Close group
                                $this->addFormGroup();
                        }
                }
 
                // 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());
        }
 }