Derek Allard | 17f4d73 | 2007-01-27 17:23:13 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | 17f4d73 | 2007-01-27 17:23:13 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
| 8 | * @author Rick Ellis
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 17f4d73 | 2007-01-27 17:23:13 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html
|
| 11 | * @link http://www.codeigniter.com
|
| 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 19 | * CodeIgniter Form Helpers
|
Derek Allard | 17f4d73 | 2007-01-27 17:23:13 +0000 | [diff] [blame] | 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 |
|
| 45 | $form = '<form action="'.$CI->config->site_url($action).'"';
|
| 46 |
|
| 47 | if ( ! isset($attributes['method']))
|
| 48 | {
|
| 49 | $form .= ' method="post"';
|
| 50 | }
|
| 51 |
|
| 52 | if (is_array($attributes) AND count($attributes) > 0)
|
| 53 | {
|
| 54 | foreach ($attributes as $key => $val)
|
| 55 | {
|
| 56 | $form .= ' '.$key.'="'.$val.'"';
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | $form .= '>';
|
| 61 |
|
| 62 | if (is_array($hidden) AND count($hidden > 0))
|
| 63 | {
|
| 64 | $form .= form_hidden($hidden);
|
| 65 | }
|
| 66 |
|
| 67 | return $form;
|
| 68 | }
|
| 69 |
|
| 70 | // ------------------------------------------------------------------------
|
| 71 |
|
| 72 | /**
|
| 73 | * Form Declaration - Multipart type
|
| 74 | *
|
| 75 | * Creates the opening portion of the form, but with "multipart/form-data".
|
| 76 | *
|
| 77 | * @access public
|
| 78 | * @param string the URI segments of the form destination
|
| 79 | * @param array a key/value pair of attributes
|
| 80 | * @param array a key/value pair hidden data
|
| 81 | * @return string
|
| 82 | */
|
| 83 | function form_open_multipart($action, $attributes = array(), $hidden = array())
|
| 84 | {
|
| 85 | $attributes['enctype'] = 'multipart/form-data';
|
| 86 | return form_open($action, $attributes, $hidden);
|
| 87 | }
|
| 88 |
|
| 89 | // ------------------------------------------------------------------------
|
| 90 |
|
| 91 | /**
|
| 92 | * Hidden Input Field
|
| 93 | *
|
| 94 | * Generates hidden fields. You can pass a simple key/value string or an associative
|
| 95 | * array with multiple values.
|
| 96 | *
|
| 97 | * @access public
|
| 98 | * @param mixed
|
| 99 | * @param string
|
| 100 | * @return string
|
| 101 | */
|
| 102 | function form_hidden($name, $value = '')
|
| 103 | {
|
| 104 | if ( ! is_array($name))
|
| 105 | {
|
| 106 | return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 107 | }
|
| 108 |
|
| 109 | $form = '';
|
| 110 | foreach ($name as $name => $value)
|
| 111 | {
|
| 112 | $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 113 | }
|
| 114 |
|
| 115 | return $form;
|
| 116 | }
|
| 117 |
|
| 118 | // ------------------------------------------------------------------------
|
| 119 |
|
| 120 | /**
|
| 121 | * Text Input Field
|
| 122 | *
|
| 123 | * @access public
|
| 124 | * @param mixed
|
| 125 | * @param string
|
| 126 | * @param string
|
| 127 | * @return string
|
| 128 | */
|
| 129 | function form_input($data = '', $value = '', $extra = '')
|
| 130 | {
|
| 131 | $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
|
| 132 |
|
| 133 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 134 | }
|
| 135 |
|
| 136 | // ------------------------------------------------------------------------
|
| 137 |
|
| 138 | /**
|
| 139 | * Password Field
|
| 140 | *
|
| 141 | * Identical to the input function but adds the "password" type
|
| 142 | *
|
| 143 | * @access public
|
| 144 | * @param mixed
|
| 145 | * @param string
|
| 146 | * @param string
|
| 147 | * @return string
|
| 148 | */
|
| 149 | function form_password($data = '', $value = '', $extra = '')
|
| 150 | {
|
| 151 | if ( ! is_array($data))
|
| 152 | {
|
| 153 | $data = array('name' => $data);
|
| 154 | }
|
| 155 |
|
| 156 | $data['type'] = 'password';
|
| 157 | return form_input($data, $value, $extra);
|
| 158 | }
|
| 159 |
|
| 160 | // ------------------------------------------------------------------------
|
| 161 |
|
| 162 | /**
|
| 163 | * Upload Field
|
| 164 | *
|
| 165 | * Identical to the input function but adds the "file" type
|
| 166 | *
|
| 167 | * @access public
|
| 168 | * @param mixed
|
| 169 | * @param string
|
| 170 | * @param string
|
| 171 | * @return string
|
| 172 | */
|
| 173 | function form_upload($data = '', $value = '', $extra = '')
|
| 174 | {
|
| 175 | if ( ! is_array($data))
|
| 176 | {
|
| 177 | $data = array('name' => $data);
|
| 178 | }
|
| 179 |
|
| 180 | $data['type'] = 'file';
|
| 181 | return form_input($data, $value, $extra);
|
| 182 | }
|
| 183 |
|
| 184 | // ------------------------------------------------------------------------
|
| 185 |
|
| 186 | /**
|
| 187 | * Textarea field
|
| 188 | *
|
| 189 | * @access public
|
| 190 | * @param mixed
|
| 191 | * @param string
|
| 192 | * @param string
|
| 193 | * @return string
|
| 194 | */
|
| 195 | function form_textarea($data = '', $value = '', $extra = '')
|
| 196 | {
|
| 197 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
|
| 198 |
|
Derek Allard | c4ac15f | 2007-02-15 17:22:31 +0000 | [diff] [blame] | 199 | if ( ! is_array($data) OR ! isset($data['value']))
|
| 200 | {
|
| 201 | $val = $value;
|
| 202 | }
|
| 203 | else
|
| 204 | {
|
| 205 | $val = $data['value'];
|
| 206 | unset($data['value']); // textareas don't use the value attribute
|
| 207 | }
|
Derek Allard | 17f4d73 | 2007-01-27 17:23:13 +0000 | [diff] [blame] | 208 |
|
| 209 | return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
|
| 210 | }
|
| 211 |
|
| 212 | // ------------------------------------------------------------------------
|
| 213 |
|
| 214 | /**
|
| 215 | * Drop-down Menu
|
| 216 | *
|
| 217 | * @access public
|
| 218 | * @param string
|
| 219 | * @param array
|
| 220 | * @param string
|
| 221 | * @param string
|
| 222 | * @return string
|
| 223 | */
|
| 224 | function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
|
| 225 | {
|
| 226 | if ($extra != '') $extra = ' '.$extra;
|
| 227 |
|
| 228 | $form = '<select name="'.$name.'"'.$extra.">\n";
|
| 229 |
|
| 230 | foreach ($options as $key => $val)
|
| 231 | {
|
| 232 | $sel = ($selected != $key) ? '' : ' selected="selected"';
|
| 233 |
|
| 234 | $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
|
| 235 | }
|
| 236 |
|
| 237 | $form .= '</select>';
|
| 238 |
|
| 239 | return $form;
|
| 240 | }
|
| 241 |
|
| 242 | // ------------------------------------------------------------------------
|
| 243 |
|
| 244 | /**
|
| 245 | * Checkbox Field
|
| 246 | *
|
| 247 | * @access public
|
| 248 | * @param mixed
|
| 249 | * @param string
|
| 250 | * @param bool
|
| 251 | * @param string
|
| 252 | * @return string
|
| 253 | */
|
| 254 | function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
|
| 255 | {
|
| 256 | $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
| 257 |
|
| 258 | if (is_array($data) AND array_key_exists('checked', $data))
|
| 259 | {
|
| 260 | $checked = $data['checked'];
|
| 261 |
|
| 262 | if ($checked == FALSE)
|
Derek Allard | cea5dbe | 2007-04-27 03:33:21 +0000 | [diff] [blame] | 263 | {
|
Derek Allard | 17f4d73 | 2007-01-27 17:23:13 +0000 | [diff] [blame] | 264 | unset($data['checked']);
|
Derek Allard | cea5dbe | 2007-04-27 03:33:21 +0000 | [diff] [blame] | 265 | }
|
| 266 | else
|
| 267 | {
|
| 268 | $data['checked'] = 'checked';
|
| 269 | }
|
Derek Allard | 17f4d73 | 2007-01-27 17:23:13 +0000 | [diff] [blame] | 270 | }
|
| 271 |
|
| 272 | if ($checked == TRUE)
|
| 273 | $defaults['checked'] = 'checked';
|
| 274 | else
|
| 275 | unset($defaults['checked']);
|
| 276 |
|
| 277 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 278 | }
|
| 279 |
|
| 280 | // ------------------------------------------------------------------------
|
| 281 |
|
| 282 | /**
|
| 283 | * Radio Button
|
| 284 | *
|
| 285 | * @access public
|
| 286 | * @param mixed
|
| 287 | * @param string
|
| 288 | * @param bool
|
| 289 | * @param string
|
| 290 | * @return string
|
| 291 | */
|
| 292 | function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
|
| 293 | {
|
| 294 | if ( ! is_array($data))
|
| 295 | {
|
| 296 | $data = array('name' => $data);
|
| 297 | }
|
| 298 |
|
| 299 | $data['type'] = 'radio';
|
| 300 | return form_checkbox($data, $value, $checked, $extra);
|
| 301 | }
|
| 302 |
|
| 303 | // ------------------------------------------------------------------------
|
| 304 |
|
| 305 | /**
|
| 306 | * Submit Button
|
| 307 | *
|
| 308 | * @access public
|
| 309 | * @param mixed
|
| 310 | * @param string
|
| 311 | * @param string
|
| 312 | * @return string
|
| 313 | */
|
| 314 | function form_submit($data = '', $value = '', $extra = '')
|
| 315 | {
|
| 316 | $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
| 317 |
|
| 318 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 319 | }
|
| 320 |
|
| 321 | // ------------------------------------------------------------------------
|
| 322 |
|
| 323 | /**
|
| 324 | * Form Close Tag
|
| 325 | *
|
| 326 | * @access public
|
| 327 | * @param string
|
| 328 | * @return string
|
| 329 | */
|
| 330 | function form_close($extra = '')
|
| 331 | {
|
| 332 | return "</form>\n".$extra;
|
| 333 | }
|
| 334 |
|
| 335 | // ------------------------------------------------------------------------
|
| 336 |
|
| 337 | /**
|
| 338 | * Form Prep
|
| 339 | *
|
| 340 | * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
|
| 341 | *
|
| 342 | * @access public
|
| 343 | * @param string
|
| 344 | * @return string
|
| 345 | */
|
| 346 | function form_prep($str = '')
|
| 347 | {
|
| 348 | if ($str === '')
|
| 349 | {
|
| 350 | return '';
|
| 351 | }
|
| 352 |
|
| 353 | $temp = '__TEMP_AMPERSANDS__';
|
| 354 |
|
| 355 | // Replace entities to temporary markers so that
|
| 356 | // htmlspecialchars won't mess them up
|
| 357 | $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
|
| 358 | $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
|
| 359 |
|
| 360 | $str = htmlspecialchars($str);
|
| 361 |
|
| 362 | // In case htmlspecialchars misses these.
|
| 363 | $str = str_replace(array("'", '"'), array("'", """), $str);
|
| 364 |
|
| 365 | // Decode the temp markers back to entities
|
| 366 | $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
|
| 367 | $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
|
| 368 |
|
| 369 | return $str;
|
| 370 | }
|
| 371 |
|
| 372 | // ------------------------------------------------------------------------
|
| 373 |
|
| 374 | /**
|
| 375 | * Parse the form attributes
|
| 376 | *
|
| 377 | * Helper function used by some of the form helpers
|
| 378 | *
|
| 379 | * @access private
|
| 380 | * @param array
|
| 381 | * @param array
|
| 382 | * @return string
|
| 383 | */
|
| 384 | function parse_form_attributes($attributes, $default)
|
| 385 | {
|
| 386 | if (is_array($attributes))
|
| 387 | {
|
| 388 | foreach ($default as $key => $val)
|
| 389 | {
|
| 390 | if (isset($attributes[$key]))
|
| 391 | {
|
| 392 | $default[$key] = $attributes[$key];
|
| 393 | unset($attributes[$key]);
|
| 394 | }
|
| 395 | }
|
| 396 |
|
| 397 | if (count($attributes) > 0)
|
| 398 | {
|
| 399 | $default = array_merge($default, $attributes);
|
| 400 | }
|
| 401 | }
|
| 402 |
|
| 403 | $att = '';
|
| 404 | foreach ($default as $key => $val)
|
| 405 | {
|
| 406 | if ($key == 'value')
|
| 407 | {
|
| 408 | $val = form_prep($val);
|
| 409 | }
|
| 410 |
|
| 411 | $att .= $key . '="' . $val . '" ';
|
| 412 | }
|
| 413 |
|
| 414 | return $att;
|
| 415 | }
|
| 416 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 417 | ?> |