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