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