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