class_WebFormHelper.php

Go to the documentation of this file.
00001 <?php
00024 class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
00029         private $formOpened = false;
00030 
00034         private $formName = "";
00035 
00039         private $formEnabled = true;
00040 
00041         // Class Constants
00042         const EXCEPTION_FORM_NAME_INVALID       = 0x120;
00043         const EXCEPTION_CLOSED_FORM             = 0x121;
00044         const EXCEPTION_OPENED_FORM             = 0x122;
00045         const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x123;
00046 
00052         protected function __construct () {
00053                 // Call parent constructor
00054                 parent::__construct(__CLASS__);
00055         }
00056 
00066         public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false, $withForm = true) {
00067                 // Get new instance
00068                 $helperInstance = new WebFormHelper();
00069 
00070                 // Set template instance
00071                 $helperInstance->setTemplateInstance($templateInstance);
00072 
00073                 // Is the form id not set?
00074                 if ($formId === false) {
00075                         // Use form id from form name
00076                         $formId = $formName;
00077                 } // END - if
00078 
00079                 // Set form name
00080                 $helperInstance->setFormName($formName);
00081 
00082                 // A form-less field may say "false" here...
00083                 if ($withForm === true) {
00084                         // Create the form
00085                         $helperInstance->addFormTag($formName, $formId);
00086                 } else {
00087                         // Disable form
00088                         $helperInstance->enableForm(false);
00089                 }
00090 
00091                 // Return the prepared instance
00092                 return $helperInstance;
00093         }
00094 
00104         public function addFormTag ($formName = false, $formId = false) {
00105                 // When the form is not yet opened at least form name must be valid
00106                 if (($this->formOpened === false) && ($formName === false)) {
00107                         // Thrown an exception
00108                         throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
00109                 } // END - if
00110 
00111                 // Close the form is default
00112                 $formContent = "</form>";
00113 
00114                 // Check wether we shall open or close the form
00115                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00116                         // Add HTML code
00117                         $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
00118                                 $formName,
00119                                 $this->getConfigInstance()->readConfig('base_url'),
00120                                 $this->getConfigInstance()->readConfig('form_action'),
00121                                 $this->getConfigInstance()->readConfig('form_method'),
00122                                 $this->getConfigInstance()->readConfig('form_target')
00123                         );
00124 
00125                         // Add form id as well
00126                         $formContent .= sprintf(" id=\"%s_form\"",
00127                                 $formId
00128                         );
00129 
00130                         // Add close bracket
00131                         $formContent .= ">";
00132 
00133                         // Open the form and remeber the form name
00134                         $this->formOpened = true;
00135 
00136                         // Add it to the content
00137                         $this->addHeaderContent($formContent);
00138                 } else {
00139                         // Add the hidden field required to identify safely this form
00140                         $this->addInputHiddenField('form', $this->getFormName());
00141 
00142                         // Is a group open?
00143                         if ($this->ifGroupOpenedPreviously()) {
00144                                 // Then automatically close it here
00145                                 $this->addFormGroup();
00146                         } // END - if
00147 
00148                         // Simply close it
00149                         $this->formOpened = false;
00150 
00151                         // Add it to the content
00152                         $this->addFooterContent($formContent);
00153                 }
00154         }
00155 
00165         public function addInputTextField ($fieldName, $fieldValue = "") {
00166                 // Is the form opened?
00167                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00168                         // Throw an exception
00169                         throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00170                 } // END - if
00171 
00172                 // Generate the content
00173                 $inputContent = sprintf("<input type=\"text\" class=\"textfield %s_field\" name=\"%s\" value=\"%s\" />",
00174                         $fieldName,
00175                         $fieldName,
00176                         $fieldValue
00177                 );
00178 
00179                 // And add it maybe with a "li" tag
00180                 $this->addContentToPreviousGroup($inputContent);
00181         }
00182 
00189         public function addInputTextFieldWithDefault ($fieldName) {
00190                 // Get the value from instance
00191                 $fieldValue = $this->getValueField($fieldName);
00192                 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
00193 
00194                 // Add the text field
00195                 $this->addInputTextField($fieldName, $fieldValue);
00196         }
00197 
00207         public function addInputPasswordField ($fieldName, $fieldValue = "") {
00208                 // Is the form opened?
00209                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00210                         // Throw an exception
00211                         throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00212                 } // END - if
00213 
00214                 // Generate the content
00215                 $inputContent = sprintf("<input type=\"password\" class=\"password %s_field\" name=\"%s\" value=\"%s\" />",
00216                         $fieldName,
00217                         $fieldName,
00218                         $fieldValue
00219                 );
00220 
00221                 // And add it
00222                 $this->addContentToPreviousGroup($inputContent);
00223         }
00224 
00234         public function addInputHiddenField ($fieldName, $fieldValue = "") {
00235                 // Is the form opened?
00236                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00237                         // Throw an exception
00238                         throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00239                 } // END - if
00240 
00241                 // Generate the content
00242                 $inputContent = sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />",
00243                         $fieldName,
00244                         $fieldValue
00245                 );
00246 
00247                 // And add it
00248                 $this->addContentToPreviousGroup($inputContent);
00249         }
00250 
00257         public function addInputHiddenFieldWithDefault ($fieldName) {
00258                 // Get the value from instance
00259                 $fieldValue = $this->getValueField($fieldName);
00260                 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
00261 
00262                 // Add the text field
00263                 $this->addInputHiddenField($fieldName, $fieldValue);
00264         }
00265 
00273         public function addInputHiddenConfiguredField ($fieldName, $prefix) {
00274                 // Get the value from instance
00275                 $fieldValue = $this->getConfigInstance()->readConfig("{$prefix}_{$fieldName}");
00276                 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
00277 
00278                 // Add the text field
00279                 $this->addInputHiddenField($fieldName, $fieldValue);
00280         }
00281 
00291         public function addInputCheckboxField ($fieldName, $fieldChecked = true) {
00292                 // Is the form opened?
00293                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00294                         // Throw an exception
00295                         throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00296                 } // END - if
00297 
00298                 // Set wether the check box is checked...
00299                 $checked = " checked=\"checked\"";
00300                 if ($fieldChecked === false) $checked = " ";
00301 
00302                 // Generate the content
00303                 $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox %s_field\" value=\"1\"%s/>",
00304                         $fieldName,
00305                         $fieldName,
00306                         $checked
00307                 );
00308 
00309                 // And add it
00310                 $this->addContentToPreviousGroup($inputContent);
00311         }
00312 
00321         public function addInputResetButton ($buttonText) {
00322                 // Is the form opened?
00323                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00324                         // Throw an exception
00325                         throw new FormClosedException (array($this, "reset"), self::EXCEPTION_CLOSED_FORM);
00326                 } // END - if
00327 
00328                 // Generate the content
00329                 $inputContent = sprintf("<input type=\"reset\" class=\"reset_button\" id=\"%s_reset\" value=\"%s\" />",
00330                         $this->getFormName(),
00331                         $buttonText
00332                 );
00333 
00334                 // And add it
00335                 $this->addContentToPreviousGroup($inputContent);
00336         }
00337 
00346         public function addInputSubmitButton ($buttonText) {
00347                 // Is the form opened?
00348                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00349                         // Throw an exception
00350                         throw new FormClosedException (array($this, "submit"), self::EXCEPTION_CLOSED_FORM);
00351                 } // END - if
00352 
00353                 // Generate the content
00354                 $inputContent = sprintf("<input type=\"submit\" class=\"submit_button\" id=\"%s_submit\" name=\"%s_button\" value=\"%s\" />",
00355                         $this->getFormName(),
00356                         $this->getFormName(),
00357                         $buttonText
00358                 );
00359 
00360                 // And add it
00361                 $this->addContentToPreviousGroup($inputContent);
00362         }
00363 
00373         public function addFormGroup ($groupId = "", $groupText = "") {
00374                 // Is a form opened?
00375                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00376                         // Throw exception here
00377                         throw new FormClosedException(array($this, $groupId), self::EXCEPTION_CLOSED_FORM);
00378                 } // END - if
00379 
00380                 // At least the group name should be set
00381                 if ((empty($groupId)) && ($this->ifGroupOpenedPreviously() === false)) {
00382                         // Throw exception here
00383                         throw new EmptyVariableException(array($this, 'groupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00384                 } elseif (empty($groupId)) {
00385                         // Close the last opened
00386                         $groupId = $this->getPreviousGroupId();
00387                 }
00388 
00389                 // Same group to open?
00390                 if (($this->ifGroupOpenedPreviously() === false) && ($groupId === $this->getPreviousGroupId())) {
00391                         // Abort here silently
00392                         return false;
00393                 } // END - if
00394 
00395                 // Initialize content with closing div by default
00396                 $content = "    </div>\n</div><!-- Group - CLOSE //-->";
00397 
00398                 // Is this group opened?
00399                 if ($this->ifGroupOpenedPreviously() === false) {
00400                         // Begin the div/span blocks
00401                         $content = sprintf("<!-- Group %s - OPEN //-->
00402 <div class=\"group_box\" id=\"%s_group_box\">
00403         <span class=\"group_text\" id=\"%s_group_text\">
00404                 %s
00405         </span>
00406         <div class=\"group_field\" id=\"%s_group_field\">",
00407                                 $groupId,
00408                                 $groupId,
00409                                 $groupId,
00410                                 $groupText,
00411                                 $groupId
00412                         );
00413 
00414                         // Switch the state
00415                         $this->openGroupByIdContent($groupId, $content, "div");
00416                 } else {
00417                         // Is a sub group opened?
00418                         if ($this->ifSubGroupOpenedPreviously()) {
00419                                 // Close it here
00420                                 $this->addFormSubGroup();
00421                         } // END - if
00422 
00423                         // Get previous group id
00424                         $prevGroupId = $this->getPreviousGroupId();
00425 
00426                         // Switch the state
00427                         $this->closePreviousGroupByContent($content);
00428 
00429                         // All call it again if group name is not empty
00430                         if ((!empty($groupId)) && ($groupId != $prevGroupId)) {
00431                                 //* DEBUG: */ echo $groupId."/".$prevGroupId."<br />\n";
00432                                 $this->addFormGroup($groupId, $groupText);
00433                         } // END - if
00434                 }
00435         }
00436 
00448         public function addFormSubGroup ($subGroupId = "", $subGroupText = "") {
00449                 // Is a group opened?
00450                 if ($this->ifGroupOpenedPreviously() === false) {
00451                         // Throw exception here
00452                         throw new FormFormClosedException(array($this, $subGroupId), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
00453                 } // END - if
00454 
00455                 // At least the sub group name should be set
00456                 if ((empty($subGroupId)) && ($this->ifSubGroupOpenedPreviously() === false)) {
00457                         // Throw exception here
00458                         throw new EmptyVariableException(array($this, 'subGroupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00459                 } elseif (empty($subGroupId)) {
00460                         // Close the last opened
00461                         $subGroupId = $this->getPreviousSubGroupId();
00462                 }
00463 
00464                 // Same sub group to open?
00465                 if (($this->ifSubGroupOpenedPreviously() === false) && ($subGroupId == $this->getPreviousSubGroupId())) {
00466                         // Abort here silently
00467                         return false;
00468                 } // END - if
00469 
00470                 // Initialize content with closing div by default
00471                 $content = "    </div>\n</div><!-- Sub group- CLOSE //-->";
00472 
00473                 // Is this group opened?
00474                 if ($this->ifSubGroupOpenedPreviously() === false) {
00475                         // Begin the span block
00476                         $content = sprintf("<!-- Sub group %s - OPEN //-->
00477 <div class=\"subgroup_box\" id=\"%s_subgroup_box\">
00478         <span class=\"subgroup_text\" id=\"%s_subgroup_text\">
00479                 %s
00480         </span>
00481         <div class=\"subgroup_field\" id=\"%s_subgroup_field\">",
00482                                 $subGroupId,
00483                                 $subGroupId,
00484                                 $subGroupId,
00485                                 $subGroupText,
00486                                 $subGroupId
00487                         );
00488 
00489                         // Switch the state and remeber the name
00490                         $this->openSubGroupByIdContent($subGroupId, $content, "div");
00491                 } else {
00492                         // Get previous sub group id
00493                         $prevSubGroupId = $this->getPreviousSubGroupId();
00494 
00495                         // Switch the state
00496                         $this->closePreviousSubGroupByContent($content);
00497 
00498                         // All call it again if sub group name is not empty
00499                         if ((!empty($subGroupId)) && ($subGroupId != $prevSubGroupId)) {
00500                                 $this->addFormSubGroup($subGroupId, $subGroupText);
00501                         } // END - if
00502                 }
00503         }
00504 
00514         public function addFieldText ($fieldName, $fieldText) {
00515                 // Is the form opened?
00516                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00517                         // Throw an exception
00518                         throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00519                 } // END - if
00520 
00521                 // Set the block type
00522                 $block = "div";
00523                 if ($this->ifGroupOpenedPreviously()) $block = "span";
00524 
00525                 // Generate the content
00526                 $inputContent = sprintf("       <%s id=\"%s_text\">
00527                 %s
00528         </%s>",
00529                         $block,
00530                         $fieldName,
00531                         $fieldText,
00532                         $block
00533                 );
00534 
00535                 // And add it
00536                 $this->addContentToPreviousGroup($inputContent);
00537         }
00538 
00548         public function addFormNote ($noteId, $formNotes) {
00549                 // Is the form opened?
00550                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00551                         // Throw an exception
00552                         throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
00553                 } // END - if
00554 
00555                 // Generate the content
00556                 $inputContent = sprintf("       <div id=\"form_note_%s\">
00557                 %s
00558         </div>",
00559                         $noteId,
00560                         $formNotes
00561                 );
00562 
00563                 // And add it
00564                 $this->addContentToPreviousGroup($inputContent);
00565         }
00566 
00576         public function addInputSelectField ($selectId, $firstEntry) {
00577                 // Is the form group opened?
00578                 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00579                         // Throw an exception
00580                         throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
00581                 } // END - if
00582 
00583                 // Shall we close or open the sub group?
00584                 if (($this->ifSubGroupOpenedPreviously() === false) && ($this->getPreviousSubGroupId() !== $selectId)) {
00585                         // Initialize first entry (which might be non-selectable if content is provided
00586                         if (!empty($firstEntry)) {
00587                                 // Add selection around it
00588                                 $firstEntry = sprintf("<option value=\"invalid\" disabled=\"disabled\">%s</option>\n",
00589                                         $firstEntry
00590                                 );
00591                         } // END - if
00592 
00593                         // Construct the opening select tag
00594                         $content = sprintf("<select class=\"select_box\" id=\"%s_%s\" name=\"%s\">\n%s",
00595                                 $this->getFormName(),
00596                                 $selectId,
00597                                 $selectId,
00598                                 $firstEntry
00599                         );
00600 
00601                         // Open the sub group
00602                         $this->openSubGroupByIdContent($selectId, $content, "select");
00603                 } elseif ($this->getPreviousSubGroupId() != $selectId) {
00604                         // Something went wrong!
00605                         $this->debugInstance(__METHOD__."(): Previous sub group id {$this->getPreviousSubGroupId()} does not match current id {$selectId}.");
00606                 } else {
00607                         // Close the sub group
00608                         $this->closePreviousSubGroupByContent("</select>");
00609                 }
00610         }
00611 
00623         public function addSelectSubOption ($subName, $subValue) {
00624                 // Is there a sub group (shall be a selection box!)
00625                 if ($this->ifSubGroupOpenedPreviously() === false) {
00626                         // Then throw an exception here
00627                         throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00628                 } // END - if
00629 
00630                 // Render the content
00631                 $content = sprintf("<option value=\"invalid\" class=\"suboption suboption_%s\" disabled=\"disabled\">%s</option>\n",
00632                         $subName,
00633                         $subValue
00634                 );
00635 
00636                 // Add the content to the previously opened sub group
00637                 $this->addContentToPreviousGroup($content);
00638         }
00639 
00651         public function addSelectOption ($optionName, $optionValue) {
00652                 // Is there a sub group (shall be a selection box!)
00653                 if ($this->ifSubGroupOpenedPreviously() === false) {
00654                         // Then throw an exception here
00655                         throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00656                 } // END - if
00657 
00658                 // Render the content
00659                 $content = sprintf("<option value=\"%s\" class=\"option option_%s\">%s</option>\n",
00660                         $optionName,
00661                         $optionName,
00662                         $optionValue
00663                 );
00664 
00665                 // Add the content to the previously opened sub group
00666                 $this->addContentToPreviousGroup($content);
00667         }
00668 
00674         public function addCaptcha () {
00675                 // Get last executed pre filter
00676                 $extraInstance = Registry::getRegistry()->getInstance('extra');
00677 
00678                 // Get a configured instance
00679                 $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName().'_captcha', array($this, $extraInstance));
00680 
00681                 // Initiate the CAPTCHA
00682                 $captchaInstance->initiateCaptcha();
00683 
00684                 // Render the CAPTCHA code
00685                 $captchaInstance->renderCode();
00686 
00687                 // Get the content and add it to the helper
00688                 $this->addContentToPreviousGroup($captchaInstance->renderContent());
00689         }
00690 
00697         public final function enableForm ($formEnabled = true) {
00698                 $this->formEnabled = (bool) $formEnabled;
00699         }
00700 
00707         public final function setFormName ($formName) {
00708                 $this->formName = (string) $formName;
00709         }
00710 
00716         public final function getFormName () {
00717                 return $this->formName;
00718         }
00719 
00725         public function ifRegisterRequiresEmailVerification () {
00726                 $required = ($this->getConfigInstance()->readConfig('register_requires_email') === "Y");
00727                 return $required;
00728         }
00729 
00735         public function ifRegisterIncludesProfile () {
00736                 $required = ($this->getConfigInstance()->readConfig('register_includes_profile') === "Y");
00737                 return $required;
00738         }
00739 
00745         public function ifFormSecuredWithCaptcha () {
00746                 $isSecured = ($this->getConfigInstance()->readConfig($this->getFormName().'_captcha_secured') === "Y");
00747                 return $isSecured;
00748         }
00749 
00757         public function flushContent () {
00758                 // Is the form still open?
00759                 if (($this->formOpened === true) && ($this->formEnabled === true)) {
00760                         // Close the form automatically
00761                         $this->addFormTag();
00762                 } elseif ($this->formEnabled === false) {
00763                         if ($this->ifSubGroupOpenedPreviously()) {
00764                                 // Close sub group
00765                                 $this->addFormSubGroup();
00766                         } elseif ($this->ifGroupOpenedPreviously()) {
00767                                 // Close group
00768                                 $this->addFormGroup();
00769                         }
00770                 }
00771 
00772                 // Send content to template engine
00773                 //* DEBUG: */ echo __METHOD__.": form=".$this->getFormName().", size=".strlen($this->renderContent())."<br />\n";
00774                 $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->renderContent());
00775         }
00776 }
00777 
00778 // [EOF]
00779 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6