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