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