Merge pull request #2515 from Innovailable/master
[dokuwiki.git] / inc / form.php
blob7afb0ba3082aafa604c9adbe614e2c37ad95e2ba
1 <?php
2 /**
3 * DokuWiki XHTML Form
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Tom N Harris <tnharris@whoopdedo.org>
7 */
9 if(!defined('DOKU_INC')) die('meh.');
11 /**
12 * Class for creating simple HTML forms.
14 * The forms is built from a list of pseudo-tags (arrays with expected keys).
15 * Every pseudo-tag must have the key '_elem' set to the name of the element.
16 * When printed, the form class calls functions named 'form_$type' for each
17 * element it contains.
19 * Standard practice is for non-attribute keys in a pseudo-element to start
20 * with '_'. Other keys are HTML attributes that will be included in the element
21 * tag. That way, the element output functions can pass the pseudo-element
22 * directly to buildAttributes.
24 * See the form_make* functions later in this file.
26 * @author Tom N Harris <tnharris@whoopdedo.org>
28 class Doku_Form {
30 // Form id attribute
31 public $params = array();
33 // Draw a border around form fields.
34 // Adds <fieldset></fieldset> around the elements
35 public $_infieldset = false;
37 // Hidden form fields.
38 public $_hidden = array();
40 // Array of pseudo-tags
41 public $_content = array();
43 /**
44 * Constructor
46 * Sets parameters and autoadds a security token. The old calling convention
47 * with up to four parameters is deprecated, instead the first parameter
48 * should be an array with parameters.
50 * @param mixed $params Parameters for the HTML form element; Using the deprecated
51 * calling convention this is the ID attribute of the form
52 * @param bool|string $action (optional, deprecated) submit URL, defaults to current page
53 * @param bool|string $method (optional, deprecated) 'POST' or 'GET', default is POST
54 * @param bool|string $enctype (optional, deprecated) Encoding type of the data
56 * @author Tom N Harris <tnharris@whoopdedo.org>
58 function __construct($params, $action=false, $method=false, $enctype=false) {
59 if(!is_array($params)) {
60 $this->params = array('id' => $params);
61 if ($action !== false) $this->params['action'] = $action;
62 if ($method !== false) $this->params['method'] = strtolower($method);
63 if ($enctype !== false) $this->params['enctype'] = $enctype;
64 } else {
65 $this->params = $params;
68 if (!isset($this->params['method'])) {
69 $this->params['method'] = 'post';
70 } else {
71 $this->params['method'] = strtolower($this->params['method']);
74 if (!isset($this->params['action'])) {
75 $this->params['action'] = '';
78 $this->addHidden('sectok', getSecurityToken());
81 /**
82 * startFieldset
84 * Add <fieldset></fieldset> tags around fields.
85 * Usually results in a border drawn around the form.
87 * @param string $legend Label that will be printed with the border.
89 * @author Tom N Harris <tnharris@whoopdedo.org>
91 function startFieldset($legend) {
92 if ($this->_infieldset) {
93 $this->addElement(array('_elem'=>'closefieldset'));
95 $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend));
96 $this->_infieldset = true;
99 /**
100 * endFieldset
102 * @author Tom N Harris <tnharris@whoopdedo.org>
104 function endFieldset() {
105 if ($this->_infieldset) {
106 $this->addElement(array('_elem'=>'closefieldset'));
108 $this->_infieldset = false;
112 * addHidden
114 * Adds a name/value pair as a hidden field.
115 * The value of the field (but not the name) will be passed to
116 * formText() before printing.
118 * @param string $name Field name.
119 * @param string $value Field value. If null, remove a previously added field.
121 * @author Tom N Harris <tnharris@whoopdedo.org>
123 function addHidden($name, $value) {
124 if (is_null($value))
125 unset($this->_hidden[$name]);
126 else
127 $this->_hidden[$name] = $value;
131 * addElement
133 * Appends a content element to the form.
134 * The element can be either a pseudo-tag or string.
135 * If string, it is printed without escaping special chars. *
137 * @param string|array $elem Pseudo-tag or string to add to the form.
139 * @author Tom N Harris <tnharris@whoopdedo.org>
141 function addElement($elem) {
142 $this->_content[] = $elem;
146 * insertElement
148 * Inserts a content element at a position.
150 * @param string $pos 0-based index where the element will be inserted.
151 * @param string|array $elem Pseudo-tag or string to add to the form.
153 * @author Tom N Harris <tnharris@whoopdedo.org>
155 function insertElement($pos, $elem) {
156 array_splice($this->_content, $pos, 0, array($elem));
160 * replaceElement
162 * Replace with NULL to remove an element.
164 * @param int $pos 0-based index the element will be placed at.
165 * @param string|array $elem Pseudo-tag or string to add to the form.
167 * @author Tom N Harris <tnharris@whoopdedo.org>
169 function replaceElement($pos, $elem) {
170 $rep = array();
171 if (!is_null($elem)) $rep[] = $elem;
172 array_splice($this->_content, $pos, 1, $rep);
176 * findElementByType
178 * Gets the position of the first of a type of element.
180 * @param string $type Element type to look for.
181 * @return int|false position of element if found, otherwise false
183 * @author Tom N Harris <tnharris@whoopdedo.org>
185 function findElementByType($type) {
186 foreach ($this->_content as $pos=>$elem) {
187 if (is_array($elem) && $elem['_elem'] == $type)
188 return $pos;
190 return false;
194 * findElementById
196 * Gets the position of the element with an ID attribute.
198 * @param string $id ID of the element to find.
199 * @return int|false position of element if found, otherwise false
201 * @author Tom N Harris <tnharris@whoopdedo.org>
203 function findElementById($id) {
204 foreach ($this->_content as $pos=>$elem) {
205 if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
206 return $pos;
208 return false;
212 * findElementByAttribute
214 * Gets the position of the first element with a matching attribute value.
216 * @param string $name Attribute name.
217 * @param string $value Attribute value.
218 * @return int|false position of element if found, otherwise false
220 * @author Tom N Harris <tnharris@whoopdedo.org>
222 function findElementByAttribute($name, $value) {
223 foreach ($this->_content as $pos=>$elem) {
224 if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
225 return $pos;
227 return false;
231 * getElementAt
233 * Returns a reference to the element at a position.
234 * A position out-of-bounds will return either the
235 * first (underflow) or last (overflow) element.
237 * @param int $pos 0-based index
238 * @return array reference pseudo-element
240 * @author Tom N Harris <tnharris@whoopdedo.org>
242 function &getElementAt($pos) {
243 if ($pos < 0) $pos = count($this->_content) + $pos;
244 if ($pos < 0) $pos = 0;
245 if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
246 return $this->_content[$pos];
250 * Return the assembled HTML for the form.
252 * Each element in the form will be passed to a function named
253 * 'form_$type'. The function should return the HTML to be printed.
255 * @author Tom N Harris <tnharris@whoopdedo.org>
257 * @return string html of the form
259 function getForm() {
260 global $lang;
261 $form = '';
262 $this->params['accept-charset'] = $lang['encoding'];
263 $form .= '<form ' . buildAttributes($this->params,false) . '><div class="no">' . DOKU_LF;
264 if (!empty($this->_hidden)) {
265 foreach ($this->_hidden as $name=>$value)
266 $form .= form_hidden(array('name'=>$name, 'value'=>$value));
268 foreach ($this->_content as $element) {
269 if (is_array($element)) {
270 $elem_type = $element['_elem'];
271 if (function_exists('form_'.$elem_type)) {
272 $form .= call_user_func('form_'.$elem_type, $element).DOKU_LF;
274 } else {
275 $form .= $element;
278 if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF;
279 $form .= '</div></form>'.DOKU_LF;
281 return $form;
285 * Print the assembled form
287 * wraps around getForm()
289 function printForm(){
290 echo $this->getForm();
294 * Add a radio set
296 * This function adds a set of radio buttons to the form. If $_POST[$name]
297 * is set, this radio is preselected, else the first radio button.
299 * @param string $name The HTML field name
300 * @param array $entries An array of entries $value => $caption
302 * @author Adrian Lang <lang@cosmocode.de>
305 function addRadioSet($name, $entries) {
306 global $INPUT;
307 $value = (array_key_exists($INPUT->post->str($name), $entries)) ?
308 $INPUT->str($name) : key($entries);
309 foreach($entries as $val => $cap) {
310 $data = ($value === $val) ? array('checked' => 'checked') : array();
311 $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data));
318 * form_makeTag
320 * Create a form element for a non-specific empty tag.
322 * @param string $tag Tag name.
323 * @param array $attrs Optional attributes.
324 * @return array pseudo-tag
326 * @author Tom N Harris <tnharris@whoopdedo.org>
328 function form_makeTag($tag, $attrs=array()) {
329 $elem = array('_elem'=>'tag', '_tag'=>$tag);
330 return array_merge($elem, $attrs);
334 * form_makeOpenTag
336 * Create a form element for a non-specific opening tag.
337 * Remember to put a matching close tag after this as well.
339 * @param string $tag Tag name.
340 * @param array $attrs Optional attributes.
341 * @return array pseudo-tag
343 * @author Tom N Harris <tnharris@whoopdedo.org>
345 function form_makeOpenTag($tag, $attrs=array()) {
346 $elem = array('_elem'=>'opentag', '_tag'=>$tag);
347 return array_merge($elem, $attrs);
351 * form_makeCloseTag
353 * Create a form element for a non-specific closing tag.
354 * Careless use of this will result in invalid XHTML.
356 * @param string $tag Tag name.
357 * @return array pseudo-tag
359 * @author Tom N Harris <tnharris@whoopdedo.org>
361 function form_makeCloseTag($tag) {
362 return array('_elem'=>'closetag', '_tag'=>$tag);
366 * form_makeWikiText
368 * Create a form element for a textarea containing wiki text.
369 * Only one wikitext element is allowed on a page. It will have
370 * a name of 'wikitext' and id 'wiki__text'. The text will
371 * be passed to formText() before printing.
373 * @param string $text Text to fill the field with.
374 * @param array $attrs Optional attributes.
375 * @return array pseudo-tag
377 * @author Tom N Harris <tnharris@whoopdedo.org>
379 function form_makeWikiText($text, $attrs=array()) {
380 $elem = array('_elem'=>'wikitext', '_text'=>$text,
381 'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
382 return array_merge($elem, $attrs);
386 * form_makeButton
388 * Create a form element for an action button.
389 * A title will automatically be generated using the value and
390 * accesskey attributes, unless you provide one.
392 * @param string $type Type attribute. 'submit' or 'cancel'
393 * @param string $act Wiki action of the button, will be used as the do= parameter
394 * @param string $value (optional) Displayed label. Uses $act if not provided.
395 * @param array $attrs Optional attributes.
396 * @return array pseudo-tag
398 * @author Tom N Harris <tnharris@whoopdedo.org>
400 function form_makeButton($type, $act, $value='', $attrs=array()) {
401 if ($value == '') $value = $act;
402 $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
403 'value'=>$value);
404 if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
405 $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']';
407 return array_merge($elem, $attrs);
411 * form_makeField
413 * Create a form element for a labelled input element.
414 * The label text will be printed before the input.
416 * @param string $type Type attribute of input.
417 * @param string $name Name attribute of the input.
418 * @param string $value (optional) Default value.
419 * @param string $class Class attribute of the label. If this is 'block',
420 * then a line break will be added after the field.
421 * @param string $label Label that will be printed before the input.
422 * @param string $id ID attribute of the input. If set, the label will
423 * reference it with a 'for' attribute.
424 * @param array $attrs Optional attributes.
425 * @return array pseudo-tag
427 * @author Tom N Harris <tnharris@whoopdedo.org>
429 function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
430 if (is_null($label)) $label = $name;
431 $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
432 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
433 return array_merge($elem, $attrs);
437 * form_makeFieldRight
439 * Create a form element for a labelled input element.
440 * The label text will be printed after the input.
442 * @see form_makeField
443 * @author Tom N Harris <tnharris@whoopdedo.org>
445 * @param string $type
446 * @param string $name
447 * @param string $value
448 * @param null|string $label
449 * @param string $id
450 * @param string $class
451 * @param array $attrs
453 * @return array
455 function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
456 if (is_null($label)) $label = $name;
457 $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
458 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
459 return array_merge($elem, $attrs);
463 * form_makeTextField
465 * Create a form element for a text input element with label.
467 * @see form_makeField
468 * @author Tom N Harris <tnharris@whoopdedo.org>
470 * @param string $name
471 * @param string $value
472 * @param null|string $label
473 * @param string $id
474 * @param string $class
475 * @param array $attrs
477 * @return array
479 function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) {
480 if (is_null($label)) $label = $name;
481 $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
482 'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
483 return array_merge($elem, $attrs);
487 * form_makePasswordField
489 * Create a form element for a password input element with label.
490 * Password elements have no default value, for obvious reasons.
492 * @see form_makeField
493 * @author Tom N Harris <tnharris@whoopdedo.org>
495 * @param string $name
496 * @param null|string $label
497 * @param string $id
498 * @param string $class
499 * @param array $attrs
501 * @return array
503 function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) {
504 if (is_null($label)) $label = $name;
505 $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
506 'id'=>$id, 'name'=>$name, 'class'=>'edit');
507 return array_merge($elem, $attrs);
511 * form_makeFileField
513 * Create a form element for a file input element with label
515 * @see form_makeField
516 * @author Michael Klier <chi@chimeric.de>
518 * @param string $name
519 * @param null|string $label
520 * @param string $id
521 * @param string $class
522 * @param array $attrs
524 * @return array
526 function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) {
527 if (is_null($label)) $label = $name;
528 $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
529 'id'=>$id, 'name'=>$name, 'class'=>'edit');
530 return array_merge($elem, $attrs);
534 * form_makeCheckboxField
536 * Create a form element for a checkbox input element with label.
537 * If $value is an array, a hidden field with the same name and the value
538 * $value[1] is constructed as well.
540 * @see form_makeFieldRight
541 * @author Tom N Harris <tnharris@whoopdedo.org>
543 * @param string $name
544 * @param string $value
545 * @param null|string $label
546 * @param string $id
547 * @param string $class
548 * @param array $attrs
550 * @return array
552 function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
553 if (is_null($label)) $label = $name;
554 if (is_null($value) || $value=='') $value='0';
555 $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
556 'id'=>$id, 'name'=>$name, 'value'=>$value);
557 return array_merge($elem, $attrs);
561 * form_makeRadioField
563 * Create a form element for a radio button input element with label.
565 * @see form_makeFieldRight
566 * @author Tom N Harris <tnharris@whoopdedo.org>
568 * @param string $name
569 * @param string $value
570 * @param null|string $label
571 * @param string $id
572 * @param string $class
573 * @param array $attrs
575 * @return array
577 function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
578 if (is_null($label)) $label = $name;
579 if (is_null($value) || $value=='') $value='0';
580 $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
581 'id'=>$id, 'name'=>$name, 'value'=>$value);
582 return array_merge($elem, $attrs);
586 * form_makeMenuField
588 * Create a form element for a drop-down menu with label.
589 * The list of values can be strings, arrays of (value,text),
590 * or an associative array with the values as keys and labels as values.
591 * An item is selected by supplying its value or integer index.
592 * If the list of values is an associative array, the selected item must be
593 * a string.
595 * @author Tom N Harris <tnharris@whoopdedo.org>
597 * @param string $name Name attribute of the input.
598 * @param string[]|array[] $values The list of values can be strings, arrays of (value,text),
599 * or an associative array with the values as keys and labels as values.
600 * @param string|int $selected default selected value, string or index number
601 * @param string $class Class attribute of the label. If this is 'block',
602 * then a line break will be added after the field.
603 * @param string $label Label that will be printed before the input.
604 * @param string $id ID attribute of the input. If set, the label will
605 * reference it with a 'for' attribute.
606 * @param array $attrs Optional attributes.
607 * @return array pseudo-tag
609 function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
610 if (is_null($label)) $label = $name;
611 $options = array();
612 reset($values);
613 // FIXME: php doesn't know the difference between a string and an integer
614 if (is_string(key($values))) {
615 foreach ($values as $val=>$text) {
616 $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
618 } else {
619 if (is_integer($selected)) $selected = $values[$selected];
620 foreach ($values as $val) {
621 if (is_array($val))
622 @list($val,$text) = $val;
623 else
624 $text = null;
625 $options[] = array($val,$text,$val===$selected);
628 $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
629 'id'=>$id, 'name'=>$name);
630 return array_merge($elem, $attrs);
634 * form_makeListboxField
636 * Create a form element for a list box with label.
637 * The list of values can be strings, arrays of (value,text),
638 * or an associative array with the values as keys and labels as values.
639 * Items are selected by supplying its value or an array of values.
641 * @author Tom N Harris <tnharris@whoopdedo.org>
643 * @param string $name Name attribute of the input.
644 * @param string[]|array[] $values The list of values can be strings, arrays of (value,text),
645 * or an associative array with the values as keys and labels as values.
646 * @param array|string $selected value or array of values of the items that need to be selected
647 * @param string $class Class attribute of the label. If this is 'block',
648 * then a line break will be added after the field.
649 * @param string $label Label that will be printed before the input.
650 * @param string $id ID attribute of the input. If set, the label will
651 * reference it with a 'for' attribute.
652 * @param array $attrs Optional attributes.
653 * @return array pseudo-tag
655 function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
656 if (is_null($label)) $label = $name;
657 $options = array();
658 reset($values);
659 if (is_null($selected) || $selected == '') {
660 $selected = array();
661 } elseif (!is_array($selected)) {
662 $selected = array($selected);
664 // FIXME: php doesn't know the difference between a string and an integer
665 if (is_string(key($values))) {
666 foreach ($values as $val=>$text) {
667 $options[] = array($val,$text,in_array($val,$selected));
669 } else {
670 foreach ($values as $val) {
671 $disabled = false;
672 if (is_array($val)) {
673 @list($val,$text,$disabled) = $val;
674 } else {
675 $text = null;
677 $options[] = array($val,$text,in_array($val,$selected),$disabled);
680 $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
681 'id'=>$id, 'name'=>$name);
682 return array_merge($elem, $attrs);
686 * form_tag
688 * Print the HTML for a generic empty tag.
689 * Requires '_tag' key with name of the tag.
690 * Attributes are passed to buildAttributes()
692 * @author Tom N Harris <tnharris@whoopdedo.org>
694 * @param array $attrs attributes
695 * @return string html of tag
697 function form_tag($attrs) {
698 return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'/>';
702 * form_opentag
704 * Print the HTML for a generic opening tag.
705 * Requires '_tag' key with name of the tag.
706 * Attributes are passed to buildAttributes()
708 * @author Tom N Harris <tnharris@whoopdedo.org>
710 * @param array $attrs attributes
711 * @return string html of tag
713 function form_opentag($attrs) {
714 return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
718 * form_closetag
720 * Print the HTML for a generic closing tag.
721 * Requires '_tag' key with name of the tag.
722 * There are no attributes.
724 * @author Tom N Harris <tnharris@whoopdedo.org>
726 * @param array $attrs attributes
727 * @return string html of tag
729 function form_closetag($attrs) {
730 return '</'.$attrs['_tag'].'>';
734 * form_openfieldset
736 * Print the HTML for an opening fieldset tag.
737 * Uses the '_legend' key.
738 * Attributes are passed to buildAttributes()
740 * @author Tom N Harris <tnharris@whoopdedo.org>
742 * @param array $attrs attributes
743 * @return string html
745 function form_openfieldset($attrs) {
746 $s = '<fieldset '.buildAttributes($attrs,true).'>';
747 if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
748 return $s;
752 * form_closefieldset
754 * Print the HTML for a closing fieldset tag.
755 * There are no attributes.
757 * @author Tom N Harris <tnharris@whoopdedo.org>
759 * @return string html
761 function form_closefieldset() {
762 return '</fieldset>';
766 * form_hidden
768 * Print the HTML for a hidden input element.
769 * Uses only 'name' and 'value' attributes.
770 * Value is passed to formText()
772 * @author Tom N Harris <tnharris@whoopdedo.org>
774 * @param array $attrs attributes
775 * @return string html
777 function form_hidden($attrs) {
778 return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
782 * form_wikitext
784 * Print the HTML for the wiki textarea.
785 * Requires '_text' with default text of the field.
786 * Text will be passed to formText(), attributes to buildAttributes()
788 * @author Tom N Harris <tnharris@whoopdedo.org>
790 * @param array $attrs attributes
791 * @return string html
793 function form_wikitext($attrs) {
794 // mandatory attributes
795 unset($attrs['name']);
796 unset($attrs['id']);
797 return '<textarea name="wikitext" id="wiki__text" dir="auto" '
798 .buildAttributes($attrs,true).'>'.DOKU_LF
799 .formText($attrs['_text'])
800 .'</textarea>';
804 * form_button
806 * Print the HTML for a form button.
807 * If '_action' is set, the button name will be "do[_action]".
808 * Other attributes are passed to buildAttributes()
810 * @author Tom N Harris <tnharris@whoopdedo.org>
812 * @param array $attrs attributes
813 * @return string html
815 function form_button($attrs) {
816 $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
817 $value = $attrs['value'];
818 unset($attrs['value']);
819 return '<button '.$p.buildAttributes($attrs,true).'>'.$value.'</button>';
823 * form_field
825 * Print the HTML for a form input field.
826 * _class : class attribute used on the label tag
827 * _text : Text to display before the input. Not escaped.
828 * Other attributes are passed to buildAttributes() for the input tag.
830 * @author Tom N Harris <tnharris@whoopdedo.org>
832 * @param array $attrs attributes
833 * @return string html
835 function form_field($attrs) {
836 $s = '<label';
837 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
838 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
839 $s .= '><span>'.$attrs['_text'].'</span>';
840 $s .= ' <input '.buildAttributes($attrs,true).' /></label>';
841 if (preg_match('/(^| )block($| )/', $attrs['_class']))
842 $s .= '<br />';
843 return $s;
847 * form_fieldright
849 * Print the HTML for a form input field. (right-aligned)
850 * _class : class attribute used on the label tag
851 * _text : Text to display after the input. Not escaped.
852 * Other attributes are passed to buildAttributes() for the input tag.
854 * @author Tom N Harris <tnharris@whoopdedo.org>
856 * @param array $attrs attributes
857 * @return string html
859 function form_fieldright($attrs) {
860 $s = '<label';
861 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
862 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
863 $s .= '><input '.buildAttributes($attrs,true).' />';
864 $s .= ' <span>'.$attrs['_text'].'</span></label>';
865 if (preg_match('/(^| )block($| )/', $attrs['_class']))
866 $s .= '<br />';
867 return $s;
871 * form_textfield
873 * Print the HTML for a text input field.
874 * _class : class attribute used on the label tag
875 * _text : Text to display before the input. Not escaped.
876 * Other attributes are passed to buildAttributes() for the input tag.
878 * @author Tom N Harris <tnharris@whoopdedo.org>
880 * @param array $attrs attributes
881 * @return string html
883 function form_textfield($attrs) {
884 // mandatory attributes
885 unset($attrs['type']);
886 $s = '<label';
887 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
888 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
889 $s .= '><span>'.$attrs['_text'].'</span> ';
890 $s .= '<input type="text" '.buildAttributes($attrs,true).' /></label>';
891 if (preg_match('/(^| )block($| )/', $attrs['_class']))
892 $s .= '<br />';
893 return $s;
897 * form_passwordfield
899 * Print the HTML for a password input field.
900 * _class : class attribute used on the label tag
901 * _text : Text to display before the input. Not escaped.
902 * Other attributes are passed to buildAttributes() for the input tag.
904 * @author Tom N Harris <tnharris@whoopdedo.org>
906 * @param array $attrs attributes
907 * @return string html
909 function form_passwordfield($attrs) {
910 // mandatory attributes
911 unset($attrs['type']);
912 $s = '<label';
913 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
914 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
915 $s .= '><span>'.$attrs['_text'].'</span> ';
916 $s .= '<input type="password" '.buildAttributes($attrs,true).' /></label>';
917 if (preg_match('/(^| )block($| )/', $attrs['_class']))
918 $s .= '<br />';
919 return $s;
923 * form_filefield
925 * Print the HTML for a file input field.
926 * _class : class attribute used on the label tag
927 * _text : Text to display before the input. Not escaped
928 * _maxlength : Allowed size in byte
929 * _accept : Accepted mime-type
930 * Other attributes are passed to buildAttributes() for the input tag
932 * @author Michael Klier <chi@chimeric.de>
934 * @param array $attrs attributes
935 * @return string html
937 function form_filefield($attrs) {
938 $s = '<label';
939 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
940 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
941 $s .= '><span>'.$attrs['_text'].'</span> ';
942 $s .= '<input type="file" '.buildAttributes($attrs,true);
943 if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
944 if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
945 $s .= ' /></label>';
946 if (preg_match('/(^| )block($| )/', $attrs['_class']))
947 $s .= '<br />';
948 return $s;
952 * form_checkboxfield
954 * Print the HTML for a checkbox input field.
955 * _class : class attribute used on the label tag
956 * _text : Text to display after the input. Not escaped.
957 * Other attributes are passed to buildAttributes() for the input tag.
958 * If value is an array, a hidden field with the same name and the value
959 * $attrs['value'][1] is constructed as well.
961 * @author Tom N Harris <tnharris@whoopdedo.org>
963 * @param array $attrs attributes
964 * @return string html
966 function form_checkboxfield($attrs) {
967 // mandatory attributes
968 unset($attrs['type']);
969 $s = '<label';
970 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
971 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
972 $s .= '>';
973 if (is_array($attrs['value'])) {
974 echo '<input type="hidden" name="' . hsc($attrs['name']) .'"'
975 . ' value="' . hsc($attrs['value'][1]) . '" />';
976 $attrs['value'] = $attrs['value'][0];
978 $s .= '<input type="checkbox" '.buildAttributes($attrs,true).' />';
979 $s .= ' <span>'.$attrs['_text'].'</span></label>';
980 if (preg_match('/(^| )block($| )/', $attrs['_class']))
981 $s .= '<br />';
982 return $s;
986 * form_radiofield
988 * Print the HTML for a radio button input field.
989 * _class : class attribute used on the label tag
990 * _text : Text to display after the input. Not escaped.
991 * Other attributes are passed to buildAttributes() for the input tag.
993 * @author Tom N Harris <tnharris@whoopdedo.org>
995 * @param array $attrs attributes
996 * @return string html
998 function form_radiofield($attrs) {
999 // mandatory attributes
1000 unset($attrs['type']);
1001 $s = '<label';
1002 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
1003 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
1004 $s .= '><input type="radio" '.buildAttributes($attrs,true).' />';
1005 $s .= ' <span>'.$attrs['_text'].'</span></label>';
1006 if (preg_match('/(^| )block($| )/', $attrs['_class']))
1007 $s .= '<br />';
1008 return $s;
1012 * form_menufield
1014 * Print the HTML for a drop-down menu.
1015 * _options : Array of (value,text,selected) for the menu.
1016 * Text can be omitted. Text and value are passed to formText()
1017 * Only one item can be selected.
1018 * _class : class attribute used on the label tag
1019 * _text : Text to display before the menu. Not escaped.
1020 * Other attributes are passed to buildAttributes() for the input tag.
1022 * @author Tom N Harris <tnharris@whoopdedo.org>
1024 * @param array $attrs attributes
1025 * @return string html
1027 function form_menufield($attrs) {
1028 $attrs['size'] = '1';
1029 $s = '<label';
1030 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
1031 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
1032 $s .= '><span>'.$attrs['_text'].'</span>';
1033 $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF;
1034 if (!empty($attrs['_options'])) {
1035 $selected = false;
1037 $cnt = count($attrs['_options']);
1038 for($n=0; $n < $cnt; $n++){
1039 @list($value,$text,$select) = $attrs['_options'][$n];
1040 $p = '';
1041 if (!is_null($text))
1042 $p .= ' value="'.formText($value).'"';
1043 else
1044 $text = $value;
1045 if (!empty($select) && !$selected) {
1046 $p .= ' selected="selected"';
1047 $selected = true;
1049 $s .= '<option'.$p.'>'.formText($text).'</option>';
1051 } else {
1052 $s .= '<option></option>';
1054 $s .= DOKU_LF.'</select></label>';
1055 if (preg_match('/(^| )block($| )/', $attrs['_class']))
1056 $s .= '<br />';
1057 return $s;
1061 * form_listboxfield
1063 * Print the HTML for a list box.
1064 * _options : Array of (value,text,selected) for the list.
1065 * Text can be omitted. Text and value are passed to formText()
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_listboxfield($attrs) {
1076 $s = '<label';
1077 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
1078 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
1079 $s .= '><span>'.$attrs['_text'].'</span> ';
1080 $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF;
1081 if (!empty($attrs['_options'])) {
1082 foreach ($attrs['_options'] as $opt) {
1083 @list($value,$text,$select,$disabled) = $opt;
1084 $p = '';
1085 if(is_null($text)) $text = $value;
1086 $p .= ' value="'.formText($value).'"';
1087 if (!empty($select)) $p .= ' selected="selected"';
1088 if ($disabled) $p .= ' disabled="disabled"';
1089 $s .= '<option'.$p.'>'.formText($text).'</option>';
1091 } else {
1092 $s .= '<option></option>';
1094 $s .= DOKU_LF.'</select></label>';
1095 if (preg_match('/(^| )block($| )/', $attrs['_class']))
1096 $s .= '<br />';
1097 return $s;