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 * Wether the form tag is opened (keep at false or else your forms will
29 private $formOpened = false;
34 private $formName = "";
37 * Wether the group is opened or not
39 private $groupOpened = false;
42 * Wether the sub group is opened or not
44 private $subGroupOpened = false;
47 * Name of the sub group
49 private $subGroupName = "";
52 const EXCEPTION_FORM_NAME_INVALID = 0x030;
53 const EXCEPTION_CLOSED_FORM = 0x031;
54 const EXCEPTION_OPENED_FORM = 0x032;
55 const EXCEPTION_UNEXPECTED_CLOSED_GROUP = 0x033;
58 * Protected constructor
62 protected function __construct () {
63 // Call parent constructor
64 parent::__construct(__CLASS__);
68 * Creates the helper class with the given template engine instance and form name
70 * @param $templateInstance An instance of a valid template engine
71 * @param $formName Name of the form
72 * @param $formId Value for "id" attribute (default: $formName)
73 * @return $helperInstance A preparedf instance of this class
75 public final static function createWebFormHelper (CompileableTemplate $templateInstance, $formName, $formId = false) {
77 $helperInstance = new WebFormHelper();
79 // Set template instance
80 $helperInstance->setTemplateInstance($templateInstance);
82 // Is the form id not set?
83 if ($formId === false) {
84 // Use form id from form name
89 $helperInstance->addFormTag($formName, $formId);
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)
103 public function addFormTag ($formName = false, $formId = false) {
104 // When the form is not yet opened at least form name must be valid
105 if (($this->formOpened === false) && ($formName === false)) {
106 // Thrown an exception
107 throw new InvalidFormNameException ($this, self::EXCEPTION_FORM_NAME_INVALID);
110 // Close the form is default
111 $formContent = "</form>";
113 // Check wether we shall open or close the form
114 if ($this->formOpened === false) {
116 $formContent = sprintf("<form name=\"%s\" class=\"forms\" action=\"%s/%s\" method=\"%s\" target=\"%s\"",
118 $this->getConfigInstance()->readConfig('base_url'),
119 $this->getConfigInstance()->readConfig('form_action'),
120 $this->getConfigInstance()->readConfig('form_method'),
121 $this->getConfigInstance()->readConfig('form_target')
124 // Is the form id set?
125 if ($formId !== false) {
126 // Then add it as well
127 $formContent .= sprintf(" id=\"%s_form\"",
135 // Open the form and remeber the form name
136 $this->formOpened = true;
137 $this->formName = $formName;
139 // Add the hidden field required to identify safely this form
140 $this->addInputHiddenField('form', $this->formName);
143 if ($this->groupOpened === true) {
144 // Then automatically close it here
145 $this->addFormGroup("", "");
148 // @TODO Add some unique PIN here to bypass problems with some browser and/or extensions
150 $this->formOpened = false;
153 // Add it to the content
154 $this->addContent($formContent);
158 * Add a text input tag to the form or throw an exception if it is not yet
159 * opened. The field's name will be set as id.
161 * @param $fieldName Input field name
162 * @param $fieldValue Input default value (default: empty)
164 * @throws FormClosedException If the form is not yet opened
166 public function addInputTextField ($fieldName, $fieldValue = "") {
167 // Is the form opened?
168 if ($this->formOpened === false) {
169 // Throw an exception
170 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
173 // Generate the content
174 $inputContent = sprintf("<input type=\"text\" class=\"textfield\" id=\"%s\" name=\"%s\" value=\"%s\" />",
180 // And add it maybe with a "li" tag
181 $this->addContent($inputContent);
185 * Add a password input tag to the form or throw an exception if it is not
186 * yet opened. The field's name will be set as id.
188 * @param $fieldName Input field name
189 * @param $fieldValue Input default value (default: empty)
191 * @throws FormClosedException If the form is not yet opened
193 public function addInputPasswordField ($fieldName, $fieldValue = "") {
194 // Is the form opened?
195 if ($this->formOpened === false) {
196 // Throw an exception
197 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
200 // Generate the content
201 $inputContent = sprintf("<input type=\"password\" class=\"password\" id=\"%s\" name=\"%s\" value=\"%s\" />",
208 $this->addContent($inputContent);
212 * Add a hidden input tag to the form or throw an exception if it is not
213 * yet opened. The field's name will be set as id.
215 * @param $fieldName Input field name
216 * @param $fieldValue Input default value (default: empty)
218 * @throws FormClosedException If the form is not yet opened
220 public function addInputHiddenField ($fieldName, $fieldValue = "") {
221 // Is the form opened?
222 if ($this->formOpened === false) {
223 // Throw an exception
224 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
227 // Generate the content
228 $inputContent = sprintf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />",
234 $this->addContent($inputContent);
238 * Add a checkbox input tag to the form or throw an exception if it is not
239 * yet opened. The field's name will be set as id.
241 * @param $fieldName Input field name
242 * @param $fieldChecked Wether the field is checked (defaut: checked)
244 * @throws FormClosedException If the form is not yet opened
246 public function addInputCheckboxField ($fieldName, $fieldChecked = true) {
247 // Is the form opened?
248 if ($this->formOpened === false) {
249 // Throw an exception
250 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
253 // Set wether the check box is checked...
254 $checked = " checked=\"checked\"";
255 if ($fieldChecked === false) $checked = " ";
257 // Generate the content
258 $inputContent = sprintf("<input type=\"checkbox\" name=\"%s\" class=\"checkbox\" id=\"%s\" value=\"1\"%s/>",
265 $this->addContent($inputContent);
269 * Add a reset input tag to the form or throw an exception if it is not
270 * yet opened. The field's name will be set as id.
272 * @param $buttonText Text displayed on the button
274 * @throws FormClosedException If the form is not yet opened
276 public function addInputResetButton ($buttonText) {
277 // Is the form opened?
278 if ($this->formOpened === false) {
279 // Throw an exception
280 throw new FormClosedException (array($this, "reset"), self::EXCEPTION_CLOSED_FORM);
283 // Generate the content
284 $inputContent = sprintf("<input type=\"reset\" class=\"reset_button\" id=\"%s_reset\" value=\"%s\" />",
290 $this->addContent($inputContent);
294 * Add a reset input tag to the form or throw an exception if it is not
295 * yet opened. The field's name will be set as id.
297 * @param $buttonText Text displayed on the button
299 * @throws FormClosedException If the form is not yet opened
301 public function addInputSubmitButton ($buttonText) {
302 // Is the form opened?
303 if ($this->formOpened === false) {
304 // Throw an exception
305 throw new FormClosedException (array($this, "submit"), self::EXCEPTION_CLOSED_FORM);
308 // Generate the content
309 $inputContent = sprintf("<input type=\"submit\" class=\"submit_button\" id=\"%s_submit\" name=\"%s_button\" value=\"%s\" />",
316 $this->addContent($inputContent);
320 * Add a form group or close an already opened and open a new one
322 * @param $groupName Name of the group
323 * @param $groupText Text including HTML to show above this group
325 * @throws FormClosedException If no form has been opened before
326 * @throws EmptyVariableException If $groupName is not set
328 public function addFormGroup ($groupName, $groupText) {
330 if ($this->formOpened === false) {
331 // Throw exception here
332 throw new FormClosedException(array($this, $groupName), self::EXCEPTION_CLOSED_FORM);
335 // At least the group name should be set
336 if ((empty($groupName)) && ($this->groupOpened === false)) {
337 // Throw exception here
338 throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
341 // Initialize content with closing div by default
342 $content = " </div>\n</div><!-- Group - CLOSE //-->";
344 // Is this group opened?
345 if ($this->groupOpened === false) {
346 // Begin the div/span blocks
347 $content = sprintf("<!-- Group %s - OPEN //-->
348 <div class=\"group_box\" id=\"%s_group_box\">
349 <span class=\"group_text\" id=\"%s_group_text\">
352 <div class=\"group_field\" id=\"%s_group_field\">",
361 $this->addContent($content);
364 $this->groupOpened = true;
366 // Is a sub group opened?
367 if ($this->subGroupOpened === true) {
369 $this->addFormSubGroup("", "");
373 $this->addContent($content);
376 $this->groupOpened = false;
378 // All call it again if the group name is not empty
379 if (!empty($groupName)) {
380 $this->addFormGroup($groupName, $groupText);
386 * Add a form sub group or close an already opened and open a new one or
387 * throws an exception if no group has been opened before or if the sub
388 * group name is empty.
390 * @param $subGroupName Name of the group
391 * @param $subGroupText Text including HTML to show above this group
393 * @throws FormGroupClosedException If no group has been opened before
394 * @throws EmptyVariableException If $subGroupName is not set
396 public function addFormSubGroup ($subGroupName, $subGroupText) {
397 // Is a group opened?
398 if ($this->groupOpened === false) {
399 // Throw exception here
400 throw new FormGroupClosedException(array($this, $subGroupName), self::EXCEPTION_UNEXPECTED_CLOSED_GROUP);
403 // At least the sub group name should be set
404 if ((empty($subGroupName)) && ($this->subGroupOpened === false)) {
405 // Throw exception here
406 throw new EmptyVariableException(array($this, 'groupName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
409 // Initialize content with closing div by default
410 $content = " </div>\n</div><!-- Sub group- CLOSE //-->";
412 // Is this group opened?
413 if ($this->subGroupOpened === false) {
414 // Begin the span block
415 $content = sprintf("<!-- Sub group %s - OPEN //-->
416 <div class=\"subgroup_box\" id=\"%s_subgroup_box\">
417 <span class=\"subgroup_text\" id=\"%s_subgroup_text\">
420 <div class=\"subgroup_field\" id=\"%s_subgroup_field\">",
429 $this->addContent($content);
431 // Switch the state and remeber the name
432 $this->subGroupOpened = true;
433 $this->subGroupName = $subGroupName;
436 $this->addContent($content);
439 $this->subGroupOpened = false;
441 // All call it again if sub group name is not empty
442 if (!empty($subGroupName)) {
443 $this->addFormSubGroup($subGroupName, $subGroupText);
449 * Add text surrounded by a span block when there is a group opened before
450 * or else by a div block.
452 * @param $fieldName Field name
453 * @param $fieldText Text for the field
455 * @throws FormClosedException If the form is not yet opened
457 public function addFieldText ($fieldName, $fieldText) {
458 // Is the form opened?
459 if ($this->formOpened === false) {
460 // Throw an exception
461 throw new FormClosedException (array($this, $fieldName), self::EXCEPTION_CLOSED_FORM);
464 // Set the block type
466 if ($this->groupOpened === true) $block = "span";
468 // Generate the content
469 $inputContent = sprintf(" <%s id=\"%s_text\">
479 $this->addContent($inputContent);
483 * Add text (notes) surrounded by a div block. Still opened groups or sub
484 * groups will be automatically closed.
486 * @param $formNotes The form notes we shell addd
488 * @throws FormClosedException If the form is not yet opened
490 public function addFormNote ($formNotes) {
491 // Is the form opened?
492 if ($this->formOpened === false) {
493 // Throw an exception
494 throw new FormClosedException (array($this, "form_notes"), self::EXCEPTION_CLOSED_FORM);
498 if ($this->groupOpened === true) {
499 // Then automatically close it here
500 $this->addFormGroup("unknown", "");
503 // Generate the content
504 $inputContent = sprintf(" <div id=\"form_note\">
511 $this->addContent($inputContent);
515 * Checks wether the registration requires a valid email address
517 * @return $required Wether the email address is required
519 public function ifRegisterRequiresEmailVerification () {
520 $required = ($this->getConfigInstance()->readConfig('register_requires_email') == "Y");
525 * Checks wether profile data shall be asked
527 * @return $required Wether profile shall be asked
529 public function ifRegisterIncludesProfile () {
530 $required = ($this->getConfigInstance()->readConfig('register_includes_profile') == "Y");
535 * Checks wether personal data shall be asked
537 * @return $required Wether personal data shall be asked
539 public function ifRegisterIncludesPersonaData () {
540 $required = ($this->getConfigInstance()->readConfig('register_personal_data') == "Y");
545 * Checks wether email addresses can only be once used
549 public function ifEmailMustBeUnique () {
550 $isUnique = ($this->getConfigInstance()->readConfig('register_email_unique') == "Y");
555 * Checks wether the specified chat protocol is enabled in this form
557 * @return $required Wether the specified chat protocol is enabled
559 public function ifChatEnabled ($chatProtocol) {
560 $required = ($this->getConfigInstance()->readConfig(sprintf("chat_enabled_%s", $chatProtocol)) == "Y");
565 * Flushs the content out (not yet secured against open forms, etc.!) or
566 * throw an exception if it is not yet closed
569 * @throws FormOpenedException If the form is still open
571 public function flushContent () {
572 // Is the form still open?
573 if ($this->formOpened === true) {
574 // Throw an exception
575 throw new FormOpenedException ($this, self::EXCEPTION_OPENED_FORM);
578 // Send content to template engine
579 $this->getTemplateInstance()->assignVariable($this->formName, $this->getContent());