admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 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, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Code Igniter 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 | */ |
admin | 141808a | 2006-08-27 01:52:51 +0000 | [diff] [blame] | 41 | function form_open($action = '', $attributes = array(), $hidden = array()) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 42 | { |
admin | 75198f9 | 2006-10-07 03:16:53 +0000 | [diff] [blame] | 43 | $CI =& get_instance(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | |
admin | 75198f9 | 2006-10-07 03:16:53 +0000 | [diff] [blame] | 45 | $form = '<form action="'.$CI->config->site_url($action).'"'; |
admin | 141808a | 2006-08-27 01:52:51 +0000 | [diff] [blame] | 46 | |
| 47 | if ( ! isset($attributes['method'])) |
| 48 | { |
| 49 | $form .= ' method="post"'; |
| 50 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 51 | |
| 52 | if (is_array($attributes) AND count($attributes) > 0) |
| 53 | { |
| 54 | foreach ($attributes as $key => $val) |
| 55 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 56 | $form .= ' '.$key.'="'.$val.'"'; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 = '') |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 103 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | |
| 199 | $val = (( ! is_array($data) OR ! isset($data['value'])) ? $value : $data['value']); |
| 200 | |
| 201 | return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n"; |
| 202 | } |
| 203 | |
| 204 | // ------------------------------------------------------------------------ |
| 205 | |
| 206 | /** |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame] | 207 | * Drop-down Menu |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 208 | * |
| 209 | * @access public |
| 210 | * @param string |
| 211 | * @param array |
| 212 | * @param string |
| 213 | * @param string |
| 214 | * @return string |
| 215 | */ |
| 216 | function form_dropdown($name = '', $options = array(), $selected = '', $extra = '') |
| 217 | { |
| 218 | if ($extra != '') $extra = ' '.$extra; |
| 219 | |
| 220 | $form = '<select name="'.$name.'"'.$extra.">\n"; |
| 221 | |
| 222 | foreach ($options as $key => $val) |
| 223 | { |
| 224 | $sel = ($selected != $key) ? '' : ' selected="selected"'; |
| 225 | |
| 226 | $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n"; |
| 227 | } |
| 228 | |
| 229 | $form .= '</select>'; |
| 230 | |
| 231 | return $form; |
| 232 | } |
| 233 | |
| 234 | // ------------------------------------------------------------------------ |
| 235 | |
| 236 | /** |
| 237 | * Checkbox Field |
| 238 | * |
| 239 | * @access public |
| 240 | * @param mixed |
| 241 | * @param string |
| 242 | * @param bool |
| 243 | * @param string |
| 244 | * @return string |
| 245 | */ |
| 246 | function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '') |
| 247 | { |
| 248 | $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); |
| 249 | |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 250 | if (is_array($data) AND array_key_exists('checked', $data)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 251 | { |
| 252 | $checked = $data['checked']; |
| 253 | |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 254 | if ($checked == FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 255 | unset($data['checked']); |
| 256 | } |
| 257 | |
| 258 | if ($checked == TRUE) |
admin | 57b3d39 | 2006-08-27 15:28:31 +0000 | [diff] [blame] | 259 | $defaults['checked'] = 'checked'; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 260 | else |
| 261 | unset($defaults['checked']); |
| 262 | |
| 263 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n"; |
| 264 | } |
| 265 | |
| 266 | // ------------------------------------------------------------------------ |
| 267 | |
| 268 | /** |
| 269 | * Radio Button |
| 270 | * |
| 271 | * @access public |
| 272 | * @param mixed |
| 273 | * @param string |
| 274 | * @param bool |
| 275 | * @param string |
| 276 | * @return string |
| 277 | */ |
| 278 | function form_radio($data = '', $value = '', $checked = TRUE, $extra = '') |
| 279 | { |
| 280 | if ( ! is_array($data)) |
| 281 | { |
| 282 | $data = array('name' => $data); |
| 283 | } |
| 284 | |
| 285 | $data['type'] = 'radio'; |
| 286 | return form_checkbox($data, $value, $checked, $extra); |
| 287 | } |
| 288 | |
| 289 | // ------------------------------------------------------------------------ |
| 290 | |
| 291 | /** |
| 292 | * Submit Button |
| 293 | * |
| 294 | * @access public |
| 295 | * @param mixed |
| 296 | * @param string |
| 297 | * @param string |
| 298 | * @return string |
| 299 | */ |
| 300 | function form_submit($data = '', $value = '', $extra = '') |
| 301 | { |
| 302 | $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); |
| 303 | |
| 304 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n"; |
| 305 | } |
| 306 | |
| 307 | // ------------------------------------------------------------------------ |
| 308 | |
| 309 | /** |
| 310 | * Form Close Tag |
| 311 | * |
| 312 | * @access public |
| 313 | * @param string |
| 314 | * @return string |
| 315 | */ |
| 316 | function form_close($extra = '') |
| 317 | { |
| 318 | return "</form>\n".$extra; |
| 319 | } |
| 320 | |
| 321 | // ------------------------------------------------------------------------ |
| 322 | |
| 323 | /** |
| 324 | * Form Prep |
| 325 | * |
| 326 | * Formats text so that it can be safely placed in a form field in the event it has HTML tags. |
| 327 | * |
| 328 | * @access public |
| 329 | * @param string |
| 330 | * @return string |
| 331 | */ |
| 332 | function form_prep($str = '') |
| 333 | { |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 334 | if ($str === '') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 335 | { |
| 336 | return ''; |
| 337 | } |
| 338 | |
| 339 | return str_replace(array("'", '"'), array("'", """), htmlspecialchars($str)); |
| 340 | } |
| 341 | |
| 342 | // ------------------------------------------------------------------------ |
| 343 | |
| 344 | /** |
| 345 | * Parse the form attributes |
| 346 | * |
| 347 | * Helper function used by some of the form helpers |
| 348 | * |
| 349 | * @access private |
| 350 | * @param array |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame] | 351 | * @param array |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 352 | * @return string |
| 353 | */ |
| 354 | function parse_form_attributes($attributes, $default) |
| 355 | { |
| 356 | if (is_array($attributes)) |
| 357 | { |
| 358 | foreach ($default as $key => $val) |
| 359 | { |
| 360 | if (isset($attributes[$key])) |
| 361 | { |
| 362 | $default[$key] = $attributes[$key]; |
| 363 | unset($attributes[$key]); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (count($attributes) > 0) |
| 368 | { |
| 369 | $default = array_merge($default, $attributes); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | $att = ''; |
| 374 | foreach ($default as $key => $val) |
| 375 | { |
| 376 | if ($key == 'value') |
| 377 | { |
| 378 | $val = form_prep($val); |
| 379 | } |
| 380 | |
| 381 | $att .= $key . '="' . $val . '" '; |
| 382 | } |
| 383 | |
| 384 | return $att; |
| 385 | } |
| 386 | |
| 387 | ?> |