PageRenderTime 57ms CodeModel.GetById 25ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/elgg/mod/dokuwiki/lib/dokuwiki/inc/form.php

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