blob: 726f9aa3a74d7589c78728b3e32c0def8e49d65b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Andrey Andreevc5536aa2012-11-01 17:33:58 +020036 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000037 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Form Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/form_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('form_open'))
53{
Timothy Warren01b129a2012-04-27 11:36:50 -040054 /**
55 * Form Declaration
56 *
57 * Creates the opening portion of the form.
58 *
59 * @param string the URI segments of the form destination
60 * @param array a key/value pair of attributes
61 * @param array a key/value pair hidden data
62 * @return string
63 */
vlakoffea19bc42013-07-27 10:07:43 +020064 function form_open($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
66 $CI =& get_instance();
67
Andrey Andreevea41c8a2014-02-26 18:31:02 +020068 // If no action is provided then set to the current url
69 if ( ! $action)
70 {
71 $action = $CI->config->site_url($CI->uri->uri_string());
72 }
vlakoffc4f9c622013-07-27 10:08:00 +020073 // If an action is not a full URL then turn it into one
Andrey Andreevea41c8a2014-02-26 18:31:02 +020074 elseif (strpos($action, '://') === FALSE)
vlakoffc4f9c622013-07-27 10:08:00 +020075 {
76 $action = $CI->config->site_url($action);
77 }
vlakoffc4f9c622013-07-27 10:08:00 +020078
vlakoffea19bc42013-07-27 10:07:43 +020079 $attributes = _attributes_to_string($attributes);
80
81 if (stripos($attributes, 'method=') === FALSE)
Andrey Andreev122ca9b2013-07-26 18:16:26 +030082 {
83 $attributes .= ' method="post"';
84 }
Derek Allard2067d1a2008-11-13 22:59:24 +000085
vlakoffea19bc42013-07-27 10:07:43 +020086 if (stripos($attributes, 'accept-charset=') === FALSE)
87 {
88 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
89 }
90
vlakoffea19bc42013-07-27 10:07:43 +020091 $form = '<form action="'.$action.'"'.$attributes.">\n";
Barry Mienydd671972010-10-04 16:33:58 +020092
Andrey Andreev93b4e782014-03-04 17:48:21 +020093 if (is_array($hidden))
Greg Aker28b425a2010-09-15 11:43:23 -050094 {
Andrey Andreev93b4e782014-03-04 17:48:21 +020095 foreach ($hidden as $name => $value)
96 {
Andrey Andreev7a49c0b2016-09-27 14:00:26 +030097 $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value).'" />'."\n";
Andrey Andreev93b4e782014-03-04 17:48:21 +020098 }
Derek Allard958543a2010-07-22 14:10:26 -040099 }
100
Andrey Andreevcfd52ed2017-01-04 16:58:08 +0200101 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
102 if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
103 {
104 // Prepend/append random-length "white noise" around the CSRF
105 // token input, as a form of protection against BREACH attacks
106 if (FALSE !== ($noise = $CI->security->get_random_bytes(1)))
107 {
108 list(, $noise) = unpack('c', $noise);
109 }
110 else
111 {
112 $noise = mt_rand(-128, 127);
113 }
114
115 // Prepend if $noise has a negative value, append if positive, do nothing for zero
116 $prepend = $append = '';
117 if ($noise < 0)
118 {
119 $prepend = str_repeat(" ", abs($noise));
120 }
121 elseif ($noise > 0)
122 {
123 $append = str_repeat(" ", $noise);
124 }
125
126 $form .= sprintf(
127 '%s<input type="hidden" name="%s" value="%s" />%s%s',
128 $prepend,
129 $CI->security->get_csrf_token_name(),
130 $CI->security->get_csrf_hash(),
131 $append,
132 "\n"
133 );
134 }
135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 return $form;
137 }
138}
139
140// ------------------------------------------------------------------------
141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142if ( ! function_exists('form_open_multipart'))
143{
Timothy Warren01b129a2012-04-27 11:36:50 -0400144 /**
145 * Form Declaration - Multipart type
146 *
147 * Creates the opening portion of the form, but with "multipart/form-data".
148 *
149 * @param string the URI segments of the form destination
150 * @param array a key/value pair of attributes
151 * @param array a key/value pair hidden data
152 * @return string
153 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500154 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000156 if (is_string($attributes))
157 {
158 $attributes .= ' enctype="multipart/form-data"';
159 }
160 else
161 {
162 $attributes['enctype'] = 'multipart/form-data';
163 }
164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 return form_open($action, $attributes, $hidden);
166 }
167}
168
169// ------------------------------------------------------------------------
170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171if ( ! function_exists('form_hidden'))
172{
Timothy Warren01b129a2012-04-27 11:36:50 -0400173 /**
174 * Hidden Input Field
175 *
176 * Generates hidden fields. You can pass a simple key/value string or
177 * an associative array with multiple values.
178 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200179 * @param mixed $name Field name
180 * @param string $value Field value
181 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400182 * @return string
183 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000184 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000186 static $form;
187
188 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000190 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192
Robin Sowell57fe4102009-03-26 18:58:46 +0000193 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000195 foreach ($name as $key => $val)
196 {
197 form_hidden($key, $val, TRUE);
198 }
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200199
Robin Sowell57fe4102009-03-26 18:58:46 +0000200 return $form;
201 }
202
203 if ( ! is_array($value))
204 {
Andrey Andreev2c245612015-01-20 15:40:27 +0200205 $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000206 }
207 else
208 {
209 foreach ($value as $k => $v)
210 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300211 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000212 form_hidden($name.'['.$k.']', $v, TRUE);
213 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
215
216 return $form;
217 }
218}
219
220// ------------------------------------------------------------------------
221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222if ( ! function_exists('form_input'))
223{
Timothy Warren01b129a2012-04-27 11:36:50 -0400224 /**
225 * Text Input Field
226 *
227 * @param mixed
228 * @param string
Adam Jackett664d25a2015-06-03 15:54:54 -0400229 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400230 * @return string
231 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 function form_input($data = '', $value = '', $extra = '')
233 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200234 $defaults = array(
235 'type' => 'text',
236 'name' => is_array($data) ? '' : $data,
237 'value' => $value
238 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000239
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300240 return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242}
243
244// ------------------------------------------------------------------------
245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246if ( ! function_exists('form_password'))
247{
Timothy Warren01b129a2012-04-27 11:36:50 -0400248 /**
249 * Password Field
250 *
251 * Identical to the input function but adds the "password" type
252 *
253 * @param mixed
254 * @param string
Adam Jackett664d25a2015-06-03 15:54:54 -0400255 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400256 * @return string
257 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 function form_password($data = '', $value = '', $extra = '')
259 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200260 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $data['type'] = 'password';
262 return form_input($data, $value, $extra);
263 }
264}
265
266// ------------------------------------------------------------------------
267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268if ( ! function_exists('form_upload'))
269{
Timothy Warren01b129a2012-04-27 11:36:50 -0400270 /**
271 * Upload Field
272 *
273 * Identical to the input function but adds the "file" type
274 *
275 * @param mixed
276 * @param string
Adam Jackett664d25a2015-06-03 15:54:54 -0400277 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400278 * @return string
279 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 function form_upload($data = '', $value = '', $extra = '')
281 {
Bo-Yi Wu06ddcf02013-02-18 08:52:05 +0800282 $defaults = array('type' => 'file', 'name' => '');
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200283 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 $data['type'] = 'file';
Adam Jackett664d25a2015-06-03 15:54:54 -0400285
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300286 return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
288}
289
290// ------------------------------------------------------------------------
291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292if ( ! function_exists('form_textarea'))
293{
Timothy Warren01b129a2012-04-27 11:36:50 -0400294 /**
295 * Textarea field
296 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200297 * @param mixed $data
298 * @param string $value
Adam Jackett664d25a2015-06-03 15:54:54 -0400299 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400300 * @return string
301 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 function form_textarea($data = '', $value = '', $extra = '')
303 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200304 $defaults = array(
305 'name' => is_array($data) ? '' : $data,
306 'cols' => '40',
307 'rows' => '10'
308 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000309
310 if ( ! is_array($data) OR ! isset($data['value']))
311 {
312 $val = $value;
313 }
314 else
315 {
Barry Mienydd671972010-10-04 16:33:58 +0200316 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 unset($data['value']); // textareas don't use the value attribute
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300320 return '<textarea '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
321 .html_escape($val)
322 ."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324}
325
326// ------------------------------------------------------------------------
327
Derek Jones68788d52010-03-05 10:11:31 -0600328if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000329{
Timothy Warren01b129a2012-04-27 11:36:50 -0400330 /**
331 * Multi-select menu
332 *
333 * @param string
334 * @param array
335 * @param mixed
Adam Jackett664d25a2015-06-03 15:54:54 -0400336 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400337 * @return string
338 */
Derek Jones26399292009-04-08 16:14:17 +0000339 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
340 {
Adam Jackett664d25a2015-06-03 15:54:54 -0400341 $extra = _attributes_to_string($extra);
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300342 if (stripos($extra, 'multiple') === FALSE)
Derek Jones26399292009-04-08 16:14:17 +0000343 {
344 $extra .= ' multiple="multiple"';
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Jones26399292009-04-08 16:14:17 +0000347 return form_dropdown($name, $options, $selected, $extra);
348 }
349}
350
351// --------------------------------------------------------------------
352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353if ( ! function_exists('form_dropdown'))
354{
Timothy Warren01b129a2012-04-27 11:36:50 -0400355 /**
356 * Drop-down Menu
357 *
Brennan Thompson40cd6002014-02-14 12:06:38 -0700358 * @param mixed $data
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200359 * @param mixed $options
360 * @param mixed $selected
361 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400362 * @return string
363 */
Brennan Thompson40cd6002014-02-14 12:06:38 -0700364 function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200366 $defaults = array();
Brennan Thompson82c78a92014-02-17 10:02:19 -0700367
Andrey Andreev15662dd2014-03-06 13:45:33 +0200368 if (is_array($data))
Andrey Andreev93a83c72012-03-26 21:24:02 +0300369 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200370 if (isset($data['selected']))
371 {
372 $selected = $data['selected'];
373 unset($data['selected']); // select tags don't have a selected attribute
374 }
375
376 if (isset($data['options']))
377 {
378 $options = $data['options'];
379 unset($data['options']); // select tags don't use an options attribute
380 }
381 }
382 else
383 {
384 $defaults = array('name' => $data);
Andrey Andreev93a83c72012-03-26 21:24:02 +0300385 }
386
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300387 is_array($selected) OR $selected = array($selected);
Andrey Andreev15662dd2014-03-06 13:45:33 +0200388 is_array($options) OR $options = array($options);
Derek Allard2067d1a2008-11-13 22:59:24 +0000389
390 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev15662dd2014-03-06 13:45:33 +0200391 if (empty($selected))
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200393 if (is_array($data))
394 {
395 if (isset($data['name'], $_POST[$data['name']]))
396 {
397 $selected = array($_POST[$data['name']]);
398 }
399 }
400 elseif (isset($_POST[$data]))
401 {
402 $selected = array($_POST[$data]);
403 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700405
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700406 $extra = _attributes_to_string($extra);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700407
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300408 $multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Brennan Thompson82c78a92014-02-17 10:02:19 -0700409
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700410 $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
Brennan Thompson82c78a92014-02-17 10:02:19 -0700411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 foreach ($options as $key => $val)
413 {
414 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300416 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000417 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300418 if (empty($val))
419 {
420 continue;
421 }
422
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200423 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000424
Derek Allard78a5fc92009-02-05 16:34:35 +0000425 foreach ($val as $optgroup_key => $optgroup_val)
426 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300427 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev2c245612015-01-20 15:40:27 +0200428 $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.'>'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700429 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000430 }
431
Andrey Andreev93a83c72012-03-26 21:24:02 +0300432 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000433 }
434 else
435 {
Andrey Andreev2c245612015-01-20 15:40:27 +0200436 $form .= '<option value="'.html_escape($key).'"'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700437 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
438 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000439 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700441
Andrey Andreev93a83c72012-03-26 21:24:02 +0300442 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444}
445
446// ------------------------------------------------------------------------
447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448if ( ! function_exists('form_checkbox'))
449{
Timothy Warren01b129a2012-04-27 11:36:50 -0400450 /**
451 * Checkbox Field
452 *
453 * @param mixed
454 * @param string
455 * @param bool
Adam Jackett664d25a2015-06-03 15:54:54 -0400456 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400457 * @return string
458 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
460 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300461 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462
Andrey Andreev93a83c72012-03-26 21:24:02 +0300463 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 $checked = $data['checked'];
466
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200467 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 unset($data['checked']);
470 }
471 else
472 {
473 $data['checked'] = 'checked';
474 }
475 }
476
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200477 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 $defaults['checked'] = 'checked';
480 }
481 else
482 {
483 unset($defaults['checked']);
484 }
485
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300486 return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488}
489
490// ------------------------------------------------------------------------
491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492if ( ! function_exists('form_radio'))
493{
Timothy Warren01b129a2012-04-27 11:36:50 -0400494 /**
495 * Radio Button
496 *
497 * @param mixed
498 * @param string
499 * @param bool
Adam Jackett664d25a2015-06-03 15:54:54 -0400500 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400501 * @return string
502 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
504 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200505 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 $data['type'] = 'radio';
Adam Jackett664d25a2015-06-03 15:54:54 -0400507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 return form_checkbox($data, $value, $checked, $extra);
509 }
510}
511
512// ------------------------------------------------------------------------
513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200515{
Timothy Warren01b129a2012-04-27 11:36:50 -0400516 /**
517 * Submit Button
518 *
519 * @param mixed
520 * @param string
Adam Jackett664d25a2015-06-03 15:54:54 -0400521 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400522 * @return string
523 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 function form_submit($data = '', $value = '', $extra = '')
525 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200526 $defaults = array(
527 'type' => 'submit',
528 'name' => is_array($data) ? '' : $data,
529 'value' => $value
530 );
531
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300532 return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 }
534}
535
536// ------------------------------------------------------------------------
537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538if ( ! function_exists('form_reset'))
539{
Timothy Warren01b129a2012-04-27 11:36:50 -0400540 /**
541 * Reset Button
542 *
543 * @param mixed
544 * @param string
Adam Jackett664d25a2015-06-03 15:54:54 -0400545 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400546 * @return string
547 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 function form_reset($data = '', $value = '', $extra = '')
549 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200550 $defaults = array(
551 'type' => 'reset',
552 'name' => is_array($data) ? '' : $data,
553 'value' => $value
554 );
555
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300556 return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
558}
559
560// ------------------------------------------------------------------------
561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562if ( ! function_exists('form_button'))
563{
Timothy Warren01b129a2012-04-27 11:36:50 -0400564 /**
565 * Form Button
566 *
567 * @param mixed
568 * @param string
Adam Jackett664d25a2015-06-03 15:54:54 -0400569 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400570 * @return string
571 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 function form_button($data = '', $content = '', $extra = '')
573 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200574 $defaults = array(
575 'name' => is_array($data) ? '' : $data,
576 'type' => 'button'
577 );
578
Andrey Andreev93a83c72012-03-26 21:24:02 +0300579 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 $content = $data['content'];
582 unset($data['content']); // content is not an attribute
583 }
584
Andrey Andreevc19f3b22015-07-15 16:41:06 +0300585 return '<button '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
586 .$content
587 ."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
589}
590
591// ------------------------------------------------------------------------
592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593if ( ! function_exists('form_label'))
594{
Timothy Warren01b129a2012-04-27 11:36:50 -0400595 /**
596 * Form Label Tag
597 *
598 * @param string The text to appear onscreen
599 * @param string The id the label applies to
Andrey Andreev26ba6942017-05-25 11:55:11 +0300600 * @param mixed Additional attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400601 * @return string
602 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 function form_label($label_text = '', $id = '', $attributes = array())
604 {
605
606 $label = '<label';
607
Alex Bilbie773ccc32012-06-02 11:11:08 +0100608 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300610 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
612
Andrey Andreev904a28b2017-05-25 11:51:07 +0300613 $label .= _attributes_to_string($attributes);
Derek Allard2067d1a2008-11-13 22:59:24 +0000614
Andrey Andreev93a83c72012-03-26 21:24:02 +0300615 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
617}
618
619// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621if ( ! function_exists('form_fieldset'))
622{
Timothy Warren01b129a2012-04-27 11:36:50 -0400623 /**
624 * Fieldset Tag
625 *
626 * Used to produce <fieldset><legend>text</legend>. To close fieldset
627 * use form_fieldset_close()
628 *
629 * @param string The legend text
vlakoffea19bc42013-07-27 10:07:43 +0200630 * @param array Additional attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400631 * @return string
632 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 function form_fieldset($legend_text = '', $attributes = array())
634 {
vlakoffea19bc42013-07-27 10:07:43 +0200635 $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100636 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300638 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
640
641 return $fieldset;
642 }
643}
644
645// ------------------------------------------------------------------------
646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647if ( ! function_exists('form_fieldset_close'))
648{
Timothy Warren01b129a2012-04-27 11:36:50 -0400649 /**
650 * Fieldset Close Tag
651 *
652 * @param string
653 * @return string
654 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 function form_fieldset_close($extra = '')
656 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300657 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 }
659}
660
661// ------------------------------------------------------------------------
662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663if ( ! function_exists('form_close'))
664{
Timothy Warren01b129a2012-04-27 11:36:50 -0400665 /**
666 * Form Close Tag
667 *
668 * @param string
669 * @return string
670 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 function form_close($extra = '')
672 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300673 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 }
675}
676
677// ------------------------------------------------------------------------
678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679if ( ! function_exists('form_prep'))
680{
Timothy Warren01b129a2012-04-27 11:36:50 -0400681 /**
682 * Form Prep
683 *
684 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
685 *
Andrey Andreev2c245612015-01-20 15:40:27 +0200686 * @deprecated 3.0.0 An alias for html_escape()
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200687 * @param string|string[] $str Value to escape
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200688 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400689 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200690 function form_prep($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
Andrey Andreev2c245612015-01-20 15:40:27 +0200692 return html_escape($str, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 }
694}
695
696// ------------------------------------------------------------------------
697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698if ( ! function_exists('set_value'))
699{
Timothy Warren01b129a2012-04-27 11:36:50 -0400700 /**
701 * Form Value
702 *
703 * Grabs a value from the POST array for the specified field so you can
704 * re-populate an input field or textarea. If Form Validation
705 * is active it retrieves the info from the validation class
706 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200707 * @param string $field Field name
708 * @param string $default Default value
Adrian Voicufa61fb22015-02-05 15:46:12 +0200709 * @param bool $html_escape Whether to escape HTML special characters or not
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200710 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400711 */
Adrian Voicu86e6a192015-02-05 13:51:26 +0200712 function set_value($field, $default = '', $html_escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530714 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000715
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530716 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
717 ? $CI->form_validation->set_value($field, $default)
718 : $CI->input->post($field, FALSE);
Andrey Andreev7df66342015-02-05 15:58:09 +0200719
Adrian Voicu86e6a192015-02-05 13:51:26 +0200720 isset($value) OR $value = $default;
721 return ($html_escape) ? html_escape($value) : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 }
723}
724
725// ------------------------------------------------------------------------
726
Derek Allard2067d1a2008-11-13 22:59:24 +0000727if ( ! function_exists('set_select'))
728{
Timothy Warren01b129a2012-04-27 11:36:50 -0400729 /**
730 * Set Select
731 *
732 * Let's you set the selected value of a <select> menu via data in the POST array.
733 * If Form Validation is active it retrieves the info from the validation class
734 *
735 * @param string
736 * @param string
737 * @param bool
738 * @return string
739 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200740 function set_select($field, $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300742 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000743
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300744 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300746 return $CI->form_validation->set_select($field, $value, $default);
747 }
748 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
749 {
750 return ($default === TRUE) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300752
753 $value = (string) $value;
754 if (is_array($input))
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300755 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300756 // Note: in_array('', array(0)) returns TRUE, do not use it
757 foreach ($input as &$v)
758 {
759 if ($value === $v)
760 {
761 return ' selected="selected"';
762 }
763 }
764
765 return '';
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300766 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000767
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300768 return ($input === $value) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 }
770}
771
772// ------------------------------------------------------------------------
773
Derek Allard2067d1a2008-11-13 22:59:24 +0000774if ( ! function_exists('set_checkbox'))
775{
Timothy Warren01b129a2012-04-27 11:36:50 -0400776 /**
777 * Set Checkbox
778 *
779 * Let's you set the selected value of a checkbox via the value in the POST array.
780 * If Form Validation is active it retrieves the info from the validation class
781 *
782 * @param string
783 * @param string
784 * @param bool
785 * @return string
786 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200787 function set_checkbox($field, $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300789 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000790
Andrey Andreevae50f552013-09-13 16:17:41 +0300791 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Barry Mienydd671972010-10-04 16:33:58 +0200792 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300793 return $CI->form_validation->set_checkbox($field, $value, $default);
794 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300795
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200796 // Form inputs are always strings ...
Andrey Andreeva587a932013-10-23 19:57:46 +0300797 $value = (string) $value;
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200798 $input = $CI->input->post($field, FALSE);
799
Andrey Andreeva587a932013-10-23 19:57:46 +0300800 if (is_array($input))
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300801 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300802 // Note: in_array('', array(0)) returns TRUE, do not use it
803 foreach ($input as &$v)
804 {
805 if ($value === $v)
806 {
807 return ' checked="checked"';
808 }
809 }
810
811 return '';
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300812 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000813
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200814 // Unchecked checkbox and radio inputs are not even submitted by browsers ...
815 if ($CI->input->method() === 'post')
816 {
Andrey Andreev0b59bdd2016-01-29 01:18:08 +0200817 return ($input === $value) ? ' checked="checked"' : '';
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200818 }
819
820 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
822}
823
824// ------------------------------------------------------------------------
825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826if ( ! function_exists('set_radio'))
827{
Timothy Warren01b129a2012-04-27 11:36:50 -0400828 /**
829 * Set Radio
830 *
831 * Let's you set the selected value of a radio field via info in the POST array.
832 * If Form Validation is active it retrieves the info from the validation class
833 *
Andrey Andreevae50f552013-09-13 16:17:41 +0300834 * @param string $field
835 * @param string $value
836 * @param bool $default
Timothy Warren01b129a2012-04-27 11:36:50 -0400837 * @return string
838 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200839 function set_radio($field, $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300841 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000842
Andrey Andreevae50f552013-09-13 16:17:41 +0300843 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300845 return $CI->form_validation->set_radio($field, $value, $default);
846 }
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200847
848 // Form inputs are always strings ...
849 $value = (string) $value;
850 $input = $CI->input->post($field, FALSE);
851
852 if (is_array($input))
Andrey Andreevae50f552013-09-13 16:17:41 +0300853 {
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200854 // Note: in_array('', array(0)) returns TRUE, do not use it
855 foreach ($input as &$v)
856 {
857 if ($value === $v)
858 {
859 return ' checked="checked"';
860 }
861 }
862
863 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 }
865
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200866 // Unchecked checkbox and radio inputs are not even submitted by browsers ...
867 if ($CI->input->method() === 'post')
868 {
Andrey Andreev0b59bdd2016-01-29 01:18:08 +0200869 return ($input === $value) ? ' checked="checked"' : '';
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200870 }
871
872 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000873 }
874}
875
876// ------------------------------------------------------------------------
877
Derek Allard2067d1a2008-11-13 22:59:24 +0000878if ( ! function_exists('form_error'))
879{
Timothy Warren01b129a2012-04-27 11:36:50 -0400880 /**
881 * Form Error
882 *
883 * Returns the error for a specific form field. This is a helper for the
884 * form validation class.
885 *
886 * @param string
887 * @param string
888 * @param string
889 * @return string
890 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 function form_error($field = '', $prefix = '', $suffix = '')
892 {
893 if (FALSE === ($OBJ =& _get_validation_object()))
894 {
895 return '';
896 }
897
898 return $OBJ->error($field, $prefix, $suffix);
899 }
900}
901
902// ------------------------------------------------------------------------
903
Luigi Santivetti7bab4942019-06-16 07:40:53 +0000904if ( ! function_exists('form_message'))
905{
906 /**
907 * Form Message
908 *
909 * Returns the message for a specific form field. This is a helper for the
910 * form validation class.
911 *
912 * @param string
913 * @param string
914 * @param string
915 * @return string
916 */
917 function form_message($field = '', $prefix = '', $suffix = '')
918 {
919 if (FALSE === ($OBJ =& _get_validation_object()))
920 {
921 return '';
922 }
923
924 return $OBJ->message($field, $prefix, $suffix);
925 }
926}
927
928// ------------------------------------------------------------------------
929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930if ( ! function_exists('validation_errors'))
931{
Timothy Warren01b129a2012-04-27 11:36:50 -0400932 /**
933 * Validation Error String
934 *
935 * Returns all the errors associated with a form submission. This is a helper
936 * function for the form validation class.
937 *
938 * @param string
939 * @param string
940 * @return string
941 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000942 function validation_errors($prefix = '', $suffix = '')
943 {
944 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500945 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 return '';
947 }
948
949 return $OBJ->error_string($prefix, $suffix);
950 }
951}
952
953// ------------------------------------------------------------------------
954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955if ( ! function_exists('_parse_form_attributes'))
956{
Timothy Warren01b129a2012-04-27 11:36:50 -0400957 /**
958 * Parse the form attributes
959 *
960 * Helper function used by some of the form helpers
961 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200962 * @param array $attributes List of attributes
963 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400964 * @return string
965 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 function _parse_form_attributes($attributes, $default)
967 {
968 if (is_array($attributes))
969 {
970 foreach ($default as $key => $val)
971 {
972 if (isset($attributes[$key]))
973 {
974 $default[$key] = $attributes[$key];
975 unset($attributes[$key]);
976 }
977 }
978
979 if (count($attributes) > 0)
980 {
981 $default = array_merge($default, $attributes);
982 }
983 }
984
985 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 foreach ($default as $key => $val)
988 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100989 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000990 {
Andrey Andreev2c245612015-01-20 15:40:27 +0200991 $val = html_escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300993 elseif ($key === 'name' && ! strlen($default['name']))
994 {
995 continue;
996 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000997
Andrey Andreev93a83c72012-03-26 21:24:02 +0300998 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 }
1000
1001 return $att;
1002 }
1003}
1004
1005// ------------------------------------------------------------------------
1006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007if ( ! function_exists('_attributes_to_string'))
1008{
Timothy Warren01b129a2012-04-27 11:36:50 -04001009 /**
1010 * Attributes To String
1011 *
1012 * Helper function used by some of the form helpers
1013 *
1014 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -04001015 * @return string
1016 */
vlakoffea19bc42013-07-27 10:07:43 +02001017 function _attributes_to_string($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +00001018 {
vlakoffbb8b0892013-07-28 22:35:04 +02001019 if (empty($attributes))
1020 {
1021 return '';
1022 }
1023
vlakoffea19bc42013-07-27 10:07:43 +02001024 if (is_object($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Andrey Andreev93a83c72012-03-26 21:24:02 +03001026 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 }
1028
vlakoffea19bc42013-07-27 10:07:43 +02001029 if (is_array($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +00001030 {
Derek Allard3241d732009-09-17 12:17:45 +00001031 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001032
Derek Allard3241d732009-09-17 12:17:45 +00001033 foreach ($attributes as $key => $val)
1034 {
1035 $atts .= ' '.$key.'="'.$val.'"';
1036 }
1037
1038 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001039 }
vlakoffea19bc42013-07-27 10:07:43 +02001040
vlakofff7464752013-07-28 22:23:21 +02001041 if (is_string($attributes))
1042 {
Brennan Thompson7a772e52014-02-16 19:22:54 -07001043 return ' '.$attributes;
vlakofff7464752013-07-28 22:23:21 +02001044 }
1045
vlakoffea19bc42013-07-27 10:07:43 +02001046 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 }
1048}
1049
1050// ------------------------------------------------------------------------
1051
Derek Allard2067d1a2008-11-13 22:59:24 +00001052if ( ! function_exists('_get_validation_object'))
1053{
Timothy Warren01b129a2012-04-27 11:36:50 -04001054 /**
1055 * Validation Object
1056 *
1057 * Determines what the form validation class was instantiated as, fetches
1058 * the object and returns it.
1059 *
1060 * @return mixed
1061 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001062 function &_get_validation_object()
1063 {
1064 $CI =& get_instance();
1065
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001066 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001068
Andrey Andreev519f87a2013-07-23 17:16:10 +03001069 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001071 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1072 {
1073 return $return;
1074 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001075
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001076 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001077 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001078
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001079 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 }
1081}