about summary refs log tree commit diff stats
path: root/wiki/inc/form.php
blob: 7afb0ba3082aafa604c9adbe614e2c37ad95e2ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
<?php
/**
 * DokuWiki XHTML Form
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Tom N Harris <tnharris@whoopdedo.org>
 */

if(!defined('DOKU_INC')) die('meh.');

/**
 * Class for creating simple HTML forms.
 *
 * The forms is built from a list of pseudo-tags (arrays with expected keys).
 * Every pseudo-tag must have the key '_elem' set to the name of the element.
 * When printed, the form class calls functions named 'form_$type' for each
 * element it contains.
 *
 * Standard practice is for non-attribute keys in a pseudo-element to start
 * with '_'. Other keys are HTML attributes that will be included in the element
 * tag. That way, the element output functions can pass the pseudo-element
 * directly to buildAttributes.
 *
 * See the form_make* functions later in this file.
 *
 * @author Tom N Harris <tnharris@whoopdedo.org>
 */
class Doku_Form {

    // Form id attribute
    public $params = array();

    // Draw a border around form fields.
    // Adds <fieldset></fieldset> around the elements
    public $_infieldset = false;

    // Hidden form fields.
    public $_hidden = array();

    // Array of pseudo-tags
    public $_content = array();

    /**
     * Constructor
     *
     * Sets parameters and autoadds a security token. The old calling convention
     * with up to four parameters is deprecated, instead the first parameter
     * should be an array with parameters.
     *
     * @param mixed       $params  Parameters for the HTML form element; Using the deprecated
     *                             calling convention this is the ID attribute of the form
     * @param bool|string $action  (optional, deprecated) submit URL, defaults to current page
     * @param bool|string $method  (optional, deprecated) 'POST' or 'GET', default is POST
     * @param bool|string $enctype (optional, deprecated) Encoding type of the data
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function __construct($params, $action=false, $method=false, $enctype=false) {
        if(!is_array($params)) {
            $this->params = array('id' => $params);
            if ($action !== false) $this->params['action'] = $action;
            if ($method !== false) $this->params['method'] = strtolower($method);
            if ($enctype !== false) $this->params['enctype'] = $enctype;
        } else {
            $this->params = $params;
        }

        if (!isset($this->params['method'])) {
            $this->params['method'] = 'post';
        } else {
            $this->params['method'] = strtolower($this->params['method']);
        }

        if (!isset($this->params['action'])) {
            $this->params['action'] = '';
        }

        $this->addHidden('sectok', getSecurityToken());
    }

    /**
     * startFieldset
     *
     * Add <fieldset></fieldset> tags around fields.
     * Usually results in a border drawn around the form.
     *
     * @param   string  $legend Label that will be printed with the border.
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function startFieldset($legend) {
        if ($this->_infieldset) {
            $this->addElement(array('_elem'=>'closefieldset'));
        }
        $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend));
        $this->_infieldset = true;
    }

    /**
     * endFieldset
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function endFieldset() {
        if ($this->_infieldset) {
            $this->addElement(array('_elem'=>'closefieldset'));
        }
        $this->_infieldset = false;
    }

    /**
     * addHidden
     *
     * Adds a name/value pair as a hidden field.
     * The value of the field (but not the name) will be passed to
     * formText() before printing.
     *
     * @param   string  $name   Field name.
     * @param   string  $value  Field value. If null, remove a previously added field.
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function addHidden($name, $value) {
        if (is_null($value))
            unset($this->_hidden[$name]);
        else
            $this->_hidden[$name] = $value;
    }

    /**
     * addElement
     *
     * Appends a content element to the form.
     * The element can be either a pseudo-tag or string.
     * If string, it is printed without escaping special chars.   *
     *
     * @param   string|array  $elem   Pseudo-tag or string to add to the form.
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function addElement($elem) {
        $this->_content[] = $elem;
    }

    /**
     * insertElement
     *
     * Inserts a content element at a position.
     *
     * @param   string       $pos  0-based index where the element will be inserted.
     * @param   string|array $elem Pseudo-tag or string to add to the form.
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function insertElement($pos, $elem) {
        array_splice($this->_content, $pos, 0, array($elem));
    }

    /**
     * replaceElement
     *
     * Replace with NULL to remove an element.
     *
     * @param   int          $pos  0-based index the element will be placed at.
     * @param   string|array $elem Pseudo-tag or string to add to the form.
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function replaceElement($pos, $elem) {
        $rep = array();
        if (!is_null($elem)) $rep[] = $elem;
        array_splice($this->_content, $pos, 1, $rep);
    }

    /**
     * findElementByType
     *
     * Gets the position of the first of a type of element.
     *
     * @param   string  $type   Element type to look for.
     * @return  int|false     position of element if found, otherwise false
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function findElementByType($type) {
        foreach ($this->_content as $pos=>$elem) {
            if (is_array($elem) && $elem['_elem'] == $type)
                return $pos;
        }
        return false;
    }

    /**
     * findElementById
     *
     * Gets the position of the element with an ID attribute.
     *
     * @param   string  $id     ID of the element to find.
     * @return  int|false     position of element if found, otherwise false
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function findElementById($id) {
        foreach ($this->_content as $pos=>$elem) {
            if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
                return $pos;
        }
        return false;
    }

    /**
     * findElementByAttribute
     *
     * Gets the position of the first element with a matching attribute value.
     *
     * @param   string  $name   Attribute name.
     * @param   string  $value  Attribute value.
     * @return  int|false     position of element if found, otherwise false
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function findElementByAttribute($name, $value) {
        foreach ($this->_content as $pos=>$elem) {
            if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
                return $pos;
        }
        return false;
    }

    /**
     * getElementAt
     *
     * Returns a reference to the element at a position.
     * A position out-of-bounds will return either the
     * first (underflow) or last (overflow) element.
     *
     * @param   int     $pos    0-based index
     * @return  array reference  pseudo-element
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     */
    function &getElementAt($pos) {
        if ($pos < 0) $pos = count($this->_content) + $pos;
        if ($pos < 0) $pos = 0;
        if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
        return $this->_content[$pos];
    }

    /**
     * Return the assembled HTML for the form.
     *
     * Each element in the form will be passed to a function named
     * 'form_$type'. The function should return the HTML to be printed.
     *
     * @author  Tom N Harris <tnharris@whoopdedo.org>
     *
     * @return string html of the form
     */
    function getForm() {
        global $lang;
        $form = '';
        $this->params['accept-charset'] = $lang['encoding'];
        $form .= '<form ' . buildAttributes($this->params,false) . '><div class="no">' . DOKU_LF;
        if (!empty($this->_hidden)) {
            foreach ($this->_hidden as $name=>$value)
                $form .= form_hidden(array('name'=>$name, 'value'=>$value));
        }
        foreach ($this->_content as $element) {
            if (is_array($element)) {
                $elem_type = $element['_elem'];
                if (function_exists('form_'.$elem_type)) {
                    $form .= call_user_func('form_'.$elem_type, $element).DOKU_LF;
                }
            } else {
                $form .= $element;
            }
        }
        if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF;
        $form .= '</div></form>'.DOKU_LF;

        return $form;
    }

    /**
     * Print the assembled form
     *
     * wraps around getForm()
     */
    function printForm(){
        echo $this->getForm();
    }

    /**
     * Add a radio set
     *
     * This function adds a set of radio buttons to the form. If $_POST[$name]
     * is set, this radio is preselected, else the first radio button.
     *
     * @param string    $name    The HTML field name
     * @param array     $entries An array of entries $value => $caption
     *
     * @author Adrian Lang <lang@cosmocode.de>
     */

    function addRadioSet($name, $entries) {
        global $INPUT;
        $value = (array_key_exists($INPUT->post->str($name), $entries)) ?
                 $INPUT->str($name) : key($entries);
        foreach($entries as $val => $cap) {
            $data = ($value === $val) ? array('checked' => 'checked') : array();
            $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data));
        }
    }

}

/**
 * form_makeTag
 *
 * Create a form element for a non-specific empty tag.
 *
 * @param   string  $tag    Tag name.
 * @param   array   $attrs  Optional attributes.
 * @return  array   pseudo-tag
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 */
function form_makeTag($tag, $attrs=array()) {
    $elem = array('_elem'=>'tag', '_tag'=>$tag);
    return array_merge($elem, $attrs);
}

/**
 * form_makeOpenTag
 *
 * Create a form element for a non-specific opening tag.
 * Remember to put a matching close tag after this as well.
 *
 * @param   string  $tag    Tag name.
 * @param   array   $attrs  Optional attributes.
 * @return  array   pseudo-tag
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 */
function form_makeOpenTag($tag, $attrs=array()) {
    $elem = array('_elem'=>'opentag', '_tag'=>$tag);
    return array_merge($elem, $attrs);
}

/**
 * form_makeCloseTag
 *
 * Create a form element for a non-specific closing tag.
 * Careless use of this will result in invalid XHTML.
 *
 * @param   string  $tag    Tag name.
 * @return  array   pseudo-tag
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 */
function form_makeCloseTag($tag) {
    return array('_elem'=>'closetag', '_tag'=>$tag);
}

/**
 * form_makeWikiText
 *
 * Create a form element for a textarea containing wiki text.
 * Only one wikitext element is allowed on a page. It will have
 * a name of 'wikitext' and id 'wiki__text'. The text will
 * be passed to formText() before printing.
 *
 * @param   string  $text   Text to fill the field with.
 * @param   array   $attrs  Optional attributes.
 * @return  array   pseudo-tag
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 */
function form_makeWikiText($text, $attrs=array()) {
    $elem = array('_elem'=>'wikitext', '_text'=>$text,
                        'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
    return array_merge($elem, $attrs);
}

/**
 * form_makeButton
 *
 * Create a form element for an action button.
 * A title will automatically be generated using the value and
 * accesskey attributes, unless you provide one.
 *
 * @param   string  $type   Type attribute. 'submit' or 'cancel'
 * @param   string  $act    Wiki action of the button, will be used as the do= parameter
 * @param   string  $value  (optional) Displayed label. Uses $act if not provided.
 * @param   array   $attrs  Optional attributes.
 * @return  array   pseudo-tag
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 */
function form_makeButton($type, $act, $value='', $attrs=array()) {
    if ($value == '') $value = $act;
    $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
                        'value'=>$value);
    if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
        $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']';
    }
    return array_merge($elem, $attrs);
}

/**
 * form_makeField
 *
 * Create a form element for a labelled input element.
 * The label text will be printed before the input.
 *
 * @param   string  $type   Type attribute of input.
 * @param   string  $name   Name attribute of the input.
 * @param   string  $value  (optional) Default value.
 * @param   string  $class  Class attribute of the label. If this is 'block',
 *                          then a line break will be added after the field.
 * @param   string  $label  Label that will be printed before the input.
 * @param   string  $id     ID attribute of the input. If set, the label will
 *                          reference it with a 'for' attribute.
 * @param   array   $attrs  Optional attributes.
 * @return  array   pseudo-tag
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 */
function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
    return array_merge($elem, $attrs);
}

/**
 * form_makeFieldRight
 *
 * Create a form element for a labelled input element.
 * The label text will be printed after the input.
 *
 * @see     form_makeField
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param string $type
 * @param string $name
 * @param string $value
 * @param null|string $label
 * @param string $id
 * @param string $class
 * @param array $attrs
 *
 * @return array
 */
function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
    return array_merge($elem, $attrs);
}

/**
 * form_makeTextField
 *
 * Create a form element for a text input element with label.
 *
 * @see     form_makeField
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param string $name
 * @param string $value
 * @param null|string $label
 * @param string $id
 * @param string $class
 * @param array $attrs
 *
 * @return array
 */
function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
                        'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
    return array_merge($elem, $attrs);
}

/**
 * form_makePasswordField
 *
 * Create a form element for a password input element with label.
 * Password elements have no default value, for obvious reasons.
 *
 * @see     form_makeField
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param string $name
 * @param null|string $label
 * @param string $id
 * @param string $class
 * @param array $attrs
 *
 * @return array
 */
function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
    return array_merge($elem, $attrs);
}

/**
 * form_makeFileField
 *
 * Create a form element for a file input element with label
 *
 * @see     form_makeField
 * @author  Michael Klier <chi@chimeric.de>
 *
 * @param string $name
 * @param null|string $label
 * @param string $id
 * @param string $class
 * @param array $attrs
 *
 * @return array
 */
function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
    return array_merge($elem, $attrs);
}

/**
 * form_makeCheckboxField
 *
 * Create a form element for a checkbox input element with label.
 * If $value is an array, a hidden field with the same name and the value
 * $value[1] is constructed as well.
 *
 * @see     form_makeFieldRight
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param string $name
 * @param string $value
 * @param null|string $label
 * @param string $id
 * @param string $class
 * @param array $attrs
 *
 * @return array
 */
function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    if (is_null($value) || $value=='') $value='0';
    $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
                        'id'=>$id, 'name'=>$name, 'value'=>$value);
    return array_merge($elem, $attrs);
}

/**
 * form_makeRadioField
 *
 * Create a form element for a radio button input element with label.
 *
 * @see     form_makeFieldRight
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param string $name
 * @param string $value
 * @param null|string $label
 * @param string $id
 * @param string $class
 * @param array $attrs
 *
 * @return array
 */
function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    if (is_null($value) || $value=='') $value='0';
    $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
                        'id'=>$id, 'name'=>$name, 'value'=>$value);
    return array_merge($elem, $attrs);
}

/**
 * form_makeMenuField
 *
 * Create a form element for a drop-down menu with label.
 * The list of values can be strings, arrays of (value,text),
 * or an associative array with the values as keys and labels as values.
 * An item is selected by supplying its value or integer index.
 * If the list of values is an associative array, the selected item must be
 * a string.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param string           $name     Name attribute of the input.
 * @param string[]|array[] $values   The list of values can be strings, arrays of (value,text),
 *                                   or an associative array with the values as keys and labels as values.
 * @param string|int       $selected default selected value, string or index number
 * @param string           $class    Class attribute of the label. If this is 'block',
 *                                   then a line break will be added after the field.
 * @param string           $label    Label that will be printed before the input.
 * @param string           $id       ID attribute of the input. If set, the label will
 *                                   reference it with a 'for' attribute.
 * @param array            $attrs    Optional attributes.
 * @return array   pseudo-tag
 */
function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    $options = array();
    reset($values);
    // FIXME: php doesn't know the difference between a string and an integer
    if (is_string(key($values))) {
        foreach ($values as $val=>$text) {
            $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
        }
    } else {
        if (is_integer($selected)) $selected = $values[$selected];
        foreach ($values as $val) {
            if (is_array($val))
                @list($val,$text) = $val;
            else
                $text = null;
            $options[] = array($val,$text,$val===$selected);
        }
    }
    $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
                        'id'=>$id, 'name'=>$name);
    return array_merge($elem, $attrs);
}

/**
 * form_makeListboxField
 *
 * Create a form element for a list box with label.
 * The list of values can be strings, arrays of (value,text),
 * or an associative array with the values as keys and labels as values.
 * Items are selected by supplying its value or an array of values.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param string           $name     Name attribute of the input.
 * @param string[]|array[] $values   The list of values can be strings, arrays of (value,text),
 *                                   or an associative array with the values as keys and labels as values.
 * @param array|string     $selected value or array of values of the items that need to be selected
 * @param string           $class    Class attribute of the label. If this is 'block',
 *                                   then a line break will be added after the field.
 * @param string           $label    Label that will be printed before the input.
 * @param string           $id       ID attribute of the input. If set, the label will
 *                                   reference it with a 'for' attribute.
 * @param array            $attrs    Optional attributes.
 * @return array   pseudo-tag
 */
function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
    if (is_null($label)) $label = $name;
    $options = array();
    reset($values);
    if (is_null($selected) || $selected == '') {
        $selected = array();
    } elseif (!is_array($selected)) {
        $selected = array($selected);
    }
    // FIXME: php doesn't know the difference between a string and an integer
    if (is_string(key($values))) {
        foreach ($values as $val=>$text) {
            $options[] = array($val,$text,in_array($val,$selected));
        }
    } else {
        foreach ($values as $val) {
            $disabled = false;
            if (is_array($val)) {
                @list($val,$text,$disabled) = $val;
            } else {
                $text = null;
            }
            $options[] = array($val,$text,in_array($val,$selected),$disabled);
        }
    }
    $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
                        'id'=>$id, 'name'=>$name);
    return array_merge($elem, $attrs);
}

/**
 * form_tag
 *
 * Print the HTML for a generic empty tag.
 * Requires '_tag' key with name of the tag.
 * Attributes are passed to buildAttributes()
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html of tag
 */
function form_tag($attrs) {
    return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'/>';
}

/**
 * form_opentag
 *
 * Print the HTML for a generic opening tag.
 * Requires '_tag' key with name of the tag.
 * Attributes are passed to buildAttributes()
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html of tag
 */
function form_opentag($attrs) {
    return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
}

/**
 * form_closetag
 *
 * Print the HTML for a generic closing tag.
 * Requires '_tag' key with name of the tag.
 * There are no attributes.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html of tag
 */
function form_closetag($attrs) {
    return '</'.$attrs['_tag'].'>';
}

/**
 * form_openfieldset
 *
 * Print the HTML for an opening fieldset tag.
 * Uses the '_legend' key.
 * Attributes are passed to buildAttributes()
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_openfieldset($attrs) {
    $s = '<fieldset '.buildAttributes($attrs,true).'>';
    if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
    return $s;
}

/**
 * form_closefieldset
 *
 * Print the HTML for a closing fieldset tag.
 * There are no attributes.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @return string html
 */
function form_closefieldset() {
    return '</fieldset>';
}

/**
 * form_hidden
 *
 * Print the HTML for a hidden input element.
 * Uses only 'name' and 'value' attributes.
 * Value is passed to formText()
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_hidden($attrs) {
    return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
}

/**
 * form_wikitext
 *
 * Print the HTML for the wiki textarea.
 * Requires '_text' with default text of the field.
 * Text will be passed to formText(), attributes to buildAttributes()
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_wikitext($attrs) {
    // mandatory attributes
    unset($attrs['name']);
    unset($attrs['id']);
    return '<textarea name="wikitext" id="wiki__text" dir="auto" '
                 .buildAttributes($attrs,true).'>'.DOKU_LF
                 .formText($attrs['_text'])
                 .'</textarea>';
}

/**
 * form_button
 *
 * Print the HTML for a form button.
 * If '_action' is set, the button name will be "do[_action]".
 * Other attributes are passed to buildAttributes()
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_button($attrs) {
    $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
    $value = $attrs['value'];
    unset($attrs['value']);
    return '<button '.$p.buildAttributes($attrs,true).'>'.$value.'</button>';
}

/**
 * form_field
 *
 * Print the HTML for a form input field.
 *   _class : class attribute used on the label tag
 *   _text  : Text to display before the input. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_field($attrs) {
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><span>'.$attrs['_text'].'</span>';
    $s .= ' <input '.buildAttributes($attrs,true).' /></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_fieldright
 *
 * Print the HTML for a form input field. (right-aligned)
 *   _class : class attribute used on the label tag
 *   _text  : Text to display after the input. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_fieldright($attrs) {
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><input '.buildAttributes($attrs,true).' />';
    $s .= ' <span>'.$attrs['_text'].'</span></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_textfield
 *
 * Print the HTML for a text input field.
 *   _class : class attribute used on the label tag
 *   _text  : Text to display before the input. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_textfield($attrs) {
    // mandatory attributes
    unset($attrs['type']);
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><span>'.$attrs['_text'].'</span> ';
    $s .= '<input type="text" '.buildAttributes($attrs,true).' /></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_passwordfield
 *
 * Print the HTML for a password input field.
 *   _class : class attribute used on the label tag
 *   _text  : Text to display before the input. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_passwordfield($attrs) {
    // mandatory attributes
    unset($attrs['type']);
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><span>'.$attrs['_text'].'</span> ';
    $s .= '<input type="password" '.buildAttributes($attrs,true).' /></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_filefield
 *
 * Print the HTML for a file input field.
 *   _class     : class attribute used on the label tag
 *   _text      : Text to display before the input. Not escaped
 *   _maxlength : Allowed size in byte
 *   _accept    : Accepted mime-type
 * Other attributes are passed to buildAttributes() for the input tag
 *
 * @author  Michael Klier <chi@chimeric.de>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_filefield($attrs) {
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><span>'.$attrs['_text'].'</span> ';
    $s .= '<input type="file" '.buildAttributes($attrs,true);
    if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
    if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
    $s .= ' /></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_checkboxfield
 *
 * Print the HTML for a checkbox input field.
 *   _class : class attribute used on the label tag
 *   _text  : Text to display after the input. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 * If value is an array, a hidden field with the same name and the value
 * $attrs['value'][1] is constructed as well.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_checkboxfield($attrs) {
    // mandatory attributes
    unset($attrs['type']);
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '>';
    if (is_array($attrs['value'])) {
        echo '<input type="hidden" name="' . hsc($attrs['name']) .'"'
                 . ' value="' . hsc($attrs['value'][1]) . '" />';
        $attrs['value'] = $attrs['value'][0];
    }
    $s .= '<input type="checkbox" '.buildAttributes($attrs,true).' />';
    $s .= ' <span>'.$attrs['_text'].'</span></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_radiofield
 *
 * Print the HTML for a radio button input field.
 *   _class : class attribute used on the label tag
 *   _text  : Text to display after the input. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_radiofield($attrs) {
    // mandatory attributes
    unset($attrs['type']);
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><input type="radio" '.buildAttributes($attrs,true).' />';
    $s .= ' <span>'.$attrs['_text'].'</span></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_menufield
 *
 * Print the HTML for a drop-down menu.
 *   _options : Array of (value,text,selected) for the menu.
 *              Text can be omitted. Text and value are passed to formText()
 *              Only one item can be selected.
 *   _class : class attribute used on the label tag
 *   _text  : Text to display before the menu. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_menufield($attrs) {
    $attrs['size'] = '1';
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><span>'.$attrs['_text'].'</span>';
    $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF;
    if (!empty($attrs['_options'])) {
        $selected = false;

        $cnt = count($attrs['_options']);
        for($n=0; $n < $cnt; $n++){
            @list($value,$text,$select) = $attrs['_options'][$n];
            $p = '';
            if (!is_null($text))
                $p .= ' value="'.formText($value).'"';
            else
                $text = $value;
            if (!empty($select) && !$selected) {
                $p .= ' selected="selected"';
                $selected = true;
            }
            $s .= '<option'.$p.'>'.formText($text).'</option>';
        }
    } else {
        $s .= '<option></option>';
    }
    $s .= DOKU_LF.'</select></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}

/**
 * form_listboxfield
 *
 * Print the HTML for a list box.
 *   _options : Array of (value,text,selected) for the list.
 *              Text can be omitted. Text and value are passed to formText()
 *   _class : class attribute used on the label tag
 *   _text  : Text to display before the menu. Not escaped.
 * Other attributes are passed to buildAttributes() for the input tag.
 *
 * @author  Tom N Harris <tnharris@whoopdedo.org>
 *
 * @param array $attrs attributes
 * @return string html
 */
function form_listboxfield($attrs) {
    $s = '<label';
    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
    $s .= '><span>'.$attrs['_text'].'</span> ';
    $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF;
    if (!empty($attrs['_options'])) {
        foreach ($attrs['_options'] as $opt) {
            @list($value,$text,$select,$disabled) = $opt;
            $p = '';
            if(is_null($text)) $text = $value;
            $p .= ' value="'.formText($value).'"';
            if (!empty($select)) $p .= ' selected="selected"';
            if ($disabled) $p .= ' disabled="disabled"';
            $s .= '<option'.$p.'>'.formText($text).'</option>';
        }
    } else {
        $s .= '<option></option>';
    }
    $s .= DOKU_LF.'</select></label>';
    if (preg_match('/(^| )block($| )/', $attrs['_class']))
        $s .= '<br />';
    return $s;
}