]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/helper/web/class_WebFormHelper.php
CAPTCHA support basicly finished (weak CAPTCHA!)
[shipsimu.git] / inc / classes / main / helper / web / class_WebFormHelper.php
index 8d20001046a071f98afbff5556decf17bb8b727c..603f70b79f94825f55791541c044ce8836a12d09 100644 (file)
@@ -48,6 +48,11 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
         */
        private $subGroupOpened = false;
 
+       /**
+        * Name of the group
+        */
+       private $groupName = "";
+
        /**
         * Name of the sub group
         */
@@ -59,10 +64,10 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
        private $formEnabled = true;
 
        // Class Constants
-       const EXCEPTION_FORM_NAME_INVALID       = 0x030;
-       const EXCEPTION_CLOSED_FORM             = 0x031;
-       const EXCEPTION_OPENED_FORM             = 0x032;
-       const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x033;
+       const EXCEPTION_FORM_NAME_INVALID       = 0x120;
+       const EXCEPTION_CLOSED_FORM             = 0x121;
+       const EXCEPTION_OPENED_FORM             = 0x122;
+       const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x123;
 
        /**
         * Protected constructor
@@ -182,7 +187,7 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
                        // Is a group open?
                        if ($this->groupOpened === true) {
                                // Then automatically close it here
-                               $this->addFormGroup("", "");
+                               $this->addFormGroup();
                        } // END - if
 
                        // Simply close it
@@ -404,13 +409,13 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
        /**
         * Add a form group or close an already opened and open a new one
         *
-        * @param       $groupName      Name of the group
+        * @param       $groupName      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
         */
-       public function addFormGroup ($groupName, $groupText) {
+       public function addFormGroup ($groupName = "", $groupText = "") {
                // Is a form opened?
                if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw exception here
@@ -421,6 +426,15 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
                if ((empty($groupName)) && ($this->groupOpened === false)) {
                        // Throw exception here
                        throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } elseif (empty($groupName)) {
+                       // Close the last opened
+                       $groupName = $this->groupName;
+               }
+
+               // Same group to open?
+               if (($this->groupOpened === false) && ($groupName == $this->groupName)) {
+                       // Abort here silently
+                       return false;
                } // END - if
 
                // Initialize content with closing div by default
@@ -446,12 +460,13 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
                        $this->addContent($content);
 
                        // Switch the state
+                       $this->groupName = $groupName;
                        $this->groupOpened = true;
                } else {
                        // Is a sub group opened?
                        if ($this->subGroupOpened === true) {
                                // Close it here
-                               $this->addFormSubGroup("", "");
+                               $this->addFormSubGroup();
                        } // END - if
 
                        // Add the content
@@ -472,13 +487,13 @@ class WebFormHelper extends BaseHelper 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
+        * @param       $subGroupName   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
         */
-       public function addFormSubGroup ($subGroupName, $subGroupText) {
+       public function addFormSubGroup ($subGroupName = "", $subGroupText = "") {
                // Is a group opened?
                if ($this->groupOpened === false) {
                        // Throw exception here
@@ -489,6 +504,15 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
                if ((empty($subGroupName)) && ($this->subGroupOpened === false)) {
                        // Throw exception here
                        throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } elseif (empty($subGroupName)) {
+                       // Close the last opened
+                       $subGroupName = $this->subGroupName;
+               }
+
+               // Same sub group to open?
+               if (($this->subGroupOpened === false) && ($subGroupName == $this->subGroupName)) {
+                       // Abort here silently
+                       return false;
                } // END - if
 
                // Initialize content with closing div by default
@@ -568,11 +592,12 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
         * Add text (notes) surrounded by a div block. Still opened groups or sub
         * groups will be automatically closed.
         *
+        * @param       $noteId         Id for this note
         * @param       $formNotes      The form notes we shell addd
         * @return      void
         * @throws      FormClosedException             If the form is not yet opened
         */
-       public function addFormNote ($formNotes) {
+       public function addFormNote ($noteId, $formNotes) {
                // Is the form opened?
                if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
@@ -582,13 +607,14 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
                // Is a group open?
                if ($this->groupOpened === true) {
                        // Then automatically close it here
-                       $this->addFormGroup("", "");
+                       $this->addFormGroup();
                } // END - if
 
                // Generate the content
-               $inputContent = sprintf("       <div id=\"form_note\">
+               $inputContent = sprintf("       <div id=\"form_note_%s\">
                %s
        </div>",
+                       $noteId,
                        $formNotes
                );
 
@@ -768,9 +794,18 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
                if (($this->formOpened === true) && ($this->formEnabled === true)) {
                        // Close the form automatically
                        $this->addFormTag();
-               } // END - if
+               } elseif ($this->formEnabled === false) {
+                       if ($this->subGroupOpened === true) {
+                               // Close sub group
+                               $this->addFormSubGroup();
+                       } elseif ($this->groupOpened === true) {
+                               // 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());
        }
 
@@ -798,7 +833,7 @@ class WebFormHelper extends BaseHelper implements HelpableTemplate {
                $extraInstance = Registry::getRegistry()->getInstance('extra');
 
                // Get a configured instance
-               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName()."_captcha", array($this->getTemplateInstance(), $extraInstance));
+               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName()."_captcha", array($this, $extraInstance));
 
                // Initiate the CAPTCHA
                $captchaInstance->initiateCaptcha();