3 * A helper for constructing web forms
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class WebFormHelper extends BaseWebHelper implements HelpableTemplate {
26 * Wether the form tag is opened (keep at false or else your forms will
29 private $formOpened = false;
34 private $formName = "";
37 * Wether form tag is enabled (default: true)
39 private $formEnabled = true;
42 const EXCEPTION_FORM_NAME_INVALID = 0x120;
43 const EXCEPTION_CLOSED_FORM = 0x121;
44 const EXCEPTION_OPENED_FORM = 0x122;
45 const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x123;
48 * Protected constructor
52 protected function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
58 * Creates the helper class with the given template engine instance and form name
60 * @param $templateInstance An instance of a valid template engine
61 * @param $formName Name of the form
62 * @param $formId Value for "id" attribute (default: $formName)
63 * @param $withForm Wether include the form tag
64 * @return $helperInstance A preparedf instance of this helper
66 public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false, $withForm = true) {
68 $helperInstance = new WebFormHelper();
70 // Set template instance
71 $helperInstance->setTemplateInstance($templateInstance);
73 // Is the form id not set?
74 if ($formId === false) {
75 // Use form id from form name
80 $helperInstance->setFormName($formName);
82 // A form-less field may say "false" here...
83 if ($withForm === true) {
85 $helperInstance->addFormTag($formName, $formId);
88 $helperInstance->enableForm(false);
91 // Return the prepared instance
92 return $helperInstance;
96 * Add the form tag or close it an already opened form tag
98 * @param $formName Name of the form (default: false)
99 * @param $formId Id of the form (attribute "id"; default: false)
101 * @throws InvalidFormNameException If the form name is invalid (=false)
102 * @todo Add some unique PIN here to bypass problems with some browser and/or extensions
104 public function addFormTag ($formName = false, $formId = false) {
105 // When the form is not yet opened at least form name must be valid
106 if (($this->formOpened === false) && ($formName === false)) {
107 // Thrown an exception
108 throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
111 // Close the form is default
112 $formContent = "</form>";
114 // Check wether we shall open or close the form
115 if (($this->formOpened === false) && ($this->formEnabled === true)) {
117 $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
119 $this->getConfigInstance()->readConfig('base_url'),
120 $this->getConfigInstance()->readConfig('form_action'),
121 $this->getConfigInstance()->readConfig('form_method'),
122 $this->getConfigInstance()->readConfig('form_target')
125 // Add form id as well
126 $formContent .= sprintf(" id=\"%s_form\"",
133 // Open the form and remeber the form name
134 $this->formOpened = true;
136 // Add the hidden field required to identify safely this form
137 $this->addInputHiddenField('form', $this->getFormName());
140 if ($this->ifGroupOpenedPreviously()) {
141 // Then automatically close it here
142 $this->addFormGroup();
146 $this->formOpened = false;
149 // Add it to the content
150 $this->addContent($formContent);
154 * Add a text input tag to the form or throw an exception if it is not yet
155 * opened. The field's name will be set as id.
157 * @param $fieldName Input field name
158 * @param $fieldValue Input default value (default: empty)
160 * @throws FormClosedException If the form is not yet opened
162 public function addInputTextField ($fieldName, $fieldValue = "") {
163 // Is the form opened?
164 if (($this->formOpened === false) && ($this->formEnabled === true)) {
165 // Throw an exception
166 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
169 // Generate the content
170 $inputContent = sprintf("<input type=\"text\" class=\"textfield %s_field\" name=\"%s\" value=\"%s\" />",
176 // And add it maybe with a "li" tag
177 $this->addContent($inputContent);
181 * Add a text input tag to the form with pre-loaded default value
183 * @param $fieldName Input field name
186 public function addInputTextFieldWithDefault ($fieldName) {
187 // Get the value from instance
188 $fieldValue = $this->getValueField($fieldName);
189 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
191 // Add the text field
192 $this->addInputTextField($fieldName, $fieldValue);
196 * Add a password input tag to the form or throw an exception if it is not
197 * yet opened. The field's name will be set as id.
199 * @param $fieldName Input field name
200 * @param $fieldValue Input default value (default: empty)
202 * @throws FormClosedException If the form is not yet opened
204 public function addInputPasswordField ($fieldName, $fieldValue = "") {
205 // Is the form opened?
206 if (($this->formOpened === false) && ($this->formEnabled === true)) {
207 // Throw an exception
208 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
211 // Generate the content
212 $inputContent = sprintf("<input type=\"password\" class=\"password %s_field\" name=\"%s\" value=\"%s\" />",
219 $this->addContent($inputContent);
223 * Add a hidden input tag to the form or throw an exception if it is not
224 * yet opened. The field's name will be set as id.
226 * @param $fieldName Input field name
227 * @param $fieldValue Input default value (default: empty)
229 * @throws FormClosedException If the form is not yet opened
231 public function addInputHiddenField ($fieldName, $fieldValue = "") {
232 // Is the form opened?
233 if (($this->formOpened === false) && ($this->formEnabled === true)) {
234 // Throw an exception
235 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
238 // Generate the content
239 $inputContent = sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />",
245 $this->addContent($inputContent);
249 * Add a hidden input tag to the form with pre-loaded default value
251 * @param $fieldName Input field name
254 public function addInputHiddenFieldWithDefault ($fieldName) {
255 // Get the value from instance
256 $fieldValue = $this->getValueField($fieldName);
257 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
259 // Add the text field
260 $this->addInputHiddenField($fieldName, $fieldValue);
264 * Add a hidden input tag to the form with configuration value
266 * @param $fieldName Input field name
267 * @param $prefix Prefix for configuration without trailing _
270 public function addInputHiddenConfiguredField ($fieldName, $prefix) {
271 // Get the value from instance
272 $fieldValue = $this->getConfigInstance()->readConfig("{$prefix}_{$fieldName}");
273 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
275 // Add the text field
276 $this->addInputHiddenField($fieldName, $fieldValue);
280 * Add a checkbox input tag to the form or throw an exception if it is not
281 * yet opened. The field's name will be set as id.
283 * @param $fieldName Input field name
284 * @param $fieldChecked Wether the field is checked (defaut: checked)
286 * @throws FormClosedException If the form is not yet opened
288 public function addInputCheckboxField ($fieldName, $fieldChecked = true) {
289 // Is the form opened?
290 if (($this->formOpened === false) && ($this->formEnabled === true)) {
291 // Throw an exception
292 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
295 // Set wether the check box is checked...
296 $checked = " checked=\"checked\"";
297 if ($fieldChecked === false) $checked = " ";
299 // Generate the content
300 $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox %s_field\" value=\"1\"%s/>",
307 $this->addContent($inputContent);
311 * Add a reset input tag to the form or throw an exception if it is not
312 * yet opened. The field's name will be set as id.
314 * @param $buttonText Text displayed on the button
316 * @throws FormClosedException If the form is not yet opened
318 public function addInputResetButton ($buttonText) {
319 // Is the form opened?
320 if (($this->formOpened === false) && ($this->formEnabled === true)) {
321 // Throw an exception
322 throw new FormClosedException (array($this, "reset"), self::EXCEPTION_CLOSED_FORM);
325 // Generate the content
326 $inputContent = sprintf("<input type=\"reset\" class=\"reset_button\" id=\"%s_reset\" value=\"%s\" />",
327 $this->getFormName(),
332 $this->addContent($inputContent);
336 * Add a reset input tag to the form or throw an exception if it is not
337 * yet opened. The field's name will be set as id.
339 * @param $buttonText Text displayed on the button
341 * @throws FormClosedException If the form is not yet opened
343 public function addInputSubmitButton ($buttonText) {
344 // Is the form opened?
345 if (($this->formOpened === false) && ($this->formEnabled === true)) {
346 // Throw an exception
347 throw new FormClosedException (array($this, "submit"), self::EXCEPTION_CLOSED_FORM);
350 // Generate the content
351 $inputContent = sprintf("<input type=\"submit\" class=\"submit_button\" id=\"%s_submit\" name=\"%s_button\" value=\"%s\" />",
352 $this->getFormName(),
353 $this->getFormName(),
358 $this->addContent($inputContent);
362 * Add a form group or close an already opened and open a new one
364 * @param $groupId Name of the group or last opened if empty
365 * @param $groupText Text including HTML to show above this group
367 * @throws FormClosedException If no form has been opened before
368 * @throws EmptyVariableException If $groupId is not set
370 public function addFormGroup ($groupId = "", $groupText = "") {
372 if (($this->formOpened === false) && ($this->formEnabled === true)) {
373 // Throw exception here
374 throw new FormClosedException(array($this, $groupId), self::EXCEPTION_CLOSED_FORM);
377 // At least the group name should be set
378 if ((empty($groupId)) && (!$this->ifGroupOpenedPreviously())) {
379 // Throw exception here
380 throw new EmptyVariableException(array($this, 'groupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
381 } elseif (empty($groupId)) {
382 // Close the last opened
383 $groupId = $this->getPreviousGroupId();
386 // Same group to open?
387 if ((!$this->ifGroupOpenedPreviously()) && ($groupId == $this->getPreviousGroupId())) {
388 // Abort here silently
392 // Initialize content with closing div by default
393 $content = " </div>\n</div><!-- Group - CLOSE //-->";
395 // Is this group opened?
396 if (!$this->ifGroupOpenedPreviously()) {
397 // Begin the div/span blocks
398 $content = sprintf("<!-- Group %s - OPEN //-->
399 <div class=\"group_box\" id=\"%s_group_box\">
400 <span class=\"group_text\" id=\"%s_group_text\">
403 <div class=\"group_field\" id=\"%s_group_field\">",
412 $this->openGroupByIdContent($groupId, $content);
414 // Is a sub group opened?
415 if ($this->ifSubGroupOpenedPreviously()) {
417 $this->addFormSubGroup();
421 $this->closePreviousGroupByContent($content);
423 // All call it again if the group name is not empty
424 if (!empty($groupId)) {
425 $this->addFormGroup($groupId, $groupText);
431 * Add a form sub group or close an already opened and open a new one or
432 * throws an exception if no group has been opened before or if the sub
433 * group name is empty.
435 * @param $subGroupId Name of the group or last opened if empty
436 * @param $subGroupText Text including HTML to show above this group
438 * @throws FormFormClosedException If no group has been opened before
439 * @throws EmptyVariableException If $subGroupId is not set
441 public function addFormSubGroup ($subGroupId = "", $subGroupText = "") {
442 // Is a group opened?
443 if (!$this->ifGroupOpenedPreviously()) {
444 // Throw exception here
445 throw new FormFormClosedException(array($this, $subGroupId), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
448 // At least the sub group name should be set
449 if ((empty($subGroupId)) && (!$this->ifSubGroupOpenedPreviously())) {
450 // Throw exception here
451 throw new EmptyVariableException(array($this, 'subGroupId'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
452 } elseif (empty($subGroupId)) {
453 // Close the last opened
454 $subGroupId = $this->getSubGroupId();
457 // Same sub group to open?
458 if ((!$this->ifSubGroupOpenedPreviously()) && ($subGroupId == $this->getSubGroupId())) {
459 // Abort here silently
463 // Initialize content with closing div by default
464 $content = " </div>\n</div><!-- Sub group- CLOSE //-->";
466 // Is this group opened?
467 if (!$this->ifSubGroupOpenedPreviously()) {
468 // Begin the span block
469 $content = sprintf("<!-- Sub group %s - OPEN //-->
470 <div class=\"subgroup_box\" id=\"%s_subgroup_box\">
471 <span class=\"subgroup_text\" id=\"%s_subgroup_text\">
474 <div class=\"subgroup_field\" id=\"%s_subgroup_field\">",
482 // Switch the state and remeber the name
483 $this->openSubGroupByIdContent($subGroupId, $content);
486 $this->closePreviousSubGroupByContent($content);
488 // All call it again if sub group name is not empty
489 if (!empty($subGroupId)) {
490 $this->addFormSubGroup($subGroupId, $subGroupText);
496 * Add text surrounded by a span block when there is a group opened before
497 * or else by a div block.
499 * @param $fieldName Field name
500 * @param $fieldText Text for the field
502 * @throws FormClosedException If the form is not yet opened
504 public function addFieldText ($fieldName, $fieldText) {
505 // Is the form opened?
506 if (($this->formOpened === false) && ($this->formEnabled === true)) {
507 // Throw an exception
508 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
511 // Set the block type
513 if ($this->ifGroupOpenedPreviously()) $block = "span";
515 // Generate the content
516 $inputContent = sprintf(" <%s id=\"%s_text\">
526 $this->addContent($inputContent);
530 * Add text (notes) surrounded by a div block. Still opened groups or sub
531 * groups will be automatically closed.
533 * @param $noteId Id for this note
534 * @param $formNotes The form notes we shell addd
536 * @throws FormClosedException If the form is not yet opened
538 public function addFormNote ($noteId, $formNotes) {
539 // Is the form opened?
540 if (($this->formOpened === false) && ($this->formEnabled === true)) {
541 // Throw an exception
542 throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
546 if ($this->ifGroupOpenedPreviously()) {
547 // Then automatically close it here
548 $this->addFormGroup();
551 // Generate the content
552 $inputContent = sprintf(" <div id=\"form_note_%s\">
560 $this->addContent($inputContent);
564 * Checks wether the registration requires a valid email address
566 * @return $required Wether the email address is required
568 public function ifRegisterRequiresEmailVerification () {
569 $required = ($this->getConfigInstance()->readConfig('register_requires_email') === "Y");
574 * Checks wether profile data shall be asked
576 * @return $required Wether profile shall be asked
578 public function ifRegisterIncludesProfile () {
579 $required = ($this->getConfigInstance()->readConfig('register_includes_profile') === "Y");
584 * Adds a pre-configured CAPTCHA
588 public function addCaptcha () {
589 // Get last executed pre filter
590 $extraInstance = Registry::getRegistry()->getInstance('extra');
592 // Get a configured instance
593 $captchaInstance = ObjectFactory::createObjectByConfiguredName($this->getFormName().'_captcha', array($this, $extraInstance));
595 // Initiate the CAPTCHA
596 $captchaInstance->initiateCaptcha();
598 // Render the CAPTCHA code
599 $captchaInstance->renderCode();
601 // Get the content and add it to the helper
602 $this->addContent($captchaInstance->getContent());
606 * Enables/disables the form tag usage
608 * @param $formEnabled Wether form is enabled or disabled
611 public final function enableForm ($formEnabled = true) {
612 $this->formEnabled = (bool) $formEnabled;
616 * Setter for form name
618 * @param $formName Name of this form
621 public final function setFormName ($formName) {
622 $this->formName = (string) $formName;
626 * Getter for form name
628 * @return $formName Name of this form
630 public final function getFormName () {
631 return $this->formName;
635 * Checks wether this form is secured by a CAPTCHA
637 * @return $isSecured Wether this form is secured by a CAPTCHA
639 public function ifFormSecuredWithCaptcha () {
640 $isSecured = ($this->getConfigInstance()->readConfig($this->getFormName().'_captcha_secured') === "Y");
645 * Flushs the content out (not yet secured against open forms, etc.!) or
646 * close the form automatically
649 * @throws FormOpenedException If the form is still open
651 public function flushContent () {
652 // Is the form still open?
653 if (($this->formOpened === true) && ($this->formEnabled === true)) {
654 // Close the form automatically
656 } elseif ($this->formEnabled === false) {
657 if ($this->ifSubGroupOpenedPreviously()) {
659 $this->addFormSubGroup();
660 } elseif ($this->ifGroupOpenedPreviously()) {
662 $this->addFormGroup();
666 // Send content to template engine
667 //* DEBUG: */ echo __METHOD__.": form=".$this->getFormName().", size=".strlen($this->getContent())."<br />\n";
668 $this->getTemplateInstance()->assignVariable($this->getFormName(), $this->getContent());