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