Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 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 |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/helpers/form_helper.html |
| 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 40 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | if ( ! function_exists('form_open')) |
| 42 | { |
| 43 | function form_open($action = '', $attributes = '', $hidden = array()) |
| 44 | { |
| 45 | $CI =& get_instance(); |
| 46 | |
| 47 | if ($attributes == '') |
| 48 | { |
Derek Allard | 3241d73 | 2009-09-17 12:17:45 +0000 | [diff] [blame] | 49 | $attributes = 'method="post"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Phil Sturgeon | 9d0e617 | 2011-04-02 12:43:55 +0100 | [diff] [blame] | 52 | // If an action is not a full URL then turn it into one |
Phil Sturgeon | 133beaf | 2011-03-10 16:38:32 +0000 | [diff] [blame] | 53 | if ($action && strpos($action, '://') === FALSE) |
| 54 | { |
Phil Sturgeon | 52e7318 | 2011-03-11 10:17:01 +0000 | [diff] [blame] | 55 | $action = $CI->config->site_url($action); |
Phil Sturgeon | 133beaf | 2011-03-10 16:38:32 +0000 | [diff] [blame] | 56 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | |
Phil Sturgeon | 9d0e617 | 2011-04-02 12:43:55 +0100 | [diff] [blame] | 58 | // If no action is provided then set to the current url |
| 59 | $action OR $action = $CI->config->site_url($CI->uri->uri_string()); |
| 60 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | $form = '<form action="'.$action.'"'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 62 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | $form .= _attributes_to_string($attributes, TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | $form .= '>'; |
| 66 | |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 67 | // CSRF |
| 68 | if ($CI->config->item('csrf_protection') === TRUE) |
| 69 | { |
Greg Aker | 1f6f0ab | 2011-04-07 18:24:53 -0500 | [diff] [blame] | 70 | $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash(); |
Greg Aker | 28b425a | 2010-09-15 11:43:23 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (is_array($hidden) AND count($hidden) > 0) |
| 74 | { |
Greg Aker | 9ce4385 | 2011-04-19 12:58:52 -0500 | [diff] [blame] | 75 | $form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden)); |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | return $form; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // ------------------------------------------------------------------------ |
| 83 | |
| 84 | /** |
| 85 | * Form Declaration - Multipart type |
| 86 | * |
| 87 | * Creates the opening portion of the form, but with "multipart/form-data". |
| 88 | * |
| 89 | * @access public |
| 90 | * @param string the URI segments of the form destination |
| 91 | * @param array a key/value pair of attributes |
| 92 | * @param array a key/value pair hidden data |
| 93 | * @return string |
| 94 | */ |
| 95 | if ( ! function_exists('form_open_multipart')) |
| 96 | { |
| 97 | function form_open_multipart($action, $attributes = array(), $hidden = array()) |
| 98 | { |
Derek Allard | 33d4b6a | 2010-01-17 07:23:00 +0000 | [diff] [blame] | 99 | if (is_string($attributes)) |
| 100 | { |
| 101 | $attributes .= ' enctype="multipart/form-data"'; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | $attributes['enctype'] = 'multipart/form-data'; |
| 106 | } |
| 107 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | return form_open($action, $attributes, $hidden); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | |
| 114 | /** |
| 115 | * Hidden Input Field |
| 116 | * |
| 117 | * Generates hidden fields. You can pass a simple key/value string or an associative |
| 118 | * array with multiple values. |
| 119 | * |
| 120 | * @access public |
| 121 | * @param mixed |
| 122 | * @param string |
| 123 | * @return string |
| 124 | */ |
| 125 | if ( ! function_exists('form_hidden')) |
| 126 | { |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 127 | function form_hidden($name, $value = '', $recursing = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | { |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 129 | static $form; |
| 130 | |
| 131 | if ($recursing === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 133 | $form = "\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 136 | if (is_array($name)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | { |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 138 | foreach ($name as $key => $val) |
| 139 | { |
| 140 | form_hidden($key, $val, TRUE); |
| 141 | } |
| 142 | return $form; |
| 143 | } |
| 144 | |
| 145 | if ( ! is_array($value)) |
| 146 | { |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 147 | $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n"; |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 148 | } |
| 149 | else |
| 150 | { |
| 151 | foreach ($value as $k => $v) |
| 152 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | $k = (is_int($k)) ? '' : $k; |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 154 | form_hidden($name.'['.$k.']', $v, TRUE); |
| 155 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return $form; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // ------------------------------------------------------------------------ |
| 163 | |
| 164 | /** |
| 165 | * Text Input Field |
| 166 | * |
| 167 | * @access public |
| 168 | * @param mixed |
| 169 | * @param string |
| 170 | * @param string |
| 171 | * @return string |
| 172 | */ |
| 173 | if ( ! function_exists('form_input')) |
| 174 | { |
| 175 | function form_input($data = '', $value = '', $extra = '') |
| 176 | { |
| 177 | $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); |
| 178 | |
| 179 | return "<input "._parse_form_attributes($data, $defaults).$extra." />"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // ------------------------------------------------------------------------ |
| 184 | |
| 185 | /** |
| 186 | * Password Field |
| 187 | * |
| 188 | * Identical to the input function but adds the "password" type |
| 189 | * |
| 190 | * @access public |
| 191 | * @param mixed |
| 192 | * @param string |
| 193 | * @param string |
| 194 | * @return string |
| 195 | */ |
| 196 | if ( ! function_exists('form_password')) |
| 197 | { |
| 198 | function form_password($data = '', $value = '', $extra = '') |
| 199 | { |
| 200 | if ( ! is_array($data)) |
| 201 | { |
| 202 | $data = array('name' => $data); |
| 203 | } |
| 204 | |
| 205 | $data['type'] = 'password'; |
| 206 | return form_input($data, $value, $extra); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // ------------------------------------------------------------------------ |
| 211 | |
| 212 | /** |
| 213 | * Upload Field |
| 214 | * |
| 215 | * Identical to the input function but adds the "file" type |
| 216 | * |
| 217 | * @access public |
| 218 | * @param mixed |
| 219 | * @param string |
| 220 | * @param string |
| 221 | * @return string |
| 222 | */ |
| 223 | if ( ! function_exists('form_upload')) |
| 224 | { |
| 225 | function form_upload($data = '', $value = '', $extra = '') |
| 226 | { |
| 227 | if ( ! is_array($data)) |
| 228 | { |
| 229 | $data = array('name' => $data); |
| 230 | } |
| 231 | |
| 232 | $data['type'] = 'file'; |
| 233 | return form_input($data, $value, $extra); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // ------------------------------------------------------------------------ |
| 238 | |
| 239 | /** |
| 240 | * Textarea field |
| 241 | * |
| 242 | * @access public |
| 243 | * @param mixed |
| 244 | * @param string |
| 245 | * @param string |
| 246 | * @return string |
| 247 | */ |
| 248 | if ( ! function_exists('form_textarea')) |
| 249 | { |
| 250 | function form_textarea($data = '', $value = '', $extra = '') |
| 251 | { |
| 252 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12'); |
| 253 | |
| 254 | if ( ! is_array($data) OR ! isset($data['value'])) |
| 255 | { |
| 256 | $val = $value; |
| 257 | } |
| 258 | else |
| 259 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 260 | $val = $data['value']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | unset($data['value']); // textareas don't use the value attribute |
| 262 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 263 | |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 264 | $name = (is_array($data)) ? $data['name'] : $data; |
| 265 | return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | // ------------------------------------------------------------------------ |
| 270 | |
| 271 | /** |
Derek Jones | 2639929 | 2009-04-08 16:14:17 +0000 | [diff] [blame] | 272 | * Multi-select menu |
| 273 | * |
| 274 | * @access public |
| 275 | * @param string |
| 276 | * @param array |
| 277 | * @param mixed |
| 278 | * @param string |
| 279 | * @return type |
| 280 | */ |
Derek Jones | 68788d5 | 2010-03-05 10:11:31 -0600 | [diff] [blame] | 281 | if ( ! function_exists('form_multiselect')) |
Derek Jones | 2639929 | 2009-04-08 16:14:17 +0000 | [diff] [blame] | 282 | { |
| 283 | function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '') |
| 284 | { |
| 285 | if ( ! strpos($extra, 'multiple')) |
| 286 | { |
| 287 | $extra .= ' multiple="multiple"'; |
| 288 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Jones | 2639929 | 2009-04-08 16:14:17 +0000 | [diff] [blame] | 290 | return form_dropdown($name, $options, $selected, $extra); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // -------------------------------------------------------------------- |
| 295 | |
| 296 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | * Drop-down Menu |
| 298 | * |
| 299 | * @access public |
| 300 | * @param string |
| 301 | * @param array |
| 302 | * @param string |
| 303 | * @param string |
| 304 | * @return string |
| 305 | */ |
| 306 | if ( ! function_exists('form_dropdown')) |
| 307 | { |
| 308 | function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '') |
| 309 | { |
| 310 | if ( ! is_array($selected)) |
| 311 | { |
| 312 | $selected = array($selected); |
| 313 | } |
| 314 | |
| 315 | // If no selected state was submitted we will attempt to set it automatically |
| 316 | if (count($selected) === 0) |
| 317 | { |
| 318 | // If the form name appears in the $_POST array we have a winner! |
| 319 | if (isset($_POST[$name])) |
| 320 | { |
| 321 | $selected = array($_POST[$name]); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if ($extra != '') $extra = ' '.$extra; |
| 326 | |
| 327 | $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; |
| 328 | |
| 329 | $form = '<select name="'.$name.'"'.$extra.$multiple.">\n"; |
Robin Sowell | 57fe410 | 2009-03-26 18:58:46 +0000 | [diff] [blame] | 330 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | foreach ($options as $key => $val) |
| 332 | { |
| 333 | $key = (string) $key; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | |
Derek Jones | 68788d5 | 2010-03-05 10:11:31 -0600 | [diff] [blame] | 335 | if (is_array($val) && ! empty($val)) |
Derek Allard | 78a5fc9 | 2009-02-05 16:34:35 +0000 | [diff] [blame] | 336 | { |
| 337 | $form .= '<optgroup label="'.$key.'">'."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | |
Derek Allard | 78a5fc9 | 2009-02-05 16:34:35 +0000 | [diff] [blame] | 339 | foreach ($val as $optgroup_key => $optgroup_val) |
| 340 | { |
Derek Allard | 86a840c | 2009-02-05 17:31:58 +0000 | [diff] [blame] | 341 | $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : ''; |
Derek Allard | 78a5fc9 | 2009-02-05 16:34:35 +0000 | [diff] [blame] | 342 | |
| 343 | $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n"; |
| 344 | } |
| 345 | |
| 346 | $form .= '</optgroup>'."\n"; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | $sel = (in_array($key, $selected)) ? ' selected="selected"' : ''; |
| 351 | |
| 352 | $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n"; |
| 353 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | $form .= '</select>'; |
| 357 | |
| 358 | return $form; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // ------------------------------------------------------------------------ |
| 363 | |
| 364 | /** |
| 365 | * Checkbox Field |
| 366 | * |
| 367 | * @access public |
| 368 | * @param mixed |
| 369 | * @param string |
| 370 | * @param bool |
| 371 | * @param string |
| 372 | * @return string |
| 373 | */ |
| 374 | if ( ! function_exists('form_checkbox')) |
| 375 | { |
| 376 | function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '') |
| 377 | { |
| 378 | $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); |
| 379 | |
| 380 | if (is_array($data) AND array_key_exists('checked', $data)) |
| 381 | { |
| 382 | $checked = $data['checked']; |
| 383 | |
| 384 | if ($checked == FALSE) |
| 385 | { |
| 386 | unset($data['checked']); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | $data['checked'] = 'checked'; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if ($checked == TRUE) |
| 395 | { |
| 396 | $defaults['checked'] = 'checked'; |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | unset($defaults['checked']); |
| 401 | } |
| 402 | |
| 403 | return "<input "._parse_form_attributes($data, $defaults).$extra." />"; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // ------------------------------------------------------------------------ |
| 408 | |
| 409 | /** |
| 410 | * Radio Button |
| 411 | * |
| 412 | * @access public |
| 413 | * @param mixed |
| 414 | * @param string |
| 415 | * @param bool |
| 416 | * @param string |
| 417 | * @return string |
| 418 | */ |
| 419 | if ( ! function_exists('form_radio')) |
| 420 | { |
| 421 | function form_radio($data = '', $value = '', $checked = FALSE, $extra = '') |
| 422 | { |
| 423 | if ( ! is_array($data)) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | $data = array('name' => $data); |
| 426 | } |
| 427 | |
| 428 | $data['type'] = 'radio'; |
| 429 | return form_checkbox($data, $value, $checked, $extra); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // ------------------------------------------------------------------------ |
| 434 | |
| 435 | /** |
| 436 | * Submit Button |
| 437 | * |
| 438 | * @access public |
| 439 | * @param mixed |
| 440 | * @param string |
| 441 | * @param string |
| 442 | * @return string |
| 443 | */ |
| 444 | if ( ! function_exists('form_submit')) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | function form_submit($data = '', $value = '', $extra = '') |
| 447 | { |
| 448 | $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); |
| 449 | |
| 450 | return "<input "._parse_form_attributes($data, $defaults).$extra." />"; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | // ------------------------------------------------------------------------ |
| 455 | |
| 456 | /** |
| 457 | * Reset Button |
| 458 | * |
| 459 | * @access public |
| 460 | * @param mixed |
| 461 | * @param string |
| 462 | * @param string |
| 463 | * @return string |
| 464 | */ |
| 465 | if ( ! function_exists('form_reset')) |
| 466 | { |
| 467 | function form_reset($data = '', $value = '', $extra = '') |
| 468 | { |
| 469 | $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); |
| 470 | |
| 471 | return "<input "._parse_form_attributes($data, $defaults).$extra." />"; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // ------------------------------------------------------------------------ |
| 476 | |
| 477 | /** |
| 478 | * Form Button |
| 479 | * |
| 480 | * @access public |
| 481 | * @param mixed |
| 482 | * @param string |
| 483 | * @param string |
| 484 | * @return string |
| 485 | */ |
| 486 | if ( ! function_exists('form_button')) |
| 487 | { |
| 488 | function form_button($data = '', $content = '', $extra = '') |
| 489 | { |
Derek Allard | 904094a | 2009-02-10 14:00:34 +0000 | [diff] [blame] | 490 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | |
| 492 | if ( is_array($data) AND isset($data['content'])) |
| 493 | { |
| 494 | $content = $data['content']; |
| 495 | unset($data['content']); // content is not an attribute |
| 496 | } |
| 497 | |
| 498 | return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>"; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // ------------------------------------------------------------------------ |
| 503 | |
| 504 | /** |
| 505 | * Form Label Tag |
| 506 | * |
| 507 | * @access public |
| 508 | * @param string The text to appear onscreen |
| 509 | * @param string The id the label applies to |
| 510 | * @param string Additional attributes |
| 511 | * @return string |
| 512 | */ |
| 513 | if ( ! function_exists('form_label')) |
| 514 | { |
| 515 | function form_label($label_text = '', $id = '', $attributes = array()) |
| 516 | { |
| 517 | |
| 518 | $label = '<label'; |
| 519 | |
| 520 | if ($id != '') |
| 521 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 522 | $label .= " for=\"$id\""; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (is_array($attributes) AND count($attributes) > 0) |
| 526 | { |
| 527 | foreach ($attributes as $key => $val) |
| 528 | { |
| 529 | $label .= ' '.$key.'="'.$val.'"'; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | $label .= ">$label_text</label>"; |
| 534 | |
| 535 | return $label; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | // ------------------------------------------------------------------------ |
| 540 | /** |
| 541 | * Fieldset Tag |
| 542 | * |
| 543 | * Used to produce <fieldset><legend>text</legend>. To close fieldset |
| 544 | * use form_fieldset_close() |
| 545 | * |
| 546 | * @access public |
| 547 | * @param string The legend text |
| 548 | * @param string Additional attributes |
| 549 | * @return string |
| 550 | */ |
| 551 | if ( ! function_exists('form_fieldset')) |
| 552 | { |
| 553 | function form_fieldset($legend_text = '', $attributes = array()) |
| 554 | { |
| 555 | $fieldset = "<fieldset"; |
| 556 | |
| 557 | $fieldset .= _attributes_to_string($attributes, FALSE); |
| 558 | |
| 559 | $fieldset .= ">\n"; |
| 560 | |
| 561 | if ($legend_text != '') |
| 562 | { |
| 563 | $fieldset .= "<legend>$legend_text</legend>\n"; |
| 564 | } |
| 565 | |
| 566 | return $fieldset; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // ------------------------------------------------------------------------ |
| 571 | |
| 572 | /** |
| 573 | * Fieldset Close Tag |
| 574 | * |
| 575 | * @access public |
| 576 | * @param string |
| 577 | * @return string |
| 578 | */ |
| 579 | if ( ! function_exists('form_fieldset_close')) |
| 580 | { |
| 581 | function form_fieldset_close($extra = '') |
| 582 | { |
| 583 | return "</fieldset>".$extra; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // ------------------------------------------------------------------------ |
| 588 | |
| 589 | /** |
| 590 | * Form Close Tag |
| 591 | * |
| 592 | * @access public |
| 593 | * @param string |
| 594 | * @return string |
| 595 | */ |
| 596 | if ( ! function_exists('form_close')) |
| 597 | { |
| 598 | function form_close($extra = '') |
| 599 | { |
| 600 | return "</form>".$extra; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | // ------------------------------------------------------------------------ |
| 605 | |
| 606 | /** |
| 607 | * Form Prep |
| 608 | * |
| 609 | * Formats text so that it can be safely placed in a form field in the event it has HTML tags. |
| 610 | * |
| 611 | * @access public |
| 612 | * @param string |
| 613 | * @return string |
| 614 | */ |
| 615 | if ( ! function_exists('form_prep')) |
| 616 | { |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 617 | function form_prep($str = '', $field_name = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 618 | { |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 619 | static $prepped_fields = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 620 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 621 | // if the field name is an array we do this recursively |
| 622 | if (is_array($str)) |
| 623 | { |
| 624 | foreach ($str as $key => $val) |
| 625 | { |
| 626 | $str[$key] = form_prep($val); |
| 627 | } |
| 628 | |
| 629 | return $str; |
| 630 | } |
| 631 | |
| 632 | if ($str === '') |
| 633 | { |
| 634 | return ''; |
| 635 | } |
| 636 | |
Derek Jones | 3eb9fac | 2009-07-17 20:25:13 +0000 | [diff] [blame] | 637 | // we've already prepped a field with this name |
| 638 | // @todo need to figure out a way to namespace this so |
| 639 | // that we know the *exact* field and not just one with |
| 640 | // the same name |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 641 | if (isset($prepped_fields[$field_name])) |
| 642 | { |
Derek Jones | 3eb9fac | 2009-07-17 20:25:13 +0000 | [diff] [blame] | 643 | return $str; |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 644 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 645 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 646 | $str = htmlspecialchars($str); |
| 647 | |
| 648 | // In case htmlspecialchars misses these. |
| 649 | $str = str_replace(array("'", '"'), array("'", """), $str); |
| 650 | |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 651 | if ($field_name != '') |
| 652 | { |
Derek Jones | 68788d5 | 2010-03-05 10:11:31 -0600 | [diff] [blame] | 653 | $prepped_fields[$field_name] = $field_name; |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 654 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 655 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 656 | return $str; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // ------------------------------------------------------------------------ |
| 661 | |
| 662 | /** |
| 663 | * Form Value |
| 664 | * |
| 665 | * Grabs a value from the POST array for the specified field so you can |
| 666 | * re-populate an input field or textarea. If Form Validation |
| 667 | * is active it retrieves the info from the validation class |
| 668 | * |
| 669 | * @access public |
| 670 | * @param string |
| 671 | * @return mixed |
| 672 | */ |
| 673 | if ( ! function_exists('set_value')) |
| 674 | { |
| 675 | function set_value($field = '', $default = '') |
| 676 | { |
| 677 | if (FALSE === ($OBJ =& _get_validation_object())) |
| 678 | { |
| 679 | if ( ! isset($_POST[$field])) |
| 680 | { |
| 681 | return $default; |
| 682 | } |
| 683 | |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 684 | return form_prep($_POST[$field], $field); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 687 | return form_prep($OBJ->set_value($field, $default), $field); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | // ------------------------------------------------------------------------ |
| 692 | |
| 693 | /** |
| 694 | * Set Select |
| 695 | * |
| 696 | * Let's you set the selected value of a <select> menu via data in the POST array. |
| 697 | * If Form Validation is active it retrieves the info from the validation class |
| 698 | * |
| 699 | * @access public |
| 700 | * @param string |
| 701 | * @param string |
| 702 | * @param bool |
| 703 | * @return string |
| 704 | */ |
| 705 | if ( ! function_exists('set_select')) |
| 706 | { |
| 707 | function set_select($field = '', $value = '', $default = FALSE) |
| 708 | { |
| 709 | $OBJ =& _get_validation_object(); |
| 710 | |
| 711 | if ($OBJ === FALSE) |
| 712 | { |
| 713 | if ( ! isset($_POST[$field])) |
| 714 | { |
Rick Ellis | 28e5c8f | 2009-03-09 22:36:48 +0000 | [diff] [blame] | 715 | if (count($_POST) === 0 AND $default == TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 716 | { |
| 717 | return ' selected="selected"'; |
| 718 | } |
| 719 | return ''; |
| 720 | } |
| 721 | |
| 722 | $field = $_POST[$field]; |
| 723 | |
| 724 | if (is_array($field)) |
| 725 | { |
| 726 | if ( ! in_array($value, $field)) |
| 727 | { |
| 728 | return ''; |
| 729 | } |
| 730 | } |
| 731 | else |
| 732 | { |
| 733 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 734 | { |
| 735 | return ''; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | return ' selected="selected"'; |
| 740 | } |
| 741 | |
| 742 | return $OBJ->set_select($field, $value, $default); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | // ------------------------------------------------------------------------ |
| 747 | |
| 748 | /** |
| 749 | * Set Checkbox |
| 750 | * |
| 751 | * Let's you set the selected value of a checkbox via the value in the POST array. |
| 752 | * If Form Validation is active it retrieves the info from the validation class |
| 753 | * |
| 754 | * @access public |
| 755 | * @param string |
| 756 | * @param string |
| 757 | * @param bool |
| 758 | * @return string |
| 759 | */ |
| 760 | if ( ! function_exists('set_checkbox')) |
| 761 | { |
| 762 | function set_checkbox($field = '', $value = '', $default = FALSE) |
| 763 | { |
| 764 | $OBJ =& _get_validation_object(); |
| 765 | |
| 766 | if ($OBJ === FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 767 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 768 | if ( ! isset($_POST[$field])) |
| 769 | { |
Rick Ellis | 28e5c8f | 2009-03-09 22:36:48 +0000 | [diff] [blame] | 770 | if (count($_POST) === 0 AND $default == TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 771 | { |
| 772 | return ' checked="checked"'; |
| 773 | } |
| 774 | return ''; |
| 775 | } |
| 776 | |
| 777 | $field = $_POST[$field]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 778 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 779 | if (is_array($field)) |
| 780 | { |
| 781 | if ( ! in_array($value, $field)) |
| 782 | { |
| 783 | return ''; |
| 784 | } |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 789 | { |
| 790 | return ''; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | return ' checked="checked"'; |
| 795 | } |
| 796 | |
| 797 | return $OBJ->set_checkbox($field, $value, $default); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | // ------------------------------------------------------------------------ |
| 802 | |
| 803 | /** |
| 804 | * Set Radio |
| 805 | * |
| 806 | * Let's you set the selected value of a radio field via info in the POST array. |
| 807 | * If Form Validation is active it retrieves the info from the validation class |
| 808 | * |
| 809 | * @access public |
| 810 | * @param string |
| 811 | * @param string |
| 812 | * @param bool |
| 813 | * @return string |
| 814 | */ |
| 815 | if ( ! function_exists('set_radio')) |
| 816 | { |
| 817 | function set_radio($field = '', $value = '', $default = FALSE) |
| 818 | { |
| 819 | $OBJ =& _get_validation_object(); |
| 820 | |
| 821 | if ($OBJ === FALSE) |
| 822 | { |
| 823 | if ( ! isset($_POST[$field])) |
| 824 | { |
Rick Ellis | 28e5c8f | 2009-03-09 22:36:48 +0000 | [diff] [blame] | 825 | if (count($_POST) === 0 AND $default == TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 826 | { |
| 827 | return ' checked="checked"'; |
| 828 | } |
| 829 | return ''; |
| 830 | } |
| 831 | |
| 832 | $field = $_POST[$field]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 833 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 834 | if (is_array($field)) |
| 835 | { |
| 836 | if ( ! in_array($value, $field)) |
| 837 | { |
| 838 | return ''; |
| 839 | } |
| 840 | } |
| 841 | else |
| 842 | { |
| 843 | if (($field == '' OR $value == '') OR ($field != $value)) |
| 844 | { |
| 845 | return ''; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | return ' checked="checked"'; |
| 850 | } |
| 851 | |
| 852 | return $OBJ->set_radio($field, $value, $default); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | // ------------------------------------------------------------------------ |
| 857 | |
| 858 | /** |
| 859 | * Form Error |
| 860 | * |
| 861 | * Returns the error for a specific form field. This is a helper for the |
| 862 | * form validation class. |
| 863 | * |
| 864 | * @access public |
| 865 | * @param string |
| 866 | * @param string |
| 867 | * @param string |
| 868 | * @return string |
| 869 | */ |
| 870 | if ( ! function_exists('form_error')) |
| 871 | { |
| 872 | function form_error($field = '', $prefix = '', $suffix = '') |
| 873 | { |
| 874 | if (FALSE === ($OBJ =& _get_validation_object())) |
| 875 | { |
| 876 | return ''; |
| 877 | } |
| 878 | |
| 879 | return $OBJ->error($field, $prefix, $suffix); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | // ------------------------------------------------------------------------ |
| 884 | |
| 885 | /** |
| 886 | * Validation Error String |
| 887 | * |
| 888 | * Returns all the errors associated with a form submission. This is a helper |
| 889 | * function for the form validation class. |
| 890 | * |
| 891 | * @access public |
| 892 | * @param string |
| 893 | * @param string |
| 894 | * @return string |
| 895 | */ |
| 896 | if ( ! function_exists('validation_errors')) |
| 897 | { |
| 898 | function validation_errors($prefix = '', $suffix = '') |
| 899 | { |
| 900 | if (FALSE === ($OBJ =& _get_validation_object())) |
| 901 | { |
| 902 | return ''; |
| 903 | } |
| 904 | |
| 905 | return $OBJ->error_string($prefix, $suffix); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | // ------------------------------------------------------------------------ |
| 910 | |
| 911 | /** |
| 912 | * Parse the form attributes |
| 913 | * |
| 914 | * Helper function used by some of the form helpers |
| 915 | * |
| 916 | * @access private |
| 917 | * @param array |
| 918 | * @param array |
| 919 | * @return string |
| 920 | */ |
| 921 | if ( ! function_exists('_parse_form_attributes')) |
| 922 | { |
| 923 | function _parse_form_attributes($attributes, $default) |
| 924 | { |
| 925 | if (is_array($attributes)) |
| 926 | { |
| 927 | foreach ($default as $key => $val) |
| 928 | { |
| 929 | if (isset($attributes[$key])) |
| 930 | { |
| 931 | $default[$key] = $attributes[$key]; |
| 932 | unset($attributes[$key]); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | if (count($attributes) > 0) |
| 937 | { |
| 938 | $default = array_merge($default, $attributes); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | $att = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 943 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 944 | foreach ($default as $key => $val) |
| 945 | { |
| 946 | if ($key == 'value') |
| 947 | { |
Derek Jones | 01a9b10 | 2009-07-17 18:30:36 +0000 | [diff] [blame] | 948 | $val = form_prep($val, $default['name']); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | $att .= $key . '="' . $val . '" '; |
| 952 | } |
| 953 | |
| 954 | return $att; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // ------------------------------------------------------------------------ |
| 959 | |
| 960 | /** |
| 961 | * Attributes To String |
| 962 | * |
| 963 | * Helper function used by some of the form helpers |
| 964 | * |
| 965 | * @access private |
| 966 | * @param mixed |
| 967 | * @param bool |
| 968 | * @return string |
| 969 | */ |
| 970 | if ( ! function_exists('_attributes_to_string')) |
| 971 | { |
| 972 | function _attributes_to_string($attributes, $formtag = FALSE) |
| 973 | { |
| 974 | if (is_string($attributes) AND strlen($attributes) > 0) |
| 975 | { |
| 976 | if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE) |
| 977 | { |
| 978 | $attributes .= ' method="post"'; |
| 979 | } |
| 980 | |
Derek Allard | 3241d73 | 2009-09-17 12:17:45 +0000 | [diff] [blame] | 981 | if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE) |
| 982 | { |
| 983 | $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; |
| 984 | } |
| 985 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 986 | return ' '.$attributes; |
| 987 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 988 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 989 | if (is_object($attributes) AND count($attributes) > 0) |
| 990 | { |
| 991 | $attributes = (array)$attributes; |
| 992 | } |
| 993 | |
| 994 | if (is_array($attributes) AND count($attributes) > 0) |
| 995 | { |
Derek Allard | 3241d73 | 2009-09-17 12:17:45 +0000 | [diff] [blame] | 996 | $atts = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 997 | |
Derek Allard | 3241d73 | 2009-09-17 12:17:45 +0000 | [diff] [blame] | 998 | if ( ! isset($attributes['method']) AND $formtag === TRUE) |
| 999 | { |
| 1000 | $atts .= ' method="post"'; |
| 1001 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1002 | |
Derek Allard | 3241d73 | 2009-09-17 12:17:45 +0000 | [diff] [blame] | 1003 | if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE) |
| 1004 | { |
| 1005 | $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"'; |
| 1006 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1007 | |
Derek Allard | 3241d73 | 2009-09-17 12:17:45 +0000 | [diff] [blame] | 1008 | foreach ($attributes as $key => $val) |
| 1009 | { |
| 1010 | $atts .= ' '.$key.'="'.$val.'"'; |
| 1011 | } |
| 1012 | |
| 1013 | return $atts; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | // ------------------------------------------------------------------------ |
| 1019 | |
| 1020 | /** |
| 1021 | * Validation Object |
| 1022 | * |
| 1023 | * Determines what the form validation class was instantiated as, fetches |
| 1024 | * the object and returns it. |
| 1025 | * |
| 1026 | * @access private |
| 1027 | * @return mixed |
| 1028 | */ |
| 1029 | if ( ! function_exists('_get_validation_object')) |
| 1030 | { |
| 1031 | function &_get_validation_object() |
| 1032 | { |
| 1033 | $CI =& get_instance(); |
| 1034 | |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame^] | 1035 | // We set this as a variable since we're returning by reference. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1036 | $return = FALSE; |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame^] | 1037 | |
| 1038 | if ( ! ($object = $CI->load->is_loaded('form_validation'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1039 | { |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame^] | 1040 | if ( ! isset($CI->$object) OR ! is_object($CI->$object)) |
| 1041 | { |
| 1042 | return $return; |
| 1043 | } |
| 1044 | |
| 1045 | return $CI->$object; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1046 | } |
Greg Aker | 0c9ee4a | 2011-04-20 09:40:17 -0500 | [diff] [blame^] | 1047 | |
| 1048 | return $return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | /* End of file form_helper.php */ |
patwork | 64e35cd | 2011-04-08 15:25:31 +0200 | [diff] [blame] | 1054 | /* Location: ./system/helpers/form_helper.php */ |