Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * CodeIgniter Form Helpers
|
| 20 | *
|
| 21 | * @package CodeIgniter
|
| 22 | * @subpackage Helpers
|
| 23 | * @category Helpers
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 24 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/helpers/form_helper.html
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 26 | */
|
| 27 |
|
| 28 | // ------------------------------------------------------------------------
|
| 29 |
|
| 30 | /**
|
| 31 | * Form Declaration
|
| 32 | *
|
| 33 | * Creates the opening portion of the form.
|
| 34 | *
|
| 35 | * @access public
|
| 36 | * @param string the URI segments of the form destination
|
| 37 | * @param array a key/value pair of attributes
|
| 38 | * @param array a key/value pair hidden data
|
| 39 | * @return string
|
| 40 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 41 | if (! function_exists('form_open'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 42 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 43 | function form_open($action = '', $attributes = array(), $hidden = array())
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 44 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 45 | $CI =& get_instance();
|
| 46 |
|
| 47 | $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
|
| 48 |
|
| 49 | $form = '<form action="'.$action.'"';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 50 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 51 | if ( ! isset($attributes['method']))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 52 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 53 | $form .= ' method="post"';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 54 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 55 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 56 | if (is_array($attributes) AND count($attributes) > 0)
|
| 57 | {
|
| 58 | foreach ($attributes as $key => $val)
|
| 59 | {
|
| 60 | $form .= ' '.$key.'="'.$val.'"';
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | $form .= '>';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 65 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 66 | if (is_array($hidden) AND count($hidden > 0))
|
| 67 | {
|
| 68 | $form .= form_hidden($hidden);
|
| 69 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 70 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 71 | return $form;
|
| 72 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 73 | }
|
| 74 |
|
| 75 | // ------------------------------------------------------------------------
|
| 76 |
|
| 77 | /**
|
| 78 | * Form Declaration - Multipart type
|
| 79 | *
|
| 80 | * Creates the opening portion of the form, but with "multipart/form-data".
|
| 81 | *
|
| 82 | * @access public
|
| 83 | * @param string the URI segments of the form destination
|
| 84 | * @param array a key/value pair of attributes
|
| 85 | * @param array a key/value pair hidden data
|
| 86 | * @return string
|
| 87 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 88 | if (! function_exists('form_open_multipart'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 89 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 90 | function form_open_multipart($action, $attributes = array(), $hidden = array())
|
| 91 | {
|
| 92 | $attributes['enctype'] = 'multipart/form-data';
|
| 93 | return form_open($action, $attributes, $hidden);
|
| 94 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 95 | }
|
| 96 |
|
| 97 | // ------------------------------------------------------------------------
|
| 98 |
|
| 99 | /**
|
| 100 | * Hidden Input Field
|
| 101 | *
|
| 102 | * Generates hidden fields. You can pass a simple key/value string or an associative
|
| 103 | * array with multiple values.
|
| 104 | *
|
| 105 | * @access public
|
| 106 | * @param mixed
|
| 107 | * @param string
|
| 108 | * @return string
|
| 109 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 110 | if (! function_exists('form_hidden'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 111 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 112 | function form_hidden($name, $value = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 113 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 114 | if ( ! is_array($name))
|
| 115 | {
|
| 116 | return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 117 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 118 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 119 | $form = '';
|
| 120 | foreach ($name as $name => $value)
|
| 121 | {
|
| 122 | $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 123 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 124 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 125 | return $form;
|
| 126 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 127 | }
|
| 128 |
|
| 129 | // ------------------------------------------------------------------------
|
| 130 |
|
| 131 | /**
|
| 132 | * Text Input Field
|
| 133 | *
|
| 134 | * @access public
|
| 135 | * @param mixed
|
| 136 | * @param string
|
| 137 | * @param string
|
| 138 | * @return string
|
| 139 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 140 | if (! function_exists('form_input'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 141 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 142 | function form_input($data = '', $value = '', $extra = '')
|
| 143 | {
|
| 144 | $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 145 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 146 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 147 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 148 | }
|
| 149 |
|
| 150 | // ------------------------------------------------------------------------
|
| 151 |
|
| 152 | /**
|
| 153 | * Password Field
|
| 154 | *
|
| 155 | * Identical to the input function but adds the "password" type
|
| 156 | *
|
| 157 | * @access public
|
| 158 | * @param mixed
|
| 159 | * @param string
|
| 160 | * @param string
|
| 161 | * @return string
|
| 162 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 163 | if (! function_exists('form_password'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 164 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 165 | function form_password($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 166 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 167 | if ( ! is_array($data))
|
| 168 | {
|
| 169 | $data = array('name' => $data);
|
| 170 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 171 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 172 | $data['type'] = 'password';
|
| 173 | return form_input($data, $value, $extra);
|
| 174 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 175 | }
|
| 176 |
|
| 177 | // ------------------------------------------------------------------------
|
| 178 |
|
| 179 | /**
|
| 180 | * Upload Field
|
| 181 | *
|
| 182 | * Identical to the input function but adds the "file" type
|
| 183 | *
|
| 184 | * @access public
|
| 185 | * @param mixed
|
| 186 | * @param string
|
| 187 | * @param string
|
| 188 | * @return string
|
| 189 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 190 | if (! function_exists('form_upload'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 191 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 192 | function form_upload($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 193 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 194 | if ( ! is_array($data))
|
| 195 | {
|
| 196 | $data = array('name' => $data);
|
| 197 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 198 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 199 | $data['type'] = 'file';
|
| 200 | return form_input($data, $value, $extra);
|
| 201 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 202 | }
|
| 203 |
|
| 204 | // ------------------------------------------------------------------------
|
| 205 |
|
| 206 | /**
|
| 207 | * Textarea field
|
| 208 | *
|
| 209 | * @access public
|
| 210 | * @param mixed
|
| 211 | * @param string
|
| 212 | * @param string
|
| 213 | * @return string
|
| 214 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 215 | if (! function_exists('form_textarea'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 216 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 217 | function form_textarea($data = '', $value = '', $extra = '')
|
| 218 | {
|
| 219 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 220 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 221 | if ( ! is_array($data) OR ! isset($data['value']))
|
| 222 | {
|
| 223 | $val = $value;
|
| 224 | }
|
| 225 | else
|
| 226 | {
|
| 227 | $val = $data['value'];
|
| 228 | unset($data['value']); // textareas don't use the value attribute
|
| 229 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 230 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 231 | return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
|
| 232 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 233 | }
|
| 234 |
|
| 235 | // ------------------------------------------------------------------------
|
| 236 |
|
| 237 | /**
|
| 238 | * Drop-down Menu
|
| 239 | *
|
| 240 | * @access public
|
| 241 | * @param string
|
| 242 | * @param array
|
| 243 | * @param string
|
| 244 | * @param string
|
| 245 | * @return string
|
| 246 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 247 | if (! function_exists('form_dropdown'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 248 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 249 | function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 250 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 251 | if ( ! is_array($selected))
|
| 252 | {
|
| 253 | $selected = array($selected);
|
| 254 | }
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 255 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 256 | if ($extra != '') $extra = ' '.$extra;
|
Derek Allard | f8f0570 | 2008-01-18 14:39:23 +0000 | [diff] [blame] | 257 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 258 | $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 259 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 260 | $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 261 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 262 | foreach ($options as $key => $val)
|
| 263 | {
|
| 264 | $key = (string) $key;
|
| 265 | $val = (string) $val;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 266 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 267 | $sel = (in_array($key, $selected))?' selected="selected"':'';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 268 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 269 | $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
|
| 270 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 271 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 272 | $form .= '</select>';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 273 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 274 | return $form;
|
| 275 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 276 | }
|
| 277 |
|
| 278 | // ------------------------------------------------------------------------
|
| 279 |
|
| 280 | /**
|
| 281 | * Checkbox Field
|
| 282 | *
|
| 283 | * @access public
|
| 284 | * @param mixed
|
| 285 | * @param string
|
| 286 | * @param bool
|
| 287 | * @param string
|
| 288 | * @return string
|
| 289 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 290 | if (! function_exists('form_checkbox'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 291 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 292 | function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 293 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 294 | $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 295 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 296 | if (is_array($data) AND array_key_exists('checked', $data))
|
| 297 | {
|
| 298 | $checked = $data['checked'];
|
| 299 |
|
| 300 | if ($checked == FALSE)
|
| 301 | {
|
| 302 | unset($data['checked']);
|
| 303 | }
|
| 304 | else
|
| 305 | {
|
| 306 | $data['checked'] = 'checked';
|
| 307 | }
|
| 308 | }
|
| 309 |
|
| 310 | if ($checked == TRUE)
|
| 311 | $defaults['checked'] = 'checked';
|
| 312 | else
|
| 313 | unset($defaults['checked']);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 314 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 315 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 316 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 317 | }
|
| 318 |
|
| 319 | // ------------------------------------------------------------------------
|
| 320 |
|
| 321 | /**
|
| 322 | * Radio Button
|
| 323 | *
|
| 324 | * @access public
|
| 325 | * @param mixed
|
| 326 | * @param string
|
| 327 | * @param bool
|
| 328 | * @param string
|
| 329 | * @return string
|
| 330 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 331 | if (! function_exists('form_radio'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 332 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 333 | function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
|
| 334 | {
|
| 335 | if ( ! is_array($data))
|
| 336 | {
|
| 337 | $data = array('name' => $data);
|
| 338 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 339 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 340 | $data['type'] = 'radio';
|
| 341 | return form_checkbox($data, $value, $checked, $extra);
|
| 342 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 343 | }
|
| 344 |
|
| 345 | // ------------------------------------------------------------------------
|
| 346 |
|
| 347 | /**
|
| 348 | * Submit Button
|
| 349 | *
|
| 350 | * @access public
|
| 351 | * @param mixed
|
| 352 | * @param string
|
| 353 | * @param string
|
| 354 | * @return string
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 355 | */
|
| 356 | if (! function_exists('form_submit'))
|
| 357 | {
|
| 358 | function form_submit($data = '', $value = '', $extra = '')
|
| 359 | {
|
| 360 | $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 361 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 362 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 363 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 364 | }
|
| 365 |
|
| 366 | // ------------------------------------------------------------------------
|
| 367 |
|
| 368 | /**
|
| 369 | * Reset Button
|
| 370 | *
|
| 371 | * @access public
|
| 372 | * @param mixed
|
| 373 | * @param string
|
| 374 | * @param string
|
| 375 | * @return string
|
| 376 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 377 | if (! function_exists('form_reset'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 378 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 379 | function form_reset($data = '', $value = '', $extra = '')
|
| 380 | {
|
| 381 | $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 382 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 383 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 384 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 385 | }
|
| 386 |
|
| 387 | // ------------------------------------------------------------------------
|
| 388 |
|
| 389 | /**
|
| 390 | * Form Label Tag
|
| 391 | *
|
| 392 | * @access public
|
| 393 | * @param string The text to appear onscreen
|
| 394 | * @param string The id the label applies to
|
| 395 | * @param string Additional attributes
|
| 396 | * @return string
|
| 397 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 398 | if (! function_exists('form_label'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 399 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 400 | function form_label($label_text = '', $id = '', $attributes = array())
|
| 401 | {
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 402 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 403 | $label = '<label';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 404 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 405 | if ($id != '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 406 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 407 | $label .= " for=\"$id\"";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 408 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 409 |
|
| 410 | if (is_array($attributes) AND count($attributes) > 0)
|
| 411 | {
|
| 412 | foreach ($attributes as $key => $val)
|
| 413 | {
|
| 414 | $label .= ' '.$key.'="'.$val.'"';
|
| 415 | }
|
| 416 | }
|
| 417 |
|
| 418 | $label .= ">$label_text</label>";
|
| 419 |
|
| 420 | return $label;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 421 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 422 | }
|
| 423 |
|
| 424 | // ------------------------------------------------------------------------
|
| 425 | /**
|
| 426 | * Fieldset Tag
|
| 427 | *
|
| 428 | * Used to produce <fieldset><legend>text</legend>. To close fieldset
|
| 429 | * use form_fieldset_close()
|
| 430 | *
|
| 431 | * @access public
|
| 432 | * @param string The legend text
|
| 433 | * @param string Additional attributes
|
| 434 | * @return string
|
| 435 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 436 | if (! function_exists('form_fieldset'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 437 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 438 | function form_fieldset($legend_text = '', $attributes = array())
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 439 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 440 |
|
| 441 | $fieldset = "<fieldset";
|
| 442 |
|
| 443 | if (is_array($attributes) AND count($attributes) > 0)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 444 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 445 | foreach ($attributes as $key => $val)
|
| 446 | {
|
| 447 | $fieldset .= ' '.$key.'="'.$val.'"';
|
| 448 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 449 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 450 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 451 | $fieldset .= ">\n";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 452 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 453 | if ($legend_text != '')
|
| 454 | {
|
| 455 | $fieldset .= "<legend>$legend_text</legend>\n";
|
| 456 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 457 |
|
| 458 |
|
| 459 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 460 | return $fieldset;
|
| 461 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 462 | }
|
| 463 |
|
| 464 | // ------------------------------------------------------------------------
|
| 465 |
|
| 466 | /**
|
| 467 | * Fieldset Close Tag
|
| 468 | *
|
| 469 | * @access public
|
| 470 | * @param string
|
| 471 | * @return string
|
| 472 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 473 | if (! function_exists('form_fieldset_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 474 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 475 | function form_fieldset_close($extra = '')
|
| 476 | {
|
| 477 | return "</fieldset>\n".$extra;
|
| 478 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 479 | }
|
| 480 |
|
| 481 | // ------------------------------------------------------------------------
|
| 482 |
|
| 483 | /**
|
| 484 | * Form Close Tag
|
| 485 | *
|
| 486 | * @access public
|
| 487 | * @param string
|
| 488 | * @return string
|
| 489 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 490 | if (! function_exists('form_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 491 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 492 | function form_close($extra = '')
|
| 493 | {
|
| 494 | return "</form>\n".$extra;
|
| 495 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 496 | }
|
| 497 |
|
| 498 | // ------------------------------------------------------------------------
|
| 499 |
|
| 500 | /**
|
| 501 | * Form Prep
|
| 502 | *
|
| 503 | * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
|
| 504 | *
|
| 505 | * @access public
|
| 506 | * @param string
|
| 507 | * @return string
|
| 508 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 509 | if (! function_exists('form_prep'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 510 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 511 | function form_prep($str = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 512 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 513 | if ($str === '')
|
| 514 | {
|
| 515 | return '';
|
| 516 | }
|
| 517 |
|
| 518 | $temp = '__TEMP_AMPERSANDS__';
|
| 519 |
|
| 520 | // Replace entities to temporary markers so that
|
| 521 | // htmlspecialchars won't mess them up
|
| 522 | $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
|
| 523 | $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
|
| 524 |
|
| 525 | $str = htmlspecialchars($str);
|
| 526 |
|
| 527 | // In case htmlspecialchars misses these.
|
| 528 | $str = str_replace(array("'", '"'), array("'", """), $str);
|
| 529 |
|
| 530 | // Decode the temp markers back to entities
|
| 531 | $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
|
| 532 | $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
|
| 533 |
|
| 534 | return $str;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 535 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 536 | }
|
| 537 |
|
| 538 | // ------------------------------------------------------------------------
|
| 539 |
|
| 540 | /**
|
| 541 | * Parse the form attributes
|
| 542 | *
|
| 543 | * Helper function used by some of the form helpers
|
| 544 | *
|
| 545 | * @access private
|
| 546 | * @param array
|
| 547 | * @param array
|
| 548 | * @return string
|
| 549 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 550 | if (! function_exists('parse_form_attributes'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 551 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 552 | function parse_form_attributes($attributes, $default)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 553 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 554 | if (is_array($attributes))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 555 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 556 | foreach ($default as $key => $val)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 557 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 558 | if (isset($attributes[$key]))
|
| 559 | {
|
| 560 | $default[$key] = $attributes[$key];
|
| 561 | unset($attributes[$key]);
|
| 562 | }
|
| 563 | }
|
| 564 |
|
| 565 | if (count($attributes) > 0)
|
| 566 | {
|
| 567 | $default = array_merge($default, $attributes);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 568 | }
|
| 569 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 570 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 571 | $att = '';
|
| 572 | foreach ($default as $key => $val)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 573 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 574 | if ($key == 'value')
|
| 575 | {
|
| 576 | $val = form_prep($val);
|
| 577 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 578 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 579 | $att .= $key . '="' . $val . '" ';
|
| 580 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 581 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 582 | return $att;
|
| 583 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 584 | }
|
| 585 |
|
| 586 | ?> |