first commit. dokuwiki.
[h2N7SspZmY.git] / inc / form.php
bloba514526b7691a5657cba9d50990b821e81b4348a
1 <?php
2 /**
3 * DokuWiki XHTML Form
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Tom N Harris <tnharris@whoopdedo.org>
7 */
9 if(!defined('DOKU_INC')) die('meh.');
10 require_once(DOKU_INC.'inc/html.php');
12 /**
13 * Class for creating simple HTML forms.
15 * The forms is built from a list of pseudo-tags (arrays with expected keys).
16 * Every pseudo-tag must have the key '_elem' set to the name of the element.
17 * When printed, the form class calls functions named 'form_$type' for each
18 * element it contains.
20 * Standard practice is for non-attribute keys in a pseudo-element to start
21 * with '_'. Other keys are HTML attributes that will be included in the element
22 * tag. That way, the element output functions can pass the pseudo-element
23 * directly to buildAttributes.
25 * See the form_make* functions later in this file.
27 * @author Tom N Harris <tnharris@whoopdedo.org>
29 class Doku_Form {
31 // Form id attribute
32 var $params = array();
34 // Draw a border around form fields.
35 // Adds <fieldset></fieldset> around the elements
36 var $_infieldset = false;
38 // Hidden form fields.
39 var $_hidden = array();
41 // Array of pseudo-tags
42 var $_content = array();
44 /**
45 * Constructor
47 * Sets parameters and autoadds a security token. The old calling convention
48 * with up to four parameters is deprecated, instead the first parameter
49 * should be an array with parameters.
51 * @param mixed $params Parameters for the HTML form element; Using the
52 * deprecated calling convention this is the ID
53 * attribute of the form
54 * @param string $action (optional, deprecated) submit URL, defaults to
55 * current page
56 * @param string $method (optional, deprecated) 'POST' or 'GET', default
57 * is POST
58 * @param string $enctype (optional, deprecated) Encoding type of the
59 * data
60 * @author Tom N Harris <tnharris@whoopdedo.org>
62 function Doku_Form($params, $action=false, $method=false, $enctype=false) {
63 if(!is_array($params)) {
64 $this->params = array('id' => $params);
65 if ($action !== false) $this->params['action'] = $action;
66 if ($method !== false) $this->params['method'] = strtolower($method);
67 if ($enctype !== false) $this->params['enctype'] = $enctype;
68 } else {
69 $this->params = $params;
72 if (!isset($this->params['method'])) {
73 $this->params['method'] = 'post';
74 } else {
75 $this->params['method'] = strtolower($this->params['method']);
78 if (!isset($this->params['action'])) {
79 $this->params['action'] = '';
82 $this->addHidden('sectok', getSecurityToken());
85 /**
86 * startFieldset
88 * Add <fieldset></fieldset> tags around fields.
89 * Usually results in a border drawn around the form.
91 * @param string $legend Label that will be printed with the border.
92 * @author Tom N Harris <tnharris@whoopdedo.org>
94 function startFieldset($legend) {
95 if ($this->_infieldset) {
96 $this->addElement(array('_elem'=>'closefieldset'));
98 $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend));
99 $this->_infieldset = true;
103 * endFieldset
105 * @author Tom N Harris <tnharris@whoopdedo.org>
107 function endFieldset() {
108 if ($this->_infieldset) {
109 $this->addElement(array('_elem'=>'closefieldset'));
111 $this->_infieldset = false;
115 * addHidden
117 * Adds a name/value pair as a hidden field.
118 * The value of the field (but not the name) will be passed to
119 * formText() before printing.
121 * @param string $name Field name.
122 * @param string $value Field value. If null, remove a previously added field.
123 * @author Tom N Harris <tnharris@whoopdedo.org>
125 function addHidden($name, $value) {
126 if (is_null($value))
127 unset($this->_hidden[$name]);
128 else
129 $this->_hidden[$name] = $value;
133 * addElement
135 * Appends a content element to the form.
136 * The element can be either a pseudo-tag or string.
137 * If string, it is printed without escaping special chars. *
139 * @param string $elem Pseudo-tag or string to add to the form.
140 * @author Tom N Harris <tnharris@whoopdedo.org>
142 function addElement($elem) {
143 $this->_content[] = $elem;
147 * insertElement
149 * Inserts a content element at a position.
151 * @param string $pos 0-based index where the element will be inserted.
152 * @param string $elem Pseudo-tag or string to add to the form.
153 * @author Tom N Harris <tnharris@whoopdedo.org>
155 function insertElement($pos, $elem) {
156 array_splice($this->_content, $pos, 0, array($elem));
160 * replaceElement
162 * Replace with NULL to remove an element.
164 * @param int $pos 0-based index the element will be placed at.
165 * @param string $elem Pseudo-tag or string to add to the form.
166 * @author Tom N Harris <tnharris@whoopdedo.org>
168 function replaceElement($pos, $elem) {
169 $rep = array();
170 if (!is_null($elem)) $rep[] = $elem;
171 array_splice($this->_content, $pos, 1, $rep);
175 * findElementByType
177 * Gets the position of the first of a type of element.
179 * @param string $type Element type to look for.
180 * @return array pseudo-element if found, false otherwise
181 * @author Tom N Harris <tnharris@whoopdedo.org>
183 function findElementByType($type) {
184 foreach ($this->_content as $pos=>$elem) {
185 if (is_array($elem) && $elem['_elem'] == $type)
186 return $pos;
188 return false;
192 * findElementById
194 * Gets the position of the element with an ID attribute.
196 * @param string $id ID of the element to find.
197 * @return array pseudo-element if found, false otherwise
198 * @author Tom N Harris <tnharris@whoopdedo.org>
200 function findElementById($id) {
201 foreach ($this->_content as $pos=>$elem) {
202 if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id)
203 return $pos;
205 return false;
209 * findElementByAttribute
211 * Gets the position of the first element with a matching attribute value.
213 * @param string $name Attribute name.
214 * @param string $value Attribute value.
215 * @return array pseudo-element if found, false otherwise
216 * @author Tom N Harris <tnharris@whoopdedo.org>
218 function findElementByAttribute($name, $value) {
219 foreach ($this->_content as $pos=>$elem) {
220 if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value)
221 return $pos;
223 return false;
227 * getElementAt
229 * Returns a reference to the element at a position.
230 * A position out-of-bounds will return either the
231 * first (underflow) or last (overflow) element.
233 * @param int $pos 0-based index
234 * @return arrayreference pseudo-element
235 * @author Tom N Harris <tnharris@whoopdedo.org>
237 function &getElementAt($pos) {
238 if ($pos < 0) $pos = count($this->_content) + $pos;
239 if ($pos < 0) $pos = 0;
240 if ($pos >= count($this->_content)) $pos = count($this->_content) - 1;
241 return $this->_content[$pos];
245 * Return the assembled HTML for the form.
247 * Each element in the form will be passed to a function named
248 * 'form_$type'. The function should return the HTML to be printed.
250 * @author Tom N Harris <tnharris@whoopdedo.org>
252 function getForm() {
253 global $lang;
254 $form = '';
255 $this->params['accept-charset'] = $lang['encoding'];
256 $form .= '<form ' . html_attbuild($this->params) . '><div class="no">' . DOKU_LF;
257 if (!empty($this->_hidden)) {
258 foreach ($this->_hidden as $name=>$value)
259 $form .= form_hidden(array('name'=>$name, 'value'=>$value));
261 foreach ($this->_content as $element) {
262 if (is_array($element)) {
263 $elem_type = $element['_elem'];
264 if (function_exists('form_'.$elem_type)) {
265 $form .= call_user_func('form_'.$elem_type, $element).DOKU_LF;
267 } else {
268 $form .= $element;
271 if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF;
272 $form .= '</div></form>'.DOKU_LF;
274 return $form;
278 * Print the assembled form
280 * wraps around getForm()
282 function printForm(){
283 echo $this->getForm();
289 * form_makeTag
291 * Create a form element for a non-specific empty tag.
293 * @param string $tag Tag name.
294 * @param array $attrs Optional attributes.
295 * @return array pseudo-tag
296 * @author Tom N Harris <tnharris@whoopdedo.org>
298 function form_makeTag($tag, $attrs=array()) {
299 $elem = array('_elem'=>'tag', '_tag'=>$tag);
300 return array_merge($elem, $attrs);
304 * form_makeOpenTag
306 * Create a form element for a non-specific opening tag.
307 * Remember to put a matching close tag after this as well.
309 * @param string $tag Tag name.
310 * @param array $attrs Optional attributes.
311 * @return array pseudo-tag
312 * @author Tom N Harris <tnharris@whoopdedo.org>
314 function form_makeOpenTag($tag, $attrs=array()) {
315 $elem = array('_elem'=>'opentag', '_tag'=>$tag);
316 return array_merge($elem, $attrs);
320 * form_makeCloseTag
322 * Create a form element for a non-specific closing tag.
323 * Careless use of this will result in invalid XHTML.
325 * @param string $tag Tag name.
326 * @return array pseudo-tag
327 * @author Tom N Harris <tnharris@whoopdedo.org>
329 function form_makeCloseTag($tag) {
330 return array('_elem'=>'closetag', '_tag'=>$tag);
334 * form_makeWikiText
336 * Create a form element for a textarea containing wiki text.
337 * Only one wikitext element is allowed on a page. It will have
338 * a name of 'wikitext' and id 'wiki__text'. The text will
339 * be passed to formText() before printing.
341 * @param string $text Text to fill the field with.
342 * @param array $attrs Optional attributes.
343 * @return array pseudo-tag
344 * @author Tom N Harris <tnharris@whoopdedo.org>
346 function form_makeWikiText($text, $attrs=array()) {
347 $elem = array('_elem'=>'wikitext', '_text'=>$text,
348 'class'=>'edit', 'cols'=>'80', 'rows'=>'10');
349 return array_merge($elem, $attrs);
353 * form_makeButton
355 * Create a form element for an action button.
356 * A title will automatically be generated using the value and
357 * accesskey attributes, unless you provide one.
359 * @param string $type Type attribute. 'submit' or 'cancel'
360 * @param string $act Wiki action of the button, will be used as the do= parameter
361 * @param string $value (optional) Displayed label. Uses $act if not provided.
362 * @param array $attrs Optional attributes.
363 * @return array pseudo-tag
364 * @author Tom N Harris <tnharris@whoopdedo.org>
366 function form_makeButton($type, $act, $value='', $attrs=array()) {
367 if ($value == '') $value = $act;
368 $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act,
369 'value'=>$value, 'class'=>'button');
370 if (!empty($attrs['accesskey']) && empty($attrs['title'])) {
371 $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']';
373 return array_merge($elem, $attrs);
377 * form_makeField
379 * Create a form element for a labelled input element.
380 * The label text will be printed before the input.
382 * @param string $type Type attribute of input.
383 * @param string $name Name attribute of the input.
384 * @param string $value (optional) Default value.
385 * @param string $class Class attribute of the label. If this is 'block',
386 * then a line break will be added after the field.
387 * @param string $label Label that will be printed before the input.
388 * @param string $id ID attribute of the input. If set, the label will
389 * reference it with a 'for' attribute.
390 * @param array $attrs Optional attributes.
391 * @return array pseudo-tag
392 * @author Tom N Harris <tnharris@whoopdedo.org>
394 function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
395 if (is_null($label)) $label = $name;
396 $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class,
397 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
398 return array_merge($elem, $attrs);
402 * form_makeFieldRight
404 * Create a form element for a labelled input element.
405 * The label text will be printed after the input.
407 * @see form_makeField
408 * @author Tom N Harris <tnharris@whoopdedo.org>
410 function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) {
411 if (is_null($label)) $label = $name;
412 $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class,
413 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value);
414 return array_merge($elem, $attrs);
418 * form_makeTextField
420 * Create a form element for a text input element with label.
422 * @see form_makeField
423 * @author Tom N Harris <tnharris@whoopdedo.org>
425 function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) {
426 if (is_null($label)) $label = $name;
427 $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class,
428 'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit');
429 return array_merge($elem, $attrs);
433 * form_makePasswordField
435 * Create a form element for a password input element with label.
436 * Password elements have no default value, for obvious reasons.
438 * @see form_makeField
439 * @author Tom N Harris <tnharris@whoopdedo.org>
441 function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) {
442 if (is_null($label)) $label = $name;
443 $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class,
444 'id'=>$id, 'name'=>$name, 'class'=>'edit');
445 return array_merge($elem, $attrs);
449 * form_makeFileField
451 * Create a form element for a file input element with label
453 * @see form_makeField
454 * @author Michael Klier <chi@chimeric.de>
456 function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) {
457 if (is_null($label)) $label = $name;
458 $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class,
459 'id'=>$id, 'name'=>$name, 'class'=>'edit');
460 return array_merge($elem, $attrs);
464 * form_makeCheckboxField
466 * Create a form element for a checkbox input element with label.
468 * @see form_makeFieldRight
469 * @author Tom N Harris <tnharris@whoopdedo.org>
471 function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
472 if (is_null($label)) $label = $name;
473 if (is_null($value) || $value=='') $value='0';
474 $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
475 'id'=>$id, 'name'=>$name, 'value'=>$value);
476 return array_merge($elem, $attrs);
480 * form_makeRadioField
482 * Create a form element for a radio button input element with label.
484 * @see form_makeFieldRight
485 * @author Tom N Harris <tnharris@whoopdedo.org>
487 function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
488 if (is_null($label)) $label = $name;
489 if (is_null($value) || $value=='') $value='0';
490 $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
491 'id'=>$id, 'name'=>$name, 'value'=>$value);
492 return array_merge($elem, $attrs);
496 * form_makeMenuField
498 * Create a form element for a drop-down menu with label.
499 * The list of values can be strings, arrays of (value,text),
500 * or an associative array with the values as keys and labels as values.
501 * An item is selected by supplying its value or integer index.
502 * If the list of values is an associative array, the selected item must be
503 * a string.
505 * @author Tom N Harris <tnharris@whoopdedo.org>
507 function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
508 if (is_null($label)) $label = $name;
509 $options = array();
510 reset($values);
511 // FIXME: php doesn't know the difference between a string and an integer
512 if (is_string(key($values))) {
513 foreach ($values as $val=>$text) {
514 $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
516 } else {
517 if (is_integer($selected)) $selected = $values[$selected];
518 foreach ($values as $val) {
519 if (is_array($val))
520 @list($val,$text) = $val;
521 else
522 $text = null;
523 $options[] = array($val,$text,$val===$selected);
526 $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
527 'id'=>$id, 'name'=>$name);
528 return array_merge($elem, $attrs);
532 * form_makeListboxField
534 * Create a form element for a list box with label.
535 * The list of values can be strings, arrays of (value,text),
536 * or an associative array with the values as keys and labels as values.
537 * Items are selected by supplying its value or an array of values.
539 * @author Tom N Harris <tnharris@whoopdedo.org>
541 function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
542 if (is_null($label)) $label = $name;
543 $options = array();
544 reset($values);
545 if (is_null($selected) || $selected == '')
546 $selected = array();
547 elseif (!is_array($selected))
548 $selected = array($selected);
549 // FIXME: php doesn't know the difference between a string and an integer
550 if (is_string(key($values))) {
551 foreach ($values as $val=>$text) {
552 $options[] = array($val,$text,in_array($val,$selected));
554 } else {
555 foreach ($values as $val) {
556 if (is_array($val))
557 @list($val,$text) = $val;
558 else
559 $text = null;
560 $options[] = array($val,$text,in_array($val,$selected));
563 $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
564 'id'=>$id, 'name'=>$name);
565 return array_merge($elem, $attrs);
569 * form_tag
571 * Print the HTML for a generic empty tag.
572 * Requires '_tag' key with name of the tag.
573 * Attributes are passed to buildAttributes()
575 * @author Tom N Harris <tnharris@whoopdedo.org>
577 function form_tag($attrs) {
578 return '<'.$attrs['_tag'].' '.buildAttributes($attrs).'/>';
582 * form_opentag
584 * Print the HTML for a generic opening tag.
585 * Requires '_tag' key with name of the tag.
586 * Attributes are passed to buildAttributes()
588 * @author Tom N Harris <tnharris@whoopdedo.org>
590 function form_opentag($attrs) {
591 return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
595 * form_closetag
597 * Print the HTML for a generic closing tag.
598 * Requires '_tag' key with name of the tag.
599 * There are no attributes.
601 * @author Tom N Harris <tnharris@whoopdedo.org>
603 function form_closetag($attrs) {
604 return '</'.$attrs['_tag'].'>';
608 * form_openfieldset
610 * Print the HTML for an opening fieldset tag.
611 * Uses the '_legend' key.
612 * Attributes are passed to buildAttributes()
614 * @author Tom N Harris <tnharris@whoopdedo.org>
616 function form_openfieldset($attrs) {
617 $s = '<fieldset '.buildAttributes($attrs,true).'>';
618 if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
619 return $s;
623 * form_closefieldset
625 * Print the HTML for a closing fieldset tag.
626 * There are no attributes.
628 * @author Tom N Harris <tnharris@whoopdedo.org>
630 function form_closefieldset() {
631 return '</fieldset>';
635 * form_hidden
637 * Print the HTML for a hidden input element.
638 * Uses only 'name' and 'value' attributes.
639 * Value is passed to formText()
641 * @author Tom N Harris <tnharris@whoopdedo.org>
643 function form_hidden($attrs) {
644 return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
648 * form_wikitext
650 * Print the HTML for the wiki textarea.
651 * Requires '_text' with default text of the field.
652 * Text will be passed to formText(), attributes to buildAttributes()
654 * @author Tom N Harris <tnharris@whoopdedo.org>
656 function form_wikitext($attrs) {
657 // mandatory attributes
658 unset($attrs['name']);
659 unset($attrs['id']);
660 return '<textarea name="wikitext" id="wiki__text" '
661 .buildAttributes($attrs,true).'>'.DOKU_LF
662 .formText($attrs['_text'])
663 .'</textarea>';
667 * form_button
669 * Print the HTML for a form button.
670 * If '_action' is set, the button name will be "do[_action]".
671 * Other attributes are passed to buildAttributes()
673 * @author Tom N Harris <tnharris@whoopdedo.org>
675 function form_button($attrs) {
676 $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
677 return '<input '.$p.buildAttributes($attrs,true).'/>';
681 * form_field
683 * Print the HTML for a form input field.
684 * _class : class attribute used on the label tag
685 * _text : Text to display before the input. Not escaped.
686 * Other attributes are passed to buildAttributes() for the input tag.
688 * @author Tom N Harris <tnharris@whoopdedo.org>
690 function form_field($attrs) {
691 $s = '<label';
692 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
693 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
694 $s .= '><span>'.$attrs['_text'].'</span>';
695 $s .= ' <input '.buildAttributes($attrs,true).'/></label>';
696 if (preg_match('/(^| )block($| )/', $attrs['_class']))
697 $s .= '<br />';
698 return $s;
702 * form_fieldright
704 * Print the HTML for a form input field. (right-aligned)
705 * _class : class attribute used on the label tag
706 * _text : Text to display after the input. Not escaped.
707 * Other attributes are passed to buildAttributes() for the input tag.
709 * @author Tom N Harris <tnharris@whoopdedo.org>
711 function form_fieldright($attrs) {
712 $s = '<label';
713 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
714 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
715 $s .= '><input '.buildAttributes($attrs,true).'/>';
716 $s .= ' <span>'.$attrs['_text'].'</span></label>';
717 if (preg_match('/(^| )block($| )/', $attrs['_class']))
718 $s .= '<br />';
719 return $s;
723 * form_textfield
725 * Print the HTML for a text input field.
726 * _class : class attribute used on the label tag
727 * _text : Text to display before the input. Not escaped.
728 * Other attributes are passed to buildAttributes() for the input tag.
730 * @author Tom N Harris <tnharris@whoopdedo.org>
732 function form_textfield($attrs) {
733 // mandatory attributes
734 unset($attrs['type']);
735 $s = '<label';
736 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
737 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
738 $s .= '><span>'.$attrs['_text'].'</span> ';
739 $s .= '<input type="text" '.buildAttributes($attrs,true).'/></label>';
740 if (preg_match('/(^| )block($| )/', $attrs['_class']))
741 $s .= '<br />';
742 return $s;
746 * form_passwordfield
748 * Print the HTML for a password input field.
749 * _class : class attribute used on the label tag
750 * _text : Text to display before the input. Not escaped.
751 * Other attributes are passed to buildAttributes() for the input tag.
753 * @author Tom N Harris <tnharris@whoopdedo.org>
755 function form_passwordfield($attrs) {
756 // mandatory attributes
757 unset($attrs['type']);
758 $s = '<label';
759 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
760 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
761 $s .= '><span>'.$attrs['_text'].'</span> ';
762 $s .= '<input type="password" '.buildAttributes($attrs,true).'/></label>';
763 if (preg_match('/(^| )block($| )/', $attrs['_class']))
764 $s .= '<br />';
765 return $s;
769 * form_filefield
771 * Print the HTML for a file input field.
772 * _class : class attribute used on the label tag
773 * _text : Text to display before the input. Not escaped
774 * _maxlength : Allowed size in byte
775 * _accept : Accepted mime-type
776 * Other attributes are passed to buildAttributes() for the input tag
778 * @author Michael Klier <chi@chimeric.de>
780 function form_filefield($attrs) {
781 $s = '<label';
782 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
783 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
784 $s .= '><span>'.$attrs['_text'].'</span> ';
785 $s .= '<input type="file" '.buildAttributes($attrs,true);
786 if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"';
787 if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"';
788 $s .= '/></label>';
789 if (preg_match('/(^| )block($| )/', $attrs['_class']))
790 $s .= '<br />';
791 return $s;
795 * form_checkboxfield
797 * Print the HTML for a checkbox input field.
798 * _class : class attribute used on the label tag
799 * _text : Text to display after the input. Not escaped.
800 * Other attributes are passed to buildAttributes() for the input tag.
802 * @author Tom N Harris <tnharris@whoopdedo.org>
804 function form_checkboxfield($attrs) {
805 // mandatory attributes
806 unset($attrs['type']);
807 $s = '<label';
808 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
809 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
810 $s .= '><input type="checkbox" '.buildAttributes($attrs,true).'/>';
811 $s .= ' <span>'.$attrs['_text'].'</span></label>';
812 if (preg_match('/(^| )block($| )/', $attrs['_class']))
813 $s .= '<br />';
814 return $s;
818 * form_radiofield
820 * Print the HTML for a radio button input field.
821 * _class : class attribute used on the label tag
822 * _text : Text to display after the input. Not escaped.
823 * Other attributes are passed to buildAttributes() for the input tag.
825 * @author Tom N Harris <tnharris@whoopdedo.org>
827 function form_radiofield($attrs) {
828 // mandatory attributes
829 unset($attrs['type']);
830 $s = '<label';
831 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
832 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
833 $s .= '><input type="radio" '.buildAttributes($attrs,true).'/>';
834 $s .= ' <span>'.$attrs['_text'].'</span></label>';
835 if (preg_match('/(^| )block($| )/', $attrs['_class']))
836 $s .= '<br />';
837 return $s;
841 * form_menufield
843 * Print the HTML for a drop-down menu.
844 * _options : Array of (value,text,selected) for the menu.
845 * Text can be omitted. Text and value are passed to formText()
846 * Only one item can be selected.
847 * _class : class attribute used on the label tag
848 * _text : Text to display before the menu. Not escaped.
849 * Other attributes are passed to buildAttributes() for the input tag.
851 * @author Tom N Harris <tnharris@whoopdedo.org>
853 function form_menufield($attrs) {
854 $attrs['size'] = '1';
855 $s = '<label';
856 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
857 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
858 $s .= '><span>'.$attrs['_text'].'</span>';
859 $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF;
860 if (!empty($attrs['_options'])) {
861 $selected = false;
862 for($n=0;$n<count($attrs['_options']);$n++){
863 @list($value,$text,$select) = $attrs['_options'][$n];
864 $p = '';
865 if (!is_null($text))
866 $p .= ' value="'.formText($value).'"';
867 else
868 $text = $value;
869 if (!empty($select) && !$selected) {
870 $p .= ' selected="selected"';
871 $selected = true;
873 $s .= '<option'.$p.'>'.formText($text).'</option>';
875 } else {
876 $s .= '<option></option>';
878 $s .= DOKU_LF.'</select></label>';
879 if (preg_match('/(^| )block($| )/', $attrs['_class']))
880 $s .= '<br />';
881 return $s;
885 * form_listboxfield
887 * Print the HTML for a list box.
888 * _options : Array of (value,text,selected) for the list.
889 * Text can be omitted. Text and value are passed to formText()
890 * _class : class attribute used on the label tag
891 * _text : Text to display before the menu. Not escaped.
892 * Other attributes are passed to buildAttributes() for the input tag.
894 * @author Tom N Harris <tnharris@whoopdedo.org>
896 function form_listboxfield($attrs) {
897 $s = '<label';
898 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
899 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
900 $s .= '><span>'.$attrs['_text'].'</span> ';
901 $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF;
902 if (!empty($attrs['_options'])) {
903 foreach ($attrs['_options'] as $opt) {
904 @list($value,$text,$select) = $opt;
905 $p = '';
906 if(is_null($text)) $text = $value;
907 $p .= ' value="'.formText($value).'"';
908 if (!empty($select)) $p .= ' selected="selected"';
909 $s .= '<option'.$p.'>'.formText($text).'</option>';
911 } else {
912 $s .= '<option></option>';
914 $s .= DOKU_LF.'</select></label>';
915 if (preg_match('/(^| )block($| )/', $attrs['_class']))
916 $s .= '<br />';
917 return $s;