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