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