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 BaseHelper {
26 * Instance to the class which provides field values
28 private $valueInstance = null;
31 * Wether the form tag is opened (keep at false or else your forms will
34 private $formOpened = false;
39 private $formName = "";
42 * Wether the group is opened or not
44 private $groupOpened = false;
47 * Wether the sub group is opened or not
49 private $subGroupOpened = false;
52 * Name of the sub group
54 private $subGroupName = "";
57 const EXCEPTION_FORM_NAME_INVALID = 0x030;
58 const EXCEPTION_CLOSED_FORM = 0x031;
59 const EXCEPTION_OPENED_FORM = 0x032;
60 const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x033;
63 * Protected constructor
67 protected function __construct () {
68 // Call parent constructor
69 parent::__construct(__CLASS__);
71 // Set part description
72 $this->setObjectDescription("Helper class for HTML forms");
74 // Create unique ID number
75 $this->generateUniqueId();
79 * Creates the helper class with the given template engine instance and form name
81 * @param $templateInstance An instance of a valid template engine
82 * @param $formName Name of the form
83 * @param $formId Value for "id" attribute (default: $formName)
84 * @return $helperInstance A preparedf instance of this class
86 public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false) {
88 $helperInstance = new WebFormHelper();
90 // Set template instance
91 $helperInstance->setTemplateInstance($templateInstance);
93 // Is the form id not set?
94 if ($formId === false) {
95 // Use form id from form name
100 $helperInstance->addFormTag($formName, $formId);
102 // Return the prepared instance
103 return $helperInstance;
107 * Pre-fetches field default values from the given registry key instance into this class
109 * @param $registryKey
111 * @throws NullPointerException If an instance from registry is null
113 public function prefetchFieldValues ($registryKey) {
114 // Get the required instance
115 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
117 // Is the instance valid?
118 if (is_null($this->valueInstance)) {
119 // Throw an exception
120 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
125 * Add the form tag or close it an already opened form tag
127 * @param $formName Name of the form (default: false)
128 * @param $formId Id of the form (attribute "id"; default: false)
130 * @throws InvalidFormNameException If the form name is invalid (=false)
132 public function addFormTag ($formName = false, $formId = false) {
133 // When the form is not yet opened at least form name must be valid
134 if (($this->formOpened === false) && ($formName === false)) {
135 // Thrown an exception
136 throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
139 // Close the form is default
140 $formContent = "</form>";
142 // Check wether we shall open or close the form
143 if ($this->formOpened === false) {
145 $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
147 $this->getConfigInstance()->readConfig('base_url'),
148 $this->getConfigInstance()->readConfig('form_action'),
149 $this->getConfigInstance()->readConfig('form_method'),
150 $this->getConfigInstance()->readConfig('form_target')
153 // Is the form id set?
154 if ($formId !== false) {
155 // Then add it as well
156 $formContent .= sprintf(" id=\"%s_form\"",
164 // Open the form and remeber the form name
165 $this->formOpened = true;
166 $this->formName = $formName;
168 // Add the hidden field required to identify safely this form
169 $this->addInputHiddenField('form', $this->formName);
172 if ($this->groupOpened === true) {
173 // Then automatically close it here
174 $this->addFormGroup("", "");
177 /* @todo Add some unique PIN here to bypass problems with some browser and/or extensions */
179 $this->formOpened = false;
182 // Add it to the content
183 $this->addContent($formContent);
187 * Add a text input tag to the form or throw an exception if it is not yet
188 * opened. The field's name will be set as id.
190 * @param $fieldName Input field name
191 * @param $fieldValue Input default value (default: empty)
193 * @throws FormClosedException If the form is not yet opened
195 public function addInputTextField ($fieldName, $fieldValue = "") {
196 // Is the form opened?
197 if ($this->formOpened === false) {
198 // Throw an exception
199 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
202 // Generate the content
203 $inputContent = sprintf("<input type=\"text\" class=\"textfield\" id=\"%s_field\" name=\"%s\" value=\"%s\" />",
209 // And add it maybe with a "li" tag
210 $this->addContent($inputContent);
214 * Add a text input tag to the form with pre-loaded default value
216 * @param $fieldName Input field name
219 public function addInputTextFieldWithDefault ($fieldName) {
220 // Get the value from instance
221 $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));
222 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
224 // Add the text field
225 $this->addInputTextField($fieldName, $fieldValue);
229 * Add a password input tag to the form or throw an exception if it is not
230 * yet opened. The field's name will be set as id.
232 * @param $fieldName Input field name
233 * @param $fieldValue Input default value (default: empty)
235 * @throws FormClosedException If the form is not yet opened
237 public function addInputPasswordField ($fieldName, $fieldValue = "") {
238 // Is the form opened?
239 if ($this->formOpened === false) {
240 // Throw an exception
241 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
244 // Generate the content
245 $inputContent = sprintf("<input type=\"password\" class=\"password\" id=\"%s_field\" name=\"%s\" value=\"%s\" />",
252 $this->addContent($inputContent);
256 * Add a hidden input tag to the form or throw an exception if it is not
257 * yet opened. The field's name will be set as id.
259 * @param $fieldName Input field name
260 * @param $fieldValue Input default value (default: empty)
262 * @throws FormClosedException If the form is not yet opened
264 public function addInputHiddenField ($fieldName, $fieldValue = "") {
265 // Is the form opened?
266 if ($this->formOpened === false) {
267 // Throw an exception
268 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
271 // Generate the content
272 $inputContent = sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />",
278 $this->addContent($inputContent);
282 * Add a checkbox input tag to the form or throw an exception if it is not
283 * yet opened. The field's name will be set as id.
285 * @param $fieldName Input field name
286 * @param $fieldChecked Wether the field is checked (defaut: checked)
288 * @throws FormClosedException If the form is not yet opened
290 public function addInputCheckboxField ($fieldName, $fieldChecked = true) {
291 // Is the form opened?
292 if ($this->formOpened === false) {
293 // Throw an exception
294 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
297 // Set wether the check box is checked...
298 $checked = " checked=\"checked\"";
299 if ($fieldChecked === false) $checked = " ";
301 // Generate the content
302 $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox\" id=\"%s_field\" value=\"1\"%s/>",
309 $this->addContent($inputContent);
313 * Add a reset input tag to the form or throw an exception if it is not
314 * yet opened. The field's name will be set as id.
316 * @param $buttonText Text displayed on the button
318 * @throws FormClosedException If the form is not yet opened
320 public function addInputResetButton ($buttonText) {
321 // Is the form opened?
322 if ($this->formOpened === false) {
323 // Throw an exception
324 throw new FormClosedException (array($this, "reset"), self::EXCEPTION_CLOSED_FORM);
327 // Generate the content
328 $inputContent = sprintf("<input type=\"reset\" class=\"reset_button\" id=\"%s_reset\" value=\"%s\" />",
334 $this->addContent($inputContent);
338 * Add a reset input tag to the form or throw an exception if it is not
339 * yet opened. The field's name will be set as id.
341 * @param $buttonText Text displayed on the button
343 * @throws FormClosedException If the form is not yet opened
345 public function addInputSubmitButton ($buttonText) {
346 // Is the form opened?
347 if ($this->formOpened === false) {
348 // Throw an exception
349 throw new FormClosedException (array($this, "submit"), self::EXCEPTION_CLOSED_FORM);
352 // Generate the content
353 $inputContent = sprintf("<input type=\"submit\" class=\"submit_button\" id=\"%s_submit\" name=\"%s_button\" value=\"%s\" />",
360 $this->addContent($inputContent);
364 * Add a form group or close an already opened and open a new one
366 * @param $groupName Name of the group
367 * @param $groupText Text including HTML to show above this group
369 * @throws FormClosedException If no form has been opened before
370 * @throws EmptyVariableException If $groupName is not set
372 public function addFormGroup ($groupName, $groupText) {
374 if ($this->formOpened === false) {
375 // Throw exception here
376 throw new FormClosedException(array($this, $groupName), self::EXCEPTION_CLOSED_FORM);
379 // At least the group name should be set
380 if ((empty($groupName)) && ($this->groupOpened === false)) {
381 // Throw exception here
382 throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
385 // Initialize content with closing div by default
386 $content = " </div>\n</div><!-- Group - CLOSE //-->";
388 // Is this group opened?
389 if ($this->groupOpened === false) {
390 // Begin the div/span blocks
391 $content = sprintf("<!-- Group %s - OPEN //-->
392 <div class=\"group_box\" id=\"%s_group_box\">
393 <span class=\"group_text\" id=\"%s_group_text\">
396 <div class=\"group_field\" id=\"%s_group_field\">",
405 $this->addContent($content);
408 $this->groupOpened = true;
410 // Is a sub group opened?
411 if ($this->subGroupOpened === true) {
413 $this->addFormSubGroup("", "");
417 $this->addContent($content);
420 $this->groupOpened = false;
422 // All call it again if the group name is not empty
423 if (!empty($groupName)) {
424 $this->addFormGroup($groupName, $groupText);
430 * Add a form sub group or close an already opened and open a new one or
431 * throws an exception if no group has been opened before or if the sub
432 * group name is empty.
434 * @param $subGroupName Name of the group
435 * @param $subGroupText Text including HTML to show above this group
437 * @throws FormGroupClosedException If no group has been opened before
438 * @throws EmptyVariableException If $subGroupName is not set
440 public function addFormSubGroup ($subGroupName, $subGroupText) {
441 // Is a group opened?
442 if ($this->groupOpened === false) {
443 // Throw exception here
444 throw new FormGroupClosedException(array($this, $subGroupName), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
447 // At least the sub group name should be set
448 if ((empty($subGroupName)) && ($this->subGroupOpened === false)) {
449 // Throw exception here
450 throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
453 // Initialize content with closing div by default
454 $content = " </div>\n</div><!-- Sub group- CLOSE //-->";
456 // Is this group opened?
457 if ($this->subGroupOpened === false) {
458 // Begin the span block
459 $content = sprintf("<!-- Sub group %s - OPEN //-->
460 <div class=\"subgroup_box\" id=\"%s_subgroup_box\">
461 <span class=\"subgroup_text\" id=\"%s_subgroup_text\">
464 <div class=\"subgroup_field\" id=\"%s_subgroup_field\">",
473 $this->addContent($content);
475 // Switch the state and remeber the name
476 $this->subGroupOpened = true;
477 $this->subGroupName = $subGroupName;
480 $this->addContent($content);
483 $this->subGroupOpened = false;
485 // All call it again if sub group name is not empty
486 if (!empty($subGroupName)) {
487 $this->addFormSubGroup($subGroupName, $subGroupText);
493 * Add text surrounded by a span block when there is a group opened before
494 * or else by a div block.
496 * @param $fieldName Field name
497 * @param $fieldText Text for the field
499 * @throws FormClosedException If the form is not yet opened
501 public function addFieldText ($fieldName, $fieldText) {
502 // Is the form opened?
503 if ($this->formOpened === false) {
504 // Throw an exception
505 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
508 // Set the block type
510 if ($this->groupOpened === true) $block = "span";
512 // Generate the content
513 $inputContent = sprintf(" <%s id=\"%s_text\">
523 $this->addContent($inputContent);
527 * Add text (notes) surrounded by a div block. Still opened groups or sub
528 * groups will be automatically closed.
530 * @param $formNotes The form notes we shell addd
532 * @throws FormClosedException If the form is not yet opened
534 public function addFormNote ($formNotes) {
535 // Is the form opened?
536 if ($this->formOpened === false) {
537 // Throw an exception
538 throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
542 if ($this->groupOpened === true) {
543 // Then automatically close it here
544 $this->addFormGroup("", "");
547 // Generate the content
548 $inputContent = sprintf(" <div id=\"form_note\">
555 $this->addContent($inputContent);
559 * Checks wether the registration requires a valid email address
561 * @return $required Wether the email address is required
563 public function ifRegisterRequiresEmailVerification () {
564 $required = ($this->getConfigInstance()->readConfig('register_requires_email') == "Y");
569 * Checks wether profile data shall be asked
571 * @return $required Wether profile shall be asked
573 public function ifRegisterIncludesProfile () {
574 $required = ($this->getConfigInstance()->readConfig('register_includes_profile') == "Y");
579 * Checks wether personal data shall be asked
581 * @return $required Wether personal data shall be asked
583 public function ifRegisterIncludesPersonaData () {
584 $required = ($this->getConfigInstance()->readConfig('register_personal_data') == "Y");
589 * Checks wether email addresses can only be once used
593 public function ifEmailMustBeUnique () {
594 $isUnique = ($this->getConfigInstance()->readConfig('register_email_unique') == "Y");
599 * Checks wether the specified chat protocol is enabled in this form
601 * @return $required Wether the specified chat protocol is enabled
603 public function ifChatEnabled ($chatProtocol) {
604 $required = ($this->getConfigInstance()->readConfig(sprintf("chat_enabled_%s", $chatProtocol)) == "Y");
609 * Checks wether login is enabled or disabled
611 * @return $isEnabled Wether the login is enabled or disabled
613 public function ifLoginIsEnabled () {
614 $isEnabled = ($this->getConfigInstance()->readConfig('login_enabled') == "Y");
619 * Checks wether login shall be done by username
621 * @return $isEnabled Wether the login shall be done by username
623 public function ifLoginWithUsername () {
624 $isEnabled = ($this->getConfigInstance()->readConfig('login_type') == "username");
629 * Checks wether login shall be done by email
631 * @return $isEnabled Wether the login shall be done by email
633 public function ifLoginWithEmail () {
634 $isEnabled = ($this->getConfigInstance()->readConfig('login_type') == "email");
639 * Checks wether guest login is allowed
641 * @return $isAllowed Wether guest login is allowed
643 public function ifGuestLoginAllowed () {
644 $isAllowed = ($this->getConfigInstance()->readConfig('guest_login_allowed') == "Y");
649 * Checks wether the email address change must be confirmed
651 * @return $requireConfirm Wether email change must be confirmed
653 public function ifEmailChangeRequireConfirmation () {
654 $requireConfirm = ($this->getConfigInstance()->readConfig('email_change_confirmation') == "Y");
655 return $requireConfirm;
659 * Checks wether the rules has been updated
661 * @return $rulesUpdated Wether rules has been updated
663 public function ifRulesHaveChanged () {
664 /* @todo Implement check if rules have been changed */
669 * Checks wether email change is allowed
671 * @return $emailChange Wether changing email address is allowed
673 public function ifEmailChangeAllowed () {
674 $emailChange = ($this->getConfigInstance()->readConfig('email_change_allowed') == "Y");
679 * Checks wether the user account is unconfirmed
681 * @return $isUnconfirmed Wether the user account is unconfirmed
683 public function ifUserAccountUnconfirmed () {
684 $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_unconfirmed'));
685 return $isUnconfirmed;
689 * Checks wether the user account is locked
691 * @return $isUnconfirmed Wether the user account is locked
693 public function ifUserAccountLocked () {
694 $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_locked'));
695 return $isUnconfirmed;
699 * Checks wether the user account is a guest
701 * @return $isUnconfirmed Wether the user account is a guest
703 public function ifUserAccountGuest () {
704 $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_guest'));
705 return $isUnconfirmed;
709 * Flushs the content out (not yet secured against open forms, etc.!) or
710 * close the form automatically
713 * @throws FormOpenedException If the form is still open
715 public function flushContent () {
716 // Is the form still open?
717 if ($this->formOpened === true) {
718 // Close the form automatically
722 // Send content to template engine
723 $this->getTemplateInstance()->assignVariable($this->formName, $this->getContent());
727 * Getter for direct field values
729 * @param $fieldName Name of the field we shall fetch
730 * @return $fieldValue Value from field
732 public function getField ($fieldName) {
733 // Get the field value
734 $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));