]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/helper/web/class_WebFormHelper.php
User/guest classes now have base class
[shipsimu.git] / inc / classes / main / helper / web / class_WebFormHelper.php
index 1369fe4595cbc3b3c33633d551df363f64088986..5b12e2b943c3c5db2e73028bcda0eb06af07a2a1 100644 (file)
@@ -21,7 +21,7 @@
  * 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 WebFormHelper extends BaseHelper {
+class WebFormHelper extends BaseHelper implements HelpableTemplate {
        /**
         * Instance to the class which provides field values
         */
@@ -48,16 +48,26 @@ class WebFormHelper extends BaseHelper {
         */
        private $subGroupOpened = false;
 
+       /**
+        * Name of the group
+        */
+       private $groupName = "";
+
        /**
         * Name of the sub group
         */
        private $subGroupName = "";
 
+       /**
+        * Wether form tag is enabled (default: true)
+        */
+       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
@@ -81,9 +91,10 @@ class WebFormHelper extends BaseHelper {
         * @param       $templateInstance       An instance of a valid template engine
         * @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
         */
-       public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false) {
+       public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false, $withForm = true) {
                // Get new instance
                $helperInstance = new WebFormHelper();
 
@@ -94,10 +105,18 @@ class WebFormHelper extends BaseHelper {
                if ($formId === false) {
                        // Use form id from form name
                        $formId = $formName;
-               }
+               } // END - if
 
-               // Create the form
-               $helperInstance->addFormTag($formName, $formId);
+               // Set form name
+               $helperInstance->setFormName($formName);
+               // A form-less field may say "false" here...
+               if ($withForm === true) {
+                       // Create the form
+                       $helperInstance->addFormTag($formName, $formId);
+               } else {
+                       // Disable form
+                       $helperInstance->enableForm(false);
+               }
 
                // Return the prepared instance
                return $helperInstance;
@@ -128,6 +147,7 @@ class WebFormHelper extends BaseHelper {
         * @param       $formId         Id of the form (attribute "id"; default: false)
         * @return      void
         * @throws      InvalidFormNameException        If the form name is invalid (=false)
+        * @todo        Add some unique PIN here to bypass problems with some browser and/or extensions
         */
        public function addFormTag ($formName = false, $formId = false) {
                // When the form is not yet opened at least form name must be valid
@@ -140,7 +160,7 @@ class WebFormHelper extends BaseHelper {
                $formContent = "</form>";
 
                // Check wether we shall open or close the form
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Add HTML code
                        $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
                                $formName,
@@ -150,31 +170,26 @@ class WebFormHelper extends BaseHelper {
                                $this->getConfigInstance()->readConfig('form_target')
                        );
 
-                       // Is the form id set?
-                       if ($formId !== false) {
-                               // Then add it as well
-                               $formContent .= sprintf(" id=\"%s_form\"",
-                                       $formId
-                               );
-                       }
+                       // Add form id as well
+                       $formContent .= sprintf(" id=\"%s_form\"",
+                               $formId
+                       );
 
                        // Add close bracket
                        $formContent .= ">";
 
                        // Open the form and remeber the form name
                        $this->formOpened = true;
-                       $this->formName = $formName;
                } else {
                        // Add the hidden field required to identify safely this form
-                       $this->addInputHiddenField('form', $this->formName);
+                       $this->addInputHiddenField('form', $this->getFormName());
 
                        // Is a group open?
                        if ($this->groupOpened === true) {
                                // Then automatically close it here
-                               $this->addFormGroup("", "");
+                               $this->addFormGroup();
                        } // END - if
 
-                       /* @TODO Add some unique PIN here to bypass problems with some browser and/or extensions */
                        // Simply close it
                        $this->formOpened = false;
                }
@@ -194,13 +209,13 @@ class WebFormHelper extends BaseHelper {
         */
        public function addInputTextField ($fieldName, $fieldValue = "") {
                // Is the form opened?
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
                } // END - if
 
                // Generate the content
-               $inputContent = sprintf("<input type=\"text\" class=\"textfield\" id=\"%s\" name=\"%s\" value=\"%s\" />",
+               $inputContent = sprintf("<input type=\"text\" class=\"textfield %s_field\" name=\"%s\" value=\"%s\" />",
                        $fieldName,
                        $fieldName,
                        $fieldValue
@@ -218,7 +233,7 @@ class WebFormHelper extends BaseHelper {
         */
        public function addInputTextFieldWithDefault ($fieldName) {
                // Get the value from instance
-               $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));
+               $fieldValue = $this->getField($fieldName);
                //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
 
                // Add the text field
@@ -236,13 +251,13 @@ class WebFormHelper extends BaseHelper {
         */
        public function addInputPasswordField ($fieldName, $fieldValue = "") {
                // Is the form opened?
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
                } // END - if
 
                // Generate the content
-               $inputContent = sprintf("<input type=\"password\" class=\"password\" id=\"%s\" name=\"%s\" value=\"%s\" />",
+               $inputContent = sprintf("<input type=\"password\" class=\"password %s_field\" name=\"%s\" value=\"%s\" />",
                        $fieldName,
                        $fieldName,
                        $fieldValue
@@ -263,7 +278,7 @@ class WebFormHelper extends BaseHelper {
         */
        public function addInputHiddenField ($fieldName, $fieldValue = "") {
                // Is the form opened?
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
                } // END - if
@@ -278,6 +293,37 @@ class WebFormHelper extends BaseHelper {
                $this->addContent($inputContent);
        }
 
+       /**
+        * Add a hidden input tag to the form with pre-loaded default value
+        *
+        * @param       $fieldName      Input field name
+        * @return      void
+        */
+       public function addInputHiddenFieldWithDefault ($fieldName) {
+               // Get the value from instance
+               $fieldValue = $this->getField($fieldName);
+               //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
+
+               // Add the text field
+               $this->addInputHiddenField($fieldName, $fieldValue);
+       }
+
+       /**
+        * Add a hidden input tag to the form with configuration value
+        *
+        * @param       $fieldName      Input field name
+        * @param       $prefix         Prefix for configuration without trailing _
+        * @return      void
+        */
+       public function addInputHiddenConfiguredField ($fieldName, $prefix) {
+               // Get the value from instance
+               $fieldValue = $this->getConfigInstance()->readConfig("{$prefix}_{$fieldName}");
+               //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
+
+               // Add the text field
+               $this->addInputHiddenField($fieldName, $fieldValue);
+       }
+
        /**
         * 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.
@@ -289,7 +335,7 @@ class WebFormHelper extends BaseHelper {
         */
        public function addInputCheckboxField ($fieldName, $fieldChecked = true) {
                // Is the form opened?
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
                } // END - if
@@ -299,7 +345,7 @@ class WebFormHelper extends BaseHelper {
                if ($fieldChecked === false) $checked = " ";
 
                // Generate the content
-               $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox\" id=\"%s\" value=\"1\"%s/>",
+               $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox %s_field\" value=\"1\"%s/>",
                        $fieldName,
                        $fieldName,
                        $checked
@@ -319,14 +365,14 @@ class WebFormHelper extends BaseHelper {
         */
        public function addInputResetButton ($buttonText) {
                // Is the form opened?
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, "reset"), self::EXCEPTION_CLOSED_FORM);
                } // END - if
 
                // Generate the content
                $inputContent = sprintf("<input type=\"reset\" class=\"reset_button\" id=\"%s_reset\" value=\"%s\" />",
-                       $this->formName,
+                       $this->getFormName(),
                        $buttonText
                );
 
@@ -344,15 +390,15 @@ class WebFormHelper extends BaseHelper {
         */
        public function addInputSubmitButton ($buttonText) {
                // Is the form opened?
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, "submit"), self::EXCEPTION_CLOSED_FORM);
                } // END - if
 
                // Generate the content
                $inputContent = sprintf("<input type=\"submit\" class=\"submit_button\" id=\"%s_submit\" name=\"%s_button\" value=\"%s\" />",
-                       $this->formName,
-                       $this->formName,
+                       $this->getFormName(),
+                       $this->getFormName(),
                        $buttonText
                );
 
@@ -363,15 +409,15 @@ class WebFormHelper extends BaseHelper {
        /**
         * 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) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw exception here
                        throw new FormClosedException(array($this, $groupName), self::EXCEPTION_CLOSED_FORM);
                } // END - if
@@ -380,6 +426,15 @@ class WebFormHelper extends BaseHelper {
                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
@@ -405,12 +460,13 @@ class WebFormHelper extends BaseHelper {
                        $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
@@ -431,13 +487,13 @@ class WebFormHelper extends BaseHelper {
         * 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
@@ -448,6 +504,15 @@ class WebFormHelper extends BaseHelper {
                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
@@ -500,7 +565,7 @@ class WebFormHelper extends BaseHelper {
         */
        public function addFieldText ($fieldName, $fieldText) {
                // Is the form opened?
-               if ($this->formOpened === false) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
                } // END - if
@@ -527,13 +592,14 @@ class WebFormHelper extends BaseHelper {
         * 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) {
+               if (($this->formOpened === false) && ($this->formEnabled === true)) {
                        // Throw an exception
                        throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
                } // END - if
@@ -541,13 +607,14 @@ class WebFormHelper extends BaseHelper {
                // 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
                );
 
@@ -659,9 +726,9 @@ class WebFormHelper extends BaseHelper {
         * Checks wether the rules has been updated
         *
         * @return      $rulesUpdated   Wether rules has been updated
+        * @todo        Implement check if rules have been changed
         */
        public function ifRulesHaveChanged () {
-               /* @TODO Implement check if rules have been changed */
                return false;
        }
 
@@ -675,22 +742,71 @@ class WebFormHelper extends BaseHelper {
                return $emailChange;
        }
 
+       /**
+        * Checks wether the user account is unconfirmed
+        *
+        * @return      $isUnconfirmed  Wether the user account is unconfirmed
+        */
+       public function ifUserAccountUnconfirmed () {
+               $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_unconfirmed'));
+               return $isUnconfirmed;
+       }
+
+       /**
+        * Checks wether the user account is locked
+        *
+        * @return      $isUnconfirmed  Wether the user account is locked
+        */
+       public function ifUserAccountLocked () {
+               $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_locked'));
+               return $isUnconfirmed;
+       }
+
+       /**
+        * Checks wether the user account is a guest
+        *
+        * @return      $isUnconfirmed  Wether the user account is a guest
+        */
+       public function ifUserAccountGuest () {
+               $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_guest'));
+               return $isUnconfirmed;
+       }
+
+       /**
+        * Checks wether this form is secured by a CAPTCHA
+        *
+        * @return      $isSecured      Wether this form is secured by a CAPTCHA
+        */
+       public function ifFormSecuredWithCaptcha () {
+               $isSecured = ($this->getConfigInstance()->readConfig($this->getFormName().'_captcha_secured') === "Y");
+               return $isSecured;
+       }
+
        /**
         * Flushs the content out (not yet secured against open forms, etc.!) or
-        * throw an exception if it is not yet closed
+        * close the form automatically
         *
         * @return      void
         * @throws      FormOpenedException             If the form is still open
         */
        public function flushContent () {
                // Is the form still open?
-               if ($this->formOpened === true) {
-                       // Throw an exception
-                       throw new FormOpenedException ($this, self::EXCEPTION_OPENED_FORM);
-               } // END - if
+               if (($this->formOpened === true) && ($this->formEnabled === true)) {
+                       // Close the form automatically
+                       $this->addFormTag();
+               } 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
-               $this->getTemplateInstance()->assignVariable($this->formName, $this->getContent());
+               //* DEBUG: */ echo __METHOD__.": form=".$this->getFormName().", size=".strlen($this->getContent())."<br />\n";
+               $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->getContent());
        }
 
        /**
@@ -701,11 +817,62 @@ class WebFormHelper extends BaseHelper {
         */
        public function getField ($fieldName) {
                // Get the field value
-               $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));
+               $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
 
                // Return it
                return $fieldValue;
        }
+
+       /**
+        * Adds a pre-configured CAPTCHA
+        *
+        * @return      void
+        */
+       public function addCaptcha () {
+               // Get last executed pre filter
+               $extraInstance = Registry::getRegistry()->getInstance('extra');
+
+               // Get a configured instance
+               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName()."_captcha", array($this, $extraInstance));
+
+               // Initiate the CAPTCHA
+               $captchaInstance->initiateCaptcha();
+
+               // Render the CAPTCHA code
+               $captchaInstance->renderCode();
+
+               // Get the content and add it to the helper
+               $this->addContent($captchaInstance->getContent());
+       }
+
+       /**
+        * Enables/disables the form tag usage
+        *
+        * @param       $formEnabled    Wether form is enabled or disabled
+        * @return      void
+        */
+       public final function enableForm ($formEnabled = true) {
+               $this->formEnabled = (bool) $formEnabled;
+       }
+
+       /**
+        * Setter for form name
+        *
+        * @param       $formName       Name of this form
+        * @return      void
+        */
+       public final function setFormName ($formName) {
+               $this->formName = (string) $formName;
+       }
+
+       /**
+        * Getter for form name
+        *
+        * @return      $formName       Name of this form
+        */
+       public final function getFormName () {
+               return $this->formName;
+       }
 }
 
 // [EOF]