blob: a49eea803c8b396a29a2ad395dcca452d38ba674 [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 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, 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/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://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 Andreeve13fa9f2016-05-20 17:30:07 +0300600 * @param array 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 Andreev93a83c72012-03-26 21:24:02 +0300613 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
615 foreach ($attributes as $key => $val)
616 {
617 $label .= ' '.$key.'="'.$val.'"';
618 }
619 }
620
Andrey Andreev93a83c72012-03-26 21:24:02 +0300621 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 }
623}
624
625// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627if ( ! function_exists('form_fieldset'))
628{
Timothy Warren01b129a2012-04-27 11:36:50 -0400629 /**
630 * Fieldset Tag
631 *
632 * Used to produce <fieldset><legend>text</legend>. To close fieldset
633 * use form_fieldset_close()
634 *
635 * @param string The legend text
vlakoffea19bc42013-07-27 10:07:43 +0200636 * @param array Additional attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400637 * @return string
638 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 function form_fieldset($legend_text = '', $attributes = array())
640 {
vlakoffea19bc42013-07-27 10:07:43 +0200641 $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100642 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300644 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 }
646
647 return $fieldset;
648 }
649}
650
651// ------------------------------------------------------------------------
652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653if ( ! function_exists('form_fieldset_close'))
654{
Timothy Warren01b129a2012-04-27 11:36:50 -0400655 /**
656 * Fieldset Close Tag
657 *
658 * @param string
659 * @return string
660 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 function form_fieldset_close($extra = '')
662 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300663 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665}
666
667// ------------------------------------------------------------------------
668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669if ( ! function_exists('form_close'))
670{
Timothy Warren01b129a2012-04-27 11:36:50 -0400671 /**
672 * Form Close Tag
673 *
674 * @param string
675 * @return string
676 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 function form_close($extra = '')
678 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300679 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 }
681}
682
683// ------------------------------------------------------------------------
684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685if ( ! function_exists('form_prep'))
686{
Timothy Warren01b129a2012-04-27 11:36:50 -0400687 /**
688 * Form Prep
689 *
690 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
691 *
Andrey Andreev2c245612015-01-20 15:40:27 +0200692 * @deprecated 3.0.0 An alias for html_escape()
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200693 * @param string|string[] $str Value to escape
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200694 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400695 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200696 function form_prep($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 {
Andrey Andreev2c245612015-01-20 15:40:27 +0200698 return html_escape($str, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 }
700}
701
702// ------------------------------------------------------------------------
703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704if ( ! function_exists('set_value'))
705{
Timothy Warren01b129a2012-04-27 11:36:50 -0400706 /**
707 * Form Value
708 *
709 * Grabs a value from the POST array for the specified field so you can
710 * re-populate an input field or textarea. If Form Validation
711 * is active it retrieves the info from the validation class
712 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200713 * @param string $field Field name
714 * @param string $default Default value
Adrian Voicufa61fb22015-02-05 15:46:12 +0200715 * @param bool $html_escape Whether to escape HTML special characters or not
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200716 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400717 */
Adrian Voicu86e6a192015-02-05 13:51:26 +0200718 function set_value($field, $default = '', $html_escape = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530720 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000721
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530722 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
723 ? $CI->form_validation->set_value($field, $default)
724 : $CI->input->post($field, FALSE);
Andrey Andreev7df66342015-02-05 15:58:09 +0200725
Adrian Voicu86e6a192015-02-05 13:51:26 +0200726 isset($value) OR $value = $default;
727 return ($html_escape) ? html_escape($value) : $value;
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 }
729}
730
731// ------------------------------------------------------------------------
732
Derek Allard2067d1a2008-11-13 22:59:24 +0000733if ( ! function_exists('set_select'))
734{
Timothy Warren01b129a2012-04-27 11:36:50 -0400735 /**
736 * Set Select
737 *
738 * Let's you set the selected value of a <select> menu via data in the POST array.
739 * If Form Validation is active it retrieves the info from the validation class
740 *
741 * @param string
742 * @param string
743 * @param bool
744 * @return string
745 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200746 function set_select($field, $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300748 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000749
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300750 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300752 return $CI->form_validation->set_select($field, $value, $default);
753 }
754 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
755 {
756 return ($default === TRUE) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300758
759 $value = (string) $value;
760 if (is_array($input))
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300761 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300762 // Note: in_array('', array(0)) returns TRUE, do not use it
763 foreach ($input as &$v)
764 {
765 if ($value === $v)
766 {
767 return ' selected="selected"';
768 }
769 }
770
771 return '';
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300772 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000773
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300774 return ($input === $value) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 }
776}
777
778// ------------------------------------------------------------------------
779
Derek Allard2067d1a2008-11-13 22:59:24 +0000780if ( ! function_exists('set_checkbox'))
781{
Timothy Warren01b129a2012-04-27 11:36:50 -0400782 /**
783 * Set Checkbox
784 *
785 * Let's you set the selected value of a checkbox via the value in the POST array.
786 * If Form Validation is active it retrieves the info from the validation class
787 *
788 * @param string
789 * @param string
790 * @param bool
791 * @return string
792 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200793 function set_checkbox($field, $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300795 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000796
Andrey Andreevae50f552013-09-13 16:17:41 +0300797 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Barry Mienydd671972010-10-04 16:33:58 +0200798 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300799 return $CI->form_validation->set_checkbox($field, $value, $default);
800 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300801
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200802 // Form inputs are always strings ...
Andrey Andreeva587a932013-10-23 19:57:46 +0300803 $value = (string) $value;
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200804 $input = $CI->input->post($field, FALSE);
805
Andrey Andreeva587a932013-10-23 19:57:46 +0300806 if (is_array($input))
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300807 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300808 // Note: in_array('', array(0)) returns TRUE, do not use it
809 foreach ($input as &$v)
810 {
811 if ($value === $v)
812 {
813 return ' checked="checked"';
814 }
815 }
816
817 return '';
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300818 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000819
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200820 // Unchecked checkbox and radio inputs are not even submitted by browsers ...
821 if ($CI->input->method() === 'post')
822 {
Andrey Andreev0b59bdd2016-01-29 01:18:08 +0200823 return ($input === $value) ? ' checked="checked"' : '';
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200824 }
825
826 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 }
828}
829
830// ------------------------------------------------------------------------
831
Derek Allard2067d1a2008-11-13 22:59:24 +0000832if ( ! function_exists('set_radio'))
833{
Timothy Warren01b129a2012-04-27 11:36:50 -0400834 /**
835 * Set Radio
836 *
837 * Let's you set the selected value of a radio field via info in the POST array.
838 * If Form Validation is active it retrieves the info from the validation class
839 *
Andrey Andreevae50f552013-09-13 16:17:41 +0300840 * @param string $field
841 * @param string $value
842 * @param bool $default
Timothy Warren01b129a2012-04-27 11:36:50 -0400843 * @return string
844 */
Andrey Andreev2c245612015-01-20 15:40:27 +0200845 function set_radio($field, $value = '', $default = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300847 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000848
Andrey Andreevae50f552013-09-13 16:17:41 +0300849 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300851 return $CI->form_validation->set_radio($field, $value, $default);
852 }
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200853
854 // Form inputs are always strings ...
855 $value = (string) $value;
856 $input = $CI->input->post($field, FALSE);
857
858 if (is_array($input))
Andrey Andreevae50f552013-09-13 16:17:41 +0300859 {
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200860 // Note: in_array('', array(0)) returns TRUE, do not use it
861 foreach ($input as &$v)
862 {
863 if ($value === $v)
864 {
865 return ' checked="checked"';
866 }
867 }
868
869 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 }
871
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200872 // Unchecked checkbox and radio inputs are not even submitted by browsers ...
873 if ($CI->input->method() === 'post')
874 {
Andrey Andreev0b59bdd2016-01-29 01:18:08 +0200875 return ($input === $value) ? ' checked="checked"' : '';
Andrey Andreev0139e6a2015-11-04 22:42:17 +0200876 }
877
878 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 }
880}
881
882// ------------------------------------------------------------------------
883
Derek Allard2067d1a2008-11-13 22:59:24 +0000884if ( ! function_exists('form_error'))
885{
Timothy Warren01b129a2012-04-27 11:36:50 -0400886 /**
887 * Form Error
888 *
889 * Returns the error for a specific form field. This is a helper for the
890 * form validation class.
891 *
892 * @param string
893 * @param string
894 * @param string
895 * @return string
896 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 function form_error($field = '', $prefix = '', $suffix = '')
898 {
899 if (FALSE === ($OBJ =& _get_validation_object()))
900 {
901 return '';
902 }
903
904 return $OBJ->error($field, $prefix, $suffix);
905 }
906}
907
908// ------------------------------------------------------------------------
909
Derek Allard2067d1a2008-11-13 22:59:24 +0000910if ( ! function_exists('validation_errors'))
911{
Timothy Warren01b129a2012-04-27 11:36:50 -0400912 /**
913 * Validation Error String
914 *
915 * Returns all the errors associated with a form submission. This is a helper
916 * function for the form validation class.
917 *
918 * @param string
919 * @param string
920 * @return string
921 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000922 function validation_errors($prefix = '', $suffix = '')
923 {
924 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500925 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 return '';
927 }
928
929 return $OBJ->error_string($prefix, $suffix);
930 }
931}
932
933// ------------------------------------------------------------------------
934
Derek Allard2067d1a2008-11-13 22:59:24 +0000935if ( ! function_exists('_parse_form_attributes'))
936{
Timothy Warren01b129a2012-04-27 11:36:50 -0400937 /**
938 * Parse the form attributes
939 *
940 * Helper function used by some of the form helpers
941 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200942 * @param array $attributes List of attributes
943 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400944 * @return string
945 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 function _parse_form_attributes($attributes, $default)
947 {
948 if (is_array($attributes))
949 {
950 foreach ($default as $key => $val)
951 {
952 if (isset($attributes[$key]))
953 {
954 $default[$key] = $attributes[$key];
955 unset($attributes[$key]);
956 }
957 }
958
959 if (count($attributes) > 0)
960 {
961 $default = array_merge($default, $attributes);
962 }
963 }
964
965 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 foreach ($default as $key => $val)
968 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100969 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000970 {
Andrey Andreev2c245612015-01-20 15:40:27 +0200971 $val = html_escape($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300973 elseif ($key === 'name' && ! strlen($default['name']))
974 {
975 continue;
976 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000977
Andrey Andreev93a83c72012-03-26 21:24:02 +0300978 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 }
980
981 return $att;
982 }
983}
984
985// ------------------------------------------------------------------------
986
Derek Allard2067d1a2008-11-13 22:59:24 +0000987if ( ! function_exists('_attributes_to_string'))
988{
Timothy Warren01b129a2012-04-27 11:36:50 -0400989 /**
990 * Attributes To String
991 *
992 * Helper function used by some of the form helpers
993 *
994 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400995 * @return string
996 */
vlakoffea19bc42013-07-27 10:07:43 +0200997 function _attributes_to_string($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 {
vlakoffbb8b0892013-07-28 22:35:04 +0200999 if (empty($attributes))
1000 {
1001 return '';
1002 }
1003
vlakoffea19bc42013-07-27 10:07:43 +02001004 if (is_object($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
Andrey Andreev93a83c72012-03-26 21:24:02 +03001006 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
1008
vlakoffea19bc42013-07-27 10:07:43 +02001009 if (is_array($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 {
Derek Allard3241d732009-09-17 12:17:45 +00001011 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001012
Derek Allard3241d732009-09-17 12:17:45 +00001013 foreach ($attributes as $key => $val)
1014 {
1015 $atts .= ' '.$key.'="'.$val.'"';
1016 }
1017
1018 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 }
vlakoffea19bc42013-07-27 10:07:43 +02001020
vlakofff7464752013-07-28 22:23:21 +02001021 if (is_string($attributes))
1022 {
Brennan Thompson7a772e52014-02-16 19:22:54 -07001023 return ' '.$attributes;
vlakofff7464752013-07-28 22:23:21 +02001024 }
1025
vlakoffea19bc42013-07-27 10:07:43 +02001026 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +00001027 }
1028}
1029
1030// ------------------------------------------------------------------------
1031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032if ( ! function_exists('_get_validation_object'))
1033{
Timothy Warren01b129a2012-04-27 11:36:50 -04001034 /**
1035 * Validation Object
1036 *
1037 * Determines what the form validation class was instantiated as, fetches
1038 * the object and returns it.
1039 *
1040 * @return mixed
1041 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001042 function &_get_validation_object()
1043 {
1044 $CI =& get_instance();
1045
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001046 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001047 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001048
Andrey Andreev519f87a2013-07-23 17:16:10 +03001049 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001050 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001051 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1052 {
1053 return $return;
1054 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001055
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001056 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001057 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001058
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001059 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 }
1061}