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