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