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