OpenAPI Generator. Better DocBlock parsing [WIP]
[dokuwiki.git] / inc / form.php
blob670d2047006edf1cbc320c3c8b5a5c54d759f7db
1 <?php
3 /**
4 * DokuWiki XHTML Form
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Tom N Harris <tnharris@whoopdedo.org>
8 */
10 // phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
11 // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
14 /**
15 * Class for creating simple HTML forms.
17 * The forms is built from a list of pseudo-tags (arrays with expected keys).
18 * Every pseudo-tag must have the key '_elem' set to the name of the element.
19 * When printed, the form class calls functions named 'form_$type' for each
20 * element it contains.
22 * Standard practice is for non-attribute keys in a pseudo-element to start
23 * with '_'. Other keys are HTML attributes that will be included in the element
24 * tag. That way, the element output functions can pass the pseudo-element
25 * directly to buildAttributes.
27 * See the form_make* functions later in this file.
29 * Please note that even though this class is technically deprecated (use dokuwiki\Form instead),
30 * it is still widely used in the core and the related form events. Until those have been rewritten,
31 * this will continue to be used
33 * @deprecated 2019-07-14
34 * @author Tom N Harris <tnharris@whoopdedo.org>
36 class Doku_Form
38 // Form id attribute
39 public $params = array();
41 // Draw a border around form fields.
42 // Adds <fieldset></fieldset> around the elements
43 public $_infieldset = false;
45 // Hidden form fields.
46 public $_hidden = array();
48 // Array of pseudo-tags
49 public $_content = array();
51 /**
52 * Constructor
54 * Sets parameters and autoadds a security token. The old calling convention
55 * with up to four parameters is deprecated, instead the first parameter
56 * should be an array with parameters.
58 * @param mixed $params Parameters for the HTML form element; Using the deprecated
59 * calling convention this is the ID attribute of the form
60 * @param bool|string $action (optional, deprecated) submit URL, defaults to current page
61 * @param bool|string $method (optional, deprecated) 'POST' or 'GET', default is POST
62 * @param bool|string $enctype (optional, deprecated) Encoding type of the data
64 * @author Tom N Harris <tnharris@whoopdedo.org>
66 public function __construct($params, $action = false, $method = false, $enctype = false)
68 if (!is_array($params)) {
69 $this->params = array('id' => $params);
70 if ($action !== false) $this->params['action'] = $action;
71 if ($method !== false) $this->params['method'] = strtolower($method);
72 if ($enctype !== false) $this->params['enctype'] = $enctype;
73 } else {
74 $this->params = $params;
77 if (!isset($this->params['method'])) {
78 $this->params['method'] = 'post';
79 } else {
80 $this->params['method'] = strtolower($this->params['method']);
83 if (!isset($this->params['action'])) {
84 $this->params['action'] = '';
87 $this->addHidden('sectok', getSecurityToken());
90 /**
91 * startFieldset
93 * Add <fieldset></fieldset> tags around fields.
94 * Usually results in a border drawn around the form.
96 * @param string $legend Label that will be printed with the border.
98 * @author Tom N Harris <tnharris@whoopdedo.org>
100 public function startFieldset($legend)
102 if ($this->_infieldset) {
103 $this->addElement(array('_elem' => 'closefieldset'));
105 $this->addElement(array('_elem' => 'openfieldset', '_legend' => $legend));
106 $this->_infieldset = true;
110 * endFieldset
112 * @author Tom N Harris <tnharris@whoopdedo.org>
114 public function endFieldset()
116 if ($this->_infieldset) {
117 $this->addElement(array('_elem' => 'closefieldset'));
119 $this->_infieldset = false;
123 * addHidden
125 * Adds a name/value pair as a hidden field.
126 * The value of the field (but not the name) will be passed to
127 * formText() before printing.
129 * @param string $name Field name.
130 * @param string $value Field value. If null, remove a previously added field.
132 * @author Tom N Harris <tnharris@whoopdedo.org>
134 public function addHidden($name, $value)
136 if (is_null($value))
137 unset($this->_hidden[$name]);
138 else $this->_hidden[$name] = $value;
142 * addElement
144 * Appends a content element to the form.
145 * The element can be either a pseudo-tag or string.
146 * If string, it is printed without escaping special chars. *
148 * @param string|array $elem Pseudo-tag or string to add to the form.
150 * @author Tom N Harris <tnharris@whoopdedo.org>
152 public function addElement($elem)
154 $this->_content[] = $elem;
158 * insertElement
160 * Inserts a content element at a position.
162 * @param string $pos 0-based index where the element will be inserted.
163 * @param string|array $elem Pseudo-tag or string to add to the form.
165 * @author Tom N Harris <tnharris@whoopdedo.org>
167 public function insertElement($pos, $elem)
169 array_splice($this->_content, $pos, 0, array($elem));
173 * replaceElement
175 * Replace with NULL to remove an element.
177 * @param int $pos 0-based index the element will be placed at.
178 * @param string|array $elem Pseudo-tag or string to add to the form.
180 * @author Tom N Harris <tnharris@whoopdedo.org>
182 public function replaceElement($pos, $elem)
184 $rep = array();
185 if (!is_null($elem)) $rep[] = $elem;
186 array_splice($this->_content, $pos, 1, $rep);
190 * findElementByType
192 * Gets the position of the first of a type of element.
194 * @param string $type Element type to look for.
195 * @return int|false position of element if found, otherwise false
197 * @author Tom N Harris <tnharris@whoopdedo.org>
199 public function findElementByType($type)
201 foreach ($this->_content as $pos => $elem) {
202 if (is_array($elem) && $elem['_elem'] == $type)
203 return $pos;
205 return false;
209 * findElementById
211 * Gets the position of the element with an ID attribute.
213 * @param string $id ID of the element to find.
214 * @return int|false position of element if found, otherwise false
216 * @author Tom N Harris <tnharris@whoopdedo.org>
218 public function findElementById($id)
220 foreach ($this->_content as $pos => $elem) {
221 if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
222 return $pos;
224 return false;
228 * findElementByAttribute
230 * Gets the position of the first element with a matching attribute value.
232 * @param string $name Attribute name.
233 * @param string $value Attribute value.
234 * @return int|false position of element if found, otherwise false
236 * @author Tom N Harris <tnharris@whoopdedo.org>
238 public function findElementByAttribute($name, $value)
240 foreach ($this->_content as $pos => $elem) {
241 if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
242 return $pos;
244 return false;
248 * getElementAt
250 * Returns a reference to the element at a position.
251 * A position out-of-bounds will return either the
252 * first (underflow) or last (overflow) element.
254 * @param int $pos 0-based index
255 * @return array reference pseudo-element
257 * @author Tom N Harris <tnharris@whoopdedo.org>
259 public function &getElementAt($pos)
261 if ($pos < 0) $pos = count($this->_content) + $pos;
262 if ($pos < 0) $pos = 0;
263 if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
264 return $this->_content[$pos];
268 * Return the assembled HTML for the form.
270 * Each element in the form will be passed to a function named
271 * 'form_$type'. The function should return the HTML to be printed.
273 * @author Tom N Harris <tnharris@whoopdedo.org>
275 * @return string html of the form
277 public function getForm()
279 global $lang;
280 $form = '';
281 $this->params['accept-charset'] = $lang['encoding'];
282 $form .= '<form ' . buildAttributes($this->params, false) . '><div class="no">' . DOKU_LF;
283 if (!empty($this->_hidden)) {
284 foreach ($this->_hidden as $name => $value)
285 $form .= form_hidden(array('name' => $name, 'value' => $value));
287 foreach ($this->_content as $element) {
288 if (is_array($element)) {
289 $elem_type = $element['_elem'];
290 if (function_exists('form_' . $elem_type)) {
291 $form .= call_user_func('form_' . $elem_type, $element) . DOKU_LF;
293 } else {
294 $form .= $element;
297 if ($this->_infieldset) $form .= form_closefieldset() . DOKU_LF;
298 $form .= '</div></form>' . DOKU_LF;
300 return $form;
304 * Print the assembled form
306 * wraps around getForm()
308 public function printForm()
310 echo $this->getForm();
314 * Add a radio set
316 * This function adds a set of radio buttons to the form. If $_POST[$name]
317 * is set, this radio is preselected, else the first radio button.
319 * @param string $name The HTML field name
320 * @param array $entries An array of entries $value => $caption
322 * @author Adrian Lang <lang@cosmocode.de>
325 public function addRadioSet($name, $entries)
327 global $INPUT;
328 $value = (array_key_exists($INPUT->post->str($name), $entries)) ?
329 $INPUT->str($name) : key($entries);
330 foreach ($entries as $val => $cap) {
331 $data = ($value === $val) ? array('checked' => 'checked') : array();
332 $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data));
338 * form_makeTag
340 * Create a form element for a non-specific empty tag.
342 * @param string $tag Tag name.
343 * @param array $attrs Optional attributes.
344 * @return array pseudo-tag
346 * @author Tom N Harris <tnharris@whoopdedo.org>
348 function form_makeTag($tag, $attrs = array())
350 $elem = array('_elem' => 'tag', '_tag' => $tag);
351 return array_merge($elem, $attrs);
355 * form_makeOpenTag
357 * Create a form element for a non-specific opening tag.
358 * Remember to put a matching close tag after this as well.
360 * @param string $tag Tag name.
361 * @param array $attrs Optional attributes.
362 * @return array pseudo-tag
364 * @author Tom N Harris <tnharris@whoopdedo.org>
366 function form_makeOpenTag($tag, $attrs = array())
368 $elem = array('_elem' => 'opentag', '_tag' => $tag);
369 return array_merge($elem, $attrs);
373 * form_makeCloseTag
375 * Create a form element for a non-specific closing tag.
376 * Careless use of this will result in invalid XHTML.
378 * @param string $tag Tag name.
379 * @return array pseudo-tag
381 * @author Tom N Harris <tnharris@whoopdedo.org>
383 function form_makeCloseTag($tag)
385 return array('_elem' => 'closetag', '_tag' => $tag);
389 * form_makeWikiText
391 * Create a form element for a textarea containing wiki text.
392 * Only one wikitext element is allowed on a page. It will have
393 * a name of 'wikitext' and id 'wiki__text'. The text will
394 * be passed to formText() before printing.
396 * @param string $text Text to fill the field with.
397 * @param array $attrs Optional attributes.
398 * @return array pseudo-tag
400 * @author Tom N Harris <tnharris@whoopdedo.org>
402 function form_makeWikiText($text, $attrs = array())
404 $elem = array('_elem' => 'wikitext', '_text' => $text,
405 'class' => 'edit', 'cols' => '80', 'rows' => '10');
406 return array_merge($elem, $attrs);
410 * form_makeButton
412 * Create a form element for an action button.
413 * A title will automatically be generated using the value and
414 * accesskey attributes, unless you provide one.
416 * @param string $type Type attribute. 'submit' or 'cancel'
417 * @param string $act Wiki action of the button, will be used as the do= parameter
418 * @param string $value (optional) Displayed label. Uses $act if not provided.
419 * @param array $attrs Optional attributes.
420 * @return array pseudo-tag
422 * @author Tom N Harris <tnharris@whoopdedo.org>
424 function form_makeButton($type, $act, $value = '', $attrs = array())
426 if ($value == '') $value = $act;
427 $elem = array('_elem' => 'button', 'type' => $type, '_action' => $act,
428 'value' => $value);
429 if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
430 $attrs['title'] = $value . ' [' . strtoupper($attrs['accesskey']) . ']';
432 return array_merge($elem, $attrs);
436 * form_makeField
438 * Create a form element for a labelled input element.
439 * The label text will be printed before the input.
441 * @param string $type Type attribute of input.
442 * @param string $name Name attribute of the input.
443 * @param string $value (optional) Default value.
444 * @param string $class Class attribute of the label. If this is 'block',
445 * then a line break will be added after the field.
446 * @param string $label Label that will be printed before the input.
447 * @param string $id ID attribute of the input. If set, the label will
448 * reference it with a 'for' attribute.
449 * @param array $attrs Optional attributes.
450 * @return array pseudo-tag
452 * @author Tom N Harris <tnharris@whoopdedo.org>
454 function form_makeField($type, $name, $value = '', $label = null, $id = '', $class = '', $attrs = array())
456 if (is_null($label)) $label = $name;
457 $elem = array('_elem' => 'field', '_text' => $label, '_class' => $class,
458 'type' => $type, 'id' => $id, 'name' => $name, 'value' => $value);
459 return array_merge($elem, $attrs);
463 * form_makeFieldRight
465 * Create a form element for a labelled input element.
466 * The label text will be printed after the input.
468 * @see form_makeField
469 * @author Tom N Harris <tnharris@whoopdedo.org>
471 * @param string $type
472 * @param string $name
473 * @param string $value
474 * @param null|string $label
475 * @param string $id
476 * @param string $class
477 * @param array $attrs
479 * @return array
481 function form_makeFieldRight($type, $name, $value = '', $label = null, $id = '', $class = '', $attrs = array())
483 if (is_null($label)) $label = $name;
484 $elem = array('_elem' => 'fieldright', '_text' => $label, '_class' => $class,
485 'type' => $type, 'id' => $id, 'name' => $name, 'value' => $value);
486 return array_merge($elem, $attrs);
490 * form_makeTextField
492 * Create a form element for a text input element with label.
494 * @see form_makeField
495 * @author Tom N Harris <tnharris@whoopdedo.org>
497 * @param string $name
498 * @param string $value
499 * @param null|string $label
500 * @param string $id
501 * @param string $class
502 * @param array $attrs
504 * @return array
506 function form_makeTextField($name, $value = '', $label = null, $id = '', $class = '', $attrs = array())
508 if (is_null($label)) $label = $name;
509 $elem = array('_elem' => 'textfield', '_text' => $label, '_class' => $class,
510 'id' => $id, 'name' => $name, 'value' => $value, 'class' => 'edit');
511 return array_merge($elem, $attrs);
515 * form_makePasswordField
517 * Create a form element for a password input element with label.
518 * Password elements have no default value, for obvious reasons.
520 * @see form_makeField
521 * @author Tom N Harris <tnharris@whoopdedo.org>
523 * @param string $name
524 * @param null|string $label
525 * @param string $id
526 * @param string $class
527 * @param array $attrs
529 * @return array
531 function form_makePasswordField($name, $label = null, $id = '', $class = '', $attrs = array())
533 if (is_null($label)) $label = $name;
534 $elem = array('_elem' => 'passwordfield', '_text' => $label, '_class' => $class,
535 'id' => $id, 'name' => $name, 'class' => 'edit');
536 return array_merge($elem, $attrs);
540 * form_makeFileField
542 * Create a form element for a file input element with label
544 * @see form_makeField
545 * @author Michael Klier <chi@chimeric.de>
547 * @param string $name
548 * @param null|string $label
549 * @param string $id
550 * @param string $class
551 * @param array $attrs
553 * @return array
555 function form_makeFileField($name, $label = null, $id = '', $class = '', $attrs = array())
557 if (is_null($label)) $label = $name;
558 $elem = array('_elem' => 'filefield', '_text' => $label, '_class' => $class,
559 'id' => $id, 'name' => $name, 'class' => 'edit');
560 return array_merge($elem, $attrs);
564 * form_makeCheckboxField
566 * Create a form element for a checkbox input element with label.
567 * If $value is an array, a hidden field with the same name and the value
568 * $value[1] is constructed as well.
570 * @see form_makeFieldRight
571 * @author Tom N Harris <tnharris@whoopdedo.org>
573 * @param string $name
574 * @param string $value
575 * @param null|string $label
576 * @param string $id
577 * @param string $class
578 * @param array $attrs
580 * @return array
582 function form_makeCheckboxField($name, $value = '1', $label = null, $id = '', $class = '', $attrs = array())
584 if (is_null($label)) $label = $name;
585 if (is_null($value) || $value == '') $value = '0';
586 $elem = array('_elem' => 'checkboxfield', '_text' => $label, '_class' => $class,
587 'id' => $id, 'name' => $name, 'value' => $value);
588 return array_merge($elem, $attrs);
592 * form_makeRadioField
594 * Create a form element for a radio button input element with label.
596 * @see form_makeFieldRight
597 * @author Tom N Harris <tnharris@whoopdedo.org>
599 * @param string $name
600 * @param string $value
601 * @param null|string $label
602 * @param string $id
603 * @param string $class
604 * @param array $attrs
606 * @return array
608 function form_makeRadioField($name, $value = '1', $label = null, $id = '', $class = '', $attrs = array())
610 if (is_null($label)) $label = $name;
611 if (is_null($value) || $value == '') $value = '0';
612 $elem = array('_elem' => 'radiofield', '_text' => $label, '_class' => $class,
613 'id' => $id, 'name' => $name, 'value' => $value);
614 return array_merge($elem, $attrs);
618 * form_makeMenuField
620 * Create a form element for a drop-down menu with label.
621 * The list of values can be strings, arrays of (value,text),
622 * or an associative array with the values as keys and labels as values.
623 * An item is selected by supplying its value or integer index.
624 * If the list of values is an associative array, the selected item must be
625 * a string.
627 * @author Tom N Harris <tnharris@whoopdedo.org>
629 * @param string $name Name attribute of the input.
630 * @param string[]|array[] $values The list of values can be strings, arrays of (value,text),
631 * or an associative array with the values as keys and labels as values.
632 * @param string|int $selected default selected value, string or index number
633 * @param string $class Class attribute of the label. If this is 'block',
634 * then a line break will be added after the field.
635 * @param string $label Label that will be printed before the input.
636 * @param string $id ID attribute of the input. If set, the label will
637 * reference it with a 'for' attribute.
638 * @param array $attrs Optional attributes.
639 * @return array pseudo-tag
641 function form_makeMenuField($name, $values, $selected = '', $label = null, $id = '', $class = '', $attrs = array())
643 if (is_null($label)) $label = $name;
644 $options = array();
645 reset($values);
646 // FIXME: php doesn't know the difference between a string and an integer
647 if (is_string(key($values))) {
648 foreach ($values as $val => $text) {
649 $options[] = array($val, $text, (!is_null($selected) && $val == $selected));
651 } else {
652 if (is_integer($selected)) $selected = $values[$selected];
653 foreach ($values as $val) {
654 if (is_array($val))
655 @list($val, $text) = $val;
656 else $text = null;
657 $options[] = array($val, $text, $val === $selected);
660 $elem = array('_elem' => 'menufield', '_options' => $options, '_text' => $label, '_class' => $class,
661 'id' => $id, 'name' => $name);
662 return array_merge($elem, $attrs);
666 * form_makeListboxField
668 * Create a form element for a list box with label.
669 * The list of values can be strings, arrays of (value,text),
670 * or an associative array with the values as keys and labels as values.
671 * Items are selected by supplying its value or an array of values.
673 * @author Tom N Harris <tnharris@whoopdedo.org>
675 * @param string $name Name attribute of the input.
676 * @param string[]|array[] $values The list of values can be strings, arrays of (value,text),
677 * or an associative array with the values as keys and labels as values.
678 * @param array|string $selected value or array of values of the items that need to be selected
679 * @param string $class Class attribute of the label. If this is 'block',
680 * then a line break will be added after the field.
681 * @param string $label Label that will be printed before the input.
682 * @param string $id ID attribute of the input. If set, the label will
683 * reference it with a 'for' attribute.
684 * @param array $attrs Optional attributes.
685 * @return array pseudo-tag
687 function form_makeListboxField($name, $values, $selected = '', $label = null, $id = '', $class = '', $attrs = array())
689 if (is_null($label)) $label = $name;
690 $options = array();
691 reset($values);
692 if (is_null($selected) || $selected == '') {
693 $selected = array();
694 } elseif (!is_array($selected)) {
695 $selected = array($selected);
697 // FIXME: php doesn't know the difference between a string and an integer
698 if (is_string(key($values))) {
699 foreach ($values as $val => $text) {
700 $options[] = array($val, $text, in_array($val, $selected));
702 } else {
703 foreach ($values as $val) {
704 $disabled = false;
705 if (is_array($val)) {
706 @list($val, $text, $disabled) = $val;
707 } else {
708 $text = null;
710 $options[] = array($val, $text, in_array($val, $selected), $disabled);
713 $elem = array('_elem' => 'listboxfield', '_options' => $options, '_text' => $label, '_class' => $class,
714 'id' => $id, 'name' => $name);
715 return array_merge($elem, $attrs);
719 * form_tag
721 * Print the HTML for a generic empty tag.
722 * Requires '_tag' key with name of the tag.
723 * Attributes are passed to buildAttributes()
725 * @author Tom N Harris <tnharris@whoopdedo.org>
727 * @param array $attrs attributes
728 * @return string html of tag
730 function form_tag($attrs)
732 return '<' . $attrs['_tag'] . ' ' . buildAttributes($attrs, true) . '/>';
736 * form_opentag
738 * Print the HTML for a generic opening tag.
739 * Requires '_tag' key with name of the tag.
740 * Attributes are passed to buildAttributes()
742 * @author Tom N Harris <tnharris@whoopdedo.org>
744 * @param array $attrs attributes
745 * @return string html of tag
747 function form_opentag($attrs)
749 return '<' . $attrs['_tag'] . ' ' . buildAttributes($attrs, true) . '>';
753 * form_closetag
755 * Print the HTML for a generic closing tag.
756 * Requires '_tag' key with name of the tag.
757 * There are no attributes.
759 * @author Tom N Harris <tnharris@whoopdedo.org>
761 * @param array $attrs attributes
762 * @return string html of tag
764 function form_closetag($attrs)
766 return '</' . $attrs['_tag'] . '>';
770 * form_openfieldset
772 * Print the HTML for an opening fieldset tag.
773 * Uses the '_legend' key.
774 * Attributes are passed to buildAttributes()
776 * @author Tom N Harris <tnharris@whoopdedo.org>
778 * @param array $attrs attributes
779 * @return string html
781 function form_openfieldset($attrs)
783 $s = '<fieldset ' . buildAttributes($attrs, true) . '>';
784 if (!is_null($attrs['_legend'])) $s .= '<legend>' . $attrs['_legend'] . '</legend>';
785 return $s;
789 * form_closefieldset
791 * Print the HTML for a closing fieldset tag.
792 * There are no attributes.
794 * @author Tom N Harris <tnharris@whoopdedo.org>
796 * @return string html
798 function form_closefieldset()
800 return '</fieldset>';
804 * form_hidden
806 * Print the HTML for a hidden input element.
807 * Uses only 'name' and 'value' attributes.
808 * Value is passed to formText()
810 * @author Tom N Harris <tnharris@whoopdedo.org>
812 * @param array $attrs attributes
813 * @return string html
815 function form_hidden($attrs)
817 return '<input type="hidden" name="' . $attrs['name'] . '" value="' . formText($attrs['value']) . '" />';
821 * form_wikitext
823 * Print the HTML for the wiki textarea.
824 * Requires '_text' with default text of the field.
825 * Text will be passed to formText(), attributes to buildAttributes()
827 * @author Tom N Harris <tnharris@whoopdedo.org>
829 * @param array $attrs attributes
830 * @return string html
832 function form_wikitext($attrs)
834 // mandatory attributes
835 unset($attrs['name']);
836 unset($attrs['id']);
837 return '<textarea name="wikitext" id="wiki__text" dir="auto" '
838 . buildAttributes($attrs, true) . '>' . DOKU_LF
839 . formText($attrs['_text'])
840 . '</textarea>';
844 * form_button
846 * Print the HTML for a form button.
847 * If '_action' is set, the button name will be "do[_action]".
848 * Other attributes are passed to buildAttributes()
850 * @author Tom N Harris <tnharris@whoopdedo.org>
852 * @param array $attrs attributes
853 * @return string html
855 function form_button($attrs)
857 $p = (!empty($attrs['_action'])) ? 'name="do[' . $attrs['_action'] . ']" ' : '';
858 $value = $attrs['value'];
859 unset($attrs['value']);
860 return '<button ' . $p . buildAttributes($attrs, true) . '>' . $value . '</button>';
864 * form_field
866 * Print the HTML for a form input field.
867 * _class : class attribute used on the label tag
868 * _text : Text to display before the input. Not escaped.
869 * Other attributes are passed to buildAttributes() for the input tag.
871 * @author Tom N Harris <tnharris@whoopdedo.org>
873 * @param array $attrs attributes
874 * @return string html
876 function form_field($attrs)
878 $s = '<label';
879 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
880 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
881 $s .= '><span>' . $attrs['_text'] . '</span>';
882 $s .= ' <input ' . buildAttributes($attrs, true) . ' /></label>';
883 if (preg_match('/(^| )block($| )/', $attrs['_class']))
884 $s .= '<br />';
885 return $s;
889 * form_fieldright
891 * Print the HTML for a form input field. (right-aligned)
892 * _class : class attribute used on the label tag
893 * _text : Text to display after the input. Not escaped.
894 * Other attributes are passed to buildAttributes() for the input tag.
896 * @author Tom N Harris <tnharris@whoopdedo.org>
898 * @param array $attrs attributes
899 * @return string html
901 function form_fieldright($attrs)
903 $s = '<label';
904 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
905 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
906 $s .= '><input ' . buildAttributes($attrs, true) . ' />';
907 $s .= ' <span>' . $attrs['_text'] . '</span></label>';
908 if (preg_match('/(^| )block($| )/', $attrs['_class']))
909 $s .= '<br />';
910 return $s;
914 * form_textfield
916 * Print the HTML for a text input field.
917 * _class : class attribute used on the label tag
918 * _text : Text to display before the input. Not escaped.
919 * Other attributes are passed to buildAttributes() for the input tag.
921 * @author Tom N Harris <tnharris@whoopdedo.org>
923 * @param array $attrs attributes
924 * @return string html
926 function form_textfield($attrs)
928 // mandatory attributes
929 unset($attrs['type']);
930 $s = '<label';
931 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
932 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
933 $s .= '><span>' . $attrs['_text'] . '</span> ';
934 $s .= '<input type="text" ' . buildAttributes($attrs, true) . ' /></label>';
935 if (preg_match('/(^| )block($| )/', $attrs['_class']))
936 $s .= '<br />';
937 return $s;
941 * form_passwordfield
943 * Print the HTML for a password input field.
944 * _class : class attribute used on the label tag
945 * _text : Text to display before the input. Not escaped.
946 * Other attributes are passed to buildAttributes() for the input tag.
948 * @author Tom N Harris <tnharris@whoopdedo.org>
950 * @param array $attrs attributes
951 * @return string html
953 function form_passwordfield($attrs)
955 // mandatory attributes
956 unset($attrs['type']);
957 $s = '<label';
958 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
959 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
960 $s .= '><span>' . $attrs['_text'] . '</span> ';
961 $s .= '<input type="password" ' . buildAttributes($attrs, true) . ' /></label>';
962 if (preg_match('/(^| )block($| )/', $attrs['_class']))
963 $s .= '<br />';
964 return $s;
968 * form_filefield
970 * Print the HTML for a file input field.
971 * _class : class attribute used on the label tag
972 * _text : Text to display before the input. Not escaped
973 * _maxlength : Allowed size in byte
974 * _accept : Accepted mime-type
975 * Other attributes are passed to buildAttributes() for the input tag
977 * @author Michael Klier <chi@chimeric.de>
979 * @param array $attrs attributes
980 * @return string html
982 function form_filefield($attrs)
984 $s = '<label';
985 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
986 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
987 $s .= '><span>' . $attrs['_text'] . '</span> ';
988 $s .= '<input type="file" ' . buildAttributes($attrs, true);
989 if (!empty($attrs['_maxlength'])) $s .= ' maxlength="' . $attrs['_maxlength'] . '"';
990 if (!empty($attrs['_accept'])) $s .= ' accept="' . $attrs['_accept'] . '"';
991 $s .= ' /></label>';
992 if (preg_match('/(^| )block($| )/', $attrs['_class']))
993 $s .= '<br />';
994 return $s;
998 * form_checkboxfield
1000 * Print the HTML for a checkbox input field.
1001 * _class : class attribute used on the label tag
1002 * _text : Text to display after the input. Not escaped.
1003 * Other attributes are passed to buildAttributes() for the input tag.
1004 * If value is an array, a hidden field with the same name and the value
1005 * $attrs['value'][1] is constructed as well.
1007 * @author Tom N Harris <tnharris@whoopdedo.org>
1009 * @param array $attrs attributes
1010 * @return string html
1012 function form_checkboxfield($attrs)
1014 // mandatory attributes
1015 unset($attrs['type']);
1016 $s = '<label';
1017 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
1018 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
1019 $s .= '>';
1020 if (is_array($attrs['value'])) {
1021 echo '<input type="hidden" name="' . hsc($attrs['name']) . '"'
1022 . ' value="' . hsc($attrs['value'][1]) . '" />';
1023 $attrs['value'] = $attrs['value'][0];
1025 $s .= '<input type="checkbox" ' . buildAttributes($attrs, true) . ' />';
1026 $s .= ' <span>' . $attrs['_text'] . '</span></label>';
1027 if (preg_match('/(^| )block($| )/', $attrs['_class']))
1028 $s .= '<br />';
1029 return $s;
1033 * form_radiofield
1035 * Print the HTML for a radio button input field.
1036 * _class : class attribute used on the label tag
1037 * _text : Text to display after the input. Not escaped.
1038 * Other attributes are passed to buildAttributes() for the input tag.
1040 * @author Tom N Harris <tnharris@whoopdedo.org>
1042 * @param array $attrs attributes
1043 * @return string html
1045 function form_radiofield($attrs)
1047 // mandatory attributes
1048 unset($attrs['type']);
1049 $s = '<label';
1050 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
1051 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
1052 $s .= '><input type="radio" ' . buildAttributes($attrs, true) . ' />';
1053 $s .= ' <span>' . $attrs['_text'] . '</span></label>';
1054 if (preg_match('/(^| )block($| )/', $attrs['_class']))
1055 $s .= '<br />';
1056 return $s;
1060 * form_menufield
1062 * Print the HTML for a drop-down menu.
1063 * _options : Array of (value,text,selected) for the menu.
1064 * Text can be omitted. Text and value are passed to formText()
1065 * Only one item can be selected.
1066 * _class : class attribute used on the label tag
1067 * _text : Text to display before the menu. Not escaped.
1068 * Other attributes are passed to buildAttributes() for the input tag.
1070 * @author Tom N Harris <tnharris@whoopdedo.org>
1072 * @param array $attrs attributes
1073 * @return string html
1075 function form_menufield($attrs)
1077 $attrs['size'] = '1';
1078 $s = '<label';
1079 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
1080 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
1081 $s .= '><span>' . $attrs['_text'] . '</span>';
1082 $s .= ' <select ' . buildAttributes($attrs, true) . '>' . DOKU_LF;
1083 if (!empty($attrs['_options'])) {
1084 $selected = false;
1086 $cnt = count($attrs['_options']);
1087 for ($n = 0; $n < $cnt; $n++) {
1088 @list($value,$text,$select) = $attrs['_options'][$n];
1089 $p = '';
1090 if (!is_null($text))
1091 $p .= ' value="' . formText($value) . '"';
1092 else $text = $value;
1093 if (!empty($select) && !$selected) {
1094 $p .= ' selected="selected"';
1095 $selected = true;
1097 $s .= '<option' . $p . '>' . formText($text) . '</option>';
1099 } else {
1100 $s .= '<option></option>';
1102 $s .= DOKU_LF . '</select></label>';
1103 if (preg_match('/(^| )block($| )/', $attrs['_class']))
1104 $s .= '<br />';
1105 return $s;
1109 * form_listboxfield
1111 * Print the HTML for a list box.
1112 * _options : Array of (value,text,selected) for the list.
1113 * Text can be omitted. Text and value are passed to formText()
1114 * _class : class attribute used on the label tag
1115 * _text : Text to display before the menu. Not escaped.
1116 * Other attributes are passed to buildAttributes() for the input tag.
1118 * @author Tom N Harris <tnharris@whoopdedo.org>
1120 * @param array $attrs attributes
1121 * @return string html
1123 function form_listboxfield($attrs)
1125 $s = '<label';
1126 if ($attrs['_class']) $s .= ' class="' . $attrs['_class'] . '"';
1127 if (!empty($attrs['id'])) $s .= ' for="' . $attrs['id'] . '"';
1128 $s .= '><span>' . $attrs['_text'] . '</span> ';
1129 $s .= '<select ' . buildAttributes($attrs, true) . '>' . DOKU_LF;
1130 if (!empty($attrs['_options'])) {
1131 foreach ($attrs['_options'] as $opt) {
1132 @list($value, $text, $select, $disabled) = $opt;
1133 $p = '';
1134 if (is_null($text)) $text = $value;
1135 $p .= ' value="' . formText($value) . '"';
1136 if (!empty($select)) $p .= ' selected="selected"';
1137 if ($disabled) $p .= ' disabled="disabled"';
1138 $s .= '<option' . $p . '>' . formText($text) . '</option>';
1140 } else {
1141 $s .= '<option></option>';
1143 $s .= DOKU_LF . '</select></label>';
1144 if (preg_match('/(^| )block($| )/', $attrs['_class']))
1145 $s .= '<br />';
1146 return $s;