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