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