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