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