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