1<?php 2/** 3 * DokuWiki XHTML Form 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Tom N Harris <tnharris@whoopdedo.org> 7 */ 8 9if(!defined('DOKU_INC')) die('meh.'); 10require_once(DOKU_INC.'inc/html.php'); 11 12/** 13 * Class for creating simple HTML forms. 14 * 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. 19 * 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. 24 * 25 * See the form_make* functions later in this file. 26 * 27 * @author Tom N Harris <tnharris@whoopdedo.org> 28 */ 29class Doku_Form { 30 31 // Form id attribute 32 var $params = array(); 33 34 // Draw a border around form fields. 35 // Adds <fieldset></fieldset> around the elements 36 var $_infieldset = false; 37 38 // Hidden form fields. 39 var $_hidden = array(); 40 41 // Array of pseudo-tags 42 var $_content = array(); 43 44 /** 45 * Constructor 46 * 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. 50 * 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> 61 */ 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; 70 } 71 72 if (!isset($this->params['method'])) { 73 $this->params['method'] = 'post'; 74 } else { 75 $this->params['method'] = strtolower($this->params['method']); 76 } 77 78 if (!isset($this->params['action'])) { 79 $this->params['action'] = ''; 80 } 81 82 $this->addHidden('sectok', getSecurityToken()); 83 } 84 85 /** 86 * startFieldset 87 * 88 * Add <fieldset></fieldset> tags around fields. 89 * Usually results in a border drawn around the form. 90 * 91 * @param string $legend Label that will be printed with the border. 92 * @author Tom N Harris <tnharris@whoopdedo.org> 93 */ 94 function startFieldset($legend) { 95 if ($this->_infieldset) { 96 $this->addElement(array('_elem'=>'closefieldset')); 97 } 98 $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend)); 99 $this->_infieldset = true; 100 } 101 102 /** 103 * endFieldset 104 * 105 * @author Tom N Harris <tnharris@whoopdedo.org> 106 */ 107 function endFieldset() { 108 if ($this->_infieldset) { 109 $this->addElement(array('_elem'=>'closefieldset')); 110 } 111 $this->_infieldset = false; 112 } 113 114 /** 115 * addHidden 116 * 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. 120 * 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> 124 */ 125 function addHidden($name, $value) { 126 if (is_null($value)) 127 unset($this->_hidden[$name]); 128 else 129 $this->_hidden[$name] = $value; 130 } 131 132 /** 133 * addElement 134 * 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. * 138 * 139 * @param string $elem Pseudo-tag or string to add to the form. 140 * @author Tom N Harris <tnharris@whoopdedo.org> 141 */ 142 function addElement($elem) { 143 $this->_content[] = $elem; 144 } 145 146 /** 147 * insertElement 148 * 149 * Inserts a content element at a position. 150 * 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> 154 */ 155 function insertElement($pos, $elem) { 156 array_splice($this->_content, $pos, 0, array($elem)); 157 } 158 159 /** 160 * replaceElement 161 * 162 * Replace with NULL to remove an element. 163 * 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> 167 */ 168 function replaceElement($pos, $elem) { 169 $rep = array(); 170 if (!is_null($elem)) $rep[] = $elem; 171 array_splice($this->_content, $pos, 1, $rep); 172 } 173 174 /** 175 * findElementByType 176 * 177 * Gets the position of the first of a type of element. 178 * 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> 182 */ 183 function findElementByType($type) { 184 foreach ($this->_content as $pos=>$elem) { 185 if (is_array($elem) && $elem['_elem'] == $type) 186 return $pos; 187 } 188 return false; 189 } 190 191 /** 192 * findElementById 193 * 194 * Gets the position of the element with an ID attribute. 195 * 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> 199 */ 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; 204 } 205 return false; 206 } 207 208 /** 209 * findElementByAttribute 210 * 211 * Gets the position of the first element with a matching attribute value. 212 * 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> 217 */ 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; 222 } 223 return false; 224 } 225 226 /** 227 * getElementAt 228 * 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. 232 * 233 * @param int $pos 0-based index 234 * @return arrayreference pseudo-element 235 * @author Tom N Harris <tnharris@whoopdedo.org> 236 */ 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]; 242 } 243 244 /** 245 * printForm 246 * 247 * Output the form. 248 * Each element in the form will be passed to a function named 249 * 'form_$type'. The function should return the HTML to be printed. 250 * 251 * @author Tom N Harris <tnharris@whoopdedo.org> 252 */ 253 function printForm() { 254 global $lang; 255 $this->params['accept-charset'] = $lang['encoding']; 256 print '<form ' . html_attbuild($this->params) . '><div class="no">' . DOKU_LF; 257 if (!empty($this->_hidden)) { 258 foreach ($this->_hidden as $name=>$value) 259 print form_hidden(array('name'=>$name, 'value'=>$value)); 260 } 261 foreach ($this->_content as $element) { 262 if (is_array($element)) { 263 $elem_type = $element['_elem']; 264 if (function_exists('form_'.$elem_type)) { 265 print call_user_func('form_'.$elem_type, $element).DOKU_LF; 266 } 267 } else { 268 print $element; 269 } 270 } 271 if ($this->_infieldset) print form_closefieldset().DOKU_LF; 272 print '</div></form>'.DOKU_LF; 273 } 274 275} 276 277/** 278 * form_makeTag 279 * 280 * Create a form element for a non-specific empty tag. 281 * 282 * @param string $tag Tag name. 283 * @param array $attrs Optional attributes. 284 * @return array pseudo-tag 285 * @author Tom N Harris <tnharris@whoopdedo.org> 286 */ 287function form_makeTag($tag, $attrs=array()) { 288 $elem = array('_elem'=>'tag', '_tag'=>$tag); 289 return array_merge($elem, $attrs); 290} 291 292/** 293 * form_makeOpenTag 294 * 295 * Create a form element for a non-specific opening tag. 296 * Remember to put a matching close tag after this as well. 297 * 298 * @param string $tag Tag name. 299 * @param array $attrs Optional attributes. 300 * @return array pseudo-tag 301 * @author Tom N Harris <tnharris@whoopdedo.org> 302 */ 303function form_makeOpenTag($tag, $attrs=array()) { 304 $elem = array('_elem'=>'opentag', '_tag'=>$tag); 305 return array_merge($elem, $attrs); 306} 307 308/** 309 * form_makeCloseTag 310 * 311 * Create a form element for a non-specific closing tag. 312 * Careless use of this will result in invalid XHTML. 313 * 314 * @param string $tag Tag name. 315 * @return array pseudo-tag 316 * @author Tom N Harris <tnharris@whoopdedo.org> 317 */ 318function form_makeCloseTag($tag) { 319 return array('_elem'=>'closetag', '_tag'=>$tag); 320} 321 322/** 323 * form_makeWikiText 324 * 325 * Create a form element for a textarea containing wiki text. 326 * Only one wikitext element is allowed on a page. It will have 327 * a name of 'wikitext' and id 'wiki__text'. The text will 328 * be passed to formText() before printing. 329 * 330 * @param string $text Text to fill the field with. 331 * @param array $attrs Optional attributes. 332 * @return array pseudo-tag 333 * @author Tom N Harris <tnharris@whoopdedo.org> 334 */ 335function form_makeWikiText($text, $attrs=array()) { 336 $elem = array('_elem'=>'wikitext', '_text'=>$text, 337 'class'=>'edit', 'cols'=>'80', 'rows'=>'10'); 338 return array_merge($elem, $attrs); 339} 340 341/** 342 * form_makeButton 343 * 344 * Create a form element for an action button. 345 * A title will automatically be generated using the value and 346 * accesskey attributes, unless you provide one. 347 * 348 * @param string $type Type attribute. 'submit' or 'cancel' 349 * @param string $act Wiki action of the button, will be used as the do= parameter 350 * @param string $value (optional) Displayed label. Uses $act if not provided. 351 * @param array $attrs Optional attributes. 352 * @return array pseudo-tag 353 * @author Tom N Harris <tnharris@whoopdedo.org> 354 */ 355function form_makeButton($type, $act, $value='', $attrs=array()) { 356 if ($value == '') $value = $act; 357 $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act, 358 'value'=>$value, 'class'=>'button'); 359 if (!empty($attrs['accesskey']) && empty($attrs['title'])) { 360 $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']'; 361 } 362 return array_merge($elem, $attrs); 363} 364 365/** 366 * form_makeField 367 * 368 * Create a form element for a labelled input element. 369 * The label text will be printed before the input. 370 * 371 * @param string $type Type attribute of input. 372 * @param string $name Name attribute of the input. 373 * @param string $value (optional) Default value. 374 * @param string $class Class attribute of the label. If this is 'block', 375 * then a line break will be added after the field. 376 * @param string $label Label that will be printed before the input. 377 * @param string $id ID attribute of the input. If set, the label will 378 * reference it with a 'for' attribute. 379 * @param array $attrs Optional attributes. 380 * @return array pseudo-tag 381 * @author Tom N Harris <tnharris@whoopdedo.org> 382 */ 383function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) { 384 if (is_null($label)) $label = $name; 385 $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class, 386 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value); 387 return array_merge($elem, $attrs); 388} 389 390/** 391 * form_makeFieldRight 392 * 393 * Create a form element for a labelled input element. 394 * The label text will be printed after the input. 395 * 396 * @see form_makeField 397 * @author Tom N Harris <tnharris@whoopdedo.org> 398 */ 399function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) { 400 if (is_null($label)) $label = $name; 401 $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class, 402 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value); 403 return array_merge($elem, $attrs); 404} 405 406/** 407 * form_makeTextField 408 * 409 * Create a form element for a text input element with label. 410 * 411 * @see form_makeField 412 * @author Tom N Harris <tnharris@whoopdedo.org> 413 */ 414function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) { 415 if (is_null($label)) $label = $name; 416 $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class, 417 'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit'); 418 return array_merge($elem, $attrs); 419} 420 421/** 422 * form_makePasswordField 423 * 424 * Create a form element for a password input element with label. 425 * Password elements have no default value, for obvious reasons. 426 * 427 * @see form_makeField 428 * @author Tom N Harris <tnharris@whoopdedo.org> 429 */ 430function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) { 431 if (is_null($label)) $label = $name; 432 $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class, 433 'id'=>$id, 'name'=>$name, 'class'=>'edit'); 434 return array_merge($elem, $attrs); 435} 436 437/** 438 * form_makeFileField 439 * 440 * Create a form element for a file input element with label 441 * 442 * @see form_makeField 443 * @author Michael Klier <chi@chimeric.de> 444 */ 445function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) { 446 if (is_null($label)) $label = $name; 447 $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class, 448 'id'=>$id, 'name'=>$name, 'class'=>'edit'); 449 return array_merge($elem, $attrs); 450} 451 452/** 453 * form_makeCheckboxField 454 * 455 * Create a form element for a checkbox input element with label. 456 * 457 * @see form_makeFieldRight 458 * @author Tom N Harris <tnharris@whoopdedo.org> 459 */ 460function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) { 461 if (is_null($label)) $label = $name; 462 if (is_null($value) || $value=='') $value='0'; 463 $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class, 464 'id'=>$id, 'name'=>$name, 'value'=>$value); 465 return array_merge($elem, $attrs); 466} 467 468/** 469 * form_makeRadioField 470 * 471 * Create a form element for a radio button input element with label. 472 * 473 * @see form_makeFieldRight 474 * @author Tom N Harris <tnharris@whoopdedo.org> 475 */ 476function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) { 477 if (is_null($label)) $label = $name; 478 if (is_null($value) || $value=='') $value='0'; 479 $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class, 480 'id'=>$id, 'name'=>$name, 'value'=>$value); 481 return array_merge($elem, $attrs); 482} 483 484/** 485 * form_makeMenuField 486 * 487 * Create a form element for a drop-down menu with label. 488 * The list of values can be strings, arrays of (value,text), 489 * or an associative array with the values as keys and labels as values. 490 * An item is selected by supplying its value or integer index. 491 * If the list of values is an associative array, the selected item must be 492 * a string. 493 * 494 * @author Tom N Harris <tnharris@whoopdedo.org> 495 */ 496function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { 497 if (is_null($label)) $label = $name; 498 $options = array(); 499 reset($values); 500 // FIXME: php doesn't know the difference between a string and an integer 501 if (is_string(key($values))) { 502 foreach ($values as $val=>$text) { 503 $options[] = array($val,$text, (!is_null($selected) && $val==$selected)); 504 } 505 } else { 506 if (is_integer($selected)) $selected = $values[$selected]; 507 foreach ($values as $val) { 508 if (is_array($val)) 509 @list($val,$text) = $val; 510 else 511 $text = null; 512 $options[] = array($val,$text,$val===$selected); 513 } 514 } 515 $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class, 516 'id'=>$id, 'name'=>$name); 517 return array_merge($elem, $attrs); 518} 519 520/** 521 * form_makeListboxField 522 * 523 * Create a form element for a list box with label. 524 * The list of values can be strings, arrays of (value,text), 525 * or an associative array with the values as keys and labels as values. 526 * Items are selected by supplying its value or an array of values. 527 * 528 * @author Tom N Harris <tnharris@whoopdedo.org> 529 */ 530function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { 531 if (is_null($label)) $label = $name; 532 $options = array(); 533 reset($values); 534 if (is_null($selected) || $selected == '') 535 $selected = array(); 536 elseif (!is_array($selected)) 537 $selected = array($selected); 538 // FIXME: php doesn't know the difference between a string and an integer 539 if (is_string(key($values))) { 540 foreach ($values as $val=>$text) { 541 $options[] = array($val,$text,in_array($val,$selected)); 542 } 543 } else { 544 foreach ($values as $val) { 545 if (is_array($val)) 546 @list($val,$text) = $val; 547 else 548 $text = null; 549 $options[] = array($val,$text,in_array($val,$selected)); 550 } 551 } 552 $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class, 553 'id'=>$id, 'name'=>$name); 554 return array_merge($elem, $attrs); 555} 556 557/** 558 * form_tag 559 * 560 * Print the HTML for a generic empty tag. 561 * Requires '_tag' key with name of the tag. 562 * Attributes are passed to buildAttributes() 563 * 564 * @author Tom N Harris <tnharris@whoopdedo.org> 565 */ 566function form_tag($attrs) { 567 return '<'.$attrs['_tag'].' '.buildAttributes($attrs).'/>'; 568} 569 570/** 571 * form_opentag 572 * 573 * Print the HTML for a generic opening tag. 574 * Requires '_tag' key with name of the tag. 575 * Attributes are passed to buildAttributes() 576 * 577 * @author Tom N Harris <tnharris@whoopdedo.org> 578 */ 579function form_opentag($attrs) { 580 return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>'; 581} 582 583/** 584 * form_closetag 585 * 586 * Print the HTML for a generic closing tag. 587 * Requires '_tag' key with name of the tag. 588 * There are no attributes. 589 * 590 * @author Tom N Harris <tnharris@whoopdedo.org> 591 */ 592function form_closetag($attrs) { 593 return '</'.$attrs['_tag'].'>'; 594} 595 596/** 597 * form_openfieldset 598 * 599 * Print the HTML for an opening fieldset tag. 600 * Uses the '_legend' key. 601 * Attributes are passed to buildAttributes() 602 * 603 * @author Tom N Harris <tnharris@whoopdedo.org> 604 */ 605function form_openfieldset($attrs) { 606 $s = '<fieldset '.buildAttributes($attrs,true).'>'; 607 if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>'; 608 return $s; 609} 610 611/** 612 * form_closefieldset 613 * 614 * Print the HTML for a closing fieldset tag. 615 * There are no attributes. 616 * 617 * @author Tom N Harris <tnharris@whoopdedo.org> 618 */ 619function form_closefieldset() { 620 return '</fieldset>'; 621} 622 623/** 624 * form_hidden 625 * 626 * Print the HTML for a hidden input element. 627 * Uses only 'name' and 'value' attributes. 628 * Value is passed to formText() 629 * 630 * @author Tom N Harris <tnharris@whoopdedo.org> 631 */ 632function form_hidden($attrs) { 633 return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />'; 634} 635 636/** 637 * form_wikitext 638 * 639 * Print the HTML for the wiki textarea. 640 * Requires '_text' with default text of the field. 641 * Text will be passed to formText(), attributes to buildAttributes() 642 * 643 * @author Tom N Harris <tnharris@whoopdedo.org> 644 */ 645function form_wikitext($attrs) { 646 // mandatory attributes 647 unset($attrs['name']); 648 unset($attrs['id']); 649 return '<textarea name="wikitext" id="wiki__text" ' 650 .buildAttributes($attrs,true).'>'.DOKU_LF 651 .formText($attrs['_text']) 652 .'</textarea>'; 653} 654 655/** 656 * form_button 657 * 658 * Print the HTML for a form button. 659 * If '_action' is set, the button name will be "do[_action]". 660 * Other attributes are passed to buildAttributes() 661 * 662 * @author Tom N Harris <tnharris@whoopdedo.org> 663 */ 664function form_button($attrs) { 665 $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : ''; 666 return '<input '.$p.buildAttributes($attrs,true).'/>'; 667} 668 669/** 670 * form_field 671 * 672 * Print the HTML for a form input field. 673 * _class : class attribute used on the label tag 674 * _text : Text to display before the input. Not escaped. 675 * Other attributes are passed to buildAttributes() for the input tag. 676 * 677 * @author Tom N Harris <tnharris@whoopdedo.org> 678 */ 679function form_field($attrs) { 680 $s = '<label'; 681 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 682 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 683 $s .= '><span>'.$attrs['_text'].'</span>'; 684 $s .= ' <input '.buildAttributes($attrs,true).'/></label>'; 685 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 686 $s .= '<br />'; 687 return $s; 688} 689 690/** 691 * form_fieldright 692 * 693 * Print the HTML for a form input field. (right-aligned) 694 * _class : class attribute used on the label tag 695 * _text : Text to display after the input. Not escaped. 696 * Other attributes are passed to buildAttributes() for the input tag. 697 * 698 * @author Tom N Harris <tnharris@whoopdedo.org> 699 */ 700function form_fieldright($attrs) { 701 $s = '<label'; 702 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 703 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 704 $s .= '><input '.buildAttributes($attrs,true).'/>'; 705 $s .= ' <span>'.$attrs['_text'].'</span></label>'; 706 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 707 $s .= '<br />'; 708 return $s; 709} 710 711/** 712 * form_textfield 713 * 714 * Print the HTML for a text input field. 715 * _class : class attribute used on the label tag 716 * _text : Text to display before the input. Not escaped. 717 * Other attributes are passed to buildAttributes() for the input tag. 718 * 719 * @author Tom N Harris <tnharris@whoopdedo.org> 720 */ 721function form_textfield($attrs) { 722 // mandatory attributes 723 unset($attrs['type']); 724 $s = '<label'; 725 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 726 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 727 $s .= '><span>'.$attrs['_text'].'</span> '; 728 $s .= '<input type="text" '.buildAttributes($attrs,true).'/></label>'; 729 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 730 $s .= '<br />'; 731 return $s; 732} 733 734/** 735 * form_passwordfield 736 * 737 * Print the HTML for a password input field. 738 * _class : class attribute used on the label tag 739 * _text : Text to display before the input. Not escaped. 740 * Other attributes are passed to buildAttributes() for the input tag. 741 * 742 * @author Tom N Harris <tnharris@whoopdedo.org> 743 */ 744function form_passwordfield($attrs) { 745 // mandatory attributes 746 unset($attrs['type']); 747 $s = '<label'; 748 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 749 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 750 $s .= '><span>'.$attrs['_text'].'</span> '; 751 $s .= '<input type="password" '.buildAttributes($attrs,true).'/></label>'; 752 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 753 $s .= '<br />'; 754 return $s; 755} 756 757/** 758 * form_filefield 759 * 760 * Print the HTML for a file input field. 761 * _class : class attribute used on the label tag 762 * _text : Text to display before the input. Not escaped 763 * _maxlength : Allowed size in byte 764 * _accept : Accepted mime-type 765 * Other attributes are passed to buildAttributes() for the input tag 766 * 767 * @author Michael Klier <chi@chimeric.de> 768 */ 769function form_filefield($attrs) { 770 $s = '<label'; 771 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 772 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 773 $s .= '><span>'.$attrs['_text'].'</span> '; 774 $s .= '<input type="file" '.buildAttributes($attrs,true); 775 if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"'; 776 if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"'; 777 $s .= '/></label>'; 778 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 779 $s .= '<br />'; 780 return $s; 781} 782 783/** 784 * form_checkboxfield 785 * 786 * Print the HTML for a checkbox input field. 787 * _class : class attribute used on the label tag 788 * _text : Text to display after the input. Not escaped. 789 * Other attributes are passed to buildAttributes() for the input tag. 790 * 791 * @author Tom N Harris <tnharris@whoopdedo.org> 792 */ 793function form_checkboxfield($attrs) { 794 // mandatory attributes 795 unset($attrs['type']); 796 $s = '<label'; 797 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 798 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 799 $s .= '><input type="checkbox" '.buildAttributes($attrs,true).'/>'; 800 $s .= ' <span>'.$attrs['_text'].'</span></label>'; 801 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 802 $s .= '<br />'; 803 return $s; 804} 805 806/** 807 * form_radiofield 808 * 809 * Print the HTML for a radio button input field. 810 * _class : class attribute used on the label tag 811 * _text : Text to display after the input. Not escaped. 812 * Other attributes are passed to buildAttributes() for the input tag. 813 * 814 * @author Tom N Harris <tnharris@whoopdedo.org> 815 */ 816function form_radiofield($attrs) { 817 // mandatory attributes 818 unset($attrs['type']); 819 $s = '<label'; 820 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 821 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 822 $s .= '><input type="radio" '.buildAttributes($attrs,true).'/>'; 823 $s .= ' <span>'.$attrs['_text'].'</span></label>'; 824 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 825 $s .= '<br />'; 826 return $s; 827} 828 829/** 830 * form_menufield 831 * 832 * Print the HTML for a drop-down menu. 833 * _options : Array of (value,text,selected) for the menu. 834 * Text can be omitted. Text and value are passed to formText() 835 * Only one item can be selected. 836 * _class : class attribute used on the label tag 837 * _text : Text to display before the menu. Not escaped. 838 * Other attributes are passed to buildAttributes() for the input tag. 839 * 840 * @author Tom N Harris <tnharris@whoopdedo.org> 841 */ 842function form_menufield($attrs) { 843 $attrs['size'] = '1'; 844 $s = '<label'; 845 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 846 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 847 $s .= '><span>'.$attrs['_text'].'</span>'; 848 $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF; 849 if (!empty($attrs['_options'])) { 850 $selected = false; 851 for($n=0;$n<count($attrs['_options']);$n++){ 852 @list($value,$text,$select) = $attrs['_options'][$n]; 853 $p = ''; 854 if (!is_null($text)) 855 $p .= ' value="'.formText($value).'"'; 856 else 857 $text = $value; 858 if (!empty($select) && !$selected) { 859 $p .= ' selected="selected"'; 860 $selected = true; 861 } 862 $s .= '<option'.$p.'>'.formText($text).'</option>'; 863 } 864 } else { 865 $s .= '<option></option>'; 866 } 867 $s .= DOKU_LF.'</select></label>'; 868 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 869 $s .= '<br />'; 870 return $s; 871} 872 873/** 874 * form_listboxfield 875 * 876 * Print the HTML for a list box. 877 * _options : Array of (value,text,selected) for the list. 878 * Text can be omitted. Text and value are passed to formText() 879 * _class : class attribute used on the label tag 880 * _text : Text to display before the menu. Not escaped. 881 * Other attributes are passed to buildAttributes() for the input tag. 882 * 883 * @author Tom N Harris <tnharris@whoopdedo.org> 884 */ 885function form_listboxfield($attrs) { 886 $s = '<label'; 887 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 888 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 889 $s .= '><span>'.$attrs['_text'].'</span> '; 890 $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF; 891 if (!empty($attrs['_options'])) { 892 foreach ($attrs['_options'] as $opt) { 893 @list($value,$text,$select) = $opt; 894 $p = ''; 895 if(is_null($text)) $text = $value; 896 $p .= ' value="'.formText($value).'"'; 897 if (!empty($select)) $p .= ' selected="selected"'; 898 $s .= '<option'.$p.'>'.formText($text).'</option>'; 899 } 900 } else { 901 $s .= '<option></option>'; 902 } 903 $s .= DOKU_LF.'</select></label>'; 904 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 905 $s .= '<br />'; 906 return $s; 907} 908