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 51 * deprecated calling convention this is the ID 52 * attribute of the form 53 * @param string $action (optional, deprecated) submit URL, defaults to 54 * current page 55 * @param string $method (optional, deprecated) 'POST' or 'GET', default 56 * is POST 57 * @param string $enctype (optional, deprecated) Encoding type of the 58 * data 59 * @author Tom N Harris <tnharris@whoopdedo.org> 60 */ 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; 67 } else { 68 $this->params = $params; 69 } 70 71 if (!isset($this->params['method'])) { 72 $this->params['method'] = 'post'; 73 } else { 74 $this->params['method'] = strtolower($this->params['method']); 75 } 76 77 if (!isset($this->params['action'])) { 78 $this->params['action'] = ''; 79 } 80 81 $this->addHidden('sectok', getSecurityToken()); 82 } 83 84 /** 85 * startFieldset 86 * 87 * Add <fieldset></fieldset> tags around fields. 88 * Usually results in a border drawn around the form. 89 * 90 * @param string $legend Label that will be printed with the border. 91 * @author Tom N Harris <tnharris@whoopdedo.org> 92 */ 93 function startFieldset($legend) { 94 if ($this->_infieldset) { 95 $this->addElement(array('_elem'=>'closefieldset')); 96 } 97 $this->addElement(array('_elem'=>'openfieldset', '_legend'=>$legend)); 98 $this->_infieldset = true; 99 } 100 101 /** 102 * endFieldset 103 * 104 * @author Tom N Harris <tnharris@whoopdedo.org> 105 */ 106 function endFieldset() { 107 if ($this->_infieldset) { 108 $this->addElement(array('_elem'=>'closefieldset')); 109 } 110 $this->_infieldset = false; 111 } 112 113 /** 114 * addHidden 115 * 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. 119 * 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> 123 */ 124 function addHidden($name, $value) { 125 if (is_null($value)) 126 unset($this->_hidden[$name]); 127 else 128 $this->_hidden[$name] = $value; 129 } 130 131 /** 132 * addElement 133 * 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. * 137 * 138 * @param string $elem Pseudo-tag or string to add to the form. 139 * @author Tom N Harris <tnharris@whoopdedo.org> 140 */ 141 function addElement($elem) { 142 $this->_content[] = $elem; 143 } 144 145 /** 146 * insertElement 147 * 148 * Inserts a content element at a position. 149 * 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> 153 */ 154 function insertElement($pos, $elem) { 155 array_splice($this->_content, $pos, 0, array($elem)); 156 } 157 158 /** 159 * replaceElement 160 * 161 * Replace with NULL to remove an element. 162 * 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> 166 */ 167 function replaceElement($pos, $elem) { 168 $rep = array(); 169 if (!is_null($elem)) $rep[] = $elem; 170 array_splice($this->_content, $pos, 1, $rep); 171 } 172 173 /** 174 * findElementByType 175 * 176 * Gets the position of the first of a type of element. 177 * 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> 181 */ 182 function findElementByType($type) { 183 foreach ($this->_content as $pos=>$elem) { 184 if (is_array($elem) && $elem['_elem'] == $type) 185 return $pos; 186 } 187 return false; 188 } 189 190 /** 191 * findElementById 192 * 193 * Gets the position of the element with an ID attribute. 194 * 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> 198 */ 199 function findElementById($id) { 200 foreach ($this->_content as $pos=>$elem) { 201 if (is_array($elem) && isset($elem['id']) && $elem['id'] == $id) 202 return $pos; 203 } 204 return false; 205 } 206 207 /** 208 * findElementByAttribute 209 * 210 * Gets the position of the first element with a matching attribute value. 211 * 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> 216 */ 217 function findElementByAttribute($name, $value) { 218 foreach ($this->_content as $pos=>$elem) { 219 if (is_array($elem) && isset($elem[$name]) && $elem[$name] == $value) 220 return $pos; 221 } 222 return false; 223 } 224 225 /** 226 * getElementAt 227 * 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. 231 * 232 * @param int $pos 0-based index 233 * @return arrayreference pseudo-element 234 * @author Tom N Harris <tnharris@whoopdedo.org> 235 */ 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]; 241 } 242 243 /** 244 * Return the assembled HTML for the form. 245 * 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. 248 * 249 * @author Tom N Harris <tnharris@whoopdedo.org> 250 */ 251 function getForm() { 252 global $lang; 253 $form = ''; 254 $this->params['accept-charset'] = $lang['encoding']; 255 $form .= '<form ' . buildAttributes($this->params,true) . '><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)); 259 } 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; 265 } 266 } else { 267 $form .= $element; 268 } 269 } 270 if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF; 271 $form .= '</div></form>'.DOKU_LF; 272 273 return $form; 274 } 275 276 /** 277 * Print the assembled form 278 * 279 * wraps around getForm() 280 */ 281 function printForm(){ 282 echo $this->getForm(); 283 } 284 285 /** 286 * Add a radio set 287 * 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. 290 * 291 * @param string $name The HTML field name 292 * @param array $entries An array of entries $value => $caption 293 * 294 * @author Adrian Lang <lang@cosmocode.de> 295 */ 296 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)); 303 } 304 } 305 306} 307 308/** 309 * form_makeTag 310 * 311 * Create a form element for a non-specific empty tag. 312 * 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> 317 */ 318function form_makeTag($tag, $attrs=array()) { 319 $elem = array('_elem'=>'tag', '_tag'=>$tag); 320 return array_merge($elem, $attrs); 321} 322 323/** 324 * form_makeOpenTag 325 * 326 * Create a form element for a non-specific opening tag. 327 * Remember to put a matching close tag after this as well. 328 * 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> 333 */ 334function form_makeOpenTag($tag, $attrs=array()) { 335 $elem = array('_elem'=>'opentag', '_tag'=>$tag); 336 return array_merge($elem, $attrs); 337} 338 339/** 340 * form_makeCloseTag 341 * 342 * Create a form element for a non-specific closing tag. 343 * Careless use of this will result in invalid XHTML. 344 * 345 * @param string $tag Tag name. 346 * @return array pseudo-tag 347 * @author Tom N Harris <tnharris@whoopdedo.org> 348 */ 349function form_makeCloseTag($tag) { 350 return array('_elem'=>'closetag', '_tag'=>$tag); 351} 352 353/** 354 * form_makeWikiText 355 * 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. 360 * 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> 365 */ 366function 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); 370} 371 372/** 373 * form_makeButton 374 * 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. 378 * 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> 385 */ 386function 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']).']'; 392 } 393 return array_merge($elem, $attrs); 394} 395 396/** 397 * form_makeField 398 * 399 * Create a form element for a labelled input element. 400 * The label text will be printed before the input. 401 * 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> 413 */ 414function 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); 419} 420 421/** 422 * form_makeFieldRight 423 * 424 * Create a form element for a labelled input element. 425 * The label text will be printed after the input. 426 * 427 * @see form_makeField 428 * @author Tom N Harris <tnharris@whoopdedo.org> 429 */ 430function 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); 435} 436 437/** 438 * form_makeTextField 439 * 440 * Create a form element for a text input element with label. 441 * 442 * @see form_makeField 443 * @author Tom N Harris <tnharris@whoopdedo.org> 444 */ 445function 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); 450} 451 452/** 453 * form_makePasswordField 454 * 455 * Create a form element for a password input element with label. 456 * Password elements have no default value, for obvious reasons. 457 * 458 * @see form_makeField 459 * @author Tom N Harris <tnharris@whoopdedo.org> 460 */ 461function 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); 466} 467 468/** 469 * form_makeFileField 470 * 471 * Create a form element for a file input element with label 472 * 473 * @see form_makeField 474 * @author Michael Klier <chi@chimeric.de> 475 */ 476function 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); 481} 482 483/** 484 * form_makeCheckboxField 485 * 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. 489 * 490 * @see form_makeFieldRight 491 * @author Tom N Harris <tnharris@whoopdedo.org> 492 */ 493function 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); 499} 500 501/** 502 * form_makeRadioField 503 * 504 * Create a form element for a radio button input element with label. 505 * 506 * @see form_makeFieldRight 507 * @author Tom N Harris <tnharris@whoopdedo.org> 508 */ 509function 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); 515} 516 517/** 518 * form_makeMenuField 519 * 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 525 * a string. 526 * 527 * @author Tom N Harris <tnharris@whoopdedo.org> 528 */ 529function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { 530 if (is_null($label)) $label = $name; 531 $options = array(); 532 reset($values); 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)); 537 } 538 } else { 539 if (is_integer($selected)) $selected = $values[$selected]; 540 foreach ($values as $val) { 541 if (is_array($val)) 542 @list($val,$text) = $val; 543 else 544 $text = null; 545 $options[] = array($val,$text,$val===$selected); 546 } 547 } 548 $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class, 549 'id'=>$id, 'name'=>$name); 550 return array_merge($elem, $attrs); 551} 552 553/** 554 * form_makeListboxField 555 * 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. 560 * 561 * @author Tom N Harris <tnharris@whoopdedo.org> 562 */ 563function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { 564 if (is_null($label)) $label = $name; 565 $options = array(); 566 reset($values); 567 if (is_null($selected) || $selected == '') 568 $selected = array(); 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)); 575 } 576 } else { 577 foreach ($values as $val) { 578 if (is_array($val)) 579 @list($val,$text) = $val; 580 else 581 $text = null; 582 $options[] = array($val,$text,in_array($val,$selected)); 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" ' 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) = $opt; 937 $p = ''; 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>'; 942 } 943 } else { 944 $s .= '<option></option>'; 945 } 946 $s .= DOKU_LF.'</select></label>'; 947 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 948 $s .= '<br />'; 949 return $s; 950} 951