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