5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Tom N Harris <tnharris@whoopdedo.org>
9 if(!defined('DOKU_INC')) die('meh.');
12 * Class for creating simple HTML forms.
14 * The forms is built from a list of pseudo-tags (arrays with expected keys).
15 * Every pseudo-tag must have the key '_elem' set to the name of the element.
16 * When printed, the form class calls functions named 'form_$type' for each
17 * element it contains.
19 * Standard practice is for non-attribute keys in a pseudo-element to start
20 * with '_'. Other keys are HTML attributes that will be included in the element
21 * tag. That way, the element output functions can pass the pseudo-element
22 * directly to buildAttributes.
24 * See the form_make* functions later in this file.
26 * @author Tom N Harris <tnharris@whoopdedo.org>
31 var $params = array();
33 // Draw a border around form fields.
34 // Adds <fieldset></fieldset> around the elements
35 var $_infieldset = false;
37 // Hidden form fields.
38 var $_hidden = array();
40 // Array of pseudo-tags
41 var $_content = array();
46 * Sets parameters and autoadds a security token. The old calling convention
47 * with up to four parameters is deprecated, instead the first parameter
48 * should be an array with parameters.
50 * @param mixed $params Parameters for the HTML form element; Using the
51 * deprecated calling convention this is the ID
52 * attribute of the form
53 * @param string $action (optional, deprecated) submit URL, defaults to
55 * @param string $method (optional, deprecated) 'POST' or 'GET', default
57 * @param string $enctype (optional, deprecated) Encoding type of the
59 * @author Tom N Harris <tnharris@whoopdedo.org>
61 function Doku_Form($params, $action=false, $method=false, $enctype=false) {
62 if(!is_array($params)) {
63 $this->params = array('id' => $params);
64 if ($action !== false) $this->params['action'] = $action;
65 if ($method !== false) $this->params['method'] = strtolower($method);
66 if ($enctype !== false) $this->params['enctype'] = $enctype;
68 $this->params = $params;
71 if (!isset($this->params['method'])) {
72 $this->params['method'] = 'post';
74 $this->params['method'] = strtolower($this->params['method']);
77 if (!isset($this->params['action'])) {
78 $this->params['action'] = '';
81 $this->addHidden('sectok', getSecurityToken());
87 * Add <fieldset></fieldset> tags around fields.
88 * Usually results in a border drawn around the form.
90 * @param string $legend Label that will be printed with the border.
91 * @author Tom N Harris <tnharris@whoopdedo.org>
93 function startFieldset($legend) {
94 if ($this->_infieldset) {
95 $this->addElement(array('_elem'=>'closefieldset'));
97 $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend));
98 $this->_infieldset = true;
104 * @author Tom N Harris <tnharris@whoopdedo.org>
106 function endFieldset() {
107 if ($this->_infieldset) {
108 $this->addElement(array('_elem'=>'closefieldset'));
110 $this->_infieldset = false;
116 * Adds a name/value pair as a hidden field.
117 * The value of the field (but not the name) will be passed to
118 * formText() before printing.
120 * @param string $name Field name.
121 * @param string $value Field value. If null, remove a previously added field.
122 * @author Tom N Harris <tnharris@whoopdedo.org>
124 function addHidden($name, $value) {
126 unset($this->_hidden[$name]);
128 $this->_hidden[$name] = $value;
134 * Appends a content element to the form.
135 * The element can be either a pseudo-tag or string.
136 * If string, it is printed without escaping special chars. *
138 * @param string $elem Pseudo-tag or string to add to the form.
139 * @author Tom N Harris <tnharris@whoopdedo.org>
141 function addElement($elem) {
142 $this->_content[] = $elem;
148 * Inserts a content element at a position.
150 * @param string $pos 0-based index where the element will be inserted.
151 * @param string $elem Pseudo-tag or string to add to the form.
152 * @author Tom N Harris <tnharris@whoopdedo.org>
154 function insertElement($pos, $elem) {
155 array_splice($this->_content, $pos, 0, array($elem));
161 * Replace with NULL to remove an element.
163 * @param int $pos 0-based index the element will be placed at.
164 * @param string $elem Pseudo-tag or string to add to the form.
165 * @author Tom N Harris <tnharris@whoopdedo.org>
167 function replaceElement($pos, $elem) {
169 if (!is_null($elem)) $rep[] = $elem;
170 array_splice($this->_content, $pos, 1, $rep);
176 * Gets the position of the first of a type of element.
178 * @param string $type Element type to look for.
179 * @return array pseudo-element if found, false otherwise
180 * @author Tom N Harris <tnharris@whoopdedo.org>
182 function findElementByType($type) {
183 foreach ($this->_content as $pos=>$elem) {
184 if (is_array($elem) && $elem['_elem'] == $type)
193 * Gets the position of the element with an ID attribute.
195 * @param string $id ID of the element to find.
196 * @return array pseudo-element if found, false otherwise
197 * @author Tom N Harris <tnharris@whoopdedo.org>
199 function findElementById($id) {
200 foreach ($this->_content as $pos=>$elem) {
201 if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
208 * findElementByAttribute
210 * Gets the position of the first element with a matching attribute value.
212 * @param string $name Attribute name.
213 * @param string $value Attribute value.
214 * @return array pseudo-element if found, false otherwise
215 * @author Tom N Harris <tnharris@whoopdedo.org>
217 function findElementByAttribute($name, $value) {
218 foreach ($this->_content as $pos=>$elem) {
219 if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
228 * Returns a reference to the element at a position.
229 * A position out-of-bounds will return either the
230 * first (underflow) or last (overflow) element.
232 * @param int $pos 0-based index
233 * @return arrayreference pseudo-element
234 * @author Tom N Harris <tnharris@whoopdedo.org>
236 function &getElementAt($pos) {
237 if ($pos < 0) $pos = count($this->_content) + $pos;
238 if ($pos < 0) $pos = 0;
239 if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
240 return $this->_content[$pos];
244 * Return the assembled HTML for the form.
246 * Each element in the form will be passed to a function named
247 * 'form_$type'. The function should return the HTML to be printed.
249 * @author Tom N Harris <tnharris@whoopdedo.org>
254 $this->params['accept-charset'] = $lang['encoding'];
255 $form .= '<form ' . buildAttributes($this->params,false) . '><div class="no">' . DOKU_LF;
256 if (!empty($this->_hidden)) {
257 foreach ($this->_hidden as $name=>$value)
258 $form .= form_hidden(array('name'=>$name, 'value'=>$value));
260 foreach ($this->_content as $element) {
261 if (is_array($element)) {
262 $elem_type = $element['_elem'];
263 if (function_exists('form_'.$elem_type)) {
264 $form .= call_user_func('form_'.$elem_type, $element).DOKU_LF;
270 if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF;
271 $form .= '</div></form>'.DOKU_LF;
277 * Print the assembled form
279 * wraps around getForm()
281 function printForm(){
282 echo $this->getForm();
288 * This function adds a set of radio buttons to the form. If $_POST[$name]
289 * is set, this radio is preselected, else the first radio button.
291 * @param string $name The HTML field name
292 * @param array $entries An array of entries $value => $caption
294 * @author Adrian Lang <lang@cosmocode.de>
297 function addRadioSet($name, $entries) {
298 $value = (isset($_POST[$name]) && isset($entries[$_POST[$name]])) ?
299 $_POST[$name] : key($entries);
300 foreach($entries as $val => $cap) {
301 $data = ($value === $val) ? array('checked' => 'checked') : array();
302 $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data));
311 * Create a form element for a non-specific empty tag.
313 * @param string $tag Tag name.
314 * @param array $attrs Optional attributes.
315 * @return array pseudo-tag
316 * @author Tom N Harris <tnharris@whoopdedo.org>
318 function form_makeTag($tag, $attrs=array()) {
319 $elem = array('_elem'=>'tag', '_tag'=>$tag);
320 return array_merge($elem, $attrs);
326 * Create a form element for a non-specific opening tag.
327 * Remember to put a matching close tag after this as well.
329 * @param string $tag Tag name.
330 * @param array $attrs Optional attributes.
331 * @return array pseudo-tag
332 * @author Tom N Harris <tnharris@whoopdedo.org>
334 function form_makeOpenTag($tag, $attrs=array()) {
335 $elem = array('_elem'=>'opentag', '_tag'=>$tag);
336 return array_merge($elem, $attrs);
342 * Create a form element for a non-specific closing tag.
343 * Careless use of this will result in invalid XHTML.
345 * @param string $tag Tag name.
346 * @return array pseudo-tag
347 * @author Tom N Harris <tnharris@whoopdedo.org>
349 function form_makeCloseTag($tag) {
350 return array('_elem'=>'closetag', '_tag'=>$tag);
356 * Create a form element for a textarea containing wiki text.
357 * Only one wikitext element is allowed on a page. It will have
358 * a name of 'wikitext' and id 'wiki__text'. The text will
359 * be passed to formText() before printing.
361 * @param string $text Text to fill the field with.
362 * @param array $attrs Optional attributes.
363 * @return array pseudo-tag
364 * @author Tom N Harris <tnharris@whoopdedo.org>
366 function form_makeWikiText($text, $attrs=array()) {
367 $elem = array('_elem'=>'wikitext', '_text'=>$text,
368 'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
369 return array_merge($elem, $attrs);
375 * Create a form element for an action button.
376 * A title will automatically be generated using the value and
377 * accesskey attributes, unless you provide one.
379 * @param string $type Type attribute. 'submit' or 'cancel'
380 * @param string $act Wiki action of the button, will be used as the do= parameter
381 * @param string $value (optional) Displayed label. Uses $act if not provided.
382 * @param array $attrs Optional attributes.
383 * @return array pseudo-tag
384 * @author Tom N Harris <tnharris@whoopdedo.org>
386 function form_makeButton($type, $act, $value='', $attrs=array()) {
387 if ($value == '') $value = $act;
388 $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
389 'value'=>$value, 'class'=>'button');
390 if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
391 $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']';
393 return array_merge($elem, $attrs);
399 * Create a form element for a labelled input element.
400 * The label text will be printed before the input.
402 * @param string $type Type attribute of input.
403 * @param string $name Name attribute of the input.
404 * @param string $value (optional) Default value.
405 * @param string $class Class attribute of the label. If this is 'block',
406 * then a line break will be added after the field.
407 * @param string $label Label that will be printed before the input.
408 * @param string $id ID attribute of the input. If set, the label will
409 * reference it with a 'for' attribute.
410 * @param array $attrs Optional attributes.
411 * @return array pseudo-tag
412 * @author Tom N Harris <tnharris@whoopdedo.org>
414 function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
415 if (is_null($label)) $label = $name;
416 $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
417 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
418 return array_merge($elem, $attrs);
422 * form_makeFieldRight
424 * Create a form element for a labelled input element.
425 * The label text will be printed after the input.
427 * @see form_makeField
428 * @author Tom N Harris <tnharris@whoopdedo.org>
430 function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
431 if (is_null($label)) $label = $name;
432 $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
433 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
434 return array_merge($elem, $attrs);
440 * Create a form element for a text input element with label.
442 * @see form_makeField
443 * @author Tom N Harris <tnharris@whoopdedo.org>
445 function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) {
446 if (is_null($label)) $label = $name;
447 $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
448 'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
449 return array_merge($elem, $attrs);
453 * form_makePasswordField
455 * Create a form element for a password input element with label.
456 * Password elements have no default value, for obvious reasons.
458 * @see form_makeField
459 * @author Tom N Harris <tnharris@whoopdedo.org>
461 function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) {
462 if (is_null($label)) $label = $name;
463 $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
464 'id'=>$id, 'name'=>$name, 'class'=>'edit');
465 return array_merge($elem, $attrs);
471 * Create a form element for a file input element with label
473 * @see form_makeField
474 * @author Michael Klier <chi@chimeric.de>
476 function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) {
477 if (is_null($label)) $label = $name;
478 $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
479 'id'=>$id, 'name'=>$name, 'class'=>'edit');
480 return array_merge($elem, $attrs);
484 * form_makeCheckboxField
486 * Create a form element for a checkbox input element with label.
487 * If $value is an array, a hidden field with the same name and the value
488 * $value[1] is constructed as well.
490 * @see form_makeFieldRight
491 * @author Tom N Harris <tnharris@whoopdedo.org>
493 function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
494 if (is_null($label)) $label = $name;
495 if (is_null($value) || $value=='') $value='0';
496 $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
497 'id'=>$id, 'name'=>$name, 'value'=>$value);
498 return array_merge($elem, $attrs);
502 * form_makeRadioField
504 * Create a form element for a radio button input element with label.
506 * @see form_makeFieldRight
507 * @author Tom N Harris <tnharris@whoopdedo.org>
509 function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
510 if (is_null($label)) $label = $name;
511 if (is_null($value) || $value=='') $value='0';
512 $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
513 'id'=>$id, 'name'=>$name, 'value'=>$value);
514 return array_merge($elem, $attrs);
520 * Create a form element for a drop-down menu with label.
521 * The list of values can be strings, arrays of (value,text),
522 * or an associative array with the values as keys and labels as values.
523 * An item is selected by supplying its value or integer index.
524 * If the list of values is an associative array, the selected item must be
527 * @author Tom N Harris <tnharris@whoopdedo.org>
529 function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
530 if (is_null($label)) $label = $name;
533 // FIXME: php doesn't know the difference between a string and an integer
534 if (is_string(key($values))) {
535 foreach ($values as $val=>$text) {
536 $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
539 if (is_integer($selected)) $selected = $values[$selected];
540 foreach ($values as $val) {
542 @list($val,$text) = $val;
545 $options[] = array($val,$text,$val===$selected);
548 $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
549 'id'=>$id, 'name'=>$name);
550 return array_merge($elem, $attrs);
554 * form_makeListboxField
556 * Create a form element for a list box with label.
557 * The list of values can be strings, arrays of (value,text),
558 * or an associative array with the values as keys and labels as values.
559 * Items are selected by supplying its value or an array of values.
561 * @author Tom N Harris <tnharris@whoopdedo.org>
563 function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
564 if (is_null($label)) $label = $name;
567 if (is_null($selected) || $selected == '')
569 elseif (!is_array($selected))
570 $selected = array($selected);
571 // FIXME: php doesn't know the difference between a string and an integer
572 if (is_string(key($values))) {
573 foreach ($values as $val=>$text) {
574 $options[] = array($val,$text,in_array($val,$selected));
577 foreach ($values as $val) {
579 @list($val,$text) = $val;
582 $options[] = array($val,$text,in_array($val,$selected));
585 $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
586 'id'=>$id, 'name'=>$name);
587 return array_merge($elem, $attrs);
593 * Print the HTML for a generic empty tag.
594 * Requires '_tag' key with name of the tag.
595 * Attributes are passed to buildAttributes()
597 * @author Tom N Harris <tnharris@whoopdedo.org>
599 function form_tag($attrs) {
600 return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'/>';
606 * Print the HTML for a generic opening tag.
607 * Requires '_tag' key with name of the tag.
608 * Attributes are passed to buildAttributes()
610 * @author Tom N Harris <tnharris@whoopdedo.org>
612 function form_opentag($attrs) {
613 return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
619 * Print the HTML for a generic closing tag.
620 * Requires '_tag' key with name of the tag.
621 * There are no attributes.
623 * @author Tom N Harris <tnharris@whoopdedo.org>
625 function form_closetag($attrs) {
626 return '</'.$attrs['_tag'].'>';
632 * Print the HTML for an opening fieldset tag.
633 * Uses the '_legend' key.
634 * Attributes are passed to buildAttributes()
636 * @author Tom N Harris <tnharris@whoopdedo.org>
638 function form_openfieldset($attrs) {
639 $s = '<fieldset '.buildAttributes($attrs,true).'>';
640 if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
647 * Print the HTML for a closing fieldset tag.
648 * There are no attributes.
650 * @author Tom N Harris <tnharris@whoopdedo.org>
652 function form_closefieldset() {
653 return '</fieldset>';
659 * Print the HTML for a hidden input element.
660 * Uses only 'name' and 'value' attributes.
661 * Value is passed to formText()
663 * @author Tom N Harris <tnharris@whoopdedo.org>
665 function form_hidden($attrs) {
666 return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
672 * Print the HTML for the wiki textarea.
673 * Requires '_text' with default text of the field.
674 * Text will be passed to formText(), attributes to buildAttributes()
676 * @author Tom N Harris <tnharris@whoopdedo.org>
678 function form_wikitext($attrs) {
679 // mandatory attributes
680 unset($attrs['name']);
682 return '<textarea name="wikitext" id="wiki__text" '
683 .buildAttributes($attrs,true).'>'.DOKU_LF
684 .formText($attrs['_text'])
691 * Print the HTML for a form button.
692 * If '_action' is set, the button name will be "do[_action]".
693 * Other attributes are passed to buildAttributes()
695 * @author Tom N Harris <tnharris@whoopdedo.org>
697 function form_button($attrs) {
698 $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
699 return '<input '.$p.buildAttributes($attrs,true).' />';
705 * Print the HTML for a form input field.
706 * _class : class attribute used on the label tag
707 * _text : Text to display before the input. Not escaped.
708 * Other attributes are passed to buildAttributes() for the input tag.
710 * @author Tom N Harris <tnharris@whoopdedo.org>
712 function form_field($attrs) {
714 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
715 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
716 $s .= '><span>'.$attrs['_text'].'</span>';
717 $s .= ' <input '.buildAttributes($attrs,true).' /></label>';
718 if (preg_match('/(^| )block($| )/', $attrs['_class']))
726 * Print the HTML for a form input field. (right-aligned)
727 * _class : class attribute used on the label tag
728 * _text : Text to display after the input. Not escaped.
729 * Other attributes are passed to buildAttributes() for the input tag.
731 * @author Tom N Harris <tnharris@whoopdedo.org>
733 function form_fieldright($attrs) {
735 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
736 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
737 $s .= '><input '.buildAttributes($attrs,true).' />';
738 $s .= ' <span>'.$attrs['_text'].'</span></label>';
739 if (preg_match('/(^| )block($| )/', $attrs['_class']))
747 * Print the HTML for a text input field.
748 * _class : class attribute used on the label tag
749 * _text : Text to display before the input. Not escaped.
750 * Other attributes are passed to buildAttributes() for the input tag.
752 * @author Tom N Harris <tnharris@whoopdedo.org>
754 function form_textfield($attrs) {
755 // mandatory attributes
756 unset($attrs['type']);
758 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
759 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
760 $s .= '><span>'.$attrs['_text'].'</span> ';
761 $s .= '<input type="text" '.buildAttributes($attrs,true).' /></label>';
762 if (preg_match('/(^| )block($| )/', $attrs['_class']))
770 * Print the HTML for a password input field.
771 * _class : class attribute used on the label tag
772 * _text : Text to display before the input. Not escaped.
773 * Other attributes are passed to buildAttributes() for the input tag.
775 * @author Tom N Harris <tnharris@whoopdedo.org>
777 function form_passwordfield($attrs) {
778 // mandatory attributes
779 unset($attrs['type']);
781 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
782 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
783 $s .= '><span>'.$attrs['_text'].'</span> ';
784 $s .= '<input type="password" '.buildAttributes($attrs,true).' /></label>';
785 if (preg_match('/(^| )block($| )/', $attrs['_class']))
793 * Print the HTML for a file input field.
794 * _class : class attribute used on the label tag
795 * _text : Text to display before the input. Not escaped
796 * _maxlength : Allowed size in byte
797 * _accept : Accepted mime-type
798 * Other attributes are passed to buildAttributes() for the input tag
800 * @author Michael Klier <chi@chimeric.de>
802 function form_filefield($attrs) {
804 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
805 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
806 $s .= '><span>'.$attrs['_text'].'</span> ';
807 $s .= '<input type="file" '.buildAttributes($attrs,true);
808 if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
809 if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
811 if (preg_match('/(^| )block($| )/', $attrs['_class']))
819 * Print the HTML for a checkbox input field.
820 * _class : class attribute used on the label tag
821 * _text : Text to display after the input. Not escaped.
822 * Other attributes are passed to buildAttributes() for the input tag.
823 * If value is an array, a hidden field with the same name and the value
824 * $attrs['value'][1] is constructed as well.
826 * @author Tom N Harris <tnharris@whoopdedo.org>
828 function form_checkboxfield($attrs) {
829 // mandatory attributes
830 unset($attrs['type']);
832 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
833 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
835 if (is_array($attrs['value'])) {
836 echo '<input type="hidden" name="' . hsc($attrs['name']) .'"'
837 . ' value="' . hsc($attrs['value'][1]) . '" />';
838 $attrs['value'] = $attrs['value'][0];
840 $s .= '<input type="checkbox" '.buildAttributes($attrs,true).' />';
841 $s .= ' <span>'.$attrs['_text'].'</span></label>';
842 if (preg_match('/(^| )block($| )/', $attrs['_class']))
850 * Print the HTML for a radio button input field.
851 * _class : class attribute used on the label tag
852 * _text : Text to display after the input. Not escaped.
853 * Other attributes are passed to buildAttributes() for the input tag.
855 * @author Tom N Harris <tnharris@whoopdedo.org>
857 function form_radiofield($attrs) {
858 // mandatory attributes
859 unset($attrs['type']);
861 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
862 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
863 $s .= '><input type="radio" '.buildAttributes($attrs,true).' />';
864 $s .= ' <span>'.$attrs['_text'].'</span></label>';
865 if (preg_match('/(^| )block($| )/', $attrs['_class']))
873 * Print the HTML for a drop-down menu.
874 * _options : Array of (value,text,selected) for the menu.
875 * Text can be omitted. Text and value are passed to formText()
876 * Only one item can be selected.
877 * _class : class attribute used on the label tag
878 * _text : Text to display before the menu. Not escaped.
879 * Other attributes are passed to buildAttributes() for the input tag.
881 * @author Tom N Harris <tnharris@whoopdedo.org>
883 function form_menufield($attrs) {
884 $attrs['size'] = '1';
886 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
887 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
888 $s .= '><span>'.$attrs['_text'].'</span>';
889 $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF;
890 if (!empty($attrs['_options'])) {
893 $cnt = count($attrs['_options']);
894 for($n=0; $n < $cnt; $n++){
895 @list($value,$text,$select) = $attrs['_options'][$n];
898 $p .= ' value="'.formText($value).'"';
901 if (!empty($select) && !$selected) {
902 $p .= ' selected="selected"';
905 $s .= '<option'.$p.'>'.formText($text).'</option>';
908 $s .= '<option></option>';
910 $s .= DOKU_LF.'</select></label>';
911 if (preg_match('/(^| )block($| )/', $attrs['_class']))
919 * Print the HTML for a list box.
920 * _options : Array of (value,text,selected) for the list.
921 * Text can be omitted. Text and value are passed to formText()
922 * _class : class attribute used on the label tag
923 * _text : Text to display before the menu. Not escaped.
924 * Other attributes are passed to buildAttributes() for the input tag.
926 * @author Tom N Harris <tnharris@whoopdedo.org>
928 function form_listboxfield($attrs) {
930 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
931 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
932 $s .= '><span>'.$attrs['_text'].'</span> ';
933 $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF;
934 if (!empty($attrs['_options'])) {
935 foreach ($attrs['_options'] as $opt) {
936 @list($value,$text,$select) = $opt;
938 if(is_null($text)) $text = $value;
939 $p .= ' value="'.formText($value).'"';
940 if (!empty($select)) $p .= ' selected="selected"';
941 $s .= '<option'.$p.'>'.formText($text).'</option>';
944 $s .= '<option></option>';
946 $s .= DOKU_LF.'</select></label>';
947 if (preg_match('/(^| )block($| )/', $attrs['_class']))