xref: /dokuwiki/inc/form.php (revision 177d6836e2f75d0e404be9c566e61725852a1e07)
1fdb8d77bSTom N Harris<?php
2fdb8d77bSTom N Harris/**
3fdb8d77bSTom N Harris * DokuWiki XHTML Form
4fdb8d77bSTom N Harris *
5fdb8d77bSTom N Harris * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6fdb8d77bSTom N Harris * @author     Tom N Harris <tnharris@whoopdedo.org>
7fdb8d77bSTom N Harris */
8fdb8d77bSTom N Harris
9f600e75aSAndreas Gohr// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
10f600e75aSAndreas Gohr// phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
11f600e75aSAndreas Gohr
12f600e75aSAndreas Gohr
13fdb8d77bSTom N Harris/**
14fdb8d77bSTom N Harris * Class for creating simple HTML forms.
15fdb8d77bSTom N Harris *
16fdb8d77bSTom N Harris * The forms is built from a list of pseudo-tags (arrays with expected keys).
17fdb8d77bSTom N Harris * Every pseudo-tag must have the key '_elem' set to the name of the element.
18fdb8d77bSTom N Harris * When printed, the form class calls functions named 'form_$type' for each
19fdb8d77bSTom N Harris * element it contains.
20fdb8d77bSTom N Harris *
21fdb8d77bSTom N Harris * Standard practice is for non-attribute keys in a pseudo-element to start
22fdb8d77bSTom N Harris * with '_'. Other keys are HTML attributes that will be included in the element
23fdb8d77bSTom N Harris * tag. That way, the element output functions can pass the pseudo-element
24fdb8d77bSTom N Harris * directly to buildAttributes.
25fdb8d77bSTom N Harris *
26fdb8d77bSTom N Harris * See the form_make* functions later in this file.
27fdb8d77bSTom N Harris *
28f600e75aSAndreas Gohr * Please note that even though this class is technically deprecated (use dokuwiki\Form instead),
29f600e75aSAndreas Gohr * it is still widely used in the core and the related form events. Until those have been rewritten,
30f600e75aSAndreas Gohr * this will continue to be used
31f600e75aSAndreas Gohr *
32f600e75aSAndreas Gohr * @deprecated 2019-07-14
33fdb8d77bSTom N Harris * @author Tom N Harris <tnharris@whoopdedo.org>
34fdb8d77bSTom N Harris */
359d01c1d9SSatoshi Saharaclass Doku_Form
369d01c1d9SSatoshi Sahara{
37fdb8d77bSTom N Harris    // Form id attribute
381534dc5bSAndreas Gohr    public $params = array();
39fdb8d77bSTom N Harris
40fdb8d77bSTom N Harris    // Draw a border around form fields.
41fdb8d77bSTom N Harris    // Adds <fieldset></fieldset> around the elements
421534dc5bSAndreas Gohr    public $_infieldset = false;
43fdb8d77bSTom N Harris
44fdb8d77bSTom N Harris    // Hidden form fields.
451534dc5bSAndreas Gohr    public $_hidden = array();
46fdb8d77bSTom N Harris
47fdb8d77bSTom N Harris    // Array of pseudo-tags
481534dc5bSAndreas Gohr    public $_content = array();
49fdb8d77bSTom N Harris
50fdb8d77bSTom N Harris    /**
51fdb8d77bSTom N Harris     * Constructor
52fdb8d77bSTom N Harris     *
53e351c80dSAdrian Lang     * Sets parameters and autoadds a security token. The old calling convention
54e351c80dSAdrian Lang     * with up to four parameters is deprecated, instead the first parameter
55e351c80dSAdrian Lang     * should be an array with parameters.
561b2a85e8SAndreas Gohr     *
575f0071ebSGerrit Uitslag     * @param mixed       $params  Parameters for the HTML form element; Using the deprecated
585f0071ebSGerrit Uitslag     *                             calling convention this is the ID attribute of the form
595f0071ebSGerrit Uitslag     * @param bool|string $action  (optional, deprecated) submit URL, defaults to current page
605f0071ebSGerrit Uitslag     * @param bool|string $method  (optional, deprecated) 'POST' or 'GET', default is POST
615f0071ebSGerrit Uitslag     * @param bool|string $enctype (optional, deprecated) Encoding type of the data
6242ea7f44SGerrit Uitslag     *
63fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
64fdb8d77bSTom N Harris     */
659d01c1d9SSatoshi Sahara    public function __construct($params, $action = false, $method = false, $enctype = false)
669d01c1d9SSatoshi Sahara    {
67e351c80dSAdrian Lang        if (!is_array($params)) {
68e351c80dSAdrian Lang            $this->params = array('id' => $params);
69e351c80dSAdrian Lang            if ($action !== false) $this->params['action'] = $action;
70b4033556SMichael Klier            if ($method !== false) $this->params['method'] = strtolower($method);
71e351c80dSAdrian Lang            if ($enctype !== false) $this->params['enctype'] = $enctype;
72e351c80dSAdrian Lang        } else {
73e351c80dSAdrian Lang            $this->params = $params;
74e351c80dSAdrian Lang        }
75804e2f2fSAdrian Lang
76e351c80dSAdrian Lang        if (!isset($this->params['method'])) {
77b4033556SMichael Klier            $this->params['method'] = 'post';
78b4033556SMichael Klier        } else {
79b4033556SMichael Klier            $this->params['method'] = strtolower($this->params['method']);
80b4033556SMichael Klier        }
81b4033556SMichael Klier
82b4033556SMichael Klier        if (!isset($this->params['action'])) {
83b4033556SMichael Klier            $this->params['action'] = '';
84e351c80dSAdrian Lang        }
851b2a85e8SAndreas Gohr
861b2a85e8SAndreas Gohr        $this->addHidden('sectok', getSecurityToken());
87fdb8d77bSTom N Harris    }
88fdb8d77bSTom N Harris
89fdb8d77bSTom N Harris    /**
90fdb8d77bSTom N Harris     * startFieldset
91fdb8d77bSTom N Harris     *
92fdb8d77bSTom N Harris     * Add <fieldset></fieldset> tags around fields.
93fdb8d77bSTom N Harris     * Usually results in a border drawn around the form.
94fdb8d77bSTom N Harris     *
95fdb8d77bSTom N Harris     * @param   string  $legend Label that will be printed with the border.
9642ea7f44SGerrit Uitslag     *
97fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
98fdb8d77bSTom N Harris     */
999d01c1d9SSatoshi Sahara    public function startFieldset($legend)
1009d01c1d9SSatoshi Sahara    {
101fdb8d77bSTom N Harris        if ($this->_infieldset) {
102fdb8d77bSTom N Harris            $this->addElement(array('_elem'=>'closefieldset'));
103fdb8d77bSTom N Harris        }
104fdb8d77bSTom N Harris        $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend));
105fdb8d77bSTom N Harris        $this->_infieldset = true;
106fdb8d77bSTom N Harris    }
107fdb8d77bSTom N Harris
108fdb8d77bSTom N Harris    /**
109fdb8d77bSTom N Harris     * endFieldset
110fdb8d77bSTom N Harris     *
111fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
112fdb8d77bSTom N Harris     */
1139d01c1d9SSatoshi Sahara    public function endFieldset()
1149d01c1d9SSatoshi Sahara    {
115fdb8d77bSTom N Harris        if ($this->_infieldset) {
116fdb8d77bSTom N Harris            $this->addElement(array('_elem'=>'closefieldset'));
117fdb8d77bSTom N Harris        }
118fdb8d77bSTom N Harris        $this->_infieldset = false;
119fdb8d77bSTom N Harris    }
120fdb8d77bSTom N Harris
121fdb8d77bSTom N Harris    /**
122fdb8d77bSTom N Harris     * addHidden
123fdb8d77bSTom N Harris     *
124fdb8d77bSTom N Harris     * Adds a name/value pair as a hidden field.
125fdb8d77bSTom N Harris     * The value of the field (but not the name) will be passed to
126fdb8d77bSTom N Harris     * formText() before printing.
127fdb8d77bSTom N Harris     *
128fdb8d77bSTom N Harris     * @param   string  $name   Field name.
129fdb8d77bSTom N Harris     * @param   string  $value  Field value. If null, remove a previously added field.
13042ea7f44SGerrit Uitslag     *
131fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
132fdb8d77bSTom N Harris     */
1339d01c1d9SSatoshi Sahara    public function addHidden($name, $value)
1349d01c1d9SSatoshi Sahara    {
135fdb8d77bSTom N Harris        if (is_null($value))
136fdb8d77bSTom N Harris            unset($this->_hidden[$name]);
137*177d6836SAndreas Gohr        else $this->_hidden[$name] = $value;
138fdb8d77bSTom N Harris    }
139fdb8d77bSTom N Harris
140fdb8d77bSTom N Harris    /**
141fdb8d77bSTom N Harris     * addElement
142fdb8d77bSTom N Harris     *
143fdb8d77bSTom N Harris     * Appends a content element to the form.
144fdb8d77bSTom N Harris     * The element can be either a pseudo-tag or string.
145fdb8d77bSTom N Harris     * If string, it is printed without escaping special chars.   *
146fdb8d77bSTom N Harris     *
1474f0bc4b2SAndreas Gohr     * @param   string|array  $elem   Pseudo-tag or string to add to the form.
14842ea7f44SGerrit Uitslag     *
149fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
150fdb8d77bSTom N Harris     */
1519d01c1d9SSatoshi Sahara    public function addElement($elem)
1529d01c1d9SSatoshi Sahara    {
153fdb8d77bSTom N Harris        $this->_content[] = $elem;
154fdb8d77bSTom N Harris    }
155fdb8d77bSTom N Harris
156fdb8d77bSTom N Harris    /**
157fdb8d77bSTom N Harris     * insertElement
158fdb8d77bSTom N Harris     *
159fdb8d77bSTom N Harris     * Inserts a content element at a position.
160fdb8d77bSTom N Harris     *
161fdb8d77bSTom N Harris     * @param   string       $pos  0-based index where the element will be inserted.
1624f0bc4b2SAndreas Gohr     * @param   string|array $elem Pseudo-tag or string to add to the form.
16342ea7f44SGerrit Uitslag     *
164fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
165fdb8d77bSTom N Harris     */
1669d01c1d9SSatoshi Sahara    public function insertElement($pos, $elem)
1679d01c1d9SSatoshi Sahara    {
168fdb8d77bSTom N Harris        array_splice($this->_content, $pos, 0, array($elem));
169fdb8d77bSTom N Harris    }
170fdb8d77bSTom N Harris
171fdb8d77bSTom N Harris    /**
172fdb8d77bSTom N Harris     * replaceElement
173fdb8d77bSTom N Harris     *
174fdb8d77bSTom N Harris     * Replace with NULL to remove an element.
175fdb8d77bSTom N Harris     *
176fdb8d77bSTom N Harris     * @param   int          $pos  0-based index the element will be placed at.
1774f0bc4b2SAndreas Gohr     * @param   string|array $elem Pseudo-tag or string to add to the form.
17842ea7f44SGerrit Uitslag     *
179fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
180fdb8d77bSTom N Harris     */
1819d01c1d9SSatoshi Sahara    public function replaceElement($pos, $elem)
1829d01c1d9SSatoshi Sahara    {
183fdb8d77bSTom N Harris        $rep = array();
184fdb8d77bSTom N Harris        if (!is_null($elem)) $rep[] = $elem;
185fdb8d77bSTom N Harris        array_splice($this->_content, $pos, 1, $rep);
186fdb8d77bSTom N Harris    }
187fdb8d77bSTom N Harris
188fdb8d77bSTom N Harris    /**
189fdb8d77bSTom N Harris     * findElementByType
190fdb8d77bSTom N Harris     *
191fdb8d77bSTom N Harris     * Gets the position of the first of a type of element.
192fdb8d77bSTom N Harris     *
193fdb8d77bSTom N Harris     * @param   string  $type   Element type to look for.
19442ea7f44SGerrit Uitslag     * @return  int|false     position of element if found, otherwise false
19542ea7f44SGerrit Uitslag     *
196fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
197fdb8d77bSTom N Harris     */
1989d01c1d9SSatoshi Sahara    public function findElementByType($type)
1999d01c1d9SSatoshi Sahara    {
200fdb8d77bSTom N Harris        foreach ($this->_content as $pos => $elem) {
201fdb8d77bSTom N Harris            if (is_array($elem) && $elem['_elem'] == $type)
202fdb8d77bSTom N Harris                return $pos;
203fdb8d77bSTom N Harris        }
204fdb8d77bSTom N Harris        return false;
205fdb8d77bSTom N Harris    }
206fdb8d77bSTom N Harris
207fdb8d77bSTom N Harris    /**
208fdb8d77bSTom N Harris     * findElementById
209fdb8d77bSTom N Harris     *
210fdb8d77bSTom N Harris     * Gets the position of the element with an ID attribute.
211fdb8d77bSTom N Harris     *
212fdb8d77bSTom N Harris     * @param   string  $id     ID of the element to find.
21342ea7f44SGerrit Uitslag     * @return  int|false     position of element if found, otherwise false
21442ea7f44SGerrit Uitslag     *
215fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
216fdb8d77bSTom N Harris     */
2179d01c1d9SSatoshi Sahara    public function findElementById($id)
2189d01c1d9SSatoshi Sahara    {
219fdb8d77bSTom N Harris        foreach ($this->_content as $pos => $elem) {
220fdb8d77bSTom N Harris            if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
221fdb8d77bSTom N Harris                return $pos;
222fdb8d77bSTom N Harris        }
223fdb8d77bSTom N Harris        return false;
224fdb8d77bSTom N Harris    }
225fdb8d77bSTom N Harris
226fdb8d77bSTom N Harris    /**
227fdb8d77bSTom N Harris     * findElementByAttribute
228fdb8d77bSTom N Harris     *
229fdb8d77bSTom N Harris     * Gets the position of the first element with a matching attribute value.
230fdb8d77bSTom N Harris     *
231fdb8d77bSTom N Harris     * @param   string  $name   Attribute name.
232fdb8d77bSTom N Harris     * @param   string  $value  Attribute value.
23342ea7f44SGerrit Uitslag     * @return  int|false     position of element if found, otherwise false
23442ea7f44SGerrit Uitslag     *
235fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
236fdb8d77bSTom N Harris     */
2379d01c1d9SSatoshi Sahara    public function findElementByAttribute($name, $value)
2389d01c1d9SSatoshi Sahara    {
239fdb8d77bSTom N Harris        foreach ($this->_content as $pos => $elem) {
240fdb8d77bSTom N Harris            if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
241fdb8d77bSTom N Harris                return $pos;
242fdb8d77bSTom N Harris        }
243fdb8d77bSTom N Harris        return false;
244fdb8d77bSTom N Harris    }
245fdb8d77bSTom N Harris
246fdb8d77bSTom N Harris    /**
247fdb8d77bSTom N Harris     * getElementAt
248fdb8d77bSTom N Harris     *
249fdb8d77bSTom N Harris     * Returns a reference to the element at a position.
250fdb8d77bSTom N Harris     * A position out-of-bounds will return either the
251fdb8d77bSTom N Harris     * first (underflow) or last (overflow) element.
252fdb8d77bSTom N Harris     *
253fdb8d77bSTom N Harris     * @param   int     $pos    0-based index
254fdb8d77bSTom N Harris     * @return  array reference  pseudo-element
25542ea7f44SGerrit Uitslag     *
256fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
257fdb8d77bSTom N Harris     */
2589d01c1d9SSatoshi Sahara    public function &getElementAt($pos)
2599d01c1d9SSatoshi Sahara    {
260fdb8d77bSTom N Harris        if ($pos < 0) $pos = count($this->_content) + $pos;
261fdb8d77bSTom N Harris        if ($pos < 0) $pos = 0;
262fdb8d77bSTom N Harris        if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
263fdb8d77bSTom N Harris        return $this->_content[$pos];
264fdb8d77bSTom N Harris    }
265fdb8d77bSTom N Harris
266fdb8d77bSTom N Harris    /**
267b81b193eSAndreas Gohr     * Return the assembled HTML for the form.
268fdb8d77bSTom N Harris     *
269fdb8d77bSTom N Harris     * Each element in the form will be passed to a function named
270fdb8d77bSTom N Harris     * 'form_$type'. The function should return the HTML to be printed.
271fdb8d77bSTom N Harris     *
272fdb8d77bSTom N Harris     * @author  Tom N Harris <tnharris@whoopdedo.org>
27342ea7f44SGerrit Uitslag     *
27442ea7f44SGerrit Uitslag     * @return string html of the form
275fdb8d77bSTom N Harris     */
2769d01c1d9SSatoshi Sahara    public function getForm()
2779d01c1d9SSatoshi Sahara    {
278fdb8d77bSTom N Harris        global $lang;
279b81b193eSAndreas Gohr        $form = '';
280e351c80dSAdrian Lang        $this->params['accept-charset'] = $lang['encoding'];
2819f09385fSAndreas Gohr        $form .= '<form '. buildAttributes($this->params, false) .'><div class="no">'. DOKU_LF;
282fdb8d77bSTom N Harris        if (!empty($this->_hidden)) {
283fdb8d77bSTom N Harris            foreach ($this->_hidden as $name => $value)
284b81b193eSAndreas Gohr                $form .= form_hidden(array('name'=>$name, 'value'=>$value));
285fdb8d77bSTom N Harris        }
286fdb8d77bSTom N Harris        foreach ($this->_content as $element) {
287fdb8d77bSTom N Harris            if (is_array($element)) {
288fdb8d77bSTom N Harris                $elem_type = $element['_elem'];
289fdb8d77bSTom N Harris                if (function_exists('form_'.$elem_type)) {
290b81b193eSAndreas Gohr                    $form .= call_user_func('form_'.$elem_type, $element).DOKU_LF;
291fdb8d77bSTom N Harris                }
292fdb8d77bSTom N Harris            } else {
293b81b193eSAndreas Gohr                $form .= $element;
294fdb8d77bSTom N Harris            }
295fdb8d77bSTom N Harris        }
296b81b193eSAndreas Gohr        if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF;
297b81b193eSAndreas Gohr        $form .= '</div></form>'.DOKU_LF;
298b81b193eSAndreas Gohr
299b81b193eSAndreas Gohr        return $form;
300b81b193eSAndreas Gohr    }
301b81b193eSAndreas Gohr
302b81b193eSAndreas Gohr    /**
303b81b193eSAndreas Gohr     * Print the assembled form
304b81b193eSAndreas Gohr     *
305b81b193eSAndreas Gohr     * wraps around getForm()
306b81b193eSAndreas Gohr     */
3079d01c1d9SSatoshi Sahara    public function printForm()
3089d01c1d9SSatoshi Sahara    {
309b81b193eSAndreas Gohr        echo $this->getForm();
310fdb8d77bSTom N Harris    }
311fdb8d77bSTom N Harris
3125b75cd1fSAdrian Lang    /**
3135b75cd1fSAdrian Lang     * Add a radio set
3145b75cd1fSAdrian Lang     *
3155b75cd1fSAdrian Lang     * This function adds a set of radio buttons to the form. If $_POST[$name]
3165b75cd1fSAdrian Lang     * is set, this radio is preselected, else the first radio button.
3175b75cd1fSAdrian Lang     *
3185b75cd1fSAdrian Lang     * @param string    $name    The HTML field name
3195b75cd1fSAdrian Lang     * @param array     $entries An array of entries $value => $caption
3205b75cd1fSAdrian Lang     *
3215b75cd1fSAdrian Lang     * @author Adrian Lang <lang@cosmocode.de>
3225b75cd1fSAdrian Lang     */
3235b75cd1fSAdrian Lang
3249d01c1d9SSatoshi Sahara    public function addRadioSet($name, $entries)
3259d01c1d9SSatoshi Sahara    {
326f0859d4bSTom N Harris        global $INPUT;
327f0859d4bSTom N Harris        $value = (array_key_exists($INPUT->post->str($name), $entries)) ?
328f0859d4bSTom N Harris                 $INPUT->str($name) : key($entries);
3295b75cd1fSAdrian Lang        foreach ($entries as $val => $cap) {
3305b75cd1fSAdrian Lang            $data = ($value === $val) ? array('checked' => 'checked') : array();
3315b75cd1fSAdrian Lang            $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data));
3325b75cd1fSAdrian Lang        }
3335b75cd1fSAdrian Lang    }
334fdb8d77bSTom N Harris}
335fdb8d77bSTom N Harris
336fdb8d77bSTom N Harris/**
337fdb8d77bSTom N Harris * form_makeTag
338fdb8d77bSTom N Harris *
339fdb8d77bSTom N Harris * Create a form element for a non-specific empty tag.
340fdb8d77bSTom N Harris *
341fdb8d77bSTom N Harris * @param   string  $tag    Tag name.
342fdb8d77bSTom N Harris * @param   array   $attrs  Optional attributes.
343fdb8d77bSTom N Harris * @return  array   pseudo-tag
34442ea7f44SGerrit Uitslag *
345fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
346fdb8d77bSTom N Harris */
347d868eb89SAndreas Gohrfunction form_makeTag($tag, $attrs = array())
348d868eb89SAndreas Gohr{
349fdb8d77bSTom N Harris    $elem = array('_elem'=>'tag', '_tag'=>$tag);
350fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
351fdb8d77bSTom N Harris}
352fdb8d77bSTom N Harris
353fdb8d77bSTom N Harris/**
354fdb8d77bSTom N Harris * form_makeOpenTag
355fdb8d77bSTom N Harris *
356fdb8d77bSTom N Harris * Create a form element for a non-specific opening tag.
357fdb8d77bSTom N Harris * Remember to put a matching close tag after this as well.
358fdb8d77bSTom N Harris *
359fdb8d77bSTom N Harris * @param   string  $tag    Tag name.
360fdb8d77bSTom N Harris * @param   array   $attrs  Optional attributes.
361fdb8d77bSTom N Harris * @return  array   pseudo-tag
36242ea7f44SGerrit Uitslag *
363fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
364fdb8d77bSTom N Harris */
365d868eb89SAndreas Gohrfunction form_makeOpenTag($tag, $attrs = array())
366d868eb89SAndreas Gohr{
367fdb8d77bSTom N Harris    $elem = array('_elem'=>'opentag', '_tag'=>$tag);
368fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
369fdb8d77bSTom N Harris}
370fdb8d77bSTom N Harris
371fdb8d77bSTom N Harris/**
372fdb8d77bSTom N Harris * form_makeCloseTag
373fdb8d77bSTom N Harris *
374fdb8d77bSTom N Harris * Create a form element for a non-specific closing tag.
375fdb8d77bSTom N Harris * Careless use of this will result in invalid XHTML.
376fdb8d77bSTom N Harris *
377fdb8d77bSTom N Harris * @param   string  $tag    Tag name.
378fdb8d77bSTom N Harris * @return  array   pseudo-tag
37942ea7f44SGerrit Uitslag *
380fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
381fdb8d77bSTom N Harris */
382d868eb89SAndreas Gohrfunction form_makeCloseTag($tag)
383d868eb89SAndreas Gohr{
384fdb8d77bSTom N Harris    return array('_elem'=>'closetag', '_tag'=>$tag);
385fdb8d77bSTom N Harris}
386fdb8d77bSTom N Harris
387fdb8d77bSTom N Harris/**
388fdb8d77bSTom N Harris * form_makeWikiText
389fdb8d77bSTom N Harris *
390fdb8d77bSTom N Harris * Create a form element for a textarea containing wiki text.
391fdb8d77bSTom N Harris * Only one wikitext element is allowed on a page. It will have
392fdb8d77bSTom N Harris * a name of 'wikitext' and id 'wiki__text'. The text will
393fdb8d77bSTom N Harris * be passed to formText() before printing.
394fdb8d77bSTom N Harris *
395fdb8d77bSTom N Harris * @param   string  $text   Text to fill the field with.
396fdb8d77bSTom N Harris * @param   array   $attrs  Optional attributes.
397fdb8d77bSTom N Harris * @return  array   pseudo-tag
39842ea7f44SGerrit Uitslag *
399fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
400fdb8d77bSTom N Harris */
401d868eb89SAndreas Gohrfunction form_makeWikiText($text, $attrs = array())
402d868eb89SAndreas Gohr{
403b2bc77d5STom N Harris    $elem = array('_elem'=>'wikitext', '_text'=>$text,
404b2bc77d5STom N Harris                        'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
405fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
406fdb8d77bSTom N Harris}
407fdb8d77bSTom N Harris
408fdb8d77bSTom N Harris/**
409fdb8d77bSTom N Harris * form_makeButton
410fdb8d77bSTom N Harris *
411fdb8d77bSTom N Harris * Create a form element for an action button.
412fdb8d77bSTom N Harris * A title will automatically be generated using the value and
413fdb8d77bSTom N Harris * accesskey attributes, unless you provide one.
414fdb8d77bSTom N Harris *
415fdb8d77bSTom N Harris * @param   string  $type   Type attribute. 'submit' or 'cancel'
416fdb8d77bSTom N Harris * @param   string  $act    Wiki action of the button, will be used as the do= parameter
417fdb8d77bSTom N Harris * @param   string  $value  (optional) Displayed label. Uses $act if not provided.
418fdb8d77bSTom N Harris * @param   array   $attrs  Optional attributes.
419fdb8d77bSTom N Harris * @return  array   pseudo-tag
42042ea7f44SGerrit Uitslag *
421fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
422fdb8d77bSTom N Harris */
423d868eb89SAndreas Gohrfunction form_makeButton($type, $act, $value = '', $attrs = array())
424d868eb89SAndreas Gohr{
425fdb8d77bSTom N Harris    if ($value == '') $value = $act;
426b2bc77d5STom N Harris    $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
427ae614416SAnika Henke                        'value'=>$value);
428fdb8d77bSTom N Harris    if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
42907493d05SAnika Henke        $attrs['title'] = $value .' ['. strtoupper($attrs['accesskey']) .']';
430fdb8d77bSTom N Harris    }
431fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
432fdb8d77bSTom N Harris}
433fdb8d77bSTom N Harris
434fdb8d77bSTom N Harris/**
435fdb8d77bSTom N Harris * form_makeField
436fdb8d77bSTom N Harris *
437fdb8d77bSTom N Harris * Create a form element for a labelled input element.
438fdb8d77bSTom N Harris * The label text will be printed before the input.
439fdb8d77bSTom N Harris *
440fdb8d77bSTom N Harris * @param   string  $type   Type attribute of input.
441fdb8d77bSTom N Harris * @param   string  $name   Name attribute of the input.
442fdb8d77bSTom N Harris * @param   string  $value  (optional) Default value.
443fdb8d77bSTom N Harris * @param   string  $class  Class attribute of the label. If this is 'block',
444fdb8d77bSTom N Harris *                          then a line break will be added after the field.
445fdb8d77bSTom N Harris * @param   string  $label  Label that will be printed before the input.
446fdb8d77bSTom N Harris * @param   string  $id     ID attribute of the input. If set, the label will
447fdb8d77bSTom N Harris *                          reference it with a 'for' attribute.
448fdb8d77bSTom N Harris * @param   array   $attrs  Optional attributes.
449fdb8d77bSTom N Harris * @return  array   pseudo-tag
45042ea7f44SGerrit Uitslag *
451fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
452fdb8d77bSTom N Harris */
453d868eb89SAndreas Gohrfunction form_makeField($type, $name, $value = '', $label = null, $id = '', $class = '', $attrs = array())
454d868eb89SAndreas Gohr{
455fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
456fdb8d77bSTom N Harris    $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
457fdb8d77bSTom N Harris                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
458fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
459fdb8d77bSTom N Harris}
460fdb8d77bSTom N Harris
461fdb8d77bSTom N Harris/**
462fdb8d77bSTom N Harris * form_makeFieldRight
463fdb8d77bSTom N Harris *
464fdb8d77bSTom N Harris * Create a form element for a labelled input element.
465fdb8d77bSTom N Harris * The label text will be printed after the input.
466fdb8d77bSTom N Harris *
467fdb8d77bSTom N Harris * @see     form_makeField
468fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
469f50a239bSTakamura *
470f50a239bSTakamura * @param string $type
471f50a239bSTakamura * @param string $name
472f50a239bSTakamura * @param string $value
473f50a239bSTakamura * @param null|string $label
474f50a239bSTakamura * @param string $id
475f50a239bSTakamura * @param string $class
476f50a239bSTakamura * @param array $attrs
477f50a239bSTakamura *
478f50a239bSTakamura * @return array
479fdb8d77bSTom N Harris */
480d868eb89SAndreas Gohrfunction form_makeFieldRight($type, $name, $value = '', $label = null, $id = '', $class = '', $attrs = array())
481d868eb89SAndreas Gohr{
482fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
483fdb8d77bSTom N Harris    $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
484fdb8d77bSTom N Harris                        'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
485fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
486fdb8d77bSTom N Harris}
487fdb8d77bSTom N Harris
488fdb8d77bSTom N Harris/**
489fdb8d77bSTom N Harris * form_makeTextField
490fdb8d77bSTom N Harris *
491fdb8d77bSTom N Harris * Create a form element for a text input element with label.
492fdb8d77bSTom N Harris *
493fdb8d77bSTom N Harris * @see     form_makeField
494fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
495f50a239bSTakamura *
496f50a239bSTakamura * @param string $name
497f50a239bSTakamura * @param string $value
498f50a239bSTakamura * @param null|string $label
499f50a239bSTakamura * @param string $id
500f50a239bSTakamura * @param string $class
501f50a239bSTakamura * @param array $attrs
502f50a239bSTakamura *
503f50a239bSTakamura * @return array
504fdb8d77bSTom N Harris */
505d868eb89SAndreas Gohrfunction form_makeTextField($name, $value = '', $label = null, $id = '', $class = '', $attrs = array())
506d868eb89SAndreas Gohr{
507fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
508fdb8d77bSTom N Harris    $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
509b2bc77d5STom N Harris                        'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
510fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
511fdb8d77bSTom N Harris}
512fdb8d77bSTom N Harris
513fdb8d77bSTom N Harris/**
514fdb8d77bSTom N Harris * form_makePasswordField
515fdb8d77bSTom N Harris *
516fdb8d77bSTom N Harris * Create a form element for a password input element with label.
517fdb8d77bSTom N Harris * Password elements have no default value, for obvious reasons.
518fdb8d77bSTom N Harris *
519fdb8d77bSTom N Harris * @see     form_makeField
520fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
521f50a239bSTakamura *
522f50a239bSTakamura * @param string $name
523f50a239bSTakamura * @param null|string $label
524f50a239bSTakamura * @param string $id
525f50a239bSTakamura * @param string $class
526f50a239bSTakamura * @param array $attrs
527f50a239bSTakamura *
528f50a239bSTakamura * @return array
529fdb8d77bSTom N Harris */
530d868eb89SAndreas Gohrfunction form_makePasswordField($name, $label = null, $id = '', $class = '', $attrs = array())
531d868eb89SAndreas Gohr{
532fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
533fdb8d77bSTom N Harris    $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
534b2bc77d5STom N Harris                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
535fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
536fdb8d77bSTom N Harris}
537fdb8d77bSTom N Harris
538fdb8d77bSTom N Harris/**
53912bbca2eSMichael Klier * form_makeFileField
54012bbca2eSMichael Klier *
54112bbca2eSMichael Klier * Create a form element for a file input element with label
54212bbca2eSMichael Klier *
54312bbca2eSMichael Klier * @see     form_makeField
54412bbca2eSMichael Klier * @author  Michael Klier <chi@chimeric.de>
545f50a239bSTakamura *
546f50a239bSTakamura * @param string $name
547f50a239bSTakamura * @param null|string $label
548f50a239bSTakamura * @param string $id
549f50a239bSTakamura * @param string $class
550f50a239bSTakamura * @param array $attrs
551f50a239bSTakamura *
552f50a239bSTakamura * @return array
55312bbca2eSMichael Klier */
554d868eb89SAndreas Gohrfunction form_makeFileField($name, $label = null, $id = '', $class = '', $attrs = array())
555d868eb89SAndreas Gohr{
55612bbca2eSMichael Klier    if (is_null($label)) $label = $name;
55712bbca2eSMichael Klier    $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
55812bbca2eSMichael Klier                        'id'=>$id, 'name'=>$name, 'class'=>'edit');
55912bbca2eSMichael Klier    return array_merge($elem, $attrs);
56012bbca2eSMichael Klier}
56112bbca2eSMichael Klier
56212bbca2eSMichael Klier/**
563fdb8d77bSTom N Harris * form_makeCheckboxField
564fdb8d77bSTom N Harris *
565fdb8d77bSTom N Harris * Create a form element for a checkbox input element with label.
5662f10258cSAdrian Lang * If $value is an array, a hidden field with the same name and the value
5672f10258cSAdrian Lang * $value[1] is constructed as well.
568fdb8d77bSTom N Harris *
569fdb8d77bSTom N Harris * @see     form_makeFieldRight
570fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
571f50a239bSTakamura *
572f50a239bSTakamura * @param string $name
573f50a239bSTakamura * @param string $value
574f50a239bSTakamura * @param null|string $label
575f50a239bSTakamura * @param string $id
576f50a239bSTakamura * @param string $class
577f50a239bSTakamura * @param array $attrs
578f50a239bSTakamura *
579f50a239bSTakamura * @return array
580fdb8d77bSTom N Harris */
581d868eb89SAndreas Gohrfunction form_makeCheckboxField($name, $value = '1', $label = null, $id = '', $class = '', $attrs = array())
582d868eb89SAndreas Gohr{
583fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
584fdb8d77bSTom N Harris    if (is_null($value) || $value=='') $value='0';
585fdb8d77bSTom N Harris    $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
586fdb8d77bSTom N Harris                        'id'=>$id, 'name'=>$name, 'value'=>$value);
587fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
588fdb8d77bSTom N Harris}
589fdb8d77bSTom N Harris
590fdb8d77bSTom N Harris/**
591fdb8d77bSTom N Harris * form_makeRadioField
592fdb8d77bSTom N Harris *
593fdb8d77bSTom N Harris * Create a form element for a radio button input element with label.
594fdb8d77bSTom N Harris *
595fdb8d77bSTom N Harris * @see     form_makeFieldRight
596fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
597f50a239bSTakamura *
598f50a239bSTakamura * @param string $name
599f50a239bSTakamura * @param string $value
600f50a239bSTakamura * @param null|string $label
601f50a239bSTakamura * @param string $id
602f50a239bSTakamura * @param string $class
603f50a239bSTakamura * @param array $attrs
604f50a239bSTakamura *
605f50a239bSTakamura * @return array
606fdb8d77bSTom N Harris */
607d868eb89SAndreas Gohrfunction form_makeRadioField($name, $value = '1', $label = null, $id = '', $class = '', $attrs = array())
608d868eb89SAndreas Gohr{
609fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
610fdb8d77bSTom N Harris    if (is_null($value) || $value=='') $value='0';
611fdb8d77bSTom N Harris    $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
612fdb8d77bSTom N Harris                        'id'=>$id, 'name'=>$name, 'value'=>$value);
613fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
614fdb8d77bSTom N Harris}
615fdb8d77bSTom N Harris
616fdb8d77bSTom N Harris/**
617fdb8d77bSTom N Harris * form_makeMenuField
618fdb8d77bSTom N Harris *
619fdb8d77bSTom N Harris * Create a form element for a drop-down menu with label.
620fdb8d77bSTom N Harris * The list of values can be strings, arrays of (value,text),
621fdb8d77bSTom N Harris * or an associative array with the values as keys and labels as values.
622fdb8d77bSTom N Harris * An item is selected by supplying its value or integer index.
623fdb8d77bSTom N Harris * If the list of values is an associative array, the selected item must be
624fdb8d77bSTom N Harris * a string.
625fdb8d77bSTom N Harris *
626fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
62742ea7f44SGerrit Uitslag *
62842ea7f44SGerrit Uitslag * @param string           $name     Name attribute of the input.
62942ea7f44SGerrit Uitslag * @param string[]|array[] $values   The list of values can be strings, arrays of (value,text),
63042ea7f44SGerrit Uitslag *                                   or an associative array with the values as keys and labels as values.
63142ea7f44SGerrit Uitslag * @param string|int       $selected default selected value, string or index number
63242ea7f44SGerrit Uitslag * @param string           $class    Class attribute of the label. If this is 'block',
63342ea7f44SGerrit Uitslag *                                   then a line break will be added after the field.
63442ea7f44SGerrit Uitslag * @param string           $label    Label that will be printed before the input.
63542ea7f44SGerrit Uitslag * @param string           $id       ID attribute of the input. If set, the label will
63642ea7f44SGerrit Uitslag *                                   reference it with a 'for' attribute.
63742ea7f44SGerrit Uitslag * @param array            $attrs    Optional attributes.
63842ea7f44SGerrit Uitslag * @return array   pseudo-tag
639fdb8d77bSTom N Harris */
640d868eb89SAndreas Gohrfunction form_makeMenuField($name, $values, $selected = '', $label = null, $id = '', $class = '', $attrs = array())
641d868eb89SAndreas Gohr{
642fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
643fdb8d77bSTom N Harris    $options = array();
644fdb8d77bSTom N Harris    reset($values);
645fdb8d77bSTom N Harris    // FIXME: php doesn't know the difference between a string and an integer
646fdb8d77bSTom N Harris    if (is_string(key($values))) {
647fdb8d77bSTom N Harris        foreach ($values as $val => $text) {
648fdb8d77bSTom N Harris            $options[] = array($val, $text, (!is_null($selected) && $val==$selected));
649fdb8d77bSTom N Harris        }
650fdb8d77bSTom N Harris    } else {
651fdb8d77bSTom N Harris        if (is_integer($selected)) $selected = $values[$selected];
652fdb8d77bSTom N Harris        foreach ($values as $val) {
653fdb8d77bSTom N Harris            if (is_array($val))
654fdb8d77bSTom N Harris                @list($val, $text) = $val;
655*177d6836SAndreas Gohr            else $text = null;
656fdb8d77bSTom N Harris            $options[] = array($val, $text, $val===$selected);
657fdb8d77bSTom N Harris        }
658fdb8d77bSTom N Harris    }
659fdb8d77bSTom N Harris    $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
660fdb8d77bSTom N Harris                        'id'=>$id, 'name'=>$name);
661fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
662fdb8d77bSTom N Harris}
663fdb8d77bSTom N Harris
664fdb8d77bSTom N Harris/**
665fdb8d77bSTom N Harris * form_makeListboxField
666fdb8d77bSTom N Harris *
667fdb8d77bSTom N Harris * Create a form element for a list box with label.
668fdb8d77bSTom N Harris * The list of values can be strings, arrays of (value,text),
669fdb8d77bSTom N Harris * or an associative array with the values as keys and labels as values.
670fdb8d77bSTom N Harris * Items are selected by supplying its value or an array of values.
671fdb8d77bSTom N Harris *
672fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
67342ea7f44SGerrit Uitslag *
67442ea7f44SGerrit Uitslag * @param string           $name     Name attribute of the input.
67542ea7f44SGerrit Uitslag * @param string[]|array[] $values   The list of values can be strings, arrays of (value,text),
67642ea7f44SGerrit Uitslag *                                   or an associative array with the values as keys and labels as values.
67742ea7f44SGerrit Uitslag * @param array|string     $selected value or array of values of the items that need to be selected
67842ea7f44SGerrit Uitslag * @param string           $class    Class attribute of the label. If this is 'block',
67942ea7f44SGerrit Uitslag *                                   then a line break will be added after the field.
68042ea7f44SGerrit Uitslag * @param string           $label    Label that will be printed before the input.
68142ea7f44SGerrit Uitslag * @param string           $id       ID attribute of the input. If set, the label will
68242ea7f44SGerrit Uitslag *                                   reference it with a 'for' attribute.
68342ea7f44SGerrit Uitslag * @param array            $attrs    Optional attributes.
68442ea7f44SGerrit Uitslag * @return array   pseudo-tag
685fdb8d77bSTom N Harris */
686d868eb89SAndreas Gohrfunction form_makeListboxField($name, $values, $selected = '', $label = null, $id = '', $class = '', $attrs = array())
687d868eb89SAndreas Gohr{
688fdb8d77bSTom N Harris    if (is_null($label)) $label = $name;
689fdb8d77bSTom N Harris    $options = array();
690fdb8d77bSTom N Harris    reset($values);
691eeb8f429SGerrit Uitslag    if (is_null($selected) || $selected == '') {
692fdb8d77bSTom N Harris        $selected = array();
693eeb8f429SGerrit Uitslag    } elseif (!is_array($selected)) {
694fdb8d77bSTom N Harris        $selected = array($selected);
695eeb8f429SGerrit Uitslag    }
696fdb8d77bSTom N Harris    // FIXME: php doesn't know the difference between a string and an integer
697fdb8d77bSTom N Harris    if (is_string(key($values))) {
698fdb8d77bSTom N Harris        foreach ($values as $val => $text) {
699fdb8d77bSTom N Harris            $options[] = array($val, $text, in_array($val, $selected));
700fdb8d77bSTom N Harris        }
701fdb8d77bSTom N Harris    } else {
702fdb8d77bSTom N Harris        foreach ($values as $val) {
703eeb8f429SGerrit Uitslag            $disabled = false;
704eeb8f429SGerrit Uitslag            if (is_array($val)) {
705eeb8f429SGerrit Uitslag                @list($val, $text, $disabled) = $val;
706eeb8f429SGerrit Uitslag            } else {
707fdb8d77bSTom N Harris                $text = null;
708eeb8f429SGerrit Uitslag            }
709eeb8f429SGerrit Uitslag            $options[] = array($val, $text, in_array($val, $selected), $disabled);
710fdb8d77bSTom N Harris        }
711fdb8d77bSTom N Harris    }
712fdb8d77bSTom N Harris    $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
713fdb8d77bSTom N Harris                        'id'=>$id, 'name'=>$name);
714fdb8d77bSTom N Harris    return array_merge($elem, $attrs);
715fdb8d77bSTom N Harris}
716fdb8d77bSTom N Harris
717fdb8d77bSTom N Harris/**
718fdb8d77bSTom N Harris * form_tag
719fdb8d77bSTom N Harris *
720fdb8d77bSTom N Harris * Print the HTML for a generic empty tag.
721fdb8d77bSTom N Harris * Requires '_tag' key with name of the tag.
722fdb8d77bSTom N Harris * Attributes are passed to buildAttributes()
723fdb8d77bSTom N Harris *
724fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
72542ea7f44SGerrit Uitslag *
72642ea7f44SGerrit Uitslag * @param array $attrs attributes
72742ea7f44SGerrit Uitslag * @return string html of tag
728fdb8d77bSTom N Harris */
729d868eb89SAndreas Gohrfunction form_tag($attrs)
730d868eb89SAndreas Gohr{
731c277a6bdSTom N Harris    return '<'.$attrs['_tag'].' '. buildAttributes($attrs, true) .'/>';
732fdb8d77bSTom N Harris}
733fdb8d77bSTom N Harris
734fdb8d77bSTom N Harris/**
735fdb8d77bSTom N Harris * form_opentag
736fdb8d77bSTom N Harris *
737fdb8d77bSTom N Harris * Print the HTML for a generic opening tag.
738fdb8d77bSTom N Harris * Requires '_tag' key with name of the tag.
739fdb8d77bSTom N Harris * Attributes are passed to buildAttributes()
740fdb8d77bSTom N Harris *
741fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
74242ea7f44SGerrit Uitslag *
74342ea7f44SGerrit Uitslag * @param array $attrs attributes
74442ea7f44SGerrit Uitslag * @return string html of tag
745fdb8d77bSTom N Harris */
746d868eb89SAndreas Gohrfunction form_opentag($attrs)
747d868eb89SAndreas Gohr{
748fdb8d77bSTom N Harris    return '<'.$attrs['_tag'].' '. buildAttributes($attrs, true) .'>';
749fdb8d77bSTom N Harris}
750fdb8d77bSTom N Harris
751fdb8d77bSTom N Harris/**
752fdb8d77bSTom N Harris * form_closetag
753fdb8d77bSTom N Harris *
754fdb8d77bSTom N Harris * Print the HTML for a generic closing tag.
755fdb8d77bSTom N Harris * Requires '_tag' key with name of the tag.
756fdb8d77bSTom N Harris * There are no attributes.
757fdb8d77bSTom N Harris *
758fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
75942ea7f44SGerrit Uitslag *
76042ea7f44SGerrit Uitslag * @param array $attrs attributes
76142ea7f44SGerrit Uitslag * @return string html of tag
762fdb8d77bSTom N Harris */
763d868eb89SAndreas Gohrfunction form_closetag($attrs)
764d868eb89SAndreas Gohr{
765fdb8d77bSTom N Harris    return '</'.$attrs['_tag'].'>';
766fdb8d77bSTom N Harris}
767fdb8d77bSTom N Harris
768fdb8d77bSTom N Harris/**
769fdb8d77bSTom N Harris * form_openfieldset
770fdb8d77bSTom N Harris *
771fdb8d77bSTom N Harris * Print the HTML for an opening fieldset tag.
772fdb8d77bSTom N Harris * Uses the '_legend' key.
773fdb8d77bSTom N Harris * Attributes are passed to buildAttributes()
774fdb8d77bSTom N Harris *
775fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
77642ea7f44SGerrit Uitslag *
77742ea7f44SGerrit Uitslag * @param array $attrs attributes
77842ea7f44SGerrit Uitslag * @return string html
779fdb8d77bSTom N Harris */
780d868eb89SAndreas Gohrfunction form_openfieldset($attrs)
781d868eb89SAndreas Gohr{
782fdb8d77bSTom N Harris    $s = '<fieldset '. buildAttributes($attrs, true) .'>';
783fdb8d77bSTom N Harris    if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
784fdb8d77bSTom N Harris    return $s;
785fdb8d77bSTom N Harris}
786fdb8d77bSTom N Harris
787fdb8d77bSTom N Harris/**
788fdb8d77bSTom N Harris * form_closefieldset
789fdb8d77bSTom N Harris *
790fdb8d77bSTom N Harris * Print the HTML for a closing fieldset tag.
791fdb8d77bSTom N Harris * There are no attributes.
792fdb8d77bSTom N Harris *
793fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
79442ea7f44SGerrit Uitslag *
79542ea7f44SGerrit Uitslag * @return string html
796fdb8d77bSTom N Harris */
797d868eb89SAndreas Gohrfunction form_closefieldset()
798d868eb89SAndreas Gohr{
799fdb8d77bSTom N Harris    return '</fieldset>';
800fdb8d77bSTom N Harris}
801fdb8d77bSTom N Harris
802fdb8d77bSTom N Harris/**
803fdb8d77bSTom N Harris * form_hidden
804fdb8d77bSTom N Harris *
805fdb8d77bSTom N Harris * Print the HTML for a hidden input element.
806fdb8d77bSTom N Harris * Uses only 'name' and 'value' attributes.
807fdb8d77bSTom N Harris * Value is passed to formText()
808fdb8d77bSTom N Harris *
809fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
81042ea7f44SGerrit Uitslag *
81142ea7f44SGerrit Uitslag * @param array $attrs attributes
81242ea7f44SGerrit Uitslag * @return string html
813fdb8d77bSTom N Harris */
814d868eb89SAndreas Gohrfunction form_hidden($attrs)
815d868eb89SAndreas Gohr{
816fdb8d77bSTom N Harris    return '<input type="hidden" name="'.$attrs['name'].'" value="'. formText($attrs['value']) .'" />';
817fdb8d77bSTom N Harris}
818fdb8d77bSTom N Harris
819fdb8d77bSTom N Harris/**
820fdb8d77bSTom N Harris * form_wikitext
821fdb8d77bSTom N Harris *
822fdb8d77bSTom N Harris * Print the HTML for the wiki textarea.
823fdb8d77bSTom N Harris * Requires '_text' with default text of the field.
824fdb8d77bSTom N Harris * Text will be passed to formText(), attributes to buildAttributes()
825fdb8d77bSTom N Harris *
826fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
82742ea7f44SGerrit Uitslag *
82842ea7f44SGerrit Uitslag * @param array $attrs attributes
82942ea7f44SGerrit Uitslag * @return string html
830fdb8d77bSTom N Harris */
831d868eb89SAndreas Gohrfunction form_wikitext($attrs)
832d868eb89SAndreas Gohr{
833b2bc77d5STom N Harris    // mandatory attributes
834b2bc77d5STom N Harris    unset($attrs['name']);
835b2bc77d5STom N Harris    unset($attrs['id']);
8360d39ad11SAnika Henke    return '<textarea name="wikitext" id="wiki__text" dir="auto" '
837804e2f2fSAdrian Lang                . buildAttributes($attrs, true).'>'.DOKU_LF
838fdb8d77bSTom N Harris                . formText($attrs['_text'])
839fdb8d77bSTom N Harris                .'</textarea>';
840fdb8d77bSTom N Harris}
841fdb8d77bSTom N Harris
842fdb8d77bSTom N Harris/**
843fdb8d77bSTom N Harris * form_button
844fdb8d77bSTom N Harris *
845fdb8d77bSTom N Harris * Print the HTML for a form button.
846fdb8d77bSTom N Harris * If '_action' is set, the button name will be "do[_action]".
847fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes()
848fdb8d77bSTom N Harris *
849fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
85042ea7f44SGerrit Uitslag *
85142ea7f44SGerrit Uitslag * @param array $attrs attributes
85242ea7f44SGerrit Uitslag * @return string html
853fdb8d77bSTom N Harris */
854d868eb89SAndreas Gohrfunction form_button($attrs)
855d868eb89SAndreas Gohr{
856fdb8d77bSTom N Harris    $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
857ae614416SAnika Henke    $value = $attrs['value'];
858ae614416SAnika Henke    unset($attrs['value']);
859ae614416SAnika Henke    return '<button '.$p. buildAttributes($attrs, true) .'>'.$value.'</button>';
860fdb8d77bSTom N Harris}
861fdb8d77bSTom N Harris
862fdb8d77bSTom N Harris/**
863fdb8d77bSTom N Harris * form_field
864fdb8d77bSTom N Harris *
865fdb8d77bSTom N Harris * Print the HTML for a form input field.
866fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
867fdb8d77bSTom N Harris *   _text  : Text to display before the input. Not escaped.
868fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
869fdb8d77bSTom N Harris *
870fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
87142ea7f44SGerrit Uitslag *
87242ea7f44SGerrit Uitslag * @param array $attrs attributes
87342ea7f44SGerrit Uitslag * @return string html
874fdb8d77bSTom N Harris */
875d868eb89SAndreas Gohrfunction form_field($attrs)
876d868eb89SAndreas Gohr{
877a57c7cafSAnika Henke    $s = '<label';
878a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
879fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
880fdb8d77bSTom N Harris    $s .= '><span>'.$attrs['_text'].'</span>';
881fdb8d77bSTom N Harris    $s .= ' <input '. buildAttributes($attrs, true) .' /></label>';
882fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
883fdb8d77bSTom N Harris        $s .= '<br />';
884fdb8d77bSTom N Harris    return $s;
885fdb8d77bSTom N Harris}
886fdb8d77bSTom N Harris
887fdb8d77bSTom N Harris/**
888fdb8d77bSTom N Harris * form_fieldright
889fdb8d77bSTom N Harris *
890fdb8d77bSTom N Harris * Print the HTML for a form input field. (right-aligned)
891fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
892fdb8d77bSTom N Harris *   _text  : Text to display after the input. Not escaped.
893fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
894fdb8d77bSTom N Harris *
895fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
89642ea7f44SGerrit Uitslag *
89742ea7f44SGerrit Uitslag * @param array $attrs attributes
89842ea7f44SGerrit Uitslag * @return string html
899fdb8d77bSTom N Harris */
900d868eb89SAndreas Gohrfunction form_fieldright($attrs)
901d868eb89SAndreas Gohr{
902a57c7cafSAnika Henke    $s = '<label';
903a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
904fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
905fdb8d77bSTom N Harris    $s .= '><input '. buildAttributes($attrs, true) .' />';
906fdb8d77bSTom N Harris    $s .= ' <span>'.$attrs['_text'].'</span></label>';
907fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
908fdb8d77bSTom N Harris        $s .= '<br />';
909fdb8d77bSTom N Harris    return $s;
910fdb8d77bSTom N Harris}
911fdb8d77bSTom N Harris
912fdb8d77bSTom N Harris/**
913fdb8d77bSTom N Harris * form_textfield
914fdb8d77bSTom N Harris *
915fdb8d77bSTom N Harris * Print the HTML for a text input field.
916fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
917fdb8d77bSTom N Harris *   _text  : Text to display before the input. Not escaped.
918fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
919fdb8d77bSTom N Harris *
920fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
92142ea7f44SGerrit Uitslag *
92242ea7f44SGerrit Uitslag * @param array $attrs attributes
92342ea7f44SGerrit Uitslag * @return string html
924fdb8d77bSTom N Harris */
925d868eb89SAndreas Gohrfunction form_textfield($attrs)
926d868eb89SAndreas Gohr{
927b2bc77d5STom N Harris    // mandatory attributes
928b2bc77d5STom N Harris    unset($attrs['type']);
929a57c7cafSAnika Henke    $s = '<label';
930a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
931fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
932fdb8d77bSTom N Harris    $s .= '><span>'.$attrs['_text'].'</span> ';
933b2bc77d5STom N Harris    $s .= '<input type="text" '. buildAttributes($attrs, true) .' /></label>';
934fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
935fdb8d77bSTom N Harris        $s .= '<br />';
936fdb8d77bSTom N Harris    return $s;
937fdb8d77bSTom N Harris}
938fdb8d77bSTom N Harris
939fdb8d77bSTom N Harris/**
940fdb8d77bSTom N Harris * form_passwordfield
941fdb8d77bSTom N Harris *
942fdb8d77bSTom N Harris * Print the HTML for a password input field.
943fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
944fdb8d77bSTom N Harris *   _text  : Text to display before the input. Not escaped.
945fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
946fdb8d77bSTom N Harris *
947fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
94842ea7f44SGerrit Uitslag *
94942ea7f44SGerrit Uitslag * @param array $attrs attributes
95042ea7f44SGerrit Uitslag * @return string html
951fdb8d77bSTom N Harris */
952d868eb89SAndreas Gohrfunction form_passwordfield($attrs)
953d868eb89SAndreas Gohr{
954b2bc77d5STom N Harris    // mandatory attributes
955b2bc77d5STom N Harris    unset($attrs['type']);
956a57c7cafSAnika Henke    $s = '<label';
957a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
958fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
959fdb8d77bSTom N Harris    $s .= '><span>'.$attrs['_text'].'</span> ';
960b2bc77d5STom N Harris    $s .= '<input type="password" '. buildAttributes($attrs, true) .' /></label>';
961fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
962fdb8d77bSTom N Harris        $s .= '<br />';
963fdb8d77bSTom N Harris    return $s;
964fdb8d77bSTom N Harris}
965fdb8d77bSTom N Harris
966fdb8d77bSTom N Harris/**
96712bbca2eSMichael Klier * form_filefield
96812bbca2eSMichael Klier *
96912bbca2eSMichael Klier * Print the HTML for a file input field.
97012bbca2eSMichael Klier *   _class     : class attribute used on the label tag
97112bbca2eSMichael Klier *   _text      : Text to display before the input. Not escaped
97212bbca2eSMichael Klier *   _maxlength : Allowed size in byte
97312bbca2eSMichael Klier *   _accept    : Accepted mime-type
97412bbca2eSMichael Klier * Other attributes are passed to buildAttributes() for the input tag
97512bbca2eSMichael Klier *
97612bbca2eSMichael Klier * @author  Michael Klier <chi@chimeric.de>
97742ea7f44SGerrit Uitslag *
97842ea7f44SGerrit Uitslag * @param array $attrs attributes
97942ea7f44SGerrit Uitslag * @return string html
98012bbca2eSMichael Klier */
981d868eb89SAndreas Gohrfunction form_filefield($attrs)
982d868eb89SAndreas Gohr{
983a57c7cafSAnika Henke    $s = '<label';
984a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
98512bbca2eSMichael Klier    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
98612bbca2eSMichael Klier    $s .= '><span>'.$attrs['_text'].'</span> ';
98712bbca2eSMichael Klier    $s .= '<input type="file" '. buildAttributes($attrs, true);
98812bbca2eSMichael Klier    if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
98912bbca2eSMichael Klier    if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
99012bbca2eSMichael Klier    $s .= ' /></label>';
99112bbca2eSMichael Klier    if (preg_match('/(^| )block($| )/', $attrs['_class']))
99212bbca2eSMichael Klier        $s .= '<br />';
99312bbca2eSMichael Klier    return $s;
99412bbca2eSMichael Klier}
99512bbca2eSMichael Klier
99612bbca2eSMichael Klier/**
997fdb8d77bSTom N Harris * form_checkboxfield
998fdb8d77bSTom N Harris *
999fdb8d77bSTom N Harris * Print the HTML for a checkbox input field.
1000fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
1001fdb8d77bSTom N Harris *   _text  : Text to display after the input. Not escaped.
1002fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
10032f10258cSAdrian Lang * If value is an array, a hidden field with the same name and the value
10042f10258cSAdrian Lang * $attrs['value'][1] is constructed as well.
1005fdb8d77bSTom N Harris *
1006fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
100742ea7f44SGerrit Uitslag *
100842ea7f44SGerrit Uitslag * @param array $attrs attributes
100942ea7f44SGerrit Uitslag * @return string html
1010fdb8d77bSTom N Harris */
1011d868eb89SAndreas Gohrfunction form_checkboxfield($attrs)
1012d868eb89SAndreas Gohr{
1013b2bc77d5STom N Harris    // mandatory attributes
1014b2bc77d5STom N Harris    unset($attrs['type']);
1015a57c7cafSAnika Henke    $s = '<label';
1016a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
1017fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
10182f10258cSAdrian Lang    $s .= '>';
10192f10258cSAdrian Lang    if (is_array($attrs['value'])) {
10202f10258cSAdrian Lang        echo '<input type="hidden" name="'. hsc($attrs['name']) .'"'
10212f10258cSAdrian Lang                .' value="'. hsc($attrs['value'][1]) .'" />';
10222f10258cSAdrian Lang        $attrs['value'] = $attrs['value'][0];
10232f10258cSAdrian Lang    }
10242f10258cSAdrian Lang    $s .= '<input type="checkbox" '. buildAttributes($attrs, true) .' />';
1025fdb8d77bSTom N Harris    $s .= ' <span>'.$attrs['_text'].'</span></label>';
1026fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
1027fdb8d77bSTom N Harris        $s .= '<br />';
1028fdb8d77bSTom N Harris    return $s;
1029fdb8d77bSTom N Harris}
1030fdb8d77bSTom N Harris
1031fdb8d77bSTom N Harris/**
1032fdb8d77bSTom N Harris * form_radiofield
1033fdb8d77bSTom N Harris *
1034fdb8d77bSTom N Harris * Print the HTML for a radio button input field.
1035fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
1036fdb8d77bSTom N Harris *   _text  : Text to display after the input. Not escaped.
1037fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
1038fdb8d77bSTom N Harris *
1039fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
104042ea7f44SGerrit Uitslag *
104142ea7f44SGerrit Uitslag * @param array $attrs attributes
104242ea7f44SGerrit Uitslag * @return string html
1043fdb8d77bSTom N Harris */
1044d868eb89SAndreas Gohrfunction form_radiofield($attrs)
1045d868eb89SAndreas Gohr{
1046b2bc77d5STom N Harris    // mandatory attributes
1047b2bc77d5STom N Harris    unset($attrs['type']);
1048a57c7cafSAnika Henke    $s = '<label';
1049a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
1050fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
1051fdb8d77bSTom N Harris    $s .= '><input type="radio" '. buildAttributes($attrs, true) .' />';
1052fdb8d77bSTom N Harris    $s .= ' <span>'.$attrs['_text'].'</span></label>';
1053fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
1054fdb8d77bSTom N Harris        $s .= '<br />';
1055fdb8d77bSTom N Harris    return $s;
1056fdb8d77bSTom N Harris}
1057fdb8d77bSTom N Harris
1058fdb8d77bSTom N Harris/**
1059fdb8d77bSTom N Harris * form_menufield
1060fdb8d77bSTom N Harris *
1061fdb8d77bSTom N Harris * Print the HTML for a drop-down menu.
1062fdb8d77bSTom N Harris *   _options : Array of (value,text,selected) for the menu.
1063fdb8d77bSTom N Harris *              Text can be omitted. Text and value are passed to formText()
1064fdb8d77bSTom N Harris *              Only one item can be selected.
1065fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
1066fdb8d77bSTom N Harris *   _text  : Text to display before the menu. Not escaped.
1067fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
1068fdb8d77bSTom N Harris *
1069fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
107042ea7f44SGerrit Uitslag *
107142ea7f44SGerrit Uitslag * @param array $attrs attributes
107242ea7f44SGerrit Uitslag * @return string html
1073fdb8d77bSTom N Harris */
1074d868eb89SAndreas Gohrfunction form_menufield($attrs)
1075d868eb89SAndreas Gohr{
1076fdb8d77bSTom N Harris    $attrs['size'] = '1';
1077a57c7cafSAnika Henke    $s = '<label';
1078a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
1079fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
1080fdb8d77bSTom N Harris    $s .= '><span>'.$attrs['_text'].'</span>';
1081804e2f2fSAdrian Lang    $s .= ' <select '. buildAttributes($attrs, true) .'>'.DOKU_LF;
1082fdb8d77bSTom N Harris    if (!empty($attrs['_options'])) {
1083fdb8d77bSTom N Harris        $selected = false;
108449eb6e38SAndreas Gohr
108549eb6e38SAndreas Gohr        $cnt = count($attrs['_options']);
108649eb6e38SAndreas Gohr        for ($n=0; $n < $cnt; $n++) {
1087fdb8d77bSTom N Harris            @list($value,$text,$select) = $attrs['_options'][$n];
1088fdb8d77bSTom N Harris            $p = '';
1089fdb8d77bSTom N Harris            if (!is_null($text))
1090fdb8d77bSTom N Harris                $p .= ' value="'. formText($value) .'"';
1091*177d6836SAndreas Gohr            else $text = $value;
1092fdb8d77bSTom N Harris            if (!empty($select) && !$selected) {
1093fdb8d77bSTom N Harris                $p .= ' selected="selected"';
1094fdb8d77bSTom N Harris                $selected = true;
1095fdb8d77bSTom N Harris            }
1096fdb8d77bSTom N Harris            $s .= '<option'.$p.'>'. formText($text) .'</option>';
1097fdb8d77bSTom N Harris        }
1098fdb8d77bSTom N Harris    } else {
1099fdb8d77bSTom N Harris        $s .= '<option></option>';
1100fdb8d77bSTom N Harris    }
1101804e2f2fSAdrian Lang    $s .= DOKU_LF.'</select></label>';
1102fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
1103fdb8d77bSTom N Harris        $s .= '<br />';
1104fdb8d77bSTom N Harris    return $s;
1105fdb8d77bSTom N Harris}
1106fdb8d77bSTom N Harris
1107fdb8d77bSTom N Harris/**
1108fdb8d77bSTom N Harris * form_listboxfield
1109fdb8d77bSTom N Harris *
1110fdb8d77bSTom N Harris * Print the HTML for a list box.
1111fdb8d77bSTom N Harris *   _options : Array of (value,text,selected) for the list.
1112fdb8d77bSTom N Harris *              Text can be omitted. Text and value are passed to formText()
1113fdb8d77bSTom N Harris *   _class : class attribute used on the label tag
1114fdb8d77bSTom N Harris *   _text  : Text to display before the menu. Not escaped.
1115fdb8d77bSTom N Harris * Other attributes are passed to buildAttributes() for the input tag.
1116fdb8d77bSTom N Harris *
1117fdb8d77bSTom N Harris * @author  Tom N Harris <tnharris@whoopdedo.org>
111842ea7f44SGerrit Uitslag *
111942ea7f44SGerrit Uitslag * @param array $attrs attributes
112042ea7f44SGerrit Uitslag * @return string html
1121fdb8d77bSTom N Harris */
1122d868eb89SAndreas Gohrfunction form_listboxfield($attrs)
1123d868eb89SAndreas Gohr{
1124a57c7cafSAnika Henke    $s = '<label';
1125a57c7cafSAnika Henke    if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
1126fdb8d77bSTom N Harris    if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
1127fdb8d77bSTom N Harris    $s .= '><span>'.$attrs['_text'].'</span> ';
1128804e2f2fSAdrian Lang    $s .= '<select '. buildAttributes($attrs, true) .'>'.DOKU_LF;
1129fdb8d77bSTom N Harris    if (!empty($attrs['_options'])) {
1130fdb8d77bSTom N Harris        foreach ($attrs['_options'] as $opt) {
1131eeb8f429SGerrit Uitslag            @list($value, $text, $select, $disabled) = $opt;
1132fdb8d77bSTom N Harris            $p = '';
1133a0fe6e28SAdrian Lang            if (is_null($text)) $text = $value;
1134fdb8d77bSTom N Harris            $p .= ' value="'. formText($value) .'"';
1135fdb8d77bSTom N Harris            if (!empty($select)) $p .= ' selected="selected"';
1136eeb8f429SGerrit Uitslag            if ($disabled) $p .= ' disabled="disabled"';
1137fdb8d77bSTom N Harris            $s .= '<option'.$p.'>'. formText($text) .'</option>';
1138fdb8d77bSTom N Harris        }
1139fdb8d77bSTom N Harris    } else {
1140fdb8d77bSTom N Harris        $s .= '<option></option>';
1141fdb8d77bSTom N Harris    }
1142804e2f2fSAdrian Lang    $s .= DOKU_LF.'</select></label>';
1143fdb8d77bSTom N Harris    if (preg_match('/(^| )block($| )/', $attrs['_class']))
1144fdb8d77bSTom N Harris        $s .= '<br />';
1145fdb8d77bSTom N Harris    return $s;
1146fdb8d77bSTom N Harris}
1147