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