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)
131 * @todo Add some unique PIN here to bypass problems with some browser and/or extensions
133 public function addFormTag ($formName = false, $formId = false) {
134 // When the form is not yet opened at least form name must be valid
135 if (($this->formOpened === false) && ($formName === false)) {
136 // Thrown an exception
137 throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
140 // Close the form is default
141 $formContent = "</form>";
143 // Check wether we shall open or close the form
144 if ($this->formOpened === false) {
146 $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
148 $this->getConfigInstance()->readConfig('base_url'),
149 $this->getConfigInstance()->readConfig('form_action'),
150 $this->getConfigInstance()->readConfig('form_method'),
151 $this->getConfigInstance()->readConfig('form_target')
154 // Is the form id set?
155 if ($formId !== false) {
156 // Then add it as well
157 $formContent .= sprintf(" id=\"%s_form\"",
165 // Open the form and remeber the form name
166 $this->formOpened = true;
167 $this->formName = $formName;
169 // Add the hidden field required to identify safely this form
170 $this->addInputHiddenField('form', $this->formName);
173 if ($this->groupOpened === true) {
174 // Then automatically close it here
175 $this->addFormGroup("", "");
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 hidden input tag to the form with pre-loaded default value
284 * @param $fieldName Input field name
287 public function addInputHiddenFieldWithDefault ($fieldName) {
288 // Get the value from instance
289 $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));
290 //* DEBUG: */ echo __METHOD__.":".$fieldName."=".$fieldValue."<br />\n";
292 // Add the text field
293 $this->addInputHiddenField($fieldName, $fieldValue);
297 * Add a checkbox input tag to the form or throw an exception if it is not
298 * yet opened. The field's name will be set as id.
300 * @param $fieldName Input field name
301 * @param $fieldChecked Wether the field is checked (defaut: checked)
303 * @throws FormClosedException If the form is not yet opened
305 public function addInputCheckboxField ($fieldName, $fieldChecked = true) {
306 // Is the form opened?
307 if ($this->formOpened === false) {
308 // Throw an exception
309 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
312 // Set wether the check box is checked...
313 $checked = " checked=\"checked\"";
314 if ($fieldChecked === false) $checked = " ";
316 // Generate the content
317 $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox\" id=\"%s_field\" value=\"1\"%s/>",
324 $this->addContent($inputContent);
328 * Add a reset input tag to the form or throw an exception if it is not
329 * yet opened. The field's name will be set as id.
331 * @param $buttonText Text displayed on the button
333 * @throws FormClosedException If the form is not yet opened
335 public function addInputResetButton ($buttonText) {
336 // Is the form opened?
337 if ($this->formOpened === false) {
338 // Throw an exception
339 throw new FormClosedException (array($this, "reset"), self::EXCEPTION_CLOSED_FORM);
342 // Generate the content
343 $inputContent = sprintf("<input type=\"reset\" class=\"reset_button\" id=\"%s_reset\" value=\"%s\" />",
349 $this->addContent($inputContent);
353 * Add a reset input tag to the form or throw an exception if it is not
354 * yet opened. The field's name will be set as id.
356 * @param $buttonText Text displayed on the button
358 * @throws FormClosedException If the form is not yet opened
360 public function addInputSubmitButton ($buttonText) {
361 // Is the form opened?
362 if ($this->formOpened === false) {
363 // Throw an exception
364 throw new FormClosedException (array($this, "submit"), self::EXCEPTION_CLOSED_FORM);
367 // Generate the content
368 $inputContent = sprintf("<input type=\"submit\" class=\"submit_button\" id=\"%s_submit\" name=\"%s_button\" value=\"%s\" />",
375 $this->addContent($inputContent);
379 * Add a form group or close an already opened and open a new one
381 * @param $groupName Name of the group
382 * @param $groupText Text including HTML to show above this group
384 * @throws FormClosedException If no form has been opened before
385 * @throws EmptyVariableException If $groupName is not set
387 public function addFormGroup ($groupName, $groupText) {
389 if ($this->formOpened === false) {
390 // Throw exception here
391 throw new FormClosedException(array($this, $groupName), self::EXCEPTION_CLOSED_FORM);
394 // At least the group name should be set
395 if ((empty($groupName)) && ($this->groupOpened === false)) {
396 // Throw exception here
397 throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
400 // Initialize content with closing div by default
401 $content = " </div>\n</div><!-- Group - CLOSE //-->";
403 // Is this group opened?
404 if ($this->groupOpened === false) {
405 // Begin the div/span blocks
406 $content = sprintf("<!-- Group %s - OPEN //-->
407 <div class=\"group_box\" id=\"%s_group_box\">
408 <span class=\"group_text\" id=\"%s_group_text\">
411 <div class=\"group_field\" id=\"%s_group_field\">",
420 $this->addContent($content);
423 $this->groupOpened = true;
425 // Is a sub group opened?
426 if ($this->subGroupOpened === true) {
428 $this->addFormSubGroup("", "");
432 $this->addContent($content);
435 $this->groupOpened = false;
437 // All call it again if the group name is not empty
438 if (!empty($groupName)) {
439 $this->addFormGroup($groupName, $groupText);
445 * Add a form sub group or close an already opened and open a new one or
446 * throws an exception if no group has been opened before or if the sub
447 * group name is empty.
449 * @param $subGroupName Name of the group
450 * @param $subGroupText Text including HTML to show above this group
452 * @throws FormGroupClosedException If no group has been opened before
453 * @throws EmptyVariableException If $subGroupName is not set
455 public function addFormSubGroup ($subGroupName, $subGroupText) {
456 // Is a group opened?
457 if ($this->groupOpened === false) {
458 // Throw exception here
459 throw new FormGroupClosedException(array($this, $subGroupName), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
462 // At least the sub group name should be set
463 if ((empty($subGroupName)) && ($this->subGroupOpened === false)) {
464 // Throw exception here
465 throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
468 // Initialize content with closing div by default
469 $content = " </div>\n</div><!-- Sub group- CLOSE //-->";
471 // Is this group opened?
472 if ($this->subGroupOpened === false) {
473 // Begin the span block
474 $content = sprintf("<!-- Sub group %s - OPEN //-->
475 <div class=\"subgroup_box\" id=\"%s_subgroup_box\">
476 <span class=\"subgroup_text\" id=\"%s_subgroup_text\">
479 <div class=\"subgroup_field\" id=\"%s_subgroup_field\">",
488 $this->addContent($content);
490 // Switch the state and remeber the name
491 $this->subGroupOpened = true;
492 $this->subGroupName = $subGroupName;
495 $this->addContent($content);
498 $this->subGroupOpened = false;
500 // All call it again if sub group name is not empty
501 if (!empty($subGroupName)) {
502 $this->addFormSubGroup($subGroupName, $subGroupText);
508 * Add text surrounded by a span block when there is a group opened before
509 * or else by a div block.
511 * @param $fieldName Field name
512 * @param $fieldText Text for the field
514 * @throws FormClosedException If the form is not yet opened
516 public function addFieldText ($fieldName, $fieldText) {
517 // Is the form opened?
518 if ($this->formOpened === false) {
519 // Throw an exception
520 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
523 // Set the block type
525 if ($this->groupOpened === true) $block = "span";
527 // Generate the content
528 $inputContent = sprintf(" <%s id=\"%s_text\">
538 $this->addContent($inputContent);
542 * Add text (notes) surrounded by a div block. Still opened groups or sub
543 * groups will be automatically closed.
545 * @param $formNotes The form notes we shell addd
547 * @throws FormClosedException If the form is not yet opened
549 public function addFormNote ($formNotes) {
550 // Is the form opened?
551 if ($this->formOpened === false) {
552 // Throw an exception
553 throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
557 if ($this->groupOpened === true) {
558 // Then automatically close it here
559 $this->addFormGroup("", "");
562 // Generate the content
563 $inputContent = sprintf(" <div id=\"form_note\">
570 $this->addContent($inputContent);
574 * Checks wether the registration requires a valid email address
576 * @return $required Wether the email address is required
578 public function ifRegisterRequiresEmailVerification () {
579 $required = ($this->getConfigInstance()->readConfig('register_requires_email') == "Y");
584 * Checks wether profile data shall be asked
586 * @return $required Wether profile shall be asked
588 public function ifRegisterIncludesProfile () {
589 $required = ($this->getConfigInstance()->readConfig('register_includes_profile') == "Y");
594 * Checks wether personal data shall be asked
596 * @return $required Wether personal data shall be asked
598 public function ifRegisterIncludesPersonaData () {
599 $required = ($this->getConfigInstance()->readConfig('register_personal_data') == "Y");
604 * Checks wether email addresses can only be once used
608 public function ifEmailMustBeUnique () {
609 $isUnique = ($this->getConfigInstance()->readConfig('register_email_unique') == "Y");
614 * Checks wether the specified chat protocol is enabled in this form
616 * @return $required Wether the specified chat protocol is enabled
618 public function ifChatEnabled ($chatProtocol) {
619 $required = ($this->getConfigInstance()->readConfig(sprintf("chat_enabled_%s", $chatProtocol)) == "Y");
624 * Checks wether login is enabled or disabled
626 * @return $isEnabled Wether the login is enabled or disabled
628 public function ifLoginIsEnabled () {
629 $isEnabled = ($this->getConfigInstance()->readConfig('login_enabled') == "Y");
634 * Checks wether login shall be done by username
636 * @return $isEnabled Wether the login shall be done by username
638 public function ifLoginWithUsername () {
639 $isEnabled = ($this->getConfigInstance()->readConfig('login_type') == "username");
644 * Checks wether login shall be done by email
646 * @return $isEnabled Wether the login shall be done by email
648 public function ifLoginWithEmail () {
649 $isEnabled = ($this->getConfigInstance()->readConfig('login_type') == "email");
654 * Checks wether guest login is allowed
656 * @return $isAllowed Wether guest login is allowed
658 public function ifGuestLoginAllowed () {
659 $isAllowed = ($this->getConfigInstance()->readConfig('guest_login_allowed') == "Y");
664 * Checks wether the email address change must be confirmed
666 * @return $requireConfirm Wether email change must be confirmed
668 public function ifEmailChangeRequireConfirmation () {
669 $requireConfirm = ($this->getConfigInstance()->readConfig('email_change_confirmation') == "Y");
670 return $requireConfirm;
674 * Checks wether the rules has been updated
676 * @return $rulesUpdated Wether rules has been updated
677 * @todo Implement check if rules have been changed
679 public function ifRulesHaveChanged () {
684 * Checks wether email change is allowed
686 * @return $emailChange Wether changing email address is allowed
688 public function ifEmailChangeAllowed () {
689 $emailChange = ($this->getConfigInstance()->readConfig('email_change_allowed') == "Y");
694 * Checks wether the user account is unconfirmed
696 * @return $isUnconfirmed Wether the user account is unconfirmed
698 public function ifUserAccountUnconfirmed () {
699 $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_unconfirmed'));
700 return $isUnconfirmed;
704 * Checks wether the user account is locked
706 * @return $isUnconfirmed Wether the user account is locked
708 public function ifUserAccountLocked () {
709 $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_locked'));
710 return $isUnconfirmed;
714 * Checks wether the user account is a guest
716 * @return $isUnconfirmed Wether the user account is a guest
718 public function ifUserAccountGuest () {
719 $isUnconfirmed = ($this->getField('user_status') === $this->getConfigInstance()->readConfig('user_status_guest'));
720 return $isUnconfirmed;
724 * Flushs the content out (not yet secured against open forms, etc.!) or
725 * close the form automatically
728 * @throws FormOpenedException If the form is still open
730 public function flushContent () {
731 // Is the form still open?
732 if ($this->formOpened === true) {
733 // Close the form automatically
737 // Send content to template engine
738 $this->getTemplateInstance()->assignVariable($this->formName, $this->getContent());
742 * Getter for direct field values
744 * @param $fieldName Name of the field we shall fetch
745 * @return $fieldValue Value from field
747 public function getField ($fieldName) {
748 // Get the field value
749 $fieldValue = call_user_func_array(array($this->valueInstance, "getField"), array($fieldName));