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