blob: a23ffcae24f4798f532146b7c66ef71442c00427 [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
Andrey Andreevea41c8a2014-02-26 18:31:02 +020057 // If no action is provided then set to the current url
58 if ( ! $action)
59 {
60 $action = $CI->config->site_url($CI->uri->uri_string());
61 }
vlakoffc4f9c622013-07-27 10:08:00 +020062 // If an action is not a full URL then turn it into one
Andrey Andreevea41c8a2014-02-26 18:31:02 +020063 elseif (strpos($action, '://') === FALSE)
vlakoffc4f9c622013-07-27 10:08:00 +020064 {
65 $action = $CI->config->site_url($action);
66 }
vlakoffc4f9c622013-07-27 10:08:00 +020067
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 Andreevea41c8a2014-02-26 18:31:02 +020083 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 -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 Andreev93b4e782014-03-04 17:48:21 +020088 if (is_array($hidden))
Greg Aker28b425a2010-09-15 11:43:23 -050089 {
Andrey Andreev93b4e782014-03-04 17:48:21 +020090 foreach ($hidden as $name => $value)
91 {
92 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" style="display:none;" />'."\n";
93 }
Derek Allard958543a2010-07-22 14:10:26 -040094 }
95
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return $form;
97 }
98}
99
100// ------------------------------------------------------------------------
101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102if ( ! function_exists('form_open_multipart'))
103{
Timothy Warren01b129a2012-04-27 11:36:50 -0400104 /**
105 * Form Declaration - Multipart type
106 *
107 * Creates the opening portion of the form, but with "multipart/form-data".
108 *
109 * @param string the URI segments of the form destination
110 * @param array a key/value pair of attributes
111 * @param array a key/value pair hidden data
112 * @return string
113 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500114 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000116 if (is_string($attributes))
117 {
118 $attributes .= ' enctype="multipart/form-data"';
119 }
120 else
121 {
122 $attributes['enctype'] = 'multipart/form-data';
123 }
124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 return form_open($action, $attributes, $hidden);
126 }
127}
128
129// ------------------------------------------------------------------------
130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131if ( ! function_exists('form_hidden'))
132{
Timothy Warren01b129a2012-04-27 11:36:50 -0400133 /**
134 * Hidden Input Field
135 *
136 * Generates hidden fields. You can pass a simple key/value string or
137 * an associative array with multiple values.
138 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200139 * @param mixed $name Field name
140 * @param string $value Field value
141 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400142 * @return string
143 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000144 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000146 static $form;
147
148 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000150 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
Robin Sowell57fe4102009-03-26 18:58:46 +0000153 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000155 foreach ($name as $key => $val)
156 {
157 form_hidden($key, $val, TRUE);
158 }
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200159
Robin Sowell57fe4102009-03-26 18:58:46 +0000160 return $form;
161 }
162
163 if ( ! is_array($value))
164 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200165 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000166 }
167 else
168 {
169 foreach ($value as $k => $v)
170 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300171 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000172 form_hidden($name.'['.$k.']', $v, TRUE);
173 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 return $form;
177 }
178}
179
180// ------------------------------------------------------------------------
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182if ( ! function_exists('form_input'))
183{
Timothy Warren01b129a2012-04-27 11:36:50 -0400184 /**
185 * Text Input Field
186 *
187 * @param mixed
188 * @param string
189 * @param string
190 * @return string
191 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 function form_input($data = '', $value = '', $extra = '')
193 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200194 $defaults = array(
195 'type' => 'text',
196 'name' => is_array($data) ? '' : $data,
197 'value' => $value
198 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200200 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202}
203
204// ------------------------------------------------------------------------
205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206if ( ! function_exists('form_password'))
207{
Timothy Warren01b129a2012-04-27 11:36:50 -0400208 /**
209 * Password Field
210 *
211 * Identical to the input function but adds the "password" type
212 *
213 * @param mixed
214 * @param string
215 * @param string
216 * @return string
217 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 function form_password($data = '', $value = '', $extra = '')
219 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200220 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $data['type'] = 'password';
222 return form_input($data, $value, $extra);
223 }
224}
225
226// ------------------------------------------------------------------------
227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228if ( ! function_exists('form_upload'))
229{
Timothy Warren01b129a2012-04-27 11:36:50 -0400230 /**
231 * Upload Field
232 *
233 * Identical to the input function but adds the "file" type
234 *
235 * @param mixed
236 * @param string
237 * @param string
238 * @return string
239 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 function form_upload($data = '', $value = '', $extra = '')
241 {
Bo-Yi Wu06ddcf02013-02-18 08:52:05 +0800242 $defaults = array('type' => 'file', 'name' => '');
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200243 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 $data['type'] = 'file';
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200245 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247}
248
249// ------------------------------------------------------------------------
250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251if ( ! function_exists('form_textarea'))
252{
Timothy Warren01b129a2012-04-27 11:36:50 -0400253 /**
254 * Textarea field
255 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200256 * @param mixed $data
257 * @param string $value
258 * @param string $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400259 * @return string
260 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 function form_textarea($data = '', $value = '', $extra = '')
262 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200263 $defaults = array(
264 'name' => is_array($data) ? '' : $data,
265 'cols' => '40',
266 'rows' => '10'
267 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
269 if ( ! is_array($data) OR ! isset($data['value']))
270 {
271 $val = $value;
272 }
273 else
274 {
Barry Mienydd671972010-10-04 16:33:58 +0200275 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 unset($data['value']); // textareas don't use the value attribute
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200279 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281}
282
283// ------------------------------------------------------------------------
284
Derek Jones68788d52010-03-05 10:11:31 -0600285if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000286{
Timothy Warren01b129a2012-04-27 11:36:50 -0400287 /**
288 * Multi-select menu
289 *
290 * @param string
291 * @param array
292 * @param mixed
293 * @param string
294 * @return string
295 */
Derek Jones26399292009-04-08 16:14:17 +0000296 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
297 {
298 if ( ! strpos($extra, 'multiple'))
299 {
300 $extra .= ' multiple="multiple"';
301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jones26399292009-04-08 16:14:17 +0000303 return form_dropdown($name, $options, $selected, $extra);
304 }
305}
306
307// --------------------------------------------------------------------
308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309if ( ! function_exists('form_dropdown'))
310{
Timothy Warren01b129a2012-04-27 11:36:50 -0400311 /**
312 * Drop-down Menu
313 *
Brennan Thompson40cd6002014-02-14 12:06:38 -0700314 * @param mixed $data
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200315 * @param mixed $options
316 * @param mixed $selected
317 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400318 * @return string
319 */
Brennan Thompson40cd6002014-02-14 12:06:38 -0700320 function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200322 $defaults = array();
Brennan Thompson82c78a92014-02-17 10:02:19 -0700323
Andrey Andreev15662dd2014-03-06 13:45:33 +0200324 if (is_array($data))
Andrey Andreev93a83c72012-03-26 21:24:02 +0300325 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200326 if (isset($data['selected']))
327 {
328 $selected = $data['selected'];
329 unset($data['selected']); // select tags don't have a selected attribute
330 }
331
332 if (isset($data['options']))
333 {
334 $options = $data['options'];
335 unset($data['options']); // select tags don't use an options attribute
336 }
337 }
338 else
339 {
340 $defaults = array('name' => $data);
Andrey Andreev93a83c72012-03-26 21:24:02 +0300341 }
342
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300343 is_array($selected) OR $selected = array($selected);
Andrey Andreev15662dd2014-03-06 13:45:33 +0200344 is_array($options) OR $options = array($options);
Derek Allard2067d1a2008-11-13 22:59:24 +0000345
346 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev15662dd2014-03-06 13:45:33 +0200347 if (empty($selected))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreev15662dd2014-03-06 13:45:33 +0200349 if (is_array($data))
350 {
351 if (isset($data['name'], $_POST[$data['name']]))
352 {
353 $selected = array($_POST[$data['name']]);
354 }
355 }
356 elseif (isset($_POST[$data]))
357 {
358 $selected = array($_POST[$data]);
359 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700361
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700362 $extra = _attributes_to_string($extra);
Brennan Thompson82c78a92014-02-17 10:02:19 -0700363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
Brennan Thompson82c78a92014-02-17 10:02:19 -0700365
Brennan Thompson1d03ef42014-02-17 12:08:22 -0700366 $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
Brennan Thompson82c78a92014-02-17 10:02:19 -0700367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 foreach ($options as $key => $val)
369 {
370 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000371
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300372 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000373 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300374 if (empty($val))
375 {
376 continue;
377 }
378
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200379 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000380
Derek Allard78a5fc92009-02-05 16:34:35 +0000381 foreach ($val as $optgroup_key => $optgroup_val)
382 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300383 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200384 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700385 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000386 }
387
Andrey Andreev93a83c72012-03-26 21:24:02 +0300388 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000389 }
390 else
391 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200392 $form .= '<option value="'.form_prep($key).'"'
Brennan Thompson82c78a92014-02-17 10:02:19 -0700393 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
394 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000395 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
Brennan Thompson82c78a92014-02-17 10:02:19 -0700397
Andrey Andreev93a83c72012-03-26 21:24:02 +0300398 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
400}
401
402// ------------------------------------------------------------------------
403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404if ( ! function_exists('form_checkbox'))
405{
Timothy Warren01b129a2012-04-27 11:36:50 -0400406 /**
407 * Checkbox Field
408 *
409 * @param mixed
410 * @param string
411 * @param bool
412 * @param string
413 * @return string
414 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
416 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300417 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000418
Andrey Andreev93a83c72012-03-26 21:24:02 +0300419 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 $checked = $data['checked'];
422
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200423 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 unset($data['checked']);
426 }
427 else
428 {
429 $data['checked'] = 'checked';
430 }
431 }
432
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200433 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 $defaults['checked'] = 'checked';
436 }
437 else
438 {
439 unset($defaults['checked']);
440 }
441
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200442 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 }
444}
445
446// ------------------------------------------------------------------------
447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448if ( ! function_exists('form_radio'))
449{
Timothy Warren01b129a2012-04-27 11:36:50 -0400450 /**
451 * Radio Button
452 *
453 * @param mixed
454 * @param string
455 * @param bool
456 * @param string
457 * @return string
458 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
460 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200461 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 $data['type'] = 'radio';
463 return form_checkbox($data, $value, $checked, $extra);
464 }
465}
466
467// ------------------------------------------------------------------------
468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200470{
Timothy Warren01b129a2012-04-27 11:36:50 -0400471 /**
472 * Submit Button
473 *
474 * @param mixed
475 * @param string
476 * @param string
477 * @return string
478 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 function form_submit($data = '', $value = '', $extra = '')
480 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200481 $defaults = array(
482 'type' => 'submit',
483 'name' => is_array($data) ? '' : $data,
484 'value' => $value
485 );
486
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200487 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489}
490
491// ------------------------------------------------------------------------
492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493if ( ! function_exists('form_reset'))
494{
Timothy Warren01b129a2012-04-27 11:36:50 -0400495 /**
496 * Reset Button
497 *
498 * @param mixed
499 * @param string
500 * @param string
501 * @return string
502 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 function form_reset($data = '', $value = '', $extra = '')
504 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200505 $defaults = array(
506 'type' => 'reset',
507 'name' => is_array($data) ? '' : $data,
508 'value' => $value
509 );
510
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200511 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513}
514
515// ------------------------------------------------------------------------
516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517if ( ! function_exists('form_button'))
518{
Timothy Warren01b129a2012-04-27 11:36:50 -0400519 /**
520 * Form Button
521 *
522 * @param mixed
523 * @param string
524 * @param string
525 * @return string
526 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 function form_button($data = '', $content = '', $extra = '')
528 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200529 $defaults = array(
530 'name' => is_array($data) ? '' : $data,
531 'type' => 'button'
532 );
533
Andrey Andreev93a83c72012-03-26 21:24:02 +0300534 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
536 $content = $data['content'];
537 unset($data['content']); // content is not an attribute
538 }
539
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200540 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 }
542}
543
544// ------------------------------------------------------------------------
545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546if ( ! function_exists('form_label'))
547{
Timothy Warren01b129a2012-04-27 11:36:50 -0400548 /**
549 * Form Label Tag
550 *
551 * @param string The text to appear onscreen
552 * @param string The id the label applies to
553 * @param string Additional attributes
554 * @return string
555 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 function form_label($label_text = '', $id = '', $attributes = array())
557 {
558
559 $label = '<label';
560
Alex Bilbie773ccc32012-06-02 11:11:08 +0100561 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300563 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
Andrey Andreev93a83c72012-03-26 21:24:02 +0300566 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
568 foreach ($attributes as $key => $val)
569 {
570 $label .= ' '.$key.'="'.$val.'"';
571 }
572 }
573
Andrey Andreev93a83c72012-03-26 21:24:02 +0300574 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576}
577
578// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580if ( ! function_exists('form_fieldset'))
581{
Timothy Warren01b129a2012-04-27 11:36:50 -0400582 /**
583 * Fieldset Tag
584 *
585 * Used to produce <fieldset><legend>text</legend>. To close fieldset
586 * use form_fieldset_close()
587 *
588 * @param string The legend text
vlakoffea19bc42013-07-27 10:07:43 +0200589 * @param array Additional attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400590 * @return string
591 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 function form_fieldset($legend_text = '', $attributes = array())
593 {
vlakoffea19bc42013-07-27 10:07:43 +0200594 $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100595 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300597 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 }
599
600 return $fieldset;
601 }
602}
603
604// ------------------------------------------------------------------------
605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606if ( ! function_exists('form_fieldset_close'))
607{
Timothy Warren01b129a2012-04-27 11:36:50 -0400608 /**
609 * Fieldset Close Tag
610 *
611 * @param string
612 * @return string
613 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 function form_fieldset_close($extra = '')
615 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300616 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 }
618}
619
620// ------------------------------------------------------------------------
621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622if ( ! function_exists('form_close'))
623{
Timothy Warren01b129a2012-04-27 11:36:50 -0400624 /**
625 * Form Close Tag
626 *
627 * @param string
628 * @return string
629 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 function form_close($extra = '')
631 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300632 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
634}
635
636// ------------------------------------------------------------------------
637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638if ( ! function_exists('form_prep'))
639{
Timothy Warren01b129a2012-04-27 11:36:50 -0400640 /**
641 * Form Prep
642 *
643 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
644 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200645 * @param string|string[] $str Value to escape
646 * @param bool $is_textarea Whether we're escaping for a textarea element
647 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400648 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200649 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200651 if (is_array($str))
652 {
653 foreach (array_keys($str) as $key)
654 {
655 $str[$key] = form_prep($str[$key], $is_textarea);
656 }
657
658 return $str;
659 }
660
661 if ($is_textarea === TRUE)
662 {
663 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
664 }
665
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200666 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 }
668}
669
670// ------------------------------------------------------------------------
671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672if ( ! function_exists('set_value'))
673{
Timothy Warren01b129a2012-04-27 11:36:50 -0400674 /**
675 * Form Value
676 *
677 * Grabs a value from the POST array for the specified field so you can
678 * re-populate an input field or textarea. If Form Validation
679 * is active it retrieves the info from the validation class
680 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200681 * @param string $field Field name
682 * @param string $default Default value
683 * @param bool $is_textarea Whether the field is a textarea element
684 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400685 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200686 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530688 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000689
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530690 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
691 ? $CI->form_validation->set_value($field, $default)
692 : $CI->input->post($field, FALSE);
693
694 return form_prep($value === NULL ? $default : $value, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 }
696}
697
698// ------------------------------------------------------------------------
699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700if ( ! function_exists('set_select'))
701{
Timothy Warren01b129a2012-04-27 11:36:50 -0400702 /**
703 * Set Select
704 *
705 * Let's you set the selected value of a <select> menu via data in the POST array.
706 * If Form Validation is active it retrieves the info from the validation class
707 *
708 * @param string
709 * @param string
710 * @param bool
711 * @return string
712 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 function set_select($field = '', $value = '', $default = FALSE)
714 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300715 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000716
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300717 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300719 return $CI->form_validation->set_select($field, $value, $default);
720 }
721 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
722 {
723 return ($default === TRUE) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300725
726 $value = (string) $value;
727 if (is_array($input))
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300728 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300729 // Note: in_array('', array(0)) returns TRUE, do not use it
730 foreach ($input as &$v)
731 {
732 if ($value === $v)
733 {
734 return ' selected="selected"';
735 }
736 }
737
738 return '';
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300739 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000740
Andrey Andreevd3b7e242013-09-13 18:36:29 +0300741 return ($input === $value) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 }
743}
744
745// ------------------------------------------------------------------------
746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747if ( ! function_exists('set_checkbox'))
748{
Timothy Warren01b129a2012-04-27 11:36:50 -0400749 /**
750 * Set Checkbox
751 *
752 * Let's you set the selected value of a checkbox via the value in the POST array.
753 * If Form Validation is active it retrieves the info from the validation class
754 *
755 * @param string
756 * @param string
757 * @param bool
758 * @return string
759 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 function set_checkbox($field = '', $value = '', $default = FALSE)
761 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300762 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000763
Andrey Andreevae50f552013-09-13 16:17:41 +0300764 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Barry Mienydd671972010-10-04 16:33:58 +0200765 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300766 return $CI->form_validation->set_checkbox($field, $value, $default);
767 }
768 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
769 {
770 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 }
Andrey Andreeva587a932013-10-23 19:57:46 +0300772
773 $value = (string) $value;
774 if (is_array($input))
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300775 {
Andrey Andreeva587a932013-10-23 19:57:46 +0300776 // Note: in_array('', array(0)) returns TRUE, do not use it
777 foreach ($input as &$v)
778 {
779 if ($value === $v)
780 {
781 return ' checked="checked"';
782 }
783 }
784
785 return '';
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300786 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000787
Andrey Andreevae50f552013-09-13 16:17:41 +0300788 return ($input === $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
790}
791
792// ------------------------------------------------------------------------
793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794if ( ! function_exists('set_radio'))
795{
Timothy Warren01b129a2012-04-27 11:36:50 -0400796 /**
797 * Set Radio
798 *
799 * Let's you set the selected value of a radio field via info in the POST array.
800 * If Form Validation is active it retrieves the info from the validation class
801 *
Andrey Andreevae50f552013-09-13 16:17:41 +0300802 * @param string $field
803 * @param string $value
804 * @param bool $default
Timothy Warren01b129a2012-04-27 11:36:50 -0400805 * @return string
806 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 function set_radio($field = '', $value = '', $default = FALSE)
808 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300809 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000810
Andrey Andreevae50f552013-09-13 16:17:41 +0300811 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300813 return $CI->form_validation->set_radio($field, $value, $default);
814 }
815 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
816 {
817 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000818 }
819
Andrey Andreeva587a932013-10-23 19:57:46 +0300820 return ($input === (string) $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 }
822}
823
824// ------------------------------------------------------------------------
825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826if ( ! function_exists('form_error'))
827{
Timothy Warren01b129a2012-04-27 11:36:50 -0400828 /**
829 * Form Error
830 *
831 * Returns the error for a specific form field. This is a helper for the
832 * form validation class.
833 *
834 * @param string
835 * @param string
836 * @param string
837 * @return string
838 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 function form_error($field = '', $prefix = '', $suffix = '')
840 {
841 if (FALSE === ($OBJ =& _get_validation_object()))
842 {
843 return '';
844 }
845
846 return $OBJ->error($field, $prefix, $suffix);
847 }
848}
849
850// ------------------------------------------------------------------------
851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852if ( ! function_exists('validation_errors'))
853{
Timothy Warren01b129a2012-04-27 11:36:50 -0400854 /**
855 * Validation Error String
856 *
857 * Returns all the errors associated with a form submission. This is a helper
858 * function for the form validation class.
859 *
860 * @param string
861 * @param string
862 * @return string
863 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 function validation_errors($prefix = '', $suffix = '')
865 {
866 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500867 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 return '';
869 }
870
871 return $OBJ->error_string($prefix, $suffix);
872 }
873}
874
875// ------------------------------------------------------------------------
876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877if ( ! function_exists('_parse_form_attributes'))
878{
Timothy Warren01b129a2012-04-27 11:36:50 -0400879 /**
880 * Parse the form attributes
881 *
882 * Helper function used by some of the form helpers
883 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200884 * @param array $attributes List of attributes
885 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400886 * @return string
887 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 function _parse_form_attributes($attributes, $default)
889 {
890 if (is_array($attributes))
891 {
892 foreach ($default as $key => $val)
893 {
894 if (isset($attributes[$key]))
895 {
896 $default[$key] = $attributes[$key];
897 unset($attributes[$key]);
898 }
899 }
900
901 if (count($attributes) > 0)
902 {
903 $default = array_merge($default, $attributes);
904 }
905 }
906
907 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 foreach ($default as $key => $val)
910 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100911 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200913 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300915 elseif ($key === 'name' && ! strlen($default['name']))
916 {
917 continue;
918 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000919
Andrey Andreev93a83c72012-03-26 21:24:02 +0300920 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
922
923 return $att;
924 }
925}
926
927// ------------------------------------------------------------------------
928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929if ( ! function_exists('_attributes_to_string'))
930{
Timothy Warren01b129a2012-04-27 11:36:50 -0400931 /**
932 * Attributes To String
933 *
934 * Helper function used by some of the form helpers
935 *
936 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400937 * @return string
938 */
vlakoffea19bc42013-07-27 10:07:43 +0200939 function _attributes_to_string($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
vlakoffbb8b0892013-07-28 22:35:04 +0200941 if (empty($attributes))
942 {
943 return '';
944 }
945
vlakoffea19bc42013-07-27 10:07:43 +0200946 if (is_object($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300948 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
950
vlakoffea19bc42013-07-27 10:07:43 +0200951 if (is_array($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Derek Allard3241d732009-09-17 12:17:45 +0000953 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000954
Derek Allard3241d732009-09-17 12:17:45 +0000955 foreach ($attributes as $key => $val)
956 {
957 $atts .= ' '.$key.'="'.$val.'"';
958 }
959
960 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 }
vlakoffea19bc42013-07-27 10:07:43 +0200962
vlakofff7464752013-07-28 22:23:21 +0200963 if (is_string($attributes))
964 {
Brennan Thompson7a772e52014-02-16 19:22:54 -0700965 return ' '.$attributes;
vlakofff7464752013-07-28 22:23:21 +0200966 }
967
vlakoffea19bc42013-07-27 10:07:43 +0200968 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 }
970}
971
972// ------------------------------------------------------------------------
973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974if ( ! function_exists('_get_validation_object'))
975{
Timothy Warren01b129a2012-04-27 11:36:50 -0400976 /**
977 * Validation Object
978 *
979 * Determines what the form validation class was instantiated as, fetches
980 * the object and returns it.
981 *
982 * @return mixed
983 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 function &_get_validation_object()
985 {
986 $CI =& get_instance();
987
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500988 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200990
Andrey Andreev519f87a2013-07-23 17:16:10 +0300991 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500993 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
994 {
995 return $return;
996 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200997
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500998 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001000
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001001 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 }
1003}
1004
Derek Allard2067d1a2008-11-13 22:59:24 +00001005/* End of file form_helper.php */
Zachary Cardoza9f27a3e2013-03-23 21:59:20 -07001006/* Location: ./system/helpers/form_helper.php */