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