00001 <?php
00024 class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
00029 private $formOpened = false;
00030
00034 private $formName = "";
00035
00039 private $formEnabled = true;
00040
00041
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
00054 parent::__construct(__CLASS__);
00055 }
00056
00066 public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false, $withForm = true) {
00067
00068 $helperInstance = new WebFormHelper();
00069
00070
00071 $helperInstance->setTemplateInstance($templateInstance);
00072
00073
00074 if ($formId === false) {
00075
00076 $formId = $formName;
00077 }
00078
00079
00080 $helperInstance->setFormName($formName);
00081
00082
00083 if ($withForm === true) {
00084
00085 $helperInstance->addFormTag($formName, $formId);
00086 } else {
00087
00088 $helperInstance->enableForm(false);
00089 }
00090
00091
00092 return $helperInstance;
00093 }
00094
00104 public function addFormTag ($formName = false, $formId = false) {
00105
00106 if (($this->formOpened === false) && ($formName === false)) {
00107
00108 throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
00109 }
00110
00111
00112 $formContent = "</form>";
00113
00114
00115 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00116
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
00126 $formContent .= sprintf(" id=\"%s_form\"",
00127 $formId
00128 );
00129
00130
00131 $formContent .= ">";
00132
00133
00134 $this->formOpened = true;
00135
00136
00137 $this->addHeaderContent($formContent);
00138 } else {
00139
00140 $this->addInputHiddenField('form', $this->getFormName());
00141
00142
00143 if ($this->ifGroupOpenedPreviously()) {
00144
00145 $this->addFormGroup();
00146 }
00147
00148
00149 $this->formOpened = false;
00150
00151
00152 $this->addFooterContent($formContent);
00153 }
00154 }
00155
00165 public function addInputTextField ($fieldName, $fieldValue = "") {
00166
00167 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00168
00169 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00170 }
00171
00172
00173 $inputContent = sprintf("<input type=\"text\" class=\"textfield %s_field\" name=\"%s\" value=\"%s\" />",
00174 $fieldName,
00175 $fieldName,
00176 $fieldValue
00177 );
00178
00179
00180 $this->addContentToPreviousGroup($inputContent);
00181 }
00182
00189 public function addInputTextFieldWithDefault ($fieldName) {
00190
00191 $fieldValue = $this->getValueField($fieldName);
00192
00193
00194
00195 $this->addInputTextField($fieldName, $fieldValue);
00196 }
00197
00207 public function addInputPasswordField ($fieldName, $fieldValue = "") {
00208
00209 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00210
00211 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00212 }
00213
00214
00215 $inputContent = sprintf("<input type=\"password\" class=\"password %s_field\" name=\"%s\" value=\"%s\" />",
00216 $fieldName,
00217 $fieldName,
00218 $fieldValue
00219 );
00220
00221
00222 $this->addContentToPreviousGroup($inputContent);
00223 }
00224
00234 public function addInputHiddenField ($fieldName, $fieldValue = "") {
00235
00236 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00237
00238 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00239 }
00240
00241
00242 $inputContent = sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />",
00243 $fieldName,
00244 $fieldValue
00245 );
00246
00247
00248 $this->addContentToPreviousGroup($inputContent);
00249 }
00250
00257 public function addInputHiddenFieldWithDefault ($fieldName) {
00258
00259 $fieldValue = $this->getValueField($fieldName);
00260
00261
00262
00263 $this->addInputHiddenField($fieldName, $fieldValue);
00264 }
00265
00273 public function addInputHiddenConfiguredField ($fieldName, $prefix) {
00274
00275 $fieldValue = $this->getConfigInstance()->readConfig("{$prefix}_{$fieldName}");
00276
00277
00278
00279 $this->addInputHiddenField($fieldName, $fieldValue);
00280 }
00281
00291 public function addInputCheckboxField ($fieldName, $fieldChecked = true) {
00292
00293 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00294
00295 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00296 }
00297
00298
00299 $checked = " checked=\"checked\"";
00300 if ($fieldChecked === false) $checked = " ";
00301
00302
00303 $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox %s_field\" value=\"1\"%s/>",
00304 $fieldName,
00305 $fieldName,
00306 $checked
00307 );
00308
00309
00310 $this->addContentToPreviousGroup($inputContent);
00311 }
00312
00321 public function addInputResetButton ($buttonText) {
00322
00323 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00324
00325 throw new FormClosedException (array($this, "reset"), self::EXCEPTION_CLOSED_FORM);
00326 }
00327
00328
00329 $inputContent = sprintf("<input type=\"reset\" class=\"reset_button\" id=\"%s_reset\" value=\"%s\" />",
00330 $this->getFormName(),
00331 $buttonText
00332 );
00333
00334
00335 $this->addContentToPreviousGroup($inputContent);
00336 }
00337
00346 public function addInputSubmitButton ($buttonText) {
00347
00348 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00349
00350 throw new FormClosedException (array($this, "submit"), self::EXCEPTION_CLOSED_FORM);
00351 }
00352
00353
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
00361 $this->addContentToPreviousGroup($inputContent);
00362 }
00363
00373 public function addFormGroup ($groupId = "", $groupText = "") {
00374
00375 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00376
00377 throw new FormClosedException(array($this, $groupId), self::EXCEPTION_CLOSED_FORM);
00378 }
00379
00380
00381 if ((empty($groupId)) && ($this->ifGroupOpenedPreviously() === false)) {
00382
00383 throw new EmptyVariableException(array($this, 'groupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00384 } elseif (empty($groupId)) {
00385
00386 $groupId = $this->getPreviousGroupId();
00387 }
00388
00389
00390 if (($this->ifGroupOpenedPreviously() === false) && ($groupId === $this->getPreviousGroupId())) {
00391
00392 return false;
00393 }
00394
00395
00396 $content = " </div>\n</div><!-- Group - CLOSE //-->";
00397
00398
00399 if ($this->ifGroupOpenedPreviously() === false) {
00400
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
00415 $this->openGroupByIdContent($groupId, $content, "div");
00416 } else {
00417
00418 if ($this->ifSubGroupOpenedPreviously()) {
00419
00420 $this->addFormSubGroup();
00421 }
00422
00423
00424 $prevGroupId = $this->getPreviousGroupId();
00425
00426
00427 $this->closePreviousGroupByContent($content);
00428
00429
00430 if ((!empty($groupId)) && ($groupId != $prevGroupId)) {
00431
00432 $this->addFormGroup($groupId, $groupText);
00433 }
00434 }
00435 }
00436
00448 public function addFormSubGroup ($subGroupId = "", $subGroupText = "") {
00449
00450 if ($this->ifGroupOpenedPreviously() === false) {
00451
00452 throw new FormFormClosedException(array($this, $subGroupId), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
00453 }
00454
00455
00456 if ((empty($subGroupId)) && ($this->ifSubGroupOpenedPreviously() === false)) {
00457
00458 throw new EmptyVariableException(array($this, 'subGroupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00459 } elseif (empty($subGroupId)) {
00460
00461 $subGroupId = $this->getPreviousSubGroupId();
00462 }
00463
00464
00465 if (($this->ifSubGroupOpenedPreviously() === false) && ($subGroupId == $this->getPreviousSubGroupId())) {
00466
00467 return false;
00468 }
00469
00470
00471 $content = " </div>\n</div><!-- Sub group- CLOSE //-->";
00472
00473
00474 if ($this->ifSubGroupOpenedPreviously() === false) {
00475
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
00490 $this->openSubGroupByIdContent($subGroupId, $content, "div");
00491 } else {
00492
00493 $prevSubGroupId = $this->getPreviousSubGroupId();
00494
00495
00496 $this->closePreviousSubGroupByContent($content);
00497
00498
00499 if ((!empty($subGroupId)) && ($subGroupId != $prevSubGroupId)) {
00500 $this->addFormSubGroup($subGroupId, $subGroupText);
00501 }
00502 }
00503 }
00504
00514 public function addFieldText ($fieldName, $fieldText) {
00515
00516 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00517
00518 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
00519 }
00520
00521
00522 $block = "div";
00523 if ($this->ifGroupOpenedPreviously()) $block = "span";
00524
00525
00526 $inputContent = sprintf(" <%s id=\"%s_text\">
00527 %s
00528 </%s>",
00529 $block,
00530 $fieldName,
00531 $fieldText,
00532 $block
00533 );
00534
00535
00536 $this->addContentToPreviousGroup($inputContent);
00537 }
00538
00548 public function addFormNote ($noteId, $formNotes) {
00549
00550 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00551
00552 throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
00553 }
00554
00555
00556 $inputContent = sprintf(" <div id=\"form_note_%s\">
00557 %s
00558 </div>",
00559 $noteId,
00560 $formNotes
00561 );
00562
00563
00564 $this->addContentToPreviousGroup($inputContent);
00565 }
00566
00576 public function addInputSelectField ($selectId, $firstEntry) {
00577
00578 if (($this->formOpened === false) && ($this->formEnabled === true)) {
00579
00580 throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
00581 }
00582
00583
00584 if (($this->ifSubGroupOpenedPreviously() === false) && ($this->getPreviousSubGroupId() !== $selectId)) {
00585
00586 if (!empty($firstEntry)) {
00587
00588 $firstEntry = sprintf("<option value=\"invalid\" disabled=\"disabled\">%s</option>\n",
00589 $firstEntry
00590 );
00591 }
00592
00593
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
00602 $this->openSubGroupByIdContent($selectId, $content, "select");
00603 } elseif ($this->getPreviousSubGroupId() != $selectId) {
00604
00605 $this->debugInstance(__METHOD__."(): Previous sub group id {$this->getPreviousSubGroupId()} does not match current id {$selectId}.");
00606 } else {
00607
00608 $this->closePreviousSubGroupByContent("</select>");
00609 }
00610 }
00611
00623 public function addSelectSubOption ($subName, $subValue) {
00624
00625 if ($this->ifSubGroupOpenedPreviously() === false) {
00626
00627 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00628 }
00629
00630
00631 $content = sprintf("<option value=\"invalid\" class=\"suboption suboption_%s\" disabled=\"disabled\">%s</option>\n",
00632 $subName,
00633 $subValue
00634 );
00635
00636
00637 $this->addContentToPreviousGroup($content);
00638 }
00639
00651 public function addSelectOption ($optionName, $optionValue) {
00652
00653 if ($this->ifSubGroupOpenedPreviously() === false) {
00654
00655 throw new HelperNoPreviousOpenedSubGroupException(array($this, $content), self::EXCEPTION_NO_PREVIOUS_SUB_GROUP_OPENED);
00656 }
00657
00658
00659 $content = sprintf("<option value=\"%s\" class=\"option option_%s\">%s</option>\n",
00660 $optionName,
00661 $optionName,
00662 $optionValue
00663 );
00664
00665
00666 $this->addContentToPreviousGroup($content);
00667 }
00668
00674 public function addCaptcha () {
00675
00676 $extraInstance = Registry::getRegistry()->getInstance('extra');
00677
00678
00679 $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName().'_captcha', array($this, $extraInstance));
00680
00681
00682 $captchaInstance->initiateCaptcha();
00683
00684
00685 $captchaInstance->renderCode();
00686
00687
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
00759 if (($this->formOpened === true) && ($this->formEnabled === true)) {
00760
00761 $this->addFormTag();
00762 } elseif ($this->formEnabled === false) {
00763 if ($this->ifSubGroupOpenedPreviously()) {
00764
00765 $this->addFormSubGroup();
00766 } elseif ($this->ifGroupOpenedPreviously()) {
00767
00768 $this->addFormGroup();
00769 }
00770 }
00771
00772
00773
00774 $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->renderContent());
00775 }
00776 }
00777
00778
00779 ?>