]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/helper/web/class_WebFormHelper.php
TemplateEngine is known as WebTemplateEngine (most parts are in BasTemplateEngine...
[shipsimu.git] / inc / classes / main / helper / web / class_WebFormHelper.php
index ddb10a84c38204725eb9dcf56f6fac23fa336e9d..8d20001046a071f98afbff5556decf17bb8b727c 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
         */
@@ -53,6 +53,11 @@ class WebFormHelper extends BaseHelper {
         */
        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;
@@ -81,9 +86,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 +100,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;
@@ -141,7 +155,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,
@@ -151,23 +165,19 @@ 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) {
@@ -194,7 +204,7 @@ 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
@@ -236,7 +246,7 @@ 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
@@ -263,7 +273,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
@@ -320,7 +330,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
@@ -350,14 +360,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
                );
 
@@ -375,15 +385,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
                );
 
@@ -402,7 +412,7 @@ class WebFormHelper extends BaseHelper {
         */
        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
@@ -531,7 +541,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
@@ -564,7 +574,7 @@ class WebFormHelper extends BaseHelper {
         */
        public function addFormNote ($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
@@ -742,7 +752,7 @@ class WebFormHelper extends BaseHelper {
         * @return      $isSecured      Wether this form is secured by a CAPTCHA
         */
        public function ifFormSecuredWithCaptcha () {
-               $isSecured = ($this->getConfigInstance()->readConfig($this->formName."_captcha_secured") == "Y");
+               $isSecured = ($this->getConfigInstance()->readConfig($this->getFormName()."_captcha_secured") == "Y");
                return $isSecured;
        }
 
@@ -755,13 +765,13 @@ class WebFormHelper extends BaseHelper {
         */
        public function flushContent () {
                // Is the form still open?
-               if ($this->formOpened === true) {
+               if (($this->formOpened === true) && ($this->formEnabled === true)) {
                        // Close the form automatically
                        $this->addFormTag();
                } // END - if
 
                // Send content to template engine
-               $this->getTemplateInstance()->assignVariable($this->formName, $this->getContent());
+               $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->getContent());
        }
 
        /**
@@ -788,7 +798,7 @@ class WebFormHelper extends BaseHelper {
                $extraInstance = Registry::getRegistry()->getInstance('extra');
 
                // Get a configured instance
-               $captchaInstance = ObjectFactory::createObjectByConfiguredName("{$this->formName}_captcha", array($this->getTemplateInstance(), $extraInstance));
+               $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName()."_captcha", array($this->getTemplateInstance(), $extraInstance));
 
                // Initiate the CAPTCHA
                $captchaInstance->initiateCaptcha();
@@ -799,6 +809,35 @@ class WebFormHelper extends BaseHelper {
                // 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]