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