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