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