xref: /dokuwiki/inc/form.php (revision d4caa43b1b7b6d8cead392e391bdfadd08b24c11)
1<?php
2/**
3 * DokuWiki XHTML Form
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Tom N Harris <tnharris@whoopdedo.org>
7 */
8
9if(!defined('DOKU_INC')) die('meh.');
10require_once(DOKU_INC.'inc/html.php');
11
12/**
13 * Class for creating simple HTML forms.
14 *
15 * The forms is built from a list of pseudo-tags (arrays with expected keys).
16 * Every pseudo-tag must have the key '_elem' set to the name of the element.
17 * When printed, the form class calls functions named 'form_$type' for each
18 * element it contains.
19 *
20 * Standard practice is for non-attribute keys in a pseudo-element to start
21 * with '_'. Other keys are HTML attributes that will be included in the element
22 * tag. That way, the element output functions can pass the pseudo-element
23 * directly to buildAttributes.
24 *
25 * See the form_make* functions later in this file.
26 *
27 * @author Tom N Harris <tnharris@whoopdedo.org>
28 */
29class Doku_Form {
30
31    // Form id attribute
32    var $params = array();
33
34    // Draw a border around form fields.
35    // Adds <fieldset></fieldset> around the elements
36    var $_infieldset = false;
37
38    // Hidden form fields.
39    var $_hidden = array();
40
41    // Array of pseudo-tags
42    var $_content = array();
43
44    /**
45     * Constructor
46     *
47     * Sets parameters and autoadds a security token. The old calling convention
48     * with up to four parameters is deprecated, instead the first parameter
49     * should be an array with parameters.
50     *
51     * @param   mixed   $params  Parameters for the HTML form element; Using the
52     *                           deprecated calling convention this is the ID
53     *                           attribute of the form
54     * @param   string  $action  (optional, deprecated) submit URL, defaults to
55     *                                                  current page
56     * @param   string  $method  (optional, deprecated) 'POST' or 'GET', default
57     *                                                  is POST
58     * @param   string  $enctype (optional, deprecated) Encoding type of the
59     *                                                  data
60     * @author  Tom N Harris <tnharris@whoopdedo.org>
61     */
62    function Doku_Form($params, $action=false, $method=false, $enctype=false) {
63        if(!is_array($params)) {
64            $this->params = array('id' => $params);
65            if ($action !== false) $this->params['action'] = $action;
66            if ($method !== false) $this->params['method'] = $method;
67            if ($enctype !== false) $this->params['enctype'] = $enctype;
68        } else {
69            $this->params = $params;
70        }
71
72        if (!isset($this->params['method'])) {
73            $this->params['method'] = 'POST';
74        }
75
76        $this->addHidden('sectok', getSecurityToken());
77    }
78
79    /**
80     * startFieldset
81     *
82     * Add <fieldset></fieldset> tags around fields.
83     * Usually results in a border drawn around the form.
84     *
85     * @param   string  $legend Label that will be printed with the border.
86     * @author  Tom N Harris <tnharris@whoopdedo.org>
87     */
88    function startFieldset($legend) {
89        if ($this->_infieldset) {
90            $this->addElement(array('_elem'=>'closefieldset'));
91        }
92        $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend));
93        $this->_infieldset = true;
94    }
95
96    /**
97     * endFieldset
98     *
99     * @author  Tom N Harris <tnharris@whoopdedo.org>
100     */
101    function endFieldset() {
102        if ($this->_infieldset) {
103            $this->addElement(array('_elem'=>'closefieldset'));
104        }
105        $this->_infieldset = false;
106    }
107
108    /**
109     * addHidden
110     *
111     * Adds a name/value pair as a hidden field.
112     * The value of the field (but not the name) will be passed to
113     * formText() before printing.
114     *
115     * @param   string  $name   Field name.
116     * @param   string  $value  Field value. If null, remove a previously added field.
117     * @author  Tom N Harris <tnharris@whoopdedo.org>
118     */
119    function addHidden($name, $value) {
120        if (is_null($value))
121            unset($this->_hidden[$name]);
122        else
123            $this->_hidden[$name] = $value;
124    }
125
126    /**
127     * addElement
128     *
129     * Appends a content element to the form.
130     * The element can be either a pseudo-tag or string.
131     * If string, it is printed without escaping special chars.   *
132     *
133     * @param   string  $elem   Pseudo-tag or string to add to the form.
134     * @author  Tom N Harris <tnharris@whoopdedo.org>
135     */
136    function addElement($elem) {
137        $this->_content[] = $elem;
138    }
139
140    /**
141     * insertElement
142     *
143     * Inserts a content element at a position.
144     *
145     * @param   string  $pos    0-based index where the element will be inserted.
146     * @param   string  $elem   Pseudo-tag or string to add to the form.
147     * @author  Tom N Harris <tnharris@whoopdedo.org>
148     */
149    function insertElement($pos, $elem) {
150        array_splice($this->_content, $pos, 0, array($elem));
151    }
152
153    /**
154     * replaceElement
155     *
156     * Replace with NULL to remove an element.
157     *
158     * @param   int     $pos    0-based index the element will be placed at.
159     * @param   string  $elem   Pseudo-tag or string to add to the form.
160     * @author  Tom N Harris <tnharris@whoopdedo.org>
161     */
162    function replaceElement($pos, $elem) {
163        $rep = array();
164        if (!is_null($elem)) $rep[] = $elem;
165        array_splice($this->_content, $pos, 1, $rep);
166    }
167
168    /**
169     * findElementByType
170     *
171     * Gets the position of the first of a type of element.
172     *
173     * @param   string  $type   Element type to look for.
174     * @return  array   pseudo-element if found, false otherwise
175     * @author  Tom N Harris <tnharris@whoopdedo.org>
176     */
177    function findElementByType($type) {
178        foreach ($this->_content as $pos=>$elem) {
179            if (is_array($elem) && $elem['_elem'] == $type)
180                return $pos;
181        }
182        return false;
183    }
184
185    /**
186     * findElementById
187     *
188     * Gets the position of the element with an ID attribute.
189     *
190     * @param   string  $id     ID of the element to find.
191     * @return  array   pseudo-element if found, false otherwise
192     * @author  Tom N Harris <tnharris@whoopdedo.org>
193     */
194    function findElementById($id) {
195        foreach ($this->_content as $pos=>$elem) {
196            if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
197                return $pos;
198        }
199        return false;
200    }
201
202    /**
203     * findElementByAttribute
204     *
205     * Gets the position of the first element with a matching attribute value.
206     *
207     * @param   string  $name   Attribute name.
208     * @param   string  $value  Attribute value.
209     * @return  array   pseudo-element if found, false otherwise
210     * @author  Tom N Harris <tnharris@whoopdedo.org>
211     */
212    function findElementByAttribute($name, $value) {
213        foreach ($this->_content as $pos=>$elem) {
214            if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
215                return $pos;
216        }
217        return false;
218    }
219
220    /**
221     * getElementAt
222     *
223     * Returns a reference to the element at a position.
224     * A position out-of-bounds will return either the
225     * first (underflow) or last (overflow) element.
226     *
227     * @param   int     $pos    0-based index
228     * @return  arrayreference  pseudo-element
229     * @author  Tom N Harris <tnharris@whoopdedo.org>
230     */
231    function &getElementAt($pos) {
232        if ($pos < 0) $pos = count($this->_content) + $pos;
233        if ($pos < 0) $pos = 0;
234        if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
235        return $this->_content[$pos];
236    }
237
238    /**
239     * printForm
240     *
241     * Output the form.
242     * Each element in the form will be passed to a function named
243     * 'form_$type'. The function should return the HTML to be printed.
244     *
245     * @author  Tom N Harris <tnharris@whoopdedo.org>
246     */
247    function printForm() {
248        global $lang;
249        $this->params['accept-charset'] = $lang['encoding'];
250        print '<form ' . html_attbuild($this->params) . '><div class="no">' . DOKU_LF;
251        if (!empty($this->_hidden)) {
252            foreach ($this->_hidden as $name=>$value)
253                print form_hidden(array('name'=>$name, 'value'=>$value));
254        }
255        foreach ($this->_content as $element) {
256            if (is_array($element)) {
257                $elem_type = $element['_elem'];
258                if (function_exists('form_'.$elem_type)) {
259                    print call_user_func('form_'.$elem_type, $element).DOKU_LF;
260                }
261            } else {
262                print $element;
263            }
264        }
265        if ($this->_infieldset) print form_closefieldset().DOKU_LF;
266        print '</div></form>'.DOKU_LF;
267    }
268
269}
270
271/**
272 * form_makeTag
273 *
274 * Create a form element for a non-specific empty tag.
275 *
276 * @param   string  $tag    Tag name.
277 * @param   array   $attrs  Optional attributes.
278 * @return  array   pseudo-tag
279 * @author  Tom N Harris <tnharris@whoopdedo.org>
280 */
281function form_makeTag($tag, $attrs=array()) {
282    $elem = array('_elem'=>'tag', '_tag'=>$tag);
283    return array_merge($elem, $attrs);
284}
285
286/**
287 * form_makeOpenTag
288 *
289 * Create a form element for a non-specific opening tag.
290 * Remember to put a matching close tag after this as well.
291 *
292 * @param   string  $tag    Tag name.
293 * @param   array   $attrs  Optional attributes.
294 * @return  array   pseudo-tag
295 * @author  Tom N Harris <tnharris@whoopdedo.org>
296 */
297function form_makeOpenTag($tag, $attrs=array()) {
298    $elem = array('_elem'=>'opentag', '_tag'=>$tag);
299    return array_merge($elem, $attrs);
300}
301
302/**
303 * form_makeCloseTag
304 *
305 * Create a form element for a non-specific closing tag.
306 * Careless use of this will result in invalid XHTML.
307 *
308 * @param   string  $tag    Tag name.
309 * @return  array   pseudo-tag
310 * @author  Tom N Harris <tnharris@whoopdedo.org>
311 */
312function form_makeCloseTag($tag) {
313    return array('_elem'=>'closetag', '_tag'=>$tag);
314}
315
316/**
317 * form_makeWikiText
318 *
319 * Create a form element for a textarea containing wiki text.
320 * Only one wikitext element is allowed on a page. It will have
321 * a name of 'wikitext' and id 'wiki__text'. The text will
322 * be passed to formText() before printing.
323 *
324 * @param   string  $text   Text to fill the field with.
325 * @param   array   $attrs  Optional attributes.
326 * @return  array   pseudo-tag
327 * @author  Tom N Harris <tnharris@whoopdedo.org>
328 */
329function form_makeWikiText($text, $attrs=array()) {
330    $elem = array('_elem'=>'wikitext', '_text'=>$text,
331                        'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
332    return array_merge($elem, $attrs);
333}
334
335/**
336 * form_makeButton
337 *
338 * Create a form element for an action button.
339 * A title will automatically be generated using the value and
340 * accesskey attributes, unless you provide one.
341 *
342 * @param   string  $type   Type attribute. 'submit' or 'cancel'
343 * @param   string  $act    Wiki action of the button, will be used as the do= parameter
344 * @param   string  $value  (optional) Displayed label. Uses $act if not provided.
345 * @param   array   $attrs  Optional attributes.
346 * @return  array   pseudo-tag
347 * @author  Tom N Harris <tnharris@whoopdedo.org>
348 */
349function form_makeButton($type, $act, $value='', $attrs=array()) {
350    if ($value == '') $value = $act;
351    $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
352                        'value'=>$value, 'class'=>'button');
353    if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
354        $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']';
355    }
356    return array_merge($elem, $attrs);
357}
358
359/**
360 * form_makeField
361 *
362 * Create a form element for a labelled input element.
363 * The label text will be printed before the input.
364 *
365 * @param   string  $type   Type attribute of input.
366 * @param   string  $name   Name attribute of the input.
367 * @param   string  $value  (optional) Default value.
368 * @param   string  $class  Class attribute of the label. If this is 'block',
369 *                          then a line break will be added after the field.
370 * @param   string  $label  Label that will be printed before the input.
371 * @param   string  $id     ID attribute of the input. If set, the label will
372 *                          reference it with a 'for' attribute.
373 * @param   array   $attrs  Optional attributes.
374 * @return  array   pseudo-tag
375 * @author  Tom N Harris <tnharris@whoopdedo.org>
376 */
377function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
378    if (is_null($label)) $label = $name;
379    $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
380                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
381    return array_merge($elem, $attrs);
382}
383
384/**
385 * form_makeFieldRight
386 *
387 * Create a form element for a labelled input element.
388 * The label text will be printed after the input.
389 *
390 * @see     form_makeField
391 * @author  Tom N Harris <tnharris@whoopdedo.org>
392 */
393function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
394    if (is_null($label)) $label = $name;
395    $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
396                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
397    return array_merge($elem, $attrs);
398}
399
400/**
401 * form_makeTextField
402 *
403 * Create a form element for a text input element with label.
404 *
405 * @see     form_makeField
406 * @author  Tom N Harris <tnharris@whoopdedo.org>
407 */
408function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) {
409    if (is_null($label)) $label = $name;
410    $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
411                        'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
412    return array_merge($elem, $attrs);
413}
414
415/**
416 * form_makePasswordField
417 *
418 * Create a form element for a password input element with label.
419 * Password elements have no default value, for obvious reasons.
420 *
421 * @see     form_makeField
422 * @author  Tom N Harris <tnharris@whoopdedo.org>
423 */
424function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) {
425    if (is_null($label)) $label = $name;
426    $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
427                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
428    return array_merge($elem, $attrs);
429}
430
431/**
432 * form_makeFileField
433 *
434 * Create a form element for a file input element with label
435 *
436 * @see     form_makeField
437 * @author  Michael Klier <chi@chimeric.de>
438 */
439function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) {
440    if (is_null($label)) $label = $name;
441    $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
442                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
443    return array_merge($elem, $attrs);
444}
445
446/**
447 * form_makeCheckboxField
448 *
449 * Create a form element for a checkbox input element with label.
450 *
451 * @see     form_makeFieldRight
452 * @author  Tom N Harris <tnharris@whoopdedo.org>
453 */
454function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
455    if (is_null($label)) $label = $name;
456    if (is_null($value) || $value=='') $value='0';
457    $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
458                        'id'=>$id, 'name'=>$name, 'value'=>$value);
459    return array_merge($elem, $attrs);
460}
461
462/**
463 * form_makeRadioField
464 *
465 * Create a form element for a radio button input element with label.
466 *
467 * @see     form_makeFieldRight
468 * @author  Tom N Harris <tnharris@whoopdedo.org>
469 */
470function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
471    if (is_null($label)) $label = $name;
472    if (is_null($value) || $value=='') $value='0';
473    $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
474                        'id'=>$id, 'name'=>$name, 'value'=>$value);
475    return array_merge($elem, $attrs);
476}
477
478/**
479 * form_makeMenuField
480 *
481 * Create a form element for a drop-down menu with label.
482 * The list of values can be strings, arrays of (value,text),
483 * or an associative array with the values as keys and labels as values.
484 * An item is selected by supplying its value or integer index.
485 * If the list of values is an associative array, the selected item must be
486 * a string.
487 *
488 * @author  Tom N Harris <tnharris@whoopdedo.org>
489 */
490function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
491    if (is_null($label)) $label = $name;
492    $options = array();
493    reset($values);
494    // FIXME: php doesn't know the difference between a string and an integer
495    if (is_string(key($values))) {
496        foreach ($values as $val=>$text) {
497            $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
498        }
499    } else {
500        if (is_integer($selected)) $selected = $values[$selected];
501        foreach ($values as $val) {
502            if (is_array($val))
503                @list($val,$text) = $val;
504            else
505                $text = null;
506            $options[] = array($val,$text,$val===$selected);
507        }
508    }
509    $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
510                        'id'=>$id, 'name'=>$name);
511    return array_merge($elem, $attrs);
512}
513
514/**
515 * form_makeListboxField
516 *
517 * Create a form element for a list box with label.
518 * The list of values can be strings, arrays of (value,text),
519 * or an associative array with the values as keys and labels as values.
520 * Items are selected by supplying its value or an array of values.
521 *
522 * @author  Tom N Harris <tnharris@whoopdedo.org>
523 */
524function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
525    if (is_null($label)) $label = $name;
526    $options = array();
527    reset($values);
528    if (is_null($selected) || $selected == '')
529        $selected = array();
530    elseif (!is_array($selected))
531        $selected = array($selected);
532    // FIXME: php doesn't know the difference between a string and an integer
533    if (is_string(key($values))) {
534        foreach ($values as $val=>$text) {
535            $options[] = array($val,$text,in_array($val,$selected));
536        }
537    } else {
538        foreach ($values as $val) {
539            if (is_array($val))
540                @list($val,$text) = $val;
541            else
542                $text = null;
543            $options[] = array($val,$text,in_array($val,$selected));
544        }
545    }
546    $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
547                        'id'=>$id, 'name'=>$name);
548    return array_merge($elem, $attrs);
549}
550
551/**
552 * form_tag
553 *
554 * Print the HTML for a generic empty tag.
555 * Requires '_tag' key with name of the tag.
556 * Attributes are passed to buildAttributes()
557 *
558 * @author  Tom N Harris <tnharris@whoopdedo.org>
559 */
560function form_tag($attrs) {
561    return '<'.$attrs['_tag'].' '.buildAttributes($attrs).'/>';
562}
563
564/**
565 * form_opentag
566 *
567 * Print the HTML for a generic opening tag.
568 * Requires '_tag' key with name of the tag.
569 * Attributes are passed to buildAttributes()
570 *
571 * @author  Tom N Harris <tnharris@whoopdedo.org>
572 */
573function form_opentag($attrs) {
574    return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
575}
576
577/**
578 * form_closetag
579 *
580 * Print the HTML for a generic closing tag.
581 * Requires '_tag' key with name of the tag.
582 * There are no attributes.
583 *
584 * @author  Tom N Harris <tnharris@whoopdedo.org>
585 */
586function form_closetag($attrs) {
587    return '</'.$attrs['_tag'].'>';
588}
589
590/**
591 * form_openfieldset
592 *
593 * Print the HTML for an opening fieldset tag.
594 * Uses the '_legend' key.
595 * Attributes are passed to buildAttributes()
596 *
597 * @author  Tom N Harris <tnharris@whoopdedo.org>
598 */
599function form_openfieldset($attrs) {
600    $s = '<fieldset '.buildAttributes($attrs,true).'>';
601    if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
602    return $s;
603}
604
605/**
606 * form_closefieldset
607 *
608 * Print the HTML for a closing fieldset tag.
609 * There are no attributes.
610 *
611 * @author  Tom N Harris <tnharris@whoopdedo.org>
612 */
613function form_closefieldset() {
614    return '</fieldset>';
615}
616
617/**
618 * form_hidden
619 *
620 * Print the HTML for a hidden input element.
621 * Uses only 'name' and 'value' attributes.
622 * Value is passed to formText()
623 *
624 * @author  Tom N Harris <tnharris@whoopdedo.org>
625 */
626function form_hidden($attrs) {
627    return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
628}
629
630/**
631 * form_wikitext
632 *
633 * Print the HTML for the wiki textarea.
634 * Requires '_text' with default text of the field.
635 * Text will be passed to formText(), attributes to buildAttributes()
636 *
637 * @author  Tom N Harris <tnharris@whoopdedo.org>
638 */
639function form_wikitext($attrs) {
640    // mandatory attributes
641    unset($attrs['name']);
642    unset($attrs['id']);
643    return '<textarea name="wikitext" id="wiki__text" '
644                 .buildAttributes($attrs,true).'>'.DOKU_LF
645                 .formText($attrs['_text'])
646                 .'</textarea>';
647}
648
649/**
650 * form_button
651 *
652 * Print the HTML for a form button.
653 * If '_action' is set, the button name will be "do[_action]".
654 * Other attributes are passed to buildAttributes()
655 *
656 * @author  Tom N Harris <tnharris@whoopdedo.org>
657 */
658function form_button($attrs) {
659    $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
660    return '<input '.$p.buildAttributes($attrs,true).'/>';
661}
662
663/**
664 * form_field
665 *
666 * Print the HTML for a form input field.
667 *   _class : class attribute used on the label tag
668 *   _text  : Text to display before the input. Not escaped.
669 * Other attributes are passed to buildAttributes() for the input tag.
670 *
671 * @author  Tom N Harris <tnharris@whoopdedo.org>
672 */
673function form_field($attrs) {
674    $s = '<label';
675    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
676    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
677    $s .= '><span>'.$attrs['_text'].'</span>';
678    $s .= ' <input '.buildAttributes($attrs,true).'/></label>';
679    if (preg_match('/(^| )block($| )/', $attrs['_class']))
680        $s .= '<br />';
681    return $s;
682}
683
684/**
685 * form_fieldright
686 *
687 * Print the HTML for a form input field. (right-aligned)
688 *   _class : class attribute used on the label tag
689 *   _text  : Text to display after the input. Not escaped.
690 * Other attributes are passed to buildAttributes() for the input tag.
691 *
692 * @author  Tom N Harris <tnharris@whoopdedo.org>
693 */
694function form_fieldright($attrs) {
695    $s = '<label';
696    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
697    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
698    $s .= '><input '.buildAttributes($attrs,true).'/>';
699    $s .= ' <span>'.$attrs['_text'].'</span></label>';
700    if (preg_match('/(^| )block($| )/', $attrs['_class']))
701        $s .= '<br />';
702    return $s;
703}
704
705/**
706 * form_textfield
707 *
708 * Print the HTML for a text input field.
709 *   _class : class attribute used on the label tag
710 *   _text  : Text to display before the input. Not escaped.
711 * Other attributes are passed to buildAttributes() for the input tag.
712 *
713 * @author  Tom N Harris <tnharris@whoopdedo.org>
714 */
715function form_textfield($attrs) {
716    // mandatory attributes
717    unset($attrs['type']);
718    $s = '<label';
719    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
720    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
721    $s .= '><span>'.$attrs['_text'].'</span> ';
722    $s .= '<input type="text" '.buildAttributes($attrs,true).'/></label>';
723    if (preg_match('/(^| )block($| )/', $attrs['_class']))
724        $s .= '<br />';
725    return $s;
726}
727
728/**
729 * form_passwordfield
730 *
731 * Print the HTML for a password input field.
732 *   _class : class attribute used on the label tag
733 *   _text  : Text to display before the input. Not escaped.
734 * Other attributes are passed to buildAttributes() for the input tag.
735 *
736 * @author  Tom N Harris <tnharris@whoopdedo.org>
737 */
738function form_passwordfield($attrs) {
739    // mandatory attributes
740    unset($attrs['type']);
741    $s = '<label';
742    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
743    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
744    $s .= '><span>'.$attrs['_text'].'</span> ';
745    $s .= '<input type="password" '.buildAttributes($attrs,true).'/></label>';
746    if (preg_match('/(^| )block($| )/', $attrs['_class']))
747        $s .= '<br />';
748    return $s;
749}
750
751/**
752 * form_filefield
753 *
754 * Print the HTML for a file input field.
755 *   _class     : class attribute used on the label tag
756 *   _text      : Text to display before the input. Not escaped
757 *   _maxlength : Allowed size in byte
758 *   _accept    : Accepted mime-type
759 * Other attributes are passed to buildAttributes() for the input tag
760 *
761 * @author  Michael Klier <chi@chimeric.de>
762 */
763function form_filefield($attrs) {
764    $s = '<label';
765    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
766    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
767    $s .= '><span>'.$attrs['_text'].'</span> ';
768    $s .= '<input type="file" '.buildAttributes($attrs,true);
769    if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
770    if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
771    $s .= '/></label>';
772    if (preg_match('/(^| )block($| )/', $attrs['_class']))
773        $s .= '<br />';
774    return $s;
775}
776
777/**
778 * form_checkboxfield
779 *
780 * Print the HTML for a checkbox input field.
781 *   _class : class attribute used on the label tag
782 *   _text  : Text to display after the input. Not escaped.
783 * Other attributes are passed to buildAttributes() for the input tag.
784 *
785 * @author  Tom N Harris <tnharris@whoopdedo.org>
786 */
787function form_checkboxfield($attrs) {
788    // mandatory attributes
789    unset($attrs['type']);
790    $s = '<label';
791    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
792    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
793    $s .= '><input type="checkbox" '.buildAttributes($attrs,true).'/>';
794    $s .= ' <span>'.$attrs['_text'].'</span></label>';
795    if (preg_match('/(^| )block($| )/', $attrs['_class']))
796        $s .= '<br />';
797    return $s;
798}
799
800/**
801 * form_radiofield
802 *
803 * Print the HTML for a radio button input field.
804 *   _class : class attribute used on the label tag
805 *   _text  : Text to display after the input. Not escaped.
806 * Other attributes are passed to buildAttributes() for the input tag.
807 *
808 * @author  Tom N Harris <tnharris@whoopdedo.org>
809 */
810function form_radiofield($attrs) {
811    // mandatory attributes
812    unset($attrs['type']);
813    $s = '<label';
814    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
815    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
816    $s .= '><input type="radio" '.buildAttributes($attrs,true).'/>';
817    $s .= ' <span>'.$attrs['_text'].'</span></label>';
818    if (preg_match('/(^| )block($| )/', $attrs['_class']))
819        $s .= '<br />';
820    return $s;
821}
822
823/**
824 * form_menufield
825 *
826 * Print the HTML for a drop-down menu.
827 *   _options : Array of (value,text,selected) for the menu.
828 *              Text can be omitted. Text and value are passed to formText()
829 *              Only one item can be selected.
830 *   _class : class attribute used on the label tag
831 *   _text  : Text to display before the menu. Not escaped.
832 * Other attributes are passed to buildAttributes() for the input tag.
833 *
834 * @author  Tom N Harris <tnharris@whoopdedo.org>
835 */
836function form_menufield($attrs) {
837    $attrs['size'] = '1';
838    $s = '<label';
839    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
840    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
841    $s .= '><span>'.$attrs['_text'].'</span>';
842    $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF;
843    if (!empty($attrs['_options'])) {
844        $selected = false;
845        for($n=0;$n<count($attrs['_options']);$n++){
846            @list($value,$text,$select) = $attrs['_options'][$n];
847            $p = '';
848            if (!is_null($text))
849                $p .= ' value="'.formText($value).'"';
850            else
851                $text = $value;
852            if (!empty($select) && !$selected) {
853                $p .= ' selected="selected"';
854                $selected = true;
855            }
856            $s .= '<option'.$p.'>'.formText($text).'</option>';
857        }
858    } else {
859        $s .= '<option></option>';
860    }
861    $s .= DOKU_LF.'</select></label>';
862    if (preg_match('/(^| )block($| )/', $attrs['_class']))
863        $s .= '<br />';
864    return $s;
865}
866
867/**
868 * form_listboxfield
869 *
870 * Print the HTML for a list box.
871 *   _options : Array of (value,text,selected) for the list.
872 *              Text can be omitted. Text and value are passed to formText()
873 *   _class : class attribute used on the label tag
874 *   _text  : Text to display before the menu. Not escaped.
875 * Other attributes are passed to buildAttributes() for the input tag.
876 *
877 * @author  Tom N Harris <tnharris@whoopdedo.org>
878 */
879function form_listboxfield($attrs) {
880    $s = '<label';
881    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
882    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
883    $s .= '><span>'.$attrs['_text'].'</span> ';
884    $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF;
885    if (!empty($attrs['_options'])) {
886        foreach ($attrs['_options'] as $opt) {
887            @list($value,$text,$select) = $opt;
888            $p = '';
889            if(is_null($text)) $text = $value;
890            $p .= ' value="'.formText($value).'"';
891            if (!empty($select)) $p .= ' selected="selected"';
892            $s .= '<option'.$p.'>'.formText($text).'</option>';
893        }
894    } else {
895        $s .= '<option></option>';
896    }
897    $s .= DOKU_LF.'</select></label>';
898    if (preg_match('/(^| )block($| )/', $attrs['_class']))
899        $s .= '<br />';
900    return $s;
901}
902