blob: 0e9207ee29face1fd2bcc70afe1dc32a8e0f62dc [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 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
48 */
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 Andreev93a83c72012-03-26 21:24:02 +030093 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
Andrey Andreevea41c8a2014-02-26 18:31:02 +020094 if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
Derek Allard958543a2010-07-22 14:10:26 -040095 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050096 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050097 }
98
Andrey Andreev93b4e782014-03-04 17:48:21 +020099 if (is_array($hidden))
Greg Aker28b425a2010-09-15 11:43:23 -0500100 {
Andrey Andreev93b4e782014-03-04 17:48:21 +0200101 foreach ($hidden as $name => $value)
102 {
103 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" style="display:none;" />'."\n";
104 }
Derek Allard958543a2010-07-22 14:10:26 -0400105 }
106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 return $form;
108 }
109}
110
111// ------------------------------------------------------------------------
112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113if ( ! function_exists('form_open_multipart'))
114{
Timothy Warren01b129a2012-04-27 11:36:50 -0400115 /**
116 * Form Declaration - Multipart type
117 *
118 * Creates the opening portion of the form, but with "multipart/form-data".
119 *
120 * @param string the URI segments of the form destination
121 * @param array a key/value pair of attributes
122 * @param array a key/value pair hidden data
123 * @return string
124 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500125 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000127 if (is_string($attributes))
128 {
129 $attributes .= ' enctype="multipart/form-data"';
130 }
131 else
132 {
133 $attributes['enctype'] = 'multipart/form-data';
134 }
135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 return form_open($action, $attributes, $hidden);
137 }
138}
139
140// ------------------------------------------------------------------------
141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142if ( ! function_exists('form_hidden'))
143{
Timothy Warren01b129a2012-04-27 11:36:50 -0400144 /**
145 * Hidden Input Field
146 *
147 * Generates hidden fields. You can pass a simple key/value string or
148 * an associative array with multiple values.
149 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200150 * @param mixed $name Field name
151 * @param string $value Field value
152 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400153 * @return string
154 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000155 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000157 static $form;
158
159 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000161 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
Robin Sowell57fe4102009-03-26 18:58:46 +0000164 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000166 foreach ($name as $key => $val)
167 {
168 form_hidden($key, $val, TRUE);
169 }
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200170
Robin Sowell57fe4102009-03-26 18:58:46 +0000171 return $form;
172 }
173
174 if ( ! is_array($value))
175 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200176 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000177 }
178 else
179 {
180 foreach ($value as $k => $v)
181 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300182 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000183 form_hidden($name.'['.$k.']', $v, TRUE);
184 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186
187 return $form;
188 }
189}
190
191// ------------------------------------------------------------------------
192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193if ( ! function_exists('form_input'))
194{
Timothy Warren01b129a2012-04-27 11:36:50 -0400195 /**
196 * Text Input Field
197 *
198 * @param mixed
199 * @param string
200 * @param string
201 * @return string
202 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 function form_input($data = '', $value = '', $extra = '')
204 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200205 $defaults = array(
206 'type' => 'text',
207 'name' => is_array($data) ? '' : $data,
208 'value' => $value
209 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200211 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213}
214
215// ------------------------------------------------------------------------
216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217if ( ! function_exists('form_password'))
218{
Timothy Warren01b129a2012-04-27 11:36:50 -0400219 /**
220 * Password Field
221 *
222 * Identical to the input function but adds the "password" type
223 *
224 * @param mixed
225 * @param string
226 * @param string
227 * @return string
228 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 function form_password($data = '', $value = '', $extra = '')
230 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200231 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $data['type'] = 'password';
233 return form_input($data, $value, $extra);
234 }
235}
236
237// ------------------------------------------------------------------------
238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239if ( ! function_exists('form_upload'))
240{
Timothy Warren01b129a2012-04-27 11:36:50 -0400241 /**
242 * Upload Field
243 *
244 * Identical to the input function but adds the "file" type
245 *
246 * @param mixed
247 * @param string
248 * @param string
249 * @return string
250 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 function form_upload($data = '', $value = '', $extra = '')
252 {
Bo-Yi Wu06ddcf02013-02-18 08:52:05 +0800253 $defaults = array('type' => 'file', 'name' => '');
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200254 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 $data['type'] = 'file';
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200256 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258}
259
260// ------------------------------------------------------------------------
261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262if ( ! function_exists('form_textarea'))
263{
Timothy Warren01b129a2012-04-27 11:36:50 -0400264 /**
265 * Textarea field
266 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200267 * @param mixed $data
268 * @param string $value
269 * @param string $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400270 * @return string
271 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 function form_textarea($data = '', $value = '', $extra = '')
273 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200274 $defaults = array(
275 'name' => is_array($data) ? '' : $data,
276 'cols' => '40',
277 'rows' => '10'
278 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000279
280 if ( ! is_array($data) OR ! isset($data['value']))
281 {
282 $val = $value;
283 }
284 else
285 {
Barry Mienydd671972010-10-04 16:33:58 +0200286 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 unset($data['value']); // textareas don't use the value attribute
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200290 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292}
293
294// ------------------------------------------------------------------------
295
Derek Jones68788d52010-03-05 10:11:31 -0600296if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000297{
Timothy Warren01b129a2012-04-27 11:36:50 -0400298 /**
299 * Multi-select menu
300 *
301 * @param string
302 * @param array
303 * @param mixed
304 * @param string
305 * @return string
306 */
Derek Jones26399292009-04-08 16:14:17 +0000307 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
308 {
309 if ( ! strpos($extra, 'multiple'))
310 {
311 $extra .= ' multiple="multiple"';
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Jones26399292009-04-08 16:14:17 +0000314 return form_dropdown($name, $options, $selected, $extra);
315 }
316}
317
318// --------------------------------------------------------------------
319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320if ( ! function_exists('form_dropdown'))
321{
Timothy Warren01b129a2012-04-27 11:36:50 -0400322 /**
323 * Drop-down Menu
324 *
Brennan Thompson40cd6002014-02-14 12:06:38 -0700325 * @param mixed $data
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200326 * @param mixed $options
327 * @param mixed $selected
328 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400329 * @return string
330 */
Brennan Thompson40cd6002014-02-14 12:06:38 -0700331 function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200333 $defaults = array();
Brennan Thompson82c78a92014-02-17 10:02:19 -0700334
Andrey Andreev15662dd2014-03-06 13:45:33 +0200335 if (is_array($data))
Andrey Andreev93a83c72012-03-26 21:24:02 +0300336 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200337 if (isset($data['selected']))
338 {
339 $selected = $data['selected'];
340 unset($data['selected']); // select tags don't have a selected attribute
341 }
342
343 if (isset($data['options']))
344 {
345 $options = $data['options'];
346 unset($data['options']); // select tags don't use an options attribute
347 }
348 }
349 else
350 {
351 $defaults = array('name' => $data);
Andrey Andreev93a83c72012-03-26 21:24:02 +0300352 }
353
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300354 is_array($selected) OR $selected = array($selected);
Andrey Andreev15662dd2014-03-06 13:45:33 +0200355 is_array($options) OR $options = array($options);
Derek Allard2067d1a2008-11-13 22:59:24 +0000356
357 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev15662dd2014-03-06 13:45:33 +0200358 if (empty($selected))
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200360 if (is_array($data))
361 {
362 if (isset($data['name'], $_POST[$data['name']]))
363 {
364 $selected = array($_POST[$data['name']]);
365 }
366 }
367 elseif (isset($_POST[$data]))
368 {
369 $selected = array($_POST[$data]);
370 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700372
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700373 $extra = _attributes_to_string($extra);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Brennan Thompson82c78a92014-02-17 10:02:19 -0700376
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700377 $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
Brennan Thompson82c78a92014-02-17 10:02:19 -0700378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 foreach ($options as $key => $val)
380 {
381 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000382
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300383 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000384 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300385 if (empty($val))
386 {
387 continue;
388 }
389
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200390 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000391
Derek Allard78a5fc92009-02-05 16:34:35 +0000392 foreach ($val as $optgroup_key => $optgroup_val)
393 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300394 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200395 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700396 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000397 }
398
Andrey Andreev93a83c72012-03-26 21:24:02 +0300399 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000400 }
401 else
402 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200403 $form .= '<option value="'.form_prep($key).'"'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700404 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
405 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000406 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700408
Andrey Andreev93a83c72012-03-26 21:24:02 +0300409 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
411}
412
413// ------------------------------------------------------------------------
414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415if ( ! function_exists('form_checkbox'))
416{
Timothy Warren01b129a2012-04-27 11:36:50 -0400417 /**
418 * Checkbox Field
419 *
420 * @param mixed
421 * @param string
422 * @param bool
423 * @param string
424 * @return string
425 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
427 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300428 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429
Andrey Andreev93a83c72012-03-26 21:24:02 +0300430 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 $checked = $data['checked'];
433
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200434 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 unset($data['checked']);
437 }
438 else
439 {
440 $data['checked'] = 'checked';
441 }
442 }
443
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200444 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
446 $defaults['checked'] = 'checked';
447 }
448 else
449 {
450 unset($defaults['checked']);
451 }
452
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200453 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455}
456
457// ------------------------------------------------------------------------
458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459if ( ! function_exists('form_radio'))
460{
Timothy Warren01b129a2012-04-27 11:36:50 -0400461 /**
462 * Radio Button
463 *
464 * @param mixed
465 * @param string
466 * @param bool
467 * @param string
468 * @return string
469 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
471 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200472 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 $data['type'] = 'radio';
474 return form_checkbox($data, $value, $checked, $extra);
475 }
476}
477
478// ------------------------------------------------------------------------
479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200481{
Timothy Warren01b129a2012-04-27 11:36:50 -0400482 /**
483 * Submit Button
484 *
485 * @param mixed
486 * @param string
487 * @param string
488 * @return string
489 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 function form_submit($data = '', $value = '', $extra = '')
491 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200492 $defaults = array(
493 'type' => 'submit',
494 'name' => is_array($data) ? '' : $data,
495 'value' => $value
496 );
497
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200498 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500}
501
502// ------------------------------------------------------------------------
503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504if ( ! function_exists('form_reset'))
505{
Timothy Warren01b129a2012-04-27 11:36:50 -0400506 /**
507 * Reset Button
508 *
509 * @param mixed
510 * @param string
511 * @param string
512 * @return string
513 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 function form_reset($data = '', $value = '', $extra = '')
515 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200516 $defaults = array(
517 'type' => 'reset',
518 'name' => is_array($data) ? '' : $data,
519 'value' => $value
520 );
521
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200522 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524}
525
526// ------------------------------------------------------------------------
527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528if ( ! function_exists('form_button'))
529{
Timothy Warren01b129a2012-04-27 11:36:50 -0400530 /**
531 * Form Button
532 *
533 * @param mixed
534 * @param string
535 * @param string
536 * @return string
537 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 function form_button($data = '', $content = '', $extra = '')
539 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200540 $defaults = array(
541 'name' => is_array($data) ? '' : $data,
542 'type' => 'button'
543 );
544
Andrey Andreev93a83c72012-03-26 21:24:02 +0300545 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
547 $content = $data['content'];
548 unset($data['content']); // content is not an attribute
549 }
550
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200551 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553}
554
555// ------------------------------------------------------------------------
556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557if ( ! function_exists('form_label'))
558{
Timothy Warren01b129a2012-04-27 11:36:50 -0400559 /**
560 * Form Label Tag
561 *
562 * @param string The text to appear onscreen
563 * @param string The id the label applies to
564 * @param string Additional attributes
565 * @return string
566 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 function form_label($label_text = '', $id = '', $attributes = array())
568 {
569
570 $label = '<label';
571
Alex Bilbie773ccc32012-06-02 11:11:08 +0100572 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300574 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576
Andrey Andreev93a83c72012-03-26 21:24:02 +0300577 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
579 foreach ($attributes as $key => $val)
580 {
581 $label .= ' '.$key.'="'.$val.'"';
582 }
583 }
584
Andrey Andreev93a83c72012-03-26 21:24:02 +0300585 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 }
587}
588
589// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591if ( ! function_exists('form_fieldset'))
592{
Timothy Warren01b129a2012-04-27 11:36:50 -0400593 /**
594 * Fieldset Tag
595 *
596 * Used to produce <fieldset><legend>text</legend>. To close fieldset
597 * use form_fieldset_close()
598 *
599 * @param string The legend text
vlakoffea19bc42013-07-27 10:07:43 +0200600 * @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_fieldset($legend_text = '', $attributes = array())
604 {
vlakoffea19bc42013-07-27 10:07:43 +0200605 $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100606 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300608 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
610
611 return $fieldset;
612 }
613}
614
615// ------------------------------------------------------------------------
616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617if ( ! function_exists('form_fieldset_close'))
618{
Timothy Warren01b129a2012-04-27 11:36:50 -0400619 /**
620 * Fieldset Close Tag
621 *
622 * @param string
623 * @return string
624 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 function form_fieldset_close($extra = '')
626 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300627 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 }
629}
630
631// ------------------------------------------------------------------------
632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633if ( ! function_exists('form_close'))
634{
Timothy Warren01b129a2012-04-27 11:36:50 -0400635 /**
636 * Form Close Tag
637 *
638 * @param string
639 * @return string
640 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 function form_close($extra = '')
642 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300643 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 }
645}
646
647// ------------------------------------------------------------------------
648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649if ( ! function_exists('form_prep'))
650{
Timothy Warren01b129a2012-04-27 11:36:50 -0400651 /**
652 * Form Prep
653 *
654 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
655 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200656 * @param string|string[] $str Value to escape
657 * @param bool $is_textarea Whether we're escaping for a textarea element
658 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400659 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200660 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200662 if (is_array($str))
663 {
664 foreach (array_keys($str) as $key)
665 {
666 $str[$key] = form_prep($str[$key], $is_textarea);
667 }
668
669 return $str;
670 }
671
672 if ($is_textarea === TRUE)
673 {
674 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
675 }
676
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200677 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 }
679}
680
681// ------------------------------------------------------------------------
682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683if ( ! function_exists('set_value'))
684{
Timothy Warren01b129a2012-04-27 11:36:50 -0400685 /**
686 * Form Value
687 *
688 * Grabs a value from the POST array for the specified field so you can
689 * re-populate an input field or textarea. If Form Validation
690 * is active it retrieves the info from the validation class
691 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200692 * @param string $field Field name
693 * @param string $default Default value
694 * @param bool $is_textarea Whether the field is a textarea element
695 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400696 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200697 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530699 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000700
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530701 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
702 ? $CI->form_validation->set_value($field, $default)
703 : $CI->input->post($field, FALSE);
704
705 return form_prep($value === NULL ? $default : $value, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 }
707}
708
709// ------------------------------------------------------------------------
710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711if ( ! function_exists('set_select'))
712{
Timothy Warren01b129a2012-04-27 11:36:50 -0400713 /**
714 * Set Select
715 *
716 * Let's you set the selected value of a <select> menu via data in the POST array.
717 * If Form Validation is active it retrieves the info from the validation class
718 *
719 * @param string
720 * @param string
721 * @param bool
722 * @return string
723 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 function set_select($field = '', $value = '', $default = FALSE)
725 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300726 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000727
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300728 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300730 return $CI->form_validation->set_select($field, $value, $default);
731 }
732 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
733 {
734 return ($default === TRUE) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300736
737 $value = (string) $value;
738 if (is_array($input))
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300739 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300740 // Note: in_array('', array(0)) returns TRUE, do not use it
741 foreach ($input as &$v)
742 {
743 if ($value === $v)
744 {
745 return ' selected="selected"';
746 }
747 }
748
749 return '';
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300750 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000751
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300752 return ($input === $value) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 }
754}
755
756// ------------------------------------------------------------------------
757
Derek Allard2067d1a2008-11-13 22:59:24 +0000758if ( ! function_exists('set_checkbox'))
759{
Timothy Warren01b129a2012-04-27 11:36:50 -0400760 /**
761 * Set Checkbox
762 *
763 * Let's you set the selected value of a checkbox via the value in the POST array.
764 * If Form Validation is active it retrieves the info from the validation class
765 *
766 * @param string
767 * @param string
768 * @param bool
769 * @return string
770 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 function set_checkbox($field = '', $value = '', $default = FALSE)
772 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300773 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000774
Andrey Andreevae50f552013-09-13 16:17:41 +0300775 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Barry Mienydd671972010-10-04 16:33:58 +0200776 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300777 return $CI->form_validation->set_checkbox($field, $value, $default);
778 }
779 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
780 {
781 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000782 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300783
784 $value = (string) $value;
785 if (is_array($input))
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300786 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300787 // Note: in_array('', array(0)) returns TRUE, do not use it
788 foreach ($input as &$v)
789 {
790 if ($value === $v)
791 {
792 return ' checked="checked"';
793 }
794 }
795
796 return '';
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300797 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000798
Andrey Andreevae50f552013-09-13 16:17:41 +0300799 return ($input === $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 }
801}
802
803// ------------------------------------------------------------------------
804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805if ( ! function_exists('set_radio'))
806{
Timothy Warren01b129a2012-04-27 11:36:50 -0400807 /**
808 * Set Radio
809 *
810 * Let's you set the selected value of a radio field via info in the POST array.
811 * If Form Validation is active it retrieves the info from the validation class
812 *
Andrey Andreevae50f552013-09-13 16:17:41 +0300813 * @param string $field
814 * @param string $value
815 * @param bool $default
Timothy Warren01b129a2012-04-27 11:36:50 -0400816 * @return string
817 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 function set_radio($field = '', $value = '', $default = FALSE)
819 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300820 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000821
Andrey Andreevae50f552013-09-13 16:17:41 +0300822 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300824 return $CI->form_validation->set_radio($field, $value, $default);
825 }
826 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
827 {
828 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 }
830
Andrey Andreeva587a932013-10-23 19:57:46 +0300831 return ($input === (string) $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000832 }
833}
834
835// ------------------------------------------------------------------------
836
Derek Allard2067d1a2008-11-13 22:59:24 +0000837if ( ! function_exists('form_error'))
838{
Timothy Warren01b129a2012-04-27 11:36:50 -0400839 /**
840 * Form Error
841 *
842 * Returns the error for a specific form field. This is a helper for the
843 * form validation class.
844 *
845 * @param string
846 * @param string
847 * @param string
848 * @return string
849 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 function form_error($field = '', $prefix = '', $suffix = '')
851 {
852 if (FALSE === ($OBJ =& _get_validation_object()))
853 {
854 return '';
855 }
856
857 return $OBJ->error($field, $prefix, $suffix);
858 }
859}
860
861// ------------------------------------------------------------------------
862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863if ( ! function_exists('validation_errors'))
864{
Timothy Warren01b129a2012-04-27 11:36:50 -0400865 /**
866 * Validation Error String
867 *
868 * Returns all the errors associated with a form submission. This is a helper
869 * function for the form validation class.
870 *
871 * @param string
872 * @param string
873 * @return string
874 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 function validation_errors($prefix = '', $suffix = '')
876 {
877 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500878 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 return '';
880 }
881
882 return $OBJ->error_string($prefix, $suffix);
883 }
884}
885
886// ------------------------------------------------------------------------
887
Derek Allard2067d1a2008-11-13 22:59:24 +0000888if ( ! function_exists('_parse_form_attributes'))
889{
Timothy Warren01b129a2012-04-27 11:36:50 -0400890 /**
891 * Parse the form attributes
892 *
893 * Helper function used by some of the form helpers
894 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200895 * @param array $attributes List of attributes
896 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400897 * @return string
898 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 function _parse_form_attributes($attributes, $default)
900 {
901 if (is_array($attributes))
902 {
903 foreach ($default as $key => $val)
904 {
905 if (isset($attributes[$key]))
906 {
907 $default[$key] = $attributes[$key];
908 unset($attributes[$key]);
909 }
910 }
911
912 if (count($attributes) > 0)
913 {
914 $default = array_merge($default, $attributes);
915 }
916 }
917
918 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920 foreach ($default as $key => $val)
921 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100922 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000923 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200924 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300926 elseif ($key === 'name' && ! strlen($default['name']))
927 {
928 continue;
929 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000930
Andrey Andreev93a83c72012-03-26 21:24:02 +0300931 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 }
933
934 return $att;
935 }
936}
937
938// ------------------------------------------------------------------------
939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940if ( ! function_exists('_attributes_to_string'))
941{
Timothy Warren01b129a2012-04-27 11:36:50 -0400942 /**
943 * Attributes To String
944 *
945 * Helper function used by some of the form helpers
946 *
947 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400948 * @return string
949 */
vlakoffea19bc42013-07-27 10:07:43 +0200950 function _attributes_to_string($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
vlakoffbb8b0892013-07-28 22:35:04 +0200952 if (empty($attributes))
953 {
954 return '';
955 }
956
vlakoffea19bc42013-07-27 10:07:43 +0200957 if (is_object($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300959 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
961
vlakoffea19bc42013-07-27 10:07:43 +0200962 if (is_array($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Derek Allard3241d732009-09-17 12:17:45 +0000964 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
Derek Allard3241d732009-09-17 12:17:45 +0000966 foreach ($attributes as $key => $val)
967 {
968 $atts .= ' '.$key.'="'.$val.'"';
969 }
970
971 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
vlakoffea19bc42013-07-27 10:07:43 +0200973
vlakofff7464752013-07-28 22:23:21 +0200974 if (is_string($attributes))
975 {
Brennan Thompson7a772e52014-02-16 19:22:54 -0700976 return ' '.$attributes;
vlakofff7464752013-07-28 22:23:21 +0200977 }
978
vlakoffea19bc42013-07-27 10:07:43 +0200979 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
981}
982
983// ------------------------------------------------------------------------
984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985if ( ! function_exists('_get_validation_object'))
986{
Timothy Warren01b129a2012-04-27 11:36:50 -0400987 /**
988 * Validation Object
989 *
990 * Determines what the form validation class was instantiated as, fetches
991 * the object and returns it.
992 *
993 * @return mixed
994 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000995 function &_get_validation_object()
996 {
997 $CI =& get_instance();
998
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500999 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001001
Andrey Andreev519f87a2013-07-23 17:16:10 +03001002 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001004 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1005 {
1006 return $return;
1007 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001008
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001009 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001011
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001012 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 }
1014}
1015
Derek Allard2067d1a2008-11-13 22:59:24 +00001016/* End of file form_helper.php */
Zachary Cardoza9f27a3e2013-03-23 21:59:20 -07001017/* Location: ./system/helpers/form_helper.php */