blob: a724406f0b347d78509336af7cdd41ff99b740d8 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
Andrey Andreevc5536aa2012-11-01 17:33:58 +020025 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Form Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('form_open'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Form Declaration
45 *
46 * Creates the opening portion of the form.
47 *
48 * @param string the URI segments of the form destination
49 * @param array a key/value pair of attributes
50 * @param array a key/value pair hidden data
51 * @return string
52 */
vlakoffea19bc42013-07-27 10:07:43 +020053 function form_open($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
55 $CI =& get_instance();
56
vlakoffc4f9c622013-07-27 10:08:00 +020057 // If an action is not a full URL then turn it into one
58 if ($action && strpos($action, '://') === FALSE)
59 {
60 $action = $CI->config->site_url($action);
61 }
62 elseif ( ! $action)
63 {
64 // If no action is provided then set to the current url
65 $action = $CI->config->site_url($CI->uri->uri_string());
66 }
67
vlakoffea19bc42013-07-27 10:07:43 +020068 $attributes = _attributes_to_string($attributes);
69
70 if (stripos($attributes, 'method=') === FALSE)
Andrey Andreev122ca9b2013-07-26 18:16:26 +030071 {
72 $attributes .= ' method="post"';
73 }
Derek Allard2067d1a2008-11-13 22:59:24 +000074
vlakoffea19bc42013-07-27 10:07:43 +020075 if (stripos($attributes, 'accept-charset=') === FALSE)
76 {
77 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
78 }
79
vlakoffea19bc42013-07-27 10:07:43 +020080 $form = '<form action="'.$action.'"'.$attributes.">\n";
Barry Mienydd671972010-10-04 16:33:58 +020081
Andrey Andreev93a83c72012-03-26 21:24:02 +030082 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
Andrey Andreev122ca9b2013-07-26 18:16:26 +030083 if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR stripos($form, 'method="get"')))
Derek Allard958543a2010-07-22 14:10:26 -040084 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050085 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050086 }
87
Andrey Andreev93a83c72012-03-26 21:24:02 +030088 if (is_array($hidden) && count($hidden) > 0)
Greg Aker28b425a2010-09-15 11:43:23 -050089 {
Andrey Andreev3c32a112012-06-16 18:05:34 +030090 $form .= '<div style="display:none;">'.form_hidden($hidden).'</div>';
Derek Allard958543a2010-07-22 14:10:26 -040091 }
92
Derek Allard2067d1a2008-11-13 22:59:24 +000093 return $form;
94 }
95}
96
97// ------------------------------------------------------------------------
98
Derek Allard2067d1a2008-11-13 22:59:24 +000099if ( ! function_exists('form_open_multipart'))
100{
Timothy Warren01b129a2012-04-27 11:36:50 -0400101 /**
102 * Form Declaration - Multipart type
103 *
104 * Creates the opening portion of the form, but with "multipart/form-data".
105 *
106 * @param string the URI segments of the form destination
107 * @param array a key/value pair of attributes
108 * @param array a key/value pair hidden data
109 * @return string
110 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500111 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000113 if (is_string($attributes))
114 {
115 $attributes .= ' enctype="multipart/form-data"';
116 }
117 else
118 {
119 $attributes['enctype'] = 'multipart/form-data';
120 }
121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 return form_open($action, $attributes, $hidden);
123 }
124}
125
126// ------------------------------------------------------------------------
127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128if ( ! function_exists('form_hidden'))
129{
Timothy Warren01b129a2012-04-27 11:36:50 -0400130 /**
131 * Hidden Input Field
132 *
133 * Generates hidden fields. You can pass a simple key/value string or
134 * an associative array with multiple values.
135 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200136 * @param mixed $name Field name
137 * @param string $value Field value
138 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400139 * @return string
140 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000141 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000143 static $form;
144
145 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000147 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149
Robin Sowell57fe4102009-03-26 18:58:46 +0000150 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000152 foreach ($name as $key => $val)
153 {
154 form_hidden($key, $val, TRUE);
155 }
156 return $form;
157 }
158
159 if ( ! is_array($value))
160 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200161 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000162 }
163 else
164 {
165 foreach ($value as $k => $v)
166 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300167 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000168 form_hidden($name.'['.$k.']', $v, TRUE);
169 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 return $form;
173 }
174}
175
176// ------------------------------------------------------------------------
177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178if ( ! function_exists('form_input'))
179{
Timothy Warren01b129a2012-04-27 11:36:50 -0400180 /**
181 * Text Input Field
182 *
183 * @param mixed
184 * @param string
185 * @param string
186 * @return string
187 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 function form_input($data = '', $value = '', $extra = '')
189 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300190 $defaults = array('type' => 'text', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200192 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
194}
195
196// ------------------------------------------------------------------------
197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198if ( ! function_exists('form_password'))
199{
Timothy Warren01b129a2012-04-27 11:36:50 -0400200 /**
201 * Password Field
202 *
203 * Identical to the input function but adds the "password" type
204 *
205 * @param mixed
206 * @param string
207 * @param string
208 * @return string
209 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 function form_password($data = '', $value = '', $extra = '')
211 {
212 if ( ! is_array($data))
213 {
214 $data = array('name' => $data);
215 }
216
217 $data['type'] = 'password';
218 return form_input($data, $value, $extra);
219 }
220}
221
222// ------------------------------------------------------------------------
223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224if ( ! function_exists('form_upload'))
225{
Timothy Warren01b129a2012-04-27 11:36:50 -0400226 /**
227 * Upload Field
228 *
229 * Identical to the input function but adds the "file" type
230 *
231 * @param mixed
232 * @param string
233 * @param string
234 * @return string
235 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 function form_upload($data = '', $value = '', $extra = '')
237 {
Bo-Yi Wu06ddcf02013-02-18 08:52:05 +0800238 $defaults = array('type' => 'file', 'name' => '');
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200239 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 $data['type'] = 'file';
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200241 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243}
244
245// ------------------------------------------------------------------------
246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247if ( ! function_exists('form_textarea'))
248{
Timothy Warren01b129a2012-04-27 11:36:50 -0400249 /**
250 * Textarea field
251 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200252 * @param mixed $data
253 * @param string $value
254 * @param string $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400255 * @return string
256 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 function form_textarea($data = '', $value = '', $extra = '')
258 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300259 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
261 if ( ! is_array($data) OR ! isset($data['value']))
262 {
263 $val = $value;
264 }
265 else
266 {
Barry Mienydd671972010-10-04 16:33:58 +0200267 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 unset($data['value']); // textareas don't use the value attribute
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200271 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273}
274
275// ------------------------------------------------------------------------
276
Derek Jones68788d52010-03-05 10:11:31 -0600277if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000278{
Timothy Warren01b129a2012-04-27 11:36:50 -0400279 /**
280 * Multi-select menu
281 *
282 * @param string
283 * @param array
284 * @param mixed
285 * @param string
286 * @return string
287 */
Derek Jones26399292009-04-08 16:14:17 +0000288 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
289 {
290 if ( ! strpos($extra, 'multiple'))
291 {
292 $extra .= ' multiple="multiple"';
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Jones26399292009-04-08 16:14:17 +0000295 return form_dropdown($name, $options, $selected, $extra);
296 }
297}
298
299// --------------------------------------------------------------------
300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301if ( ! function_exists('form_dropdown'))
302{
Timothy Warren01b129a2012-04-27 11:36:50 -0400303 /**
304 * Drop-down Menu
305 *
Brennan Thompson40cd6002014-02-14 12:06:38 -0700306 * @param mixed $data
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200307 * @param mixed $options
308 * @param mixed $selected
309 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400310 * @return string
311 */
Brennan Thompson40cd6002014-02-14 12:06:38 -0700312 function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700314 $defaults = array('name' => is_array($data) ? '' : $data);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700315
316 if (is_array($data) && isset($data['selected']))
Andrey Andreev93a83c72012-03-26 21:24:02 +0300317 {
Brennan Thompson40cd6002014-02-14 12:06:38 -0700318 $selected = $data['selected'];
319 unset($data['selected']); // selects don't have a selected attribute
Andrey Andreev93a83c72012-03-26 21:24:02 +0300320 }
321
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300322 is_array($selected) OR $selected = array($selected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323
324 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200325 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200327 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700329
Brennan Thompsonc8ba6632014-02-16 19:36:00 -0700330 if (is_array($data) && isset($data['options']))
Brennan Thompson40cd6002014-02-14 12:06:38 -0700331 {
332 $options = $data['options'];
333 unset($data['options']); // selects don't use an options attribute
334 }
Brennan Thompson21ef97e2014-02-16 11:01:03 -0700335
336 is_array($options) OR $options = array($options);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700337
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700338 $extra = _attributes_to_string($extra);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Brennan Thompson82c78a92014-02-17 10:02:19 -0700341
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700342 $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
Brennan Thompson82c78a92014-02-17 10:02:19 -0700343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 foreach ($options as $key => $val)
345 {
346 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300348 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000349 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300350 if (empty($val))
351 {
352 continue;
353 }
354
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200355 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000356
Derek Allard78a5fc92009-02-05 16:34:35 +0000357 foreach ($val as $optgroup_key => $optgroup_val)
358 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300359 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200360 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700361 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000362 }
363
Andrey Andreev93a83c72012-03-26 21:24:02 +0300364 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000365 }
366 else
367 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200368 $form .= '<option value="'.form_prep($key).'"'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700369 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
370 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000371 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700373
Andrey Andreev93a83c72012-03-26 21:24:02 +0300374 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376}
377
378// ------------------------------------------------------------------------
379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380if ( ! function_exists('form_checkbox'))
381{
Timothy Warren01b129a2012-04-27 11:36:50 -0400382 /**
383 * Checkbox Field
384 *
385 * @param mixed
386 * @param string
387 * @param bool
388 * @param string
389 * @return string
390 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
392 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300393 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000394
Andrey Andreev93a83c72012-03-26 21:24:02 +0300395 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
397 $checked = $data['checked'];
398
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200399 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 unset($data['checked']);
402 }
403 else
404 {
405 $data['checked'] = 'checked';
406 }
407 }
408
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200409 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
411 $defaults['checked'] = 'checked';
412 }
413 else
414 {
415 unset($defaults['checked']);
416 }
417
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200418 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
420}
421
422// ------------------------------------------------------------------------
423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424if ( ! function_exists('form_radio'))
425{
Timothy Warren01b129a2012-04-27 11:36:50 -0400426 /**
427 * Radio Button
428 *
429 * @param mixed
430 * @param string
431 * @param bool
432 * @param string
433 * @return string
434 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
436 {
437 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200438 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 $data = array('name' => $data);
440 }
441
442 $data['type'] = 'radio';
443 return form_checkbox($data, $value, $checked, $extra);
444 }
445}
446
447// ------------------------------------------------------------------------
448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200450{
Timothy Warren01b129a2012-04-27 11:36:50 -0400451 /**
452 * Submit Button
453 *
454 * @param mixed
455 * @param string
456 * @param string
457 * @return string
458 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 function form_submit($data = '', $value = '', $extra = '')
460 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300461 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200462 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464}
465
466// ------------------------------------------------------------------------
467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468if ( ! function_exists('form_reset'))
469{
Timothy Warren01b129a2012-04-27 11:36:50 -0400470 /**
471 * Reset Button
472 *
473 * @param mixed
474 * @param string
475 * @param string
476 * @return string
477 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 function form_reset($data = '', $value = '', $extra = '')
479 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300480 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200481 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483}
484
485// ------------------------------------------------------------------------
486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487if ( ! function_exists('form_button'))
488{
Timothy Warren01b129a2012-04-27 11:36:50 -0400489 /**
490 * Form Button
491 *
492 * @param mixed
493 * @param string
494 * @param string
495 * @return string
496 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 function form_button($data = '', $content = '', $extra = '')
498 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300499 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
500 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 $content = $data['content'];
503 unset($data['content']); // content is not an attribute
504 }
505
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200506 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508}
509
510// ------------------------------------------------------------------------
511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512if ( ! function_exists('form_label'))
513{
Timothy Warren01b129a2012-04-27 11:36:50 -0400514 /**
515 * Form Label Tag
516 *
517 * @param string The text to appear onscreen
518 * @param string The id the label applies to
519 * @param string Additional attributes
520 * @return string
521 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 function form_label($label_text = '', $id = '', $attributes = array())
523 {
524
525 $label = '<label';
526
Alex Bilbie773ccc32012-06-02 11:11:08 +0100527 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300529 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
531
Andrey Andreev93a83c72012-03-26 21:24:02 +0300532 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 foreach ($attributes as $key => $val)
535 {
536 $label .= ' '.$key.'="'.$val.'"';
537 }
538 }
539
Andrey Andreev93a83c72012-03-26 21:24:02 +0300540 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542}
543
544// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546if ( ! function_exists('form_fieldset'))
547{
Timothy Warren01b129a2012-04-27 11:36:50 -0400548 /**
549 * Fieldset Tag
550 *
551 * Used to produce <fieldset><legend>text</legend>. To close fieldset
552 * use form_fieldset_close()
553 *
554 * @param string The legend text
vlakoffea19bc42013-07-27 10:07:43 +0200555 * @param array Additional attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400556 * @return string
557 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 function form_fieldset($legend_text = '', $attributes = array())
559 {
vlakoffea19bc42013-07-27 10:07:43 +0200560 $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100561 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300563 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
566 return $fieldset;
567 }
568}
569
570// ------------------------------------------------------------------------
571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572if ( ! function_exists('form_fieldset_close'))
573{
Timothy Warren01b129a2012-04-27 11:36:50 -0400574 /**
575 * Fieldset Close Tag
576 *
577 * @param string
578 * @return string
579 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 function form_fieldset_close($extra = '')
581 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300582 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
584}
585
586// ------------------------------------------------------------------------
587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588if ( ! function_exists('form_close'))
589{
Timothy Warren01b129a2012-04-27 11:36:50 -0400590 /**
591 * Form Close Tag
592 *
593 * @param string
594 * @return string
595 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 function form_close($extra = '')
597 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300598 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 }
600}
601
602// ------------------------------------------------------------------------
603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604if ( ! function_exists('form_prep'))
605{
Timothy Warren01b129a2012-04-27 11:36:50 -0400606 /**
607 * Form Prep
608 *
609 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
610 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200611 * @param string|string[] $str Value to escape
612 * @param bool $is_textarea Whether we're escaping for a textarea element
613 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400614 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200615 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200617 if (is_array($str))
618 {
619 foreach (array_keys($str) as $key)
620 {
621 $str[$key] = form_prep($str[$key], $is_textarea);
622 }
623
624 return $str;
625 }
626
627 if ($is_textarea === TRUE)
628 {
629 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
630 }
631
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200632 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
634}
635
636// ------------------------------------------------------------------------
637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638if ( ! function_exists('set_value'))
639{
Timothy Warren01b129a2012-04-27 11:36:50 -0400640 /**
641 * Form Value
642 *
643 * Grabs a value from the POST array for the specified field so you can
644 * re-populate an input field or textarea. If Form Validation
645 * is active it retrieves the info from the validation class
646 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200647 * @param string $field Field name
648 * @param string $default Default value
649 * @param bool $is_textarea Whether the field is a textarea element
650 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400651 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200652 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530654 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000655
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530656 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
657 ? $CI->form_validation->set_value($field, $default)
658 : $CI->input->post($field, FALSE);
659
660 return form_prep($value === NULL ? $default : $value, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 }
662}
663
664// ------------------------------------------------------------------------
665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666if ( ! function_exists('set_select'))
667{
Timothy Warren01b129a2012-04-27 11:36:50 -0400668 /**
669 * Set Select
670 *
671 * Let's you set the selected value of a <select> menu via data in the POST array.
672 * If Form Validation is active it retrieves the info from the validation class
673 *
674 * @param string
675 * @param string
676 * @param bool
677 * @return string
678 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 function set_select($field = '', $value = '', $default = FALSE)
680 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300681 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000682
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300683 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300685 return $CI->form_validation->set_select($field, $value, $default);
686 }
687 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
688 {
689 return ($default === TRUE) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300691
692 $value = (string) $value;
693 if (is_array($input))
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300694 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300695 // Note: in_array('', array(0)) returns TRUE, do not use it
696 foreach ($input as &$v)
697 {
698 if ($value === $v)
699 {
700 return ' selected="selected"';
701 }
702 }
703
704 return '';
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300705 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000706
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300707 return ($input === $value) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 }
709}
710
711// ------------------------------------------------------------------------
712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713if ( ! function_exists('set_checkbox'))
714{
Timothy Warren01b129a2012-04-27 11:36:50 -0400715 /**
716 * Set Checkbox
717 *
718 * Let's you set the selected value of a checkbox via the value in the POST array.
719 * If Form Validation is active it retrieves the info from the validation class
720 *
721 * @param string
722 * @param string
723 * @param bool
724 * @return string
725 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 function set_checkbox($field = '', $value = '', $default = FALSE)
727 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300728 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000729
Andrey Andreevae50f552013-09-13 16:17:41 +0300730 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Barry Mienydd671972010-10-04 16:33:58 +0200731 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300732 return $CI->form_validation->set_checkbox($field, $value, $default);
733 }
734 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
735 {
736 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300738
739 $value = (string) $value;
740 if (is_array($input))
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300741 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300742 // Note: in_array('', array(0)) returns TRUE, do not use it
743 foreach ($input as &$v)
744 {
745 if ($value === $v)
746 {
747 return ' checked="checked"';
748 }
749 }
750
751 return '';
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300752 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000753
Andrey Andreevae50f552013-09-13 16:17:41 +0300754 return ($input === $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
756}
757
758// ------------------------------------------------------------------------
759
Derek Allard2067d1a2008-11-13 22:59:24 +0000760if ( ! function_exists('set_radio'))
761{
Timothy Warren01b129a2012-04-27 11:36:50 -0400762 /**
763 * Set Radio
764 *
765 * Let's you set the selected value of a radio field via info in the POST array.
766 * If Form Validation is active it retrieves the info from the validation class
767 *
Andrey Andreevae50f552013-09-13 16:17:41 +0300768 * @param string $field
769 * @param string $value
770 * @param bool $default
Timothy Warren01b129a2012-04-27 11:36:50 -0400771 * @return string
772 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 function set_radio($field = '', $value = '', $default = FALSE)
774 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300775 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000776
Andrey Andreevae50f552013-09-13 16:17:41 +0300777 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300779 return $CI->form_validation->set_radio($field, $value, $default);
780 }
781 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
782 {
783 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 }
785
Andrey Andreeva587a932013-10-23 19:57:46 +0300786 return ($input === (string) $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
788}
789
790// ------------------------------------------------------------------------
791
Derek Allard2067d1a2008-11-13 22:59:24 +0000792if ( ! function_exists('form_error'))
793{
Timothy Warren01b129a2012-04-27 11:36:50 -0400794 /**
795 * Form Error
796 *
797 * Returns the error for a specific form field. This is a helper for the
798 * form validation class.
799 *
800 * @param string
801 * @param string
802 * @param string
803 * @return string
804 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 function form_error($field = '', $prefix = '', $suffix = '')
806 {
807 if (FALSE === ($OBJ =& _get_validation_object()))
808 {
809 return '';
810 }
811
812 return $OBJ->error($field, $prefix, $suffix);
813 }
814}
815
816// ------------------------------------------------------------------------
817
Derek Allard2067d1a2008-11-13 22:59:24 +0000818if ( ! function_exists('validation_errors'))
819{
Timothy Warren01b129a2012-04-27 11:36:50 -0400820 /**
821 * Validation Error String
822 *
823 * Returns all the errors associated with a form submission. This is a helper
824 * function for the form validation class.
825 *
826 * @param string
827 * @param string
828 * @return string
829 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 function validation_errors($prefix = '', $suffix = '')
831 {
832 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500833 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 return '';
835 }
836
837 return $OBJ->error_string($prefix, $suffix);
838 }
839}
840
841// ------------------------------------------------------------------------
842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843if ( ! function_exists('_parse_form_attributes'))
844{
Timothy Warren01b129a2012-04-27 11:36:50 -0400845 /**
846 * Parse the form attributes
847 *
848 * Helper function used by some of the form helpers
849 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200850 * @param array $attributes List of attributes
851 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400852 * @return string
853 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000854 function _parse_form_attributes($attributes, $default)
855 {
856 if (is_array($attributes))
857 {
858 foreach ($default as $key => $val)
859 {
860 if (isset($attributes[$key]))
861 {
862 $default[$key] = $attributes[$key];
863 unset($attributes[$key]);
864 }
865 }
866
867 if (count($attributes) > 0)
868 {
869 $default = array_merge($default, $attributes);
870 }
871 }
872
873 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200874
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 foreach ($default as $key => $val)
876 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100877 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000878 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200879 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300881 elseif ($key === 'name' && ! strlen($default['name']))
882 {
883 continue;
884 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000885
Andrey Andreev93a83c72012-03-26 21:24:02 +0300886 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 }
888
889 return $att;
890 }
891}
892
893// ------------------------------------------------------------------------
894
Derek Allard2067d1a2008-11-13 22:59:24 +0000895if ( ! function_exists('_attributes_to_string'))
896{
Timothy Warren01b129a2012-04-27 11:36:50 -0400897 /**
898 * Attributes To String
899 *
900 * Helper function used by some of the form helpers
901 *
902 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400903 * @return string
904 */
vlakoffea19bc42013-07-27 10:07:43 +0200905 function _attributes_to_string($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
vlakoffbb8b0892013-07-28 22:35:04 +0200907 if (empty($attributes))
908 {
909 return '';
910 }
911
vlakoffea19bc42013-07-27 10:07:43 +0200912 if (is_object($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300914 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
916
vlakoffea19bc42013-07-27 10:07:43 +0200917 if (is_array($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 {
Derek Allard3241d732009-09-17 12:17:45 +0000919 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000920
Derek Allard3241d732009-09-17 12:17:45 +0000921 foreach ($attributes as $key => $val)
922 {
923 $atts .= ' '.$key.'="'.$val.'"';
924 }
925
926 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 }
vlakoffea19bc42013-07-27 10:07:43 +0200928
vlakofff7464752013-07-28 22:23:21 +0200929 if (is_string($attributes))
930 {
Brennan Thompson7a772e52014-02-16 19:22:54 -0700931 return ' '.$attributes;
vlakofff7464752013-07-28 22:23:21 +0200932 }
933
vlakoffea19bc42013-07-27 10:07:43 +0200934 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
936}
937
938// ------------------------------------------------------------------------
939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940if ( ! function_exists('_get_validation_object'))
941{
Timothy Warren01b129a2012-04-27 11:36:50 -0400942 /**
943 * Validation Object
944 *
945 * Determines what the form validation class was instantiated as, fetches
946 * the object and returns it.
947 *
948 * @return mixed
949 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 function &_get_validation_object()
951 {
952 $CI =& get_instance();
953
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500954 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200956
Andrey Andreev519f87a2013-07-23 17:16:10 +0300957 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500959 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
960 {
961 return $return;
962 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200963
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500964 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200966
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500967 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 }
969}
970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971/* End of file form_helper.php */
Zachary Cardoza9f27a3e2013-03-23 21:59:20 -0700972/* Location: ./system/helpers/form_helper.php */