]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/helper/web/class_WebFormHelper.php
Simple exception handler and error handler added, profile update added with stubs
[shipsimu.git] / inc / classes / main / helper / web / class_WebFormHelper.php
index fb0f1b24d39d2784c6629e09dd06cb1648816d75..1369fe4595cbc3b3c33633d551df363f64088986 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class WebFormHelper extends BaseHelper {
+       /**
+        * Instance to the class which provides field values
+        */
+       private $valueInstance = null;
+
        /**
         * Wether the form tag is opened (keep at false or else your forms will
         * never work!)
@@ -49,13 +54,13 @@ class WebFormHelper extends BaseHelper {
        private $subGroupName = "";
 
        // Class Constants
-       const EXCEPTION_FORM_NAME_INVALID       = 0xb00;
-       const EXCEPTION_CLOSED_FORM             = 0xb01;
-       const EXCEPTION_OPENED_FORM             = 0xb02;
-       const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0xb03;
+       const EXCEPTION_FORM_NAME_INVALID       = 0x030;
+       const EXCEPTION_CLOSED_FORM             = 0x031;
+       const EXCEPTION_OPENED_FORM             = 0x032;
+       const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x033;
 
        /**
-        * Private constructor
+        * Protected constructor
         *
         * @return      void
         */
@@ -67,7 +72,7 @@ class WebFormHelper extends BaseHelper {
                $this->setObjectDescription("Helper class for HTML forms");
 
                // Create unique ID number
-               $this->createUniqueID();
+               $this->generateUniqueId();
        }
 
        /**
@@ -98,6 +103,24 @@ class WebFormHelper extends BaseHelper {
                return $helperInstance;
        }
 
+       /**
+        * Pre-fetches field default values from the given registry key instance into this class
+        *
+        * @param       $registryKey
+        * @return      void
+        * @throws      NullPointerException    If an instance from registry is null
+        */
+       public function prefetchFieldValues ($registryKey) {
+               // Get the required instance
+               $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
+
+               // Is the instance valid?
+               if (is_null($this->valueInstance)) {
+                       // Throw an exception
+                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
+               } // END - if
+       }
+
        /**
         * Add the form tag or close it an already opened form tag
         *
@@ -111,7 +134,7 @@ class WebFormHelper extends BaseHelper {
                if (($this->formOpened === false) && ($formName === false)) {
                        // Thrown an exception
                        throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
-               }
+               } // END - if
 
                // Close the form is default
                $formContent = "</form>";
@@ -119,8 +142,9 @@ class WebFormHelper extends BaseHelper {
                // Check wether we shall open or close the form
                if ($this->formOpened === false) {
                        // Add HTML code
-                       $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s\" method=\"%s\" target=\"%s\"",
+                       $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
                                $formName,
+                               $this->getConfigInstance()->readConfig('base_url'),
                                $this->getConfigInstance()->readConfig('form_action'),
                                $this->getConfigInstance()->readConfig('form_method'),
                                $this->getConfigInstance()->readConfig('form_target')
@@ -148,9 +172,9 @@ class WebFormHelper extends BaseHelper {
                        if ($this->groupOpened === true) {
                                // Then automatically close it here
                                $this->addFormGroup("", "");
-                       }
+                       } // END - if
 
-                       // @TODO Add some unique PIN here to bypass problems with some browser and/or extensions
+                       /* @TODO Add some unique PIN here to bypass problems with some browser and/or extensions */
                        // Simply close it
                        $this->formOpened = false;
                }
@@ -163,8 +187,8 @@ class WebFormHelper extends BaseHelper {
         * Add a text 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
         */
@@ -173,7 +197,7 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // 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\" />",
@@ -186,6 +210,21 @@ class WebFormHelper extends BaseHelper {
                $this->addContent($inputContent);
        }
 
+       /**
+        * Add a text input tag to the form with pre-loaded default value
+        *
+        * @param       $fieldName      Input field name
+        * @return      void
+        */
+       public function addInputTextFieldWithDefault ($fieldName) {
+               // Get the value from instance
+               $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));
+               //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
+
+               // Add the text field
+               $this->addInputTextField($fieldName, $fieldValue);
+       }
+
        /**
         * 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.
@@ -200,7 +239,7 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // 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\" />",
@@ -227,7 +266,7 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
-               }
+               } // END - if
 
                // Generate the content
                $inputContent = sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />",
@@ -253,7 +292,7 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
-               }
+               } // END - if
 
                // Set wether the check box is checked...
                $checked = " checked=\"checked\"";
@@ -283,7 +322,7 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // 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\" />",
@@ -308,7 +347,7 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // 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\" />",
@@ -335,13 +374,13 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // Throw exception here
                        throw new FormClosedException(array($this, $groupName), self::EXCEPTION_CLOSED_FORM);
-               }
+               } // END - if
 
                // At least the group name should be set
                if ((empty($groupName)) && ($this->groupOpened === false)) {
                        // Throw exception here
                        throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               }
+               } // END - if
 
                // Initialize content with closing div by default
                $content = "    </div>\n</div><!-- Group - CLOSE //-->";
@@ -372,7 +411,7 @@ class WebFormHelper extends BaseHelper {
                        if ($this->subGroupOpened === true) {
                                // Close it here
                                $this->addFormSubGroup("", "");
-                       }
+                       } // END - if
 
                        // Add the content
                        $this->addContent($content);
@@ -383,7 +422,7 @@ class WebFormHelper extends BaseHelper {
                        // All call it again if the group name is not empty
                        if (!empty($groupName)) {
                                $this->addFormGroup($groupName, $groupText);
-                       }
+                       } // END - if
                }
        }
 
@@ -403,13 +442,13 @@ class WebFormHelper extends BaseHelper {
                if ($this->groupOpened === false) {
                        // Throw exception here
                        throw new FormGroupClosedException(array($this, $subGroupName), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
-               }
+               } // END - if
 
                // At least the sub group name should be set
                if ((empty($subGroupName)) && ($this->subGroupOpened === false)) {
                        // Throw exception here
                        throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               }
+               } // END - if
 
                // Initialize content with closing div by default
                $content = "    </div>\n</div><!-- Sub group- CLOSE //-->";
@@ -446,7 +485,7 @@ class WebFormHelper extends BaseHelper {
                        // All call it again if sub group name is not empty
                        if (!empty($subGroupName)) {
                                $this->addFormSubGroup($subGroupName, $subGroupText);
-                       }
+                       } // END - if
                }
        }
 
@@ -464,7 +503,7 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // Throw an exception
                        throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
-               }
+               } // END - if
 
                // Set the block type
                $block = "div";
@@ -497,13 +536,13 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === false) {
                        // Throw an exception
                        throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
-               }
+               } // END - if
 
                // Is a group open?
                if ($this->groupOpened === true) {
                        // Then automatically close it here
-                       $this->addFormGroup("unknown", "");
-               }
+                       $this->addFormGroup("", "");
+               } // END - if
 
                // Generate the content
                $inputContent = sprintf("       <div id=\"form_note\">
@@ -566,6 +605,76 @@ class WebFormHelper extends BaseHelper {
                return $required;
        }
 
+       /**
+        * Checks wether login is enabled or disabled
+        *
+        * @return      $isEnabled      Wether the login is enabled or disabled
+        */
+       public function ifLoginIsEnabled () {
+               $isEnabled = ($this->getConfigInstance()->readConfig('login_enabled') == "Y");
+               return $isEnabled;
+       }
+
+       /**
+        * Checks wether login shall be done by username
+        *
+        * @return      $isEnabled      Wether the login shall be done by username
+        */
+       public function ifLoginWithUsername () {
+               $isEnabled = ($this->getConfigInstance()->readConfig('login_type') == "username");
+               return $isEnabled;
+       }
+
+       /**
+        * Checks wether login shall be done by email
+        *
+        * @return      $isEnabled      Wether the login shall be done by email
+        */
+       public function ifLoginWithEmail () {
+               $isEnabled = ($this->getConfigInstance()->readConfig('login_type') == "email");
+               return $isEnabled;
+       }
+
+       /**
+        * Checks wether guest login is allowed
+        *
+        * @return      $isAllowed      Wether guest login is allowed
+        */
+       public function ifGuestLoginAllowed () {
+               $isAllowed = ($this->getConfigInstance()->readConfig('guest_login_allowed') == "Y");
+               return $isAllowed;
+       }
+
+       /**
+        * Checks wether the email address change must be confirmed
+        *
+        * @return      $requireConfirm         Wether email change must be confirmed
+        */
+       public function ifEmailChangeRequireConfirmation () {
+               $requireConfirm = ($this->getConfigInstance()->readConfig('email_change_confirmation') == "Y");
+               return $requireConfirm;
+       }
+
+       /**
+        * Checks wether the rules has been updated
+        *
+        * @return      $rulesUpdated   Wether rules has been updated
+        */
+       public function ifRulesHaveChanged () {
+               /* @TODO Implement check if rules have been changed */
+               return false;
+       }
+
+       /**
+        * Checks wether email change is allowed
+        *
+        * @return      $emailChange    Wether changing email address is allowed
+        */
+       public function ifEmailChangeAllowed () {
+               $emailChange = ($this->getConfigInstance()->readConfig('email_change_allowed') == "Y");
+               return $emailChange;
+       }
+
        /**
         * Flushs the content out (not yet secured against open forms, etc.!) or
         * throw an exception if it is not yet closed
@@ -578,11 +687,25 @@ class WebFormHelper extends BaseHelper {
                if ($this->formOpened === true) {
                        // Throw an exception
                        throw new FormOpenedException ($this, self::EXCEPTION_OPENED_FORM);
-               }
+               } // END - if
 
                // Send content to template engine
                $this->getTemplateInstance()->assignVariable($this->formName, $this->getContent());
        }
+
+       /**
+        * Getter for direct field values
+        *
+        * @param       $fieldName              Name of the field we shall fetch
+        * @return      $fieldValue             Value from field
+        */
+       public function getField ($fieldName) {
+               // Get the field value
+               $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));
+
+               // Return it
+               return $fieldValue;
+       }
 }
 
 // [EOF]