[289] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
---|
| 2 | /** |
---|
| 3 | * CodeIgniter |
---|
| 4 | * |
---|
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
---|
| 6 | * |
---|
| 7 | * @package CodeIgniter |
---|
| 8 | * @author ExpressionEngine Dev Team |
---|
| 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
---|
| 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 |
---|
| 40 | */ |
---|
| 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 | { |
---|
| 49 | $attributes = 'method="post"'; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | // If an action is not a full URL then turn it into one |
---|
| 53 | if ($action && strpos($action, '://') === FALSE) |
---|
| 54 | { |
---|
| 55 | $action = $CI->config->site_url($action); |
---|
| 56 | } |
---|
| 57 | |
---|
| 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 | |
---|
| 61 | $form = '<form action="'.$action.'"'; |
---|
| 62 | |
---|
| 63 | $form .= _attributes_to_string($attributes, TRUE); |
---|
| 64 | |
---|
| 65 | $form .= '>'; |
---|
| 66 | |
---|
| 67 | // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites |
---|
| 68 | if ($CI->config->item('csrf_protection') === TRUE AND ! (strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"'))) |
---|
| 69 | { |
---|
| 70 | $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | if (is_array($hidden) AND count($hidden) > 0) |
---|
| 74 | { |
---|
| 75 | $form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden)); |
---|
| 76 | } |
---|
| 77 | |
---|
| 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 | { |
---|
| 99 | if (is_string($attributes)) |
---|
| 100 | { |
---|
| 101 | $attributes .= ' enctype="multipart/form-data"'; |
---|
| 102 | } |
---|
| 103 | else |
---|
| 104 | { |
---|
| 105 | $attributes['enctype'] = 'multipart/form-data'; |
---|
| 106 | } |
---|
| 107 | |
---|
| 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 | { |
---|
| 127 | function form_hidden($name, $value = '', $recursing = FALSE) |
---|
| 128 | { |
---|
| 129 | static $form; |
---|
| 130 | |
---|
| 131 | if ($recursing === FALSE) |
---|
| 132 | { |
---|
| 133 | $form = "\n"; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (is_array($name)) |
---|
| 137 | { |
---|
| 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 | { |
---|
| 147 | $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n"; |
---|
| 148 | } |
---|
| 149 | else |
---|
| 150 | { |
---|
| 151 | foreach ($value as $k => $v) |
---|
| 152 | { |
---|
| 153 | $k = (is_int($k)) ? '' : $k; |
---|
| 154 | form_hidden($name.'['.$k.']', $v, TRUE); |
---|
| 155 | } |
---|
| 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' => '40', 'rows' => '10'); |
---|
| 253 | |
---|
| 254 | if ( ! is_array($data) OR ! isset($data['value'])) |
---|
| 255 | { |
---|
| 256 | $val = $value; |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | $val = $data['value']; |
---|
| 261 | unset($data['value']); // textareas don't use the value attribute |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | $name = (is_array($data)) ? $data['name'] : $data; |
---|
| 265 | return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>"; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | // ------------------------------------------------------------------------ |
---|
| 270 | |
---|
| 271 | /** |
---|
| 272 | * Multi-select menu |
---|
| 273 | * |
---|
| 274 | * @access public |
---|
| 275 | * @param string |
---|
| 276 | * @param array |
---|
| 277 | * @param mixed |
---|
| 278 | * @param string |
---|
| 279 | * @return type |
---|
| 280 | */ |
---|
| 281 | if ( ! function_exists('form_multiselect')) |
---|
| 282 | { |
---|
| 283 | function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '') |
---|
| 284 | { |
---|
| 285 | if ( ! strpos($extra, 'multiple')) |
---|
| 286 | { |
---|
| 287 | $extra .= ' multiple="multiple"'; |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | return form_dropdown($name, $options, $selected, $extra); |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | // -------------------------------------------------------------------- |
---|
| 295 | |
---|
| 296 | /** |
---|
| 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"; |
---|
| 330 | |
---|
| 331 | foreach ($options as $key => $val) |
---|
| 332 | { |
---|
| 333 | $key = (string) $key; |
---|
| 334 | |
---|
| 335 | if (is_array($val) && ! empty($val)) |
---|
| 336 | { |
---|
| 337 | $form .= '<optgroup label="'.$key.'">'."\n"; |
---|
| 338 | |
---|
| 339 | foreach ($val as $optgroup_key => $optgroup_val) |
---|
| 340 | { |
---|
| 341 | $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : ''; |
---|
| 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 | } |
---|
| 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)) |
---|
| 424 | { |
---|
| 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')) |
---|
| 445 | { |
---|
| 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 | { |
---|
| 490 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button'); |
---|
| 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 | { |
---|
| 522 | $label .= " for=\"$id\""; |
---|
| 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 | { |
---|
| 617 | function form_prep($str = '', $field_name = '') |
---|
| 618 | { |
---|
| 619 | static $prepped_fields = array(); |
---|
| 620 | |
---|
| 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 | |
---|
| 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 |
---|
| 641 | if (isset($prepped_fields[$field_name])) |
---|
| 642 | { |
---|
| 643 | return $str; |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | $str = htmlspecialchars($str); |
---|
| 647 | |
---|
| 648 | // In case htmlspecialchars misses these. |
---|
| 649 | $str = str_replace(array("'", '"'), array("'", """), $str); |
---|
| 650 | |
---|
| 651 | if ($field_name != '') |
---|
| 652 | { |
---|
| 653 | $prepped_fields[$field_name] = $field_name; |
---|
| 654 | } |
---|
| 655 | |
---|
| 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 | |
---|
| 684 | return form_prep($_POST[$field], $field); |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | return form_prep($OBJ->set_value($field, $default), $field); |
---|
| 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 | { |
---|
| 715 | if (count($_POST) === 0 AND $default == TRUE) |
---|
| 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) |
---|
| 767 | { |
---|
| 768 | if ( ! isset($_POST[$field])) |
---|
| 769 | { |
---|
| 770 | if (count($_POST) === 0 AND $default == TRUE) |
---|
| 771 | { |
---|
| 772 | return ' checked="checked"'; |
---|
| 773 | } |
---|
| 774 | return ''; |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | $field = $_POST[$field]; |
---|
| 778 | |
---|
| 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 | { |
---|
| 825 | if (count($_POST) === 0 AND $default == TRUE) |
---|
| 826 | { |
---|
| 827 | return ' checked="checked"'; |
---|
| 828 | } |
---|
| 829 | return ''; |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | $field = $_POST[$field]; |
---|
| 833 | |
---|
| 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 = ''; |
---|
| 943 | |
---|
| 944 | foreach ($default as $key => $val) |
---|
| 945 | { |
---|
| 946 | if ($key == 'value') |
---|
| 947 | { |
---|
| 948 | $val = form_prep($val, $default['name']); |
---|
| 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 | |
---|
| 981 | if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE) |
---|
| 982 | { |
---|
| 983 | $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; |
---|
| 984 | } |
---|
| 985 | |
---|
| 986 | return ' '.$attributes; |
---|
| 987 | } |
---|
| 988 | |
---|
| 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 | { |
---|
| 996 | $atts = ''; |
---|
| 997 | |
---|
| 998 | if ( ! isset($attributes['method']) AND $formtag === TRUE) |
---|
| 999 | { |
---|
| 1000 | $atts .= ' method="post"'; |
---|
| 1001 | } |
---|
| 1002 | |
---|
| 1003 | if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE) |
---|
| 1004 | { |
---|
| 1005 | $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"'; |
---|
| 1006 | } |
---|
| 1007 | |
---|
| 1008 | foreach ($attributes as $key => $val) |
---|
| 1009 | { |
---|
| 1010 | $atts .= ' '.$key.'="'.$val.'"'; |
---|
| 1011 | } |
---|
| 1012 | |
---|
| 1013 | return $atts; |
---|
| 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 | |
---|
| 1035 | // We set this as a variable since we're returning by reference. |
---|
| 1036 | $return = FALSE; |
---|
| 1037 | |
---|
| 1038 | if (FALSE !== ($object = $CI->load->is_loaded('form_validation'))) |
---|
| 1039 | { |
---|
| 1040 | if ( ! isset($CI->$object) OR ! is_object($CI->$object)) |
---|
| 1041 | { |
---|
| 1042 | return $return; |
---|
| 1043 | } |
---|
| 1044 | |
---|
| 1045 | return $CI->$object; |
---|
| 1046 | } |
---|
| 1047 | |
---|
| 1048 | return $return; |
---|
| 1049 | } |
---|
| 1050 | } |
---|
| 1051 | |
---|
| 1052 | |
---|
| 1053 | /* End of file form_helper.php */ |
---|
| 1054 | /* Location: ./system/helpers/form_helper.php */ |
---|