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