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 * Return the assembled HTML for the form. 246 * 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. 249 * 250 * @author Tom N Harris <tnharris@whoopdedo.org> 251 */ 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)); 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 $form .= call_user_func('form_'.$elem_type, $element).DOKU_LF; 266 } 267 } else { 268 $form .= $element; 269 } 270 } 271 if ($this->_infieldset) $form .= form_closefieldset().DOKU_LF; 272 $form .= '</div></form>'.DOKU_LF; 273 274 return $form; 275 } 276 277 /** 278 * Print the assembled form 279 * 280 * wraps around getForm() 281 */ 282 function printForm(){ 283 echo $this->getForm(); 284 } 285 286 /** 287 * Add a radio set 288 * 289 * This function adds a set of radio buttons to the form. If $_POST[$name] 290 * is set, this radio is preselected, else the first radio button. 291 * 292 * @param string $name The HTML field name 293 * @param array $entries An array of entries $value => $caption 294 * 295 * @author Adrian Lang <lang@cosmocode.de> 296 */ 297 298 function addRadioSet($name, $entries) { 299 $value = (isset($_POST[$name]) && isset($entries[$_POST[$name]])) ? 300 $_POST[$name] : key($entries); 301 foreach($entries as $val => $cap) { 302 $data = ($value === $val) ? array('checked' => 'checked') : array(); 303 $this->addElement(form_makeRadioField($name, $val, $cap, '', '', $data)); 304 } 305 } 306 307} 308 309/** 310 * form_makeTag 311 * 312 * Create a form element for a non-specific empty tag. 313 * 314 * @param string $tag Tag name. 315 * @param array $attrs Optional attributes. 316 * @return array pseudo-tag 317 * @author Tom N Harris <tnharris@whoopdedo.org> 318 */ 319function form_makeTag($tag, $attrs=array()) { 320 $elem = array('_elem'=>'tag', '_tag'=>$tag); 321 return array_merge($elem, $attrs); 322} 323 324/** 325 * form_makeOpenTag 326 * 327 * Create a form element for a non-specific opening tag. 328 * Remember to put a matching close tag after this as well. 329 * 330 * @param string $tag Tag name. 331 * @param array $attrs Optional attributes. 332 * @return array pseudo-tag 333 * @author Tom N Harris <tnharris@whoopdedo.org> 334 */ 335function form_makeOpenTag($tag, $attrs=array()) { 336 $elem = array('_elem'=>'opentag', '_tag'=>$tag); 337 return array_merge($elem, $attrs); 338} 339 340/** 341 * form_makeCloseTag 342 * 343 * Create a form element for a non-specific closing tag. 344 * Careless use of this will result in invalid XHTML. 345 * 346 * @param string $tag Tag name. 347 * @return array pseudo-tag 348 * @author Tom N Harris <tnharris@whoopdedo.org> 349 */ 350function form_makeCloseTag($tag) { 351 return array('_elem'=>'closetag', '_tag'=>$tag); 352} 353 354/** 355 * form_makeWikiText 356 * 357 * Create a form element for a textarea containing wiki text. 358 * Only one wikitext element is allowed on a page. It will have 359 * a name of 'wikitext' and id 'wiki__text'. The text will 360 * be passed to formText() before printing. 361 * 362 * @param string $text Text to fill the field with. 363 * @param array $attrs Optional attributes. 364 * @return array pseudo-tag 365 * @author Tom N Harris <tnharris@whoopdedo.org> 366 */ 367function form_makeWikiText($text, $attrs=array()) { 368 $elem = array('_elem'=>'wikitext', '_text'=>$text, 369 'class'=>'edit', 'cols'=>'80', 'rows'=>'10'); 370 return array_merge($elem, $attrs); 371} 372 373/** 374 * form_makeButton 375 * 376 * Create a form element for an action button. 377 * A title will automatically be generated using the value and 378 * accesskey attributes, unless you provide one. 379 * 380 * @param string $type Type attribute. 'submit' or 'cancel' 381 * @param string $act Wiki action of the button, will be used as the do= parameter 382 * @param string $value (optional) Displayed label. Uses $act if not provided. 383 * @param array $attrs Optional attributes. 384 * @return array pseudo-tag 385 * @author Tom N Harris <tnharris@whoopdedo.org> 386 */ 387function form_makeButton($type, $act, $value='', $attrs=array()) { 388 if ($value == '') $value = $act; 389 $elem = array('_elem'=>'button', 'type'=>$type, '_action'=>$act, 390 'value'=>$value, 'class'=>'button'); 391 if (!empty($attrs['accesskey']) && empty($attrs['title'])) { 392 $attrs['title'] = $value . ' ['.strtoupper($attrs['accesskey']).']'; 393 } 394 return array_merge($elem, $attrs); 395} 396 397/** 398 * form_makeField 399 * 400 * Create a form element for a labelled input element. 401 * The label text will be printed before the input. 402 * 403 * @param string $type Type attribute of input. 404 * @param string $name Name attribute of the input. 405 * @param string $value (optional) Default value. 406 * @param string $class Class attribute of the label. If this is 'block', 407 * then a line break will be added after the field. 408 * @param string $label Label that will be printed before the input. 409 * @param string $id ID attribute of the input. If set, the label will 410 * reference it with a 'for' attribute. 411 * @param array $attrs Optional attributes. 412 * @return array pseudo-tag 413 * @author Tom N Harris <tnharris@whoopdedo.org> 414 */ 415function form_makeField($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) { 416 if (is_null($label)) $label = $name; 417 $elem = array('_elem'=>'field', '_text'=>$label, '_class'=>$class, 418 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value); 419 return array_merge($elem, $attrs); 420} 421 422/** 423 * form_makeFieldRight 424 * 425 * Create a form element for a labelled input element. 426 * The label text will be printed after the input. 427 * 428 * @see form_makeField 429 * @author Tom N Harris <tnharris@whoopdedo.org> 430 */ 431function form_makeFieldRight($type, $name, $value='', $label=null, $id='', $class='', $attrs=array()) { 432 if (is_null($label)) $label = $name; 433 $elem = array('_elem'=>'fieldright', '_text'=>$label, '_class'=>$class, 434 'type'=>$type, 'id'=>$id, 'name'=>$name, 'value'=>$value); 435 return array_merge($elem, $attrs); 436} 437 438/** 439 * form_makeTextField 440 * 441 * Create a form element for a text input element with label. 442 * 443 * @see form_makeField 444 * @author Tom N Harris <tnharris@whoopdedo.org> 445 */ 446function form_makeTextField($name, $value='', $label=null, $id='', $class='', $attrs=array()) { 447 if (is_null($label)) $label = $name; 448 $elem = array('_elem'=>'textfield', '_text'=>$label, '_class'=>$class, 449 'id'=>$id, 'name'=>$name, 'value'=>$value, 'class'=>'edit'); 450 return array_merge($elem, $attrs); 451} 452 453/** 454 * form_makePasswordField 455 * 456 * Create a form element for a password input element with label. 457 * Password elements have no default value, for obvious reasons. 458 * 459 * @see form_makeField 460 * @author Tom N Harris <tnharris@whoopdedo.org> 461 */ 462function form_makePasswordField($name, $label=null, $id='', $class='', $attrs=array()) { 463 if (is_null($label)) $label = $name; 464 $elem = array('_elem'=>'passwordfield', '_text'=>$label, '_class'=>$class, 465 'id'=>$id, 'name'=>$name, 'class'=>'edit'); 466 return array_merge($elem, $attrs); 467} 468 469/** 470 * form_makeFileField 471 * 472 * Create a form element for a file input element with label 473 * 474 * @see form_makeField 475 * @author Michael Klier <chi@chimeric.de> 476 */ 477function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array()) { 478 if (is_null($label)) $label = $name; 479 $elem = array('_elem'=>'filefield', '_text'=>$label, '_class'=>$class, 480 'id'=>$id, 'name'=>$name, 'class'=>'edit'); 481 return array_merge($elem, $attrs); 482} 483 484/** 485 * form_makeCheckboxField 486 * 487 * Create a form element for a checkbox input element with label. 488 * 489 * @see form_makeFieldRight 490 * @author Tom N Harris <tnharris@whoopdedo.org> 491 */ 492function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) { 493 if (is_null($label)) $label = $name; 494 if (is_null($value) || $value=='') $value='0'; 495 $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class, 496 'id'=>$id, 'name'=>$name, 'value'=>$value); 497 return array_merge($elem, $attrs); 498} 499 500/** 501 * form_makeRadioField 502 * 503 * Create a form element for a radio button input element with label. 504 * 505 * @see form_makeFieldRight 506 * @author Tom N Harris <tnharris@whoopdedo.org> 507 */ 508function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) { 509 if (is_null($label)) $label = $name; 510 if (is_null($value) || $value=='') $value='0'; 511 $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class, 512 'id'=>$id, 'name'=>$name, 'value'=>$value); 513 return array_merge($elem, $attrs); 514} 515 516/** 517 * form_makeMenuField 518 * 519 * Create a form element for a drop-down menu with label. 520 * The list of values can be strings, arrays of (value,text), 521 * or an associative array with the values as keys and labels as values. 522 * An item is selected by supplying its value or integer index. 523 * If the list of values is an associative array, the selected item must be 524 * a string. 525 * 526 * @author Tom N Harris <tnharris@whoopdedo.org> 527 */ 528function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { 529 if (is_null($label)) $label = $name; 530 $options = array(); 531 reset($values); 532 // FIXME: php doesn't know the difference between a string and an integer 533 if (is_string(key($values))) { 534 foreach ($values as $val=>$text) { 535 $options[] = array($val,$text, (!is_null($selected) && $val==$selected)); 536 } 537 } else { 538 if (is_integer($selected)) $selected = $values[$selected]; 539 foreach ($values as $val) { 540 if (is_array($val)) 541 @list($val,$text) = $val; 542 else 543 $text = null; 544 $options[] = array($val,$text,$val===$selected); 545 } 546 } 547 $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class, 548 'id'=>$id, 'name'=>$name); 549 return array_merge($elem, $attrs); 550} 551 552/** 553 * form_makeListboxField 554 * 555 * Create a form element for a list box with label. 556 * The list of values can be strings, arrays of (value,text), 557 * or an associative array with the values as keys and labels as values. 558 * Items are selected by supplying its value or an array of values. 559 * 560 * @author Tom N Harris <tnharris@whoopdedo.org> 561 */ 562function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) { 563 if (is_null($label)) $label = $name; 564 $options = array(); 565 reset($values); 566 if (is_null($selected) || $selected == '') 567 $selected = array(); 568 elseif (!is_array($selected)) 569 $selected = array($selected); 570 // FIXME: php doesn't know the difference between a string and an integer 571 if (is_string(key($values))) { 572 foreach ($values as $val=>$text) { 573 $options[] = array($val,$text,in_array($val,$selected)); 574 } 575 } else { 576 foreach ($values as $val) { 577 if (is_array($val)) 578 @list($val,$text) = $val; 579 else 580 $text = null; 581 $options[] = array($val,$text,in_array($val,$selected)); 582 } 583 } 584 $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class, 585 'id'=>$id, 'name'=>$name); 586 return array_merge($elem, $attrs); 587} 588 589/** 590 * form_tag 591 * 592 * Print the HTML for a generic empty tag. 593 * Requires '_tag' key with name of the tag. 594 * Attributes are passed to buildAttributes() 595 * 596 * @author Tom N Harris <tnharris@whoopdedo.org> 597 */ 598function form_tag($attrs) { 599 return '<'.$attrs['_tag'].' '.buildAttributes($attrs).'/>'; 600} 601 602/** 603 * form_opentag 604 * 605 * Print the HTML for a generic opening tag. 606 * Requires '_tag' key with name of the tag. 607 * Attributes are passed to buildAttributes() 608 * 609 * @author Tom N Harris <tnharris@whoopdedo.org> 610 */ 611function form_opentag($attrs) { 612 return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>'; 613} 614 615/** 616 * form_closetag 617 * 618 * Print the HTML for a generic closing tag. 619 * Requires '_tag' key with name of the tag. 620 * There are no attributes. 621 * 622 * @author Tom N Harris <tnharris@whoopdedo.org> 623 */ 624function form_closetag($attrs) { 625 return '</'.$attrs['_tag'].'>'; 626} 627 628/** 629 * form_openfieldset 630 * 631 * Print the HTML for an opening fieldset tag. 632 * Uses the '_legend' key. 633 * Attributes are passed to buildAttributes() 634 * 635 * @author Tom N Harris <tnharris@whoopdedo.org> 636 */ 637function form_openfieldset($attrs) { 638 $s = '<fieldset '.buildAttributes($attrs,true).'>'; 639 if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>'; 640 return $s; 641} 642 643/** 644 * form_closefieldset 645 * 646 * Print the HTML for a closing fieldset tag. 647 * There are no attributes. 648 * 649 * @author Tom N Harris <tnharris@whoopdedo.org> 650 */ 651function form_closefieldset() { 652 return '</fieldset>'; 653} 654 655/** 656 * form_hidden 657 * 658 * Print the HTML for a hidden input element. 659 * Uses only 'name' and 'value' attributes. 660 * Value is passed to formText() 661 * 662 * @author Tom N Harris <tnharris@whoopdedo.org> 663 */ 664function form_hidden($attrs) { 665 return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />'; 666} 667 668/** 669 * form_wikitext 670 * 671 * Print the HTML for the wiki textarea. 672 * Requires '_text' with default text of the field. 673 * Text will be passed to formText(), attributes to buildAttributes() 674 * 675 * @author Tom N Harris <tnharris@whoopdedo.org> 676 */ 677function form_wikitext($attrs) { 678 // mandatory attributes 679 unset($attrs['name']); 680 unset($attrs['id']); 681 return '<textarea name="wikitext" id="wiki__text" ' 682 .buildAttributes($attrs,true).'>'.DOKU_LF 683 .formText($attrs['_text']) 684 .'</textarea>'; 685} 686 687/** 688 * form_button 689 * 690 * Print the HTML for a form button. 691 * If '_action' is set, the button name will be "do[_action]". 692 * Other attributes are passed to buildAttributes() 693 * 694 * @author Tom N Harris <tnharris@whoopdedo.org> 695 */ 696function form_button($attrs) { 697 $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : ''; 698 return '<input '.$p.buildAttributes($attrs,true).'/>'; 699} 700 701/** 702 * form_field 703 * 704 * Print the HTML for a form input field. 705 * _class : class attribute used on the label tag 706 * _text : Text to display before the input. Not escaped. 707 * Other attributes are passed to buildAttributes() for the input tag. 708 * 709 * @author Tom N Harris <tnharris@whoopdedo.org> 710 */ 711function form_field($attrs) { 712 $s = '<label'; 713 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 714 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 715 $s .= '><span>'.$attrs['_text'].'</span>'; 716 $s .= ' <input '.buildAttributes($attrs,true).'/></label>'; 717 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 718 $s .= '<br />'; 719 return $s; 720} 721 722/** 723 * form_fieldright 724 * 725 * Print the HTML for a form input field. (right-aligned) 726 * _class : class attribute used on the label tag 727 * _text : Text to display after the input. Not escaped. 728 * Other attributes are passed to buildAttributes() for the input tag. 729 * 730 * @author Tom N Harris <tnharris@whoopdedo.org> 731 */ 732function form_fieldright($attrs) { 733 $s = '<label'; 734 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 735 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 736 $s .= '><input '.buildAttributes($attrs,true).'/>'; 737 $s .= ' <span>'.$attrs['_text'].'</span></label>'; 738 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 739 $s .= '<br />'; 740 return $s; 741} 742 743/** 744 * form_textfield 745 * 746 * Print the HTML for a text input field. 747 * _class : class attribute used on the label tag 748 * _text : Text to display before the input. Not escaped. 749 * Other attributes are passed to buildAttributes() for the input tag. 750 * 751 * @author Tom N Harris <tnharris@whoopdedo.org> 752 */ 753function form_textfield($attrs) { 754 // mandatory attributes 755 unset($attrs['type']); 756 $s = '<label'; 757 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 758 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 759 $s .= '><span>'.$attrs['_text'].'</span> '; 760 $s .= '<input type="text" '.buildAttributes($attrs,true).'/></label>'; 761 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 762 $s .= '<br />'; 763 return $s; 764} 765 766/** 767 * form_passwordfield 768 * 769 * Print the HTML for a password input field. 770 * _class : class attribute used on the label tag 771 * _text : Text to display before the input. Not escaped. 772 * Other attributes are passed to buildAttributes() for the input tag. 773 * 774 * @author Tom N Harris <tnharris@whoopdedo.org> 775 */ 776function form_passwordfield($attrs) { 777 // mandatory attributes 778 unset($attrs['type']); 779 $s = '<label'; 780 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 781 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 782 $s .= '><span>'.$attrs['_text'].'</span> '; 783 $s .= '<input type="password" '.buildAttributes($attrs,true).'/></label>'; 784 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 785 $s .= '<br />'; 786 return $s; 787} 788 789/** 790 * form_filefield 791 * 792 * Print the HTML for a file input field. 793 * _class : class attribute used on the label tag 794 * _text : Text to display before the input. Not escaped 795 * _maxlength : Allowed size in byte 796 * _accept : Accepted mime-type 797 * Other attributes are passed to buildAttributes() for the input tag 798 * 799 * @author Michael Klier <chi@chimeric.de> 800 */ 801function form_filefield($attrs) { 802 $s = '<label'; 803 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 804 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 805 $s .= '><span>'.$attrs['_text'].'</span> '; 806 $s .= '<input type="file" '.buildAttributes($attrs,true); 807 if (!empty($attrs['_maxlength'])) $s .= ' maxlength="'.$attrs['_maxlength'].'"'; 808 if (!empty($attrs['_accept'])) $s .= ' accept="'.$attrs['_accept'].'"'; 809 $s .= '/></label>'; 810 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 811 $s .= '<br />'; 812 return $s; 813} 814 815/** 816 * form_checkboxfield 817 * 818 * Print the HTML for a checkbox input field. 819 * _class : class attribute used on the label tag 820 * _text : Text to display after the input. Not escaped. 821 * Other attributes are passed to buildAttributes() for the input tag. 822 * 823 * @author Tom N Harris <tnharris@whoopdedo.org> 824 */ 825function form_checkboxfield($attrs) { 826 // mandatory attributes 827 unset($attrs['type']); 828 $s = '<label'; 829 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 830 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 831 $s .= '><input type="checkbox" '.buildAttributes($attrs,true).'/>'; 832 $s .= ' <span>'.$attrs['_text'].'</span></label>'; 833 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 834 $s .= '<br />'; 835 return $s; 836} 837 838/** 839 * form_radiofield 840 * 841 * Print the HTML for a radio button input field. 842 * _class : class attribute used on the label tag 843 * _text : Text to display after the input. Not escaped. 844 * Other attributes are passed to buildAttributes() for the input tag. 845 * 846 * @author Tom N Harris <tnharris@whoopdedo.org> 847 */ 848function form_radiofield($attrs) { 849 // mandatory attributes 850 unset($attrs['type']); 851 $s = '<label'; 852 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 853 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 854 $s .= '><input type="radio" '.buildAttributes($attrs,true).'/>'; 855 $s .= ' <span>'.$attrs['_text'].'</span></label>'; 856 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 857 $s .= '<br />'; 858 return $s; 859} 860 861/** 862 * form_menufield 863 * 864 * Print the HTML for a drop-down menu. 865 * _options : Array of (value,text,selected) for the menu. 866 * Text can be omitted. Text and value are passed to formText() 867 * Only one item can be selected. 868 * _class : class attribute used on the label tag 869 * _text : Text to display before the menu. Not escaped. 870 * Other attributes are passed to buildAttributes() for the input tag. 871 * 872 * @author Tom N Harris <tnharris@whoopdedo.org> 873 */ 874function form_menufield($attrs) { 875 $attrs['size'] = '1'; 876 $s = '<label'; 877 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 878 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 879 $s .= '><span>'.$attrs['_text'].'</span>'; 880 $s .= ' <select '.buildAttributes($attrs,true).'>'.DOKU_LF; 881 if (!empty($attrs['_options'])) { 882 $selected = false; 883 884 $cnt = count($attrs['_options']); 885 for($n=0; $n < $cnt; $n++){ 886 @list($value,$text,$select) = $attrs['_options'][$n]; 887 $p = ''; 888 if (!is_null($text)) 889 $p .= ' value="'.formText($value).'"'; 890 else 891 $text = $value; 892 if (!empty($select) && !$selected) { 893 $p .= ' selected="selected"'; 894 $selected = true; 895 } 896 $s .= '<option'.$p.'>'.formText($text).'</option>'; 897 } 898 } else { 899 $s .= '<option></option>'; 900 } 901 $s .= DOKU_LF.'</select></label>'; 902 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 903 $s .= '<br />'; 904 return $s; 905} 906 907/** 908 * form_listboxfield 909 * 910 * Print the HTML for a list box. 911 * _options : Array of (value,text,selected) for the list. 912 * Text can be omitted. Text and value are passed to formText() 913 * _class : class attribute used on the label tag 914 * _text : Text to display before the menu. Not escaped. 915 * Other attributes are passed to buildAttributes() for the input tag. 916 * 917 * @author Tom N Harris <tnharris@whoopdedo.org> 918 */ 919function form_listboxfield($attrs) { 920 $s = '<label'; 921 if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"'; 922 if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"'; 923 $s .= '><span>'.$attrs['_text'].'</span> '; 924 $s .= '<select '.buildAttributes($attrs,true).'>'.DOKU_LF; 925 if (!empty($attrs['_options'])) { 926 foreach ($attrs['_options'] as $opt) { 927 @list($value,$text,$select) = $opt; 928 $p = ''; 929 if(is_null($text)) $text = $value; 930 $p .= ' value="'.formText($value).'"'; 931 if (!empty($select)) $p .= ' selected="selected"'; 932 $s .= '<option'.$p.'>'.formText($text).'</option>'; 933 } 934 } else { 935 $s .= '<option></option>'; 936 } 937 $s .= DOKU_LF.'</select></label>'; 938 if (preg_match('/(^| )block($| )/', $attrs['_class'])) 939 $s .= '<br />'; 940 return $s; 941} 942