Allow extra javascript to be added to checkboxes
[squirrelmail.git] / functions / forms.php
1 <?php
2 /**
3 * forms.php
4 *
5 * Copyright (c) 2004-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Functions to build HTML forms in a safe and consistent manner.
9 * All name, value attributes are htmlentitied.
10 *
11 * @version $Id$
12 * @package squirrelmail
13 * @subpackage forms
14 */
15
16 /**
17 * Helper function to create form fields, not to be called directly,
18 * only by other functions below.
19 */
20 function addInputField($type, $name = null, $value = null, $attributes = '', $id = null) {
21 return '<input type="'.$type.'"'.
22 ($name !== null ? ' name="'.htmlspecialchars($name).'"' : '').
23 ($id !== null ? ' id="'.htmlspecialchars($id).'"'
24 : ($name !== null ? ' id="'.htmlspecialchars($name).'"' : '')).
25 ($value !== null ? ' value="'.htmlspecialchars($value).'"' : '').
26 $attributes . " />\n";
27 }
28
29 /**
30 * Password input field
31 */
32 function addPwField($name , $value = null) {
33 return addInputField('password', $name , $value);
34 }
35
36
37 /**
38 * Form checkbox
39 */
40 function addCheckBox($name, $checked = false, $value = null, $xtra = '') {
41 return addInputField('checkbox', $name, $value,
42 ($checked ? ' checked="checked"' : '') . ' ' . $xtra);
43 }
44
45 /**
46 * Form radio box
47 */
48 function addRadioBox($name, $checked = false, $value = null) {
49 return addInputField('radio', $name, $value,
50 ($checked ? ' checked="checked"' : ''), $name . $value);
51 }
52
53 /**
54 * A hidden form field.
55 */
56 function addHidden($name, $value) {
57 return addInputField('hidden', $name, $value);
58 }
59
60 /**
61 * An input textbox.
62 */
63 function addInput($name, $value = '', $size = 0, $maxlength = 0) {
64
65 $attr = '';
66 if ($size) {
67 $attr.= ' size="'.(int)$size.'"';
68 }
69 if ($maxlength) {
70 $attr.= ' maxlength="'.(int)$maxlength .'"';
71 }
72
73 return addInputField('text', $name, $value, $attr);
74 }
75
76
77 /**
78 * Function to create a selectlist from an array.
79 * Usage:
80 * name: html name attribute
81 * values: array ( key => value ) -> <option value="key">value</option>
82 * default: the key that will be selected
83 * usekeys: use the keys of the array as option value or not
84 */
85 function addSelect($name, $values, $default = null, $usekeys = false)
86 {
87 // only one element
88 if(count($values) == 1) {
89 $k = key($values); $v = array_pop($values);
90 return addHidden($name, ($usekeys ? $k:$v)).
91 htmlspecialchars($v) . "\n";
92 }
93
94 $ret = '<select name="'.htmlspecialchars($name) . "\">\n";
95 foreach ($values as $k => $v) {
96 if(!$usekeys) $k = $v;
97 $ret .= '<option value="' .
98 htmlspecialchars( $k ) . '"' .
99 (($default == $k) ? ' selected="selected"' : '') .
100 '>' . htmlspecialchars($v) ."</option>\n";
101 }
102 $ret .= "</select>\n";
103
104 return $ret;
105 }
106
107 /**
108 * Form submission button
109 * Note the switched value/name parameters!
110 */
111 function addSubmit($value, $name = null) {
112 return addInputField('submit', $name, $value);
113 }
114 /**
115 * Form reset button, $value = caption
116 */
117 function addReset($value) {
118 return addInputField('reset', null, $value);
119 }
120
121 /**
122 * Textarea form element.
123 */
124 function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
125 return '<textarea name="'.htmlspecialchars($name).'" '.
126 'rows="'.(int)$rows .'" cols="'.(int)$cols.'" '.
127 $attr . '>'.htmlspecialchars($text) ."</textarea>\n";
128 }
129
130 /**
131 * Make a <form> start-tag.
132 */
133 function addForm($action, $method = 'post', $name = '', $enctype = '', $charset = '')
134 {
135 if($name) {
136 $name = ' name="'.$name.'"';
137 }
138 if($enctype) {
139 $enctype = ' enctype="'.$enctype.'"';
140 }
141 if($charset) {
142 $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
143 }
144
145 return '<form action="'. $action .'" method="'. $method .'"'.
146 $enctype . $name . $charset . ">\n";
147 }
148
149 ?>