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