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