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