xref: /dokuwiki/inc/form.php (revision 3e182cc04a2ccc158b57996f1c1fc20957b6422b)
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 . ' [ALT+'.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_makeCheckboxField
432 *
433 * Create a form element for a checkbox input element with label.
434 *
435 * @see     form_makeFieldRight
436 * @author  Tom N Harris <tnharris@whoopdedo.org>
437 */
438function form_makeCheckboxField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
439  if (is_null($label)) $label = $name;
440  if (is_null($value) || $value=='') $value='0';
441  $elem = array('_elem'=>'checkboxfield', '_text'=>$label, '_class'=>$class,
442                'id'=>$id, 'name'=>$name, 'value'=>$value);
443  return array_merge($elem, $attrs);
444}
445
446/**
447 * form_makeRadioField
448 *
449 * Create a form element for a radio button input element with label.
450 *
451 * @see     form_makeFieldRight
452 * @author  Tom N Harris <tnharris@whoopdedo.org>
453 */
454function form_makeRadioField($name, $value='1', $label=null, $id='', $class='', $attrs=array()) {
455  if (is_null($label)) $label = $name;
456  if (is_null($value) || $value=='') $value='0';
457  $elem = array('_elem'=>'radiofield', '_text'=>$label, '_class'=>$class,
458                'id'=>$id, 'name'=>$name, 'value'=>$value);
459  return array_merge($elem, $attrs);
460}
461
462/**
463 * form_makeMenuField
464 *
465 * Create a form element for a drop-down menu with label.
466 * The list of values can be strings, arrays of (value,text),
467 * or an associative array with the values as keys and labels as values.
468 * An item is selected by supplying its value or integer index.
469 * If the list of values is an associative array, the selected item must be
470 * a string.
471 *
472 * @author  Tom N Harris <tnharris@whoopdedo.org>
473 */
474function form_makeMenuField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
475  if (is_null($label)) $label = $name;
476  $options = array();
477  reset($values);
478  // FIXME: php doesn't know the difference between a string and an integer
479  if (is_string(key($values))) {
480    foreach ($values as $val=>$text) {
481      $options[] = array($val,$text, (!is_null($selected) && $val==$selected));
482    }
483  } else {
484    if (is_integer($selected)) $selected = $values[$selected];
485    foreach ($values as $val) {
486      if (is_array($val))
487        @list($val,$text) = $val;
488      else
489        $text = null;
490      $options[] = array($val,$text,$val===$selected);
491    }
492  }
493  $elem = array('_elem'=>'menufield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
494                'id'=>$id, 'name'=>$name);
495  return array_merge($elem, $attrs);
496}
497
498/**
499 * form_makeListboxField
500 *
501 * Create a form element for a list box with label.
502 * The list of values can be strings, arrays of (value,text),
503 * or an associative array with the values as keys and labels as values.
504 * Items are selected by supplying its value or an array of values.
505 *
506 * @author  Tom N Harris <tnharris@whoopdedo.org>
507 */
508function form_makeListboxField($name, $values, $selected='', $label=null, $id='', $class='', $attrs=array()) {
509  if (is_null($label)) $label = $name;
510  $options = array();
511  reset($values);
512  if (is_null($selected) || $selected == '')
513    $selected = array();
514  elseif (!is_array($selected))
515    $selected = array($selected);
516  // FIXME: php doesn't know the difference between a string and an integer
517  if (is_string(key($values))) {
518    foreach ($values as $val=>$text) {
519      $options[] = array($val,$text,in_array($val,$selected));
520    }
521  } else {
522    foreach ($values as $val) {
523      if (is_array($val))
524        @list($val,$text) = $val;
525      else
526        $text = null;
527      $options[] = array($val,$text,in_array($val,$selected));
528    }
529  }
530  $elem = array('_elem'=>'listboxfield', '_options'=>$options, '_text'=>$label, '_class'=>$class,
531                'id'=>$id, 'name'=>$name);
532  return array_merge($elem, $attrs);
533}
534
535/**
536 * form_tag
537 *
538 * Print the HTML for a generic empty tag.
539 * Requires '_tag' key with name of the tag.
540 * Attributes are passed to buildAttributes()
541 *
542 * @author  Tom N Harris <tnharris@whoopdedo.org>
543 */
544function form_tag($attrs) {
545  return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'/>';
546}
547
548/**
549 * form_opentag
550 *
551 * Print the HTML for a generic opening 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_opentag($attrs) {
558  return '<'.$attrs['_tag'].' '.buildAttributes($attrs,true).'>';
559}
560
561/**
562 * form_closetag
563 *
564 * Print the HTML for a generic closing tag.
565 * Requires '_tag' key with name of the tag.
566 * There are no attributes.
567 *
568 * @author  Tom N Harris <tnharris@whoopdedo.org>
569 */
570function form_closetag($attrs) {
571  return '</'.$attrs['_tag'].'>';
572}
573
574/**
575 * form_openfieldset
576 *
577 * Print the HTML for an opening fieldset tag.
578 * Uses the '_legend' key.
579 * Attributes are passed to buildAttributes()
580 *
581 * @author  Tom N Harris <tnharris@whoopdedo.org>
582 */
583function form_openfieldset($attrs) {
584  $s = '<fieldset '.buildAttributes($attrs,true).'>';
585  if (!is_null($attrs['_legend'])) $s .= '<legend>'.$attrs['_legend'].'</legend>';
586  return $s;
587}
588
589/**
590 * form_closefieldset
591 *
592 * Print the HTML for a closing fieldset tag.
593 * There are no attributes.
594 *
595 * @author  Tom N Harris <tnharris@whoopdedo.org>
596 */
597function form_closefieldset() {
598  return '</fieldset>';
599}
600
601/**
602 * form_hidden
603 *
604 * Print the HTML for a hidden input element.
605 * Uses only 'name' and 'value' attributes.
606 * Value is passed to formText()
607 *
608 * @author  Tom N Harris <tnharris@whoopdedo.org>
609 */
610function form_hidden($attrs) {
611  return '<input type="hidden" name="'.$attrs['name'].'" value="'.formText($attrs['value']).'" />';
612}
613
614/**
615 * form_wikitext
616 *
617 * Print the HTML for the wiki textarea.
618 * Requires '_text' with default text of the field.
619 * Text will be passed to formText(), attributes to buildAttributes()
620 *
621 * @author  Tom N Harris <tnharris@whoopdedo.org>
622 */
623function form_wikitext($attrs) {
624  // mandatory attributes
625  unset($attrs['name']);
626  unset($attrs['id']);
627  return '<textarea name="wikitext" id="wiki__text" '
628         .buildAttributes($attrs,true).'>'.NL
629         .formText($attrs['_text'])
630         .'</textarea>';
631}
632
633/**
634 * form_button
635 *
636 * Print the HTML for a form button.
637 * If '_action' is set, the button name will be "do[_action]".
638 * Other attributes are passed to buildAttributes()
639 *
640 * @author  Tom N Harris <tnharris@whoopdedo.org>
641 */
642function form_button($attrs) {
643  $p = (!empty($attrs['_action'])) ? 'name="do['.$attrs['_action'].']" ' : '';
644  return '<input '.$p.buildAttributes($attrs,true).'/>';
645}
646
647/**
648 * form_field
649 *
650 * Print the HTML for a form input field.
651 *   _class : class attribute used on the label tag
652 *   _text  : Text to display before the input. Not escaped.
653 * Other attributes are passed to buildAttributes() for the input tag.
654 *
655 * @author  Tom N Harris <tnharris@whoopdedo.org>
656 */
657function form_field($attrs) {
658  $s = '<label class="'.$attrs['_class'].'"';
659  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
660  $s .= '><span>'.$attrs['_text'].'</span>';
661  $s .= ' <input '.buildAttributes($attrs,true).'/></label>';
662  if (preg_match('/(^| )block($| )/', $attrs['_class']))
663    $s .= '<br />';
664  return $s;
665}
666
667/**
668 * form_fieldright
669 *
670 * Print the HTML for a form input field. (right-aligned)
671 *   _class : class attribute used on the label tag
672 *   _text  : Text to display after the input. Not escaped.
673 * Other attributes are passed to buildAttributes() for the input tag.
674 *
675 * @author  Tom N Harris <tnharris@whoopdedo.org>
676 */
677function form_fieldright($attrs) {
678  $s = '<label class="'.$attrs['_class'].'"';
679  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
680  $s .= '><input '.buildAttributes($attrs,true).'/>';
681  $s .= ' <span>'.$attrs['_text'].'</span></label>';
682  if (preg_match('/(^| )block($| )/', $attrs['_class']))
683    $s .= '<br />';
684  return $s;
685}
686
687/**
688 * form_textfield
689 *
690 * Print the HTML for a text input field.
691 *   _class : class attribute used on the label tag
692 *   _text  : Text to display before the input. Not escaped.
693 * Other attributes are passed to buildAttributes() for the input tag.
694 *
695 * @author  Tom N Harris <tnharris@whoopdedo.org>
696 */
697function form_textfield($attrs) {
698  // mandatory attributes
699  unset($attrs['type']);
700  $s = '<label class="'.$attrs['_class'].'"';
701  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
702  $s .= '><span>'.$attrs['_text'].'</span> ';
703  $s .= '<input type="text" '.buildAttributes($attrs,true).'/></label>';
704  if (preg_match('/(^| )block($| )/', $attrs['_class']))
705    $s .= '<br />';
706  return $s;
707}
708
709/**
710 * form_passwordfield
711 *
712 * Print the HTML for a password input field.
713 *   _class : class attribute used on the label tag
714 *   _text  : Text to display before the input. Not escaped.
715 * Other attributes are passed to buildAttributes() for the input tag.
716 *
717 * @author  Tom N Harris <tnharris@whoopdedo.org>
718 */
719function form_passwordfield($attrs) {
720  // mandatory attributes
721  unset($attrs['type']);
722  $s = '<label class="'.$attrs['_class'].'"';
723  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
724  $s .= '><span>'.$attrs['_text'].'</span> ';
725  $s .= '<input type="password" '.buildAttributes($attrs,true).'/></label>';
726  if (preg_match('/(^| )block($| )/', $attrs['_class']))
727    $s .= '<br />';
728  return $s;
729}
730
731/**
732 * form_checkboxfield
733 *
734 * Print the HTML for a checkbox input field.
735 *   _class : class attribute used on the label tag
736 *   _text  : Text to display after the input. Not escaped.
737 * Other attributes are passed to buildAttributes() for the input tag.
738 *
739 * @author  Tom N Harris <tnharris@whoopdedo.org>
740 */
741function form_checkboxfield($attrs) {
742  // mandatory attributes
743  unset($attrs['type']);
744  $s = '<label class="'.$attrs['_class'].'"';
745  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
746  $s .= '><input type="checkbox" '.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_radiofield
755 *
756 * Print the HTML for a radio button input field.
757 *   _class : class attribute used on the label tag
758 *   _text  : Text to display after the input. Not escaped.
759 * Other attributes are passed to buildAttributes() for the input tag.
760 *
761 * @author  Tom N Harris <tnharris@whoopdedo.org>
762 */
763function form_radiofield($attrs) {
764  // mandatory attributes
765  unset($attrs['type']);
766  $s = '<label class="'.$attrs['_class'].'"';
767  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
768  $s .= '><input type="radio" '.buildAttributes($attrs,true).'/>';
769  $s .= ' <span>'.$attrs['_text'].'</span></label>';
770  if (preg_match('/(^| )block($| )/', $attrs['_class']))
771    $s .= '<br />';
772  return $s;
773}
774
775/**
776 * form_menufield
777 *
778 * Print the HTML for a drop-down menu.
779 *   _options : Array of (value,text,selected) for the menu.
780 *              Text can be omitted. Text and value are passed to formText()
781 *              Only one item can be selected.
782 *   _class : class attribute used on the label tag
783 *   _text  : Text to display before the menu. Not escaped.
784 * Other attributes are passed to buildAttributes() for the input tag.
785 *
786 * @author  Tom N Harris <tnharris@whoopdedo.org>
787 */
788function form_menufield($attrs) {
789  $attrs['size'] = '1';
790  $s = '<label class="'.$attrs['_class'].'"';
791  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
792  $s .= '><span>'.$attrs['_text'].'</span>';
793  $s .= ' <select '.buildAttributes($attrs,true).'>'.NL;
794  if (!empty($attrs['_options'])) {
795    $selected = false;
796    for($n=0;$n<count($attrs['_options']);$n++){
797      @list($value,$text,$select) = $attrs['_options'][$n];
798      $p = '';
799      if (!is_null($text))
800        $p .= ' value="'.formText($value).'"';
801      else
802        $text = $value;
803      if (!empty($select) && !$selected) {
804        $p .= ' selected="selected"';
805        $selected = true;
806      }
807      $s .= '<option'.$p.'>'.formText($text).'</option>';
808    }
809  } else {
810    $s .= '<option></option>';
811  }
812  $s .= NL.'</select></label>';
813  if (preg_match('/(^| )block($| )/', $attrs['_class']))
814    $s .= '<br />';
815  return $s;
816}
817
818/**
819 * form_listboxfield
820 *
821 * Print the HTML for a list box.
822 *   _options : Array of (value,text,selected) for the list.
823 *              Text can be omitted. Text and value are passed to formText()
824 *   _class : class attribute used on the label tag
825 *   _text  : Text to display before the menu. Not escaped.
826 * Other attributes are passed to buildAttributes() for the input tag.
827 *
828 * @author  Tom N Harris <tnharris@whoopdedo.org>
829 */
830function form_listboxfield($attrs) {
831  $s = '<label class="'.$attrs['_class'].'"';
832  if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
833  $s .= '><span>'.$attrs['_text'].'</span> ';
834  $s = '<select '.buildAttributes($attrs,true).'>'.NL;
835  if (!empty($attrs['_options'])) {
836    foreach ($attrs['_options'] as $opt) {
837      @list($value,$text,$select) = $opt;
838      $p = '';
839      if (!is_null($text))
840        $p .= ' value="'.formText($value).'"';
841      else
842        $text = $value;
843      if (!empty($select)) $p .= ' selected="selected"';
844      $s .= '<option'.$p.'>'.formText($text).'</option>';
845    }
846  } else {
847    $s .= '<option></option>';
848  }
849  $s .= NL.'</select></label>';
850  if (preg_match('/(^| )block($| )/', $attrs['_class']))
851    $s .= '<br />';
852  return $s;
853}
854