xref: /dokuwiki/inc/form.php (revision 9d706dd2a380574a0f89b771c7b3fd9a77de9dc7)
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  array   pseudo-element if found, false otherwise
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  array   pseudo-element if found, false otherwise
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  array   pseudo-element if found, false otherwise
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 ' . html_attbuild($this->params) . '><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        $value = (isset($_POST[$name]) && isset($entries[$_POST[$name]])) ?
299                 $_POST[$name] : key($entries);
300        foreach($entries as $val => $cap) {
301            $data = ($value === $val) ? array('checked' => 'checked') : array();
302            $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data));
303        }
304    }
305
306}
307
308/**
309 * form_makeTag
310 *
311 * Create a form element for a non-specific empty tag.
312 *
313 * @param   string  $tag    Tag name.
314 * @param   array   $attrs  Optional attributes.
315 * @return  array   pseudo-tag
316 * @author  Tom N Harris <tnharris@whoopdedo.org>
317 */
318function form_makeTag($tag, $attrs=array()) {
319    $elem = array('_elem'=>'tag', '_tag'=>$tag);
320    return array_merge($elem, $attrs);
321}
322
323/**
324 * form_makeOpenTag
325 *
326 * Create a form element for a non-specific opening tag.
327 * Remember to put a matching close tag after this as well.
328 *
329 * @param   string  $tag    Tag name.
330 * @param   array   $attrs  Optional attributes.
331 * @return  array   pseudo-tag
332 * @author  Tom N Harris <tnharris@whoopdedo.org>
333 */
334function form_makeOpenTag($tag, $attrs=array()) {
335    $elem = array('_elem'=>'opentag', '_tag'=>$tag);
336    return array_merge($elem, $attrs);
337}
338
339/**
340 * form_makeCloseTag
341 *
342 * Create a form element for a non-specific closing tag.
343 * Careless use of this will result in invalid XHTML.
344 *
345 * @param   string  $tag    Tag name.
346 * @return  array   pseudo-tag
347 * @author  Tom N Harris <tnharris@whoopdedo.org>
348 */
349function form_makeCloseTag($tag) {
350    return array('_elem'=>'closetag', '_tag'=>$tag);
351}
352
353/**
354 * form_makeWikiText
355 *
356 * Create a form element for a textarea containing wiki text.
357 * Only one wikitext element is allowed on a page. It will have
358 * a name of 'wikitext' and id 'wiki__text'. The text will
359 * be passed to formText() before printing.
360 *
361 * @param   string  $text   Text to fill the field with.
362 * @param   array   $attrs  Optional attributes.
363 * @return  array   pseudo-tag
364 * @author  Tom N Harris <tnharris@whoopdedo.org>
365 */
366function form_makeWikiText($text, $attrs=array()) {
367    $elem = array('_elem'=>'wikitext', '_text'=>$text,
368                        'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
369    return array_merge($elem, $attrs);
370}
371
372/**
373 * form_makeButton
374 *
375 * Create a form element for an action button.
376 * A title will automatically be generated using the value and
377 * accesskey attributes, unless you provide one.
378 *
379 * @param   string  $type   Type attribute. 'submit' or 'cancel'
380 * @param   string  $act    Wiki action of the button, will be used as the do= parameter
381 * @param   string  $value  (optional) Displayed label. Uses $act if not provided.
382 * @param   array   $attrs  Optional attributes.
383 * @return  array   pseudo-tag
384 * @author  Tom N Harris <tnharris@whoopdedo.org>
385 */
386function form_makeButton($type, $act, $value='', $attrs=array()) {
387    if ($value == '') $value = $act;
388    $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
389                        'value'=>$value, 'class'=>'button');
390    if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
391        $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']';
392    }
393    return array_merge($elem, $attrs);
394}
395
396/**
397 * form_makeField
398 *
399 * Create a form element for a labelled input element.
400 * The label text will be printed before the input.
401 *
402 * @param   string  $type   Type attribute of input.
403 * @param   string  $name   Name attribute of the input.
404 * @param   string  $value  (optional) Default value.
405 * @param   string  $class  Class attribute of the label. If this is 'block',
406 *                          then a line break will be added after the field.
407 * @param   string  $label  Label that will be printed before the input.
408 * @param   string  $id     ID attribute of the input. If set, the label will
409 *                          reference it with a 'for' attribute.
410 * @param   array   $attrs  Optional attributes.
411 * @return  array   pseudo-tag
412 * @author  Tom N Harris <tnharris@whoopdedo.org>
413 */
414function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
415    if (is_null($label)) $label = $name;
416    $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
417                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
418    return array_merge($elem, $attrs);
419}
420
421/**
422 * form_makeFieldRight
423 *
424 * Create a form element for a labelled input element.
425 * The label text will be printed after the input.
426 *
427 * @see     form_makeField
428 * @author  Tom N Harris <tnharris@whoopdedo.org>
429 */
430function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
431    if (is_null($label)) $label = $name;
432    $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
433                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
434    return array_merge($elem, $attrs);
435}
436
437/**
438 * form_makeTextField
439 *
440 * Create a form element for a text input element with label.
441 *
442 * @see     form_makeField
443 * @author  Tom N Harris <tnharris@whoopdedo.org>
444 */
445function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) {
446    if (is_null($label)) $label = $name;
447    $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
448                        'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
449    return array_merge($elem, $attrs);
450}
451
452/**
453 * form_makePasswordField
454 *
455 * Create a form element for a password input element with label.
456 * Password elements have no default value, for obvious reasons.
457 *
458 * @see     form_makeField
459 * @author  Tom N Harris <tnharris@whoopdedo.org>
460 */
461function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) {
462    if (is_null($label)) $label = $name;
463    $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
464                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
465    return array_merge($elem, $attrs);
466}
467
468/**
469 * form_makeFileField
470 *
471 * Create a form element for a file input element with label
472 *
473 * @see     form_makeField
474 * @author  Michael Klier <chi@chimeric.de>
475 */
476function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) {
477    if (is_null($label)) $label = $name;
478    $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
479                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
480    return array_merge($elem, $attrs);
481}
482
483/**
484 * form_makeCheckboxField
485 *
486 * Create a form element for a checkbox input element with label.
487 *
488 * @see     form_makeFieldRight
489 * @author  Tom N Harris <tnharris@whoopdedo.org>
490 */
491function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
492    if (is_null($label)) $label = $name;
493    if (is_null($value) || $value=='') $value='0';
494    $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
495                        'id'=>$id, 'name'=>$name, 'value'=>$value);
496    return array_merge($elem, $attrs);
497}
498
499/**
500 * form_makeRadioField
501 *
502 * Create a form element for a radio button input element with label.
503 *
504 * @see     form_makeFieldRight
505 * @author  Tom N Harris <tnharris@whoopdedo.org>
506 */
507function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
508    if (is_null($label)) $label = $name;
509    if (is_null($value) || $value=='') $value='0';
510    $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
511                        'id'=>$id, 'name'=>$name, 'value'=>$value);
512    return array_merge($elem, $attrs);
513}
514
515/**
516 * form_makeMenuField
517 *
518 * Create a form element for a drop-down menu with label.
519 * The list of values can be strings, arrays of (value,text),
520 * or an associative array with the values as keys and labels as values.
521 * An item is selected by supplying its value or integer index.
522 * If the list of values is an associative array, the selected item must be
523 * a string.
524 *
525 * @author  Tom N Harris <tnharris@whoopdedo.org>
526 */
527function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
528    if (is_null($label)) $label = $name;
529    $options = array();
530    reset($values);
531    // FIXME: php doesn't know the difference between a string and an integer
532    if (is_string(key($values))) {
533        foreach ($values as $val=>$text) {
534            $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
535        }
536    } else {
537        if (is_integer($selected)) $selected = $values[$selected];
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,$val===$selected);
544        }
545    }
546    $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
547                        'id'=>$id, 'name'=>$name);
548    return array_merge($elem, $attrs);
549}
550
551/**
552 * form_makeListboxField
553 *
554 * Create a form element for a list box with label.
555 * The list of values can be strings, arrays of (value,text),
556 * or an associative array with the values as keys and labels as values.
557 * Items are selected by supplying its value or an array of values.
558 *
559 * @author  Tom N Harris <tnharris@whoopdedo.org>
560 */
561function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
562    if (is_null($label)) $label = $name;
563    $options = array();
564    reset($values);
565    if (is_null($selected) || $selected == '')
566        $selected = array();
567    elseif (!is_array($selected))
568        $selected = array($selected);
569    // FIXME: php doesn't know the difference between a string and an integer
570    if (is_string(key($values))) {
571        foreach ($values as $val=>$text) {
572            $options[] = array($val,$text,in_array($val,$selected));
573        }
574    } else {
575        foreach ($values as $val) {
576            if (is_array($val))
577                @list($val,$text) = $val;
578            else
579                $text = null;
580            $options[] = array($val,$text,in_array($val,$selected));
581        }
582    }
583    $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
584                        'id'=>$id, 'name'=>$name);
585    return array_merge($elem, $attrs);
586}
587
588/**
589 * form_tag
590 *
591 * Print the HTML for a generic empty tag.
592 * Requires '_tag' key with name of the tag.
593 * Attributes are passed to buildAttributes()
594 *
595 * @author  Tom N Harris <tnharris@whoopdedo.org>
596 */
597function form_tag($attrs) {
598    return '<'.$attrs['_tag'].' '.buildAttributes($attrs).'/>';
599}
600
601/**
602 * form_opentag
603 *
604 * Print the HTML for a generic opening tag.
605 * Requires '_tag' key with name of the tag.
606 * Attributes are passed to buildAttributes()
607 *
608 * @author  Tom N Harris <tnharris@whoopdedo.org>
609 */
610function form_opentag($attrs) {
611    return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
612}
613
614/**
615 * form_closetag
616 *
617 * Print the HTML for a generic closing tag.
618 * Requires '_tag' key with name of the tag.
619 * There are no attributes.
620 *
621 * @author  Tom N Harris <tnharris@whoopdedo.org>
622 */
623function form_closetag($attrs) {
624    return '</'.$attrs['_tag'].'>';
625}
626
627/**
628 * form_openfieldset
629 *
630 * Print the HTML for an opening fieldset tag.
631 * Uses the '_legend' key.
632 * Attributes are passed to buildAttributes()
633 *
634 * @author  Tom N Harris <tnharris@whoopdedo.org>
635 */
636function form_openfieldset($attrs) {
637    $s = '<fieldset '.buildAttributes($attrs,true).'>';
638    if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
639    return $s;
640}
641
642/**
643 * form_closefieldset
644 *
645 * Print the HTML for a closing fieldset tag.
646 * There are no attributes.
647 *
648 * @author  Tom N Harris <tnharris@whoopdedo.org>
649 */
650function form_closefieldset() {
651    return '</fieldset>';
652}
653
654/**
655 * form_hidden
656 *
657 * Print the HTML for a hidden input element.
658 * Uses only 'name' and 'value' attributes.
659 * Value is passed to formText()
660 *
661 * @author  Tom N Harris <tnharris@whoopdedo.org>
662 */
663function form_hidden($attrs) {
664    return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
665}
666
667/**
668 * form_wikitext
669 *
670 * Print the HTML for the wiki textarea.
671 * Requires '_text' with default text of the field.
672 * Text will be passed to formText(), attributes to buildAttributes()
673 *
674 * @author  Tom N Harris <tnharris@whoopdedo.org>
675 */
676function form_wikitext($attrs) {
677    // mandatory attributes
678    unset($attrs['name']);
679    unset($attrs['id']);
680    return '<textarea name="wikitext" id="wiki__text" '
681                 .buildAttributes($attrs,true).'>'.DOKU_LF
682                 .formText($attrs['_text'])
683                 .'</textarea>';
684}
685
686/**
687 * form_button
688 *
689 * Print the HTML for a form button.
690 * If '_action' is set, the button name will be "do[_action]".
691 * Other attributes are passed to buildAttributes()
692 *
693 * @author  Tom N Harris <tnharris@whoopdedo.org>
694 */
695function form_button($attrs) {
696    $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
697    return '<input '.$p.buildAttributes($attrs,true).'/>';
698}
699
700/**
701 * form_field
702 *
703 * Print the HTML for a form input field.
704 *   _class : class attribute used on the label tag
705 *   _text  : Text to display before the input. Not escaped.
706 * Other attributes are passed to buildAttributes() for the input tag.
707 *
708 * @author  Tom N Harris <tnharris@whoopdedo.org>
709 */
710function form_field($attrs) {
711    $s = '<label';
712    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
713    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
714    $s .= '><span>'.$attrs['_text'].'</span>';
715    $s .= ' <input '.buildAttributes($attrs,true).'/></label>';
716    if (preg_match('/(^| )block($| )/', $attrs['_class']))
717        $s .= '<br />';
718    return $s;
719}
720
721/**
722 * form_fieldright
723 *
724 * Print the HTML for a form input field. (right-aligned)
725 *   _class : class attribute used on the label tag
726 *   _text  : Text to display after the input. Not escaped.
727 * Other attributes are passed to buildAttributes() for the input tag.
728 *
729 * @author  Tom N Harris <tnharris@whoopdedo.org>
730 */
731function form_fieldright($attrs) {
732    $s = '<label';
733    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
734    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
735    $s .= '><input '.buildAttributes($attrs,true).'/>';
736    $s .= ' <span>'.$attrs['_text'].'</span></label>';
737    if (preg_match('/(^| )block($| )/', $attrs['_class']))
738        $s .= '<br />';
739    return $s;
740}
741
742/**
743 * form_textfield
744 *
745 * Print the HTML for a text input field.
746 *   _class : class attribute used on the label tag
747 *   _text  : Text to display before the input. Not escaped.
748 * Other attributes are passed to buildAttributes() for the input tag.
749 *
750 * @author  Tom N Harris <tnharris@whoopdedo.org>
751 */
752function form_textfield($attrs) {
753    // mandatory attributes
754    unset($attrs['type']);
755    $s = '<label';
756    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
757    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
758    $s .= '><span>'.$attrs['_text'].'</span> ';
759    $s .= '<input type="text" '.buildAttributes($attrs,true).'/></label>';
760    if (preg_match('/(^| )block($| )/', $attrs['_class']))
761        $s .= '<br />';
762    return $s;
763}
764
765/**
766 * form_passwordfield
767 *
768 * Print the HTML for a password input field.
769 *   _class : class attribute used on the label tag
770 *   _text  : Text to display before the input. Not escaped.
771 * Other attributes are passed to buildAttributes() for the input tag.
772 *
773 * @author  Tom N Harris <tnharris@whoopdedo.org>
774 */
775function form_passwordfield($attrs) {
776    // mandatory attributes
777    unset($attrs['type']);
778    $s = '<label';
779    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
780    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
781    $s .= '><span>'.$attrs['_text'].'</span> ';
782    $s .= '<input type="password" '.buildAttributes($attrs,true).'/></label>';
783    if (preg_match('/(^| )block($| )/', $attrs['_class']))
784        $s .= '<br />';
785    return $s;
786}
787
788/**
789 * form_filefield
790 *
791 * Print the HTML for a file input field.
792 *   _class     : class attribute used on the label tag
793 *   _text      : Text to display before the input. Not escaped
794 *   _maxlength : Allowed size in byte
795 *   _accept    : Accepted mime-type
796 * Other attributes are passed to buildAttributes() for the input tag
797 *
798 * @author  Michael Klier <chi@chimeric.de>
799 */
800function form_filefield($attrs) {
801    $s = '<label';
802    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
803    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
804    $s .= '><span>'.$attrs['_text'].'</span> ';
805    $s .= '<input type="file" '.buildAttributes($attrs,true);
806    if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
807    if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
808    $s .= '/></label>';
809    if (preg_match('/(^| )block($| )/', $attrs['_class']))
810        $s .= '<br />';
811    return $s;
812}
813
814/**
815 * form_checkboxfield
816 *
817 * Print the HTML for a checkbox input field.
818 *   _class : class attribute used on the label tag
819 *   _text  : Text to display after the input. Not escaped.
820 * Other attributes are passed to buildAttributes() for the input tag.
821 *
822 * @author  Tom N Harris <tnharris@whoopdedo.org>
823 */
824function form_checkboxfield($attrs) {
825    // mandatory attributes
826    unset($attrs['type']);
827    $s = '<label';
828    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
829    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
830    $s .= '><input type="checkbox" '.buildAttributes($attrs,true).'/>';
831    $s .= ' <span>'.$attrs['_text'].'</span></label>';
832    if (preg_match('/(^| )block($| )/', $attrs['_class']))
833        $s .= '<br />';
834    return $s;
835}
836
837/**
838 * form_radiofield
839 *
840 * Print the HTML for a radio button input field.
841 *   _class : class attribute used on the label tag
842 *   _text  : Text to display after the input. Not escaped.
843 * Other attributes are passed to buildAttributes() for the input tag.
844 *
845 * @author  Tom N Harris <tnharris@whoopdedo.org>
846 */
847function form_radiofield($attrs) {
848    // mandatory attributes
849    unset($attrs['type']);
850    $s = '<label';
851    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
852    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
853    $s .= '><input type="radio" '.buildAttributes($attrs,true).'/>';
854    $s .= ' <span>'.$attrs['_text'].'</span></label>';
855    if (preg_match('/(^| )block($| )/', $attrs['_class']))
856        $s .= '<br />';
857    return $s;
858}
859
860/**
861 * form_menufield
862 *
863 * Print the HTML for a drop-down menu.
864 *   _options : Array of (value,text,selected) for the menu.
865 *              Text can be omitted. Text and value are passed to formText()
866 *              Only one item can be selected.
867 *   _class : class attribute used on the label tag
868 *   _text  : Text to display before the menu. Not escaped.
869 * Other attributes are passed to buildAttributes() for the input tag.
870 *
871 * @author  Tom N Harris <tnharris@whoopdedo.org>
872 */
873function form_menufield($attrs) {
874    $attrs['size'] = '1';
875    $s = '<label';
876    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
877    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
878    $s .= '><span>'.$attrs['_text'].'</span>';
879    $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF;
880    if (!empty($attrs['_options'])) {
881        $selected = false;
882
883        $cnt = count($attrs['_options']);
884        for($n=0; $n < $cnt; $n++){
885            @list($value,$text,$select) = $attrs['_options'][$n];
886            $p = '';
887            if (!is_null($text))
888                $p .= ' value="'.formText($value).'"';
889            else
890                $text = $value;
891            if (!empty($select) && !$selected) {
892                $p .= ' selected="selected"';
893                $selected = true;
894            }
895            $s .= '<option'.$p.'>'.formText($text).'</option>';
896        }
897    } else {
898        $s .= '<option></option>';
899    }
900    $s .= DOKU_LF.'</select></label>';
901    if (preg_match('/(^| )block($| )/', $attrs['_class']))
902        $s .= '<br />';
903    return $s;
904}
905
906/**
907 * form_listboxfield
908 *
909 * Print the HTML for a list box.
910 *   _options : Array of (value,text,selected) for the list.
911 *              Text can be omitted. Text and value are passed to formText()
912 *   _class : class attribute used on the label tag
913 *   _text  : Text to display before the menu. Not escaped.
914 * Other attributes are passed to buildAttributes() for the input tag.
915 *
916 * @author  Tom N Harris <tnharris@whoopdedo.org>
917 */
918function form_listboxfield($attrs) {
919    $s = '<label';
920    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
921    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
922    $s .= '><span>'.$attrs['_text'].'</span> ';
923    $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF;
924    if (!empty($attrs['_options'])) {
925        foreach ($attrs['_options'] as $opt) {
926            @list($value,$text,$select) = $opt;
927            $p = '';
928            if(is_null($text)) $text = $value;
929            $p .= ' value="'.formText($value).'"';
930            if (!empty($select)) $p .= ' selected="selected"';
931            $s .= '<option'.$p.'>'.formText($text).'</option>';
932        }
933    } else {
934        $s .= '<option></option>';
935    }
936    $s .= DOKU_LF.'</select></label>';
937    if (preg_match('/(^| )block($| )/', $attrs['_class']))
938        $s .= '<br />';
939    return $s;
940}
941