blob: 7112a99b700052ccbc510c5f0753077aa0d4f63f [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200306 * @param mixed $name
307 * @param mixed $options
308 * @param mixed $selected
309 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400310 * @return string
311 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
313 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300314 // If name is really an array then we'll call the function again using the array
315 if (is_array($name) && isset($name['name']))
316 {
317 isset($name['options']) OR $name['options'] = array();
318 isset($name['selected']) OR $name['selected'] = array();
319 isset($name['extra']) OR $name['extra'] = array();
320
321 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
322 }
323
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300324 is_array($selected) OR $selected = array($selected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000325
326 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200327 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200329 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200332 if ($extra != '')
333 {
334 $extra = ' '.$extra;
335 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
337 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
338
339 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 foreach ($options as $key => $val)
342 {
343 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300345 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000346 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300347 if (empty($val))
348 {
349 continue;
350 }
351
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200352 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000353
Derek Allard78a5fc92009-02-05 16:34:35 +0000354 foreach ($val as $optgroup_key => $optgroup_val)
355 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300356 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200357 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300358 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000359 }
360
Andrey Andreev93a83c72012-03-26 21:24:02 +0300361 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000362 }
363 else
364 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200365 $form .= '<option value="'.form_prep($key).'"'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300366 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
367 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000368 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370
Andrey Andreev93a83c72012-03-26 21:24:02 +0300371 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373}
374
375// ------------------------------------------------------------------------
376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377if ( ! function_exists('form_checkbox'))
378{
Timothy Warren01b129a2012-04-27 11:36:50 -0400379 /**
380 * Checkbox Field
381 *
382 * @param mixed
383 * @param string
384 * @param bool
385 * @param string
386 * @return string
387 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
389 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300390 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000391
Andrey Andreev93a83c72012-03-26 21:24:02 +0300392 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 $checked = $data['checked'];
395
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200396 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 unset($data['checked']);
399 }
400 else
401 {
402 $data['checked'] = 'checked';
403 }
404 }
405
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200406 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
408 $defaults['checked'] = 'checked';
409 }
410 else
411 {
412 unset($defaults['checked']);
413 }
414
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200415 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417}
418
419// ------------------------------------------------------------------------
420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421if ( ! function_exists('form_radio'))
422{
Timothy Warren01b129a2012-04-27 11:36:50 -0400423 /**
424 * Radio Button
425 *
426 * @param mixed
427 * @param string
428 * @param bool
429 * @param string
430 * @return string
431 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
433 {
434 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200435 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 $data = array('name' => $data);
437 }
438
439 $data['type'] = 'radio';
440 return form_checkbox($data, $value, $checked, $extra);
441 }
442}
443
444// ------------------------------------------------------------------------
445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200447{
Timothy Warren01b129a2012-04-27 11:36:50 -0400448 /**
449 * Submit Button
450 *
451 * @param mixed
452 * @param string
453 * @param string
454 * @return string
455 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 function form_submit($data = '', $value = '', $extra = '')
457 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300458 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200459 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
461}
462
463// ------------------------------------------------------------------------
464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465if ( ! function_exists('form_reset'))
466{
Timothy Warren01b129a2012-04-27 11:36:50 -0400467 /**
468 * Reset Button
469 *
470 * @param mixed
471 * @param string
472 * @param string
473 * @return string
474 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 function form_reset($data = '', $value = '', $extra = '')
476 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300477 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200478 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480}
481
482// ------------------------------------------------------------------------
483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484if ( ! function_exists('form_button'))
485{
Timothy Warren01b129a2012-04-27 11:36:50 -0400486 /**
487 * Form Button
488 *
489 * @param mixed
490 * @param string
491 * @param string
492 * @return string
493 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 function form_button($data = '', $content = '', $extra = '')
495 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300496 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
497 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 $content = $data['content'];
500 unset($data['content']); // content is not an attribute
501 }
502
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200503 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505}
506
507// ------------------------------------------------------------------------
508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509if ( ! function_exists('form_label'))
510{
Timothy Warren01b129a2012-04-27 11:36:50 -0400511 /**
512 * Form Label Tag
513 *
514 * @param string The text to appear onscreen
515 * @param string The id the label applies to
516 * @param string Additional attributes
517 * @return string
518 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 function form_label($label_text = '', $id = '', $attributes = array())
520 {
521
522 $label = '<label';
523
Alex Bilbie773ccc32012-06-02 11:11:08 +0100524 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300526 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528
Andrey Andreev93a83c72012-03-26 21:24:02 +0300529 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
531 foreach ($attributes as $key => $val)
532 {
533 $label .= ' '.$key.'="'.$val.'"';
534 }
535 }
536
Andrey Andreev93a83c72012-03-26 21:24:02 +0300537 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539}
540
541// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543if ( ! function_exists('form_fieldset'))
544{
Timothy Warren01b129a2012-04-27 11:36:50 -0400545 /**
546 * Fieldset Tag
547 *
548 * Used to produce <fieldset><legend>text</legend>. To close fieldset
549 * use form_fieldset_close()
550 *
551 * @param string The legend text
vlakoffea19bc42013-07-27 10:07:43 +0200552 * @param array Additional attributes
Timothy Warren01b129a2012-04-27 11:36:50 -0400553 * @return string
554 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 function form_fieldset($legend_text = '', $attributes = array())
556 {
vlakoffea19bc42013-07-27 10:07:43 +0200557 $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100558 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300560 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
562
563 return $fieldset;
564 }
565}
566
567// ------------------------------------------------------------------------
568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569if ( ! function_exists('form_fieldset_close'))
570{
Timothy Warren01b129a2012-04-27 11:36:50 -0400571 /**
572 * Fieldset Close Tag
573 *
574 * @param string
575 * @return string
576 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 function form_fieldset_close($extra = '')
578 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300579 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
581}
582
583// ------------------------------------------------------------------------
584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585if ( ! function_exists('form_close'))
586{
Timothy Warren01b129a2012-04-27 11:36:50 -0400587 /**
588 * Form Close Tag
589 *
590 * @param string
591 * @return string
592 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 function form_close($extra = '')
594 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300595 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
597}
598
599// ------------------------------------------------------------------------
600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601if ( ! function_exists('form_prep'))
602{
Timothy Warren01b129a2012-04-27 11:36:50 -0400603 /**
604 * Form Prep
605 *
606 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
607 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200608 * @param string|string[] $str Value to escape
609 * @param bool $is_textarea Whether we're escaping for a textarea element
610 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400611 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200612 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200614 if (is_array($str))
615 {
616 foreach (array_keys($str) as $key)
617 {
618 $str[$key] = form_prep($str[$key], $is_textarea);
619 }
620
621 return $str;
622 }
623
624 if ($is_textarea === TRUE)
625 {
626 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
627 }
628
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200629 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
631}
632
633// ------------------------------------------------------------------------
634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635if ( ! function_exists('set_value'))
636{
Timothy Warren01b129a2012-04-27 11:36:50 -0400637 /**
638 * Form Value
639 *
640 * Grabs a value from the POST array for the specified field so you can
641 * re-populate an input field or textarea. If Form Validation
642 * is active it retrieves the info from the validation class
643 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200644 * @param string $field Field name
645 * @param string $default Default value
646 * @param bool $is_textarea Whether the field is a textarea element
647 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400648 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200649 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530651 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000652
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530653 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
654 ? $CI->form_validation->set_value($field, $default)
655 : $CI->input->post($field, FALSE);
656
657 return form_prep($value === NULL ? $default : $value, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 }
659}
660
661// ------------------------------------------------------------------------
662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663if ( ! function_exists('set_select'))
664{
Timothy Warren01b129a2012-04-27 11:36:50 -0400665 /**
666 * Set Select
667 *
668 * Let's you set the selected value of a <select> menu via data in the POST array.
669 * If Form Validation is active it retrieves the info from the validation class
670 *
671 * @param string
672 * @param string
673 * @param bool
674 * @return string
675 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 function set_select($field = '', $value = '', $default = FALSE)
677 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300678 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000679
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300680 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 {
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300682 return $CI->form_validation->set_select($field, $value, $default);
683 }
684 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
685 {
686 return ($default === TRUE) ? ' selected="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 }
688
Andrey Andreev67f6a5e2013-09-13 16:21:31 +0300689 return ($input === $value) ? ' checked="selected"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 }
691}
692
693// ------------------------------------------------------------------------
694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695if ( ! function_exists('set_checkbox'))
696{
Timothy Warren01b129a2012-04-27 11:36:50 -0400697 /**
698 * Set Checkbox
699 *
700 * Let's you set the selected value of a checkbox via the value in the POST array.
701 * If Form Validation is active it retrieves the info from the validation class
702 *
703 * @param string
704 * @param string
705 * @param bool
706 * @return string
707 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 function set_checkbox($field = '', $value = '', $default = FALSE)
709 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300710 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000711
Andrey Andreevae50f552013-09-13 16:17:41 +0300712 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Barry Mienydd671972010-10-04 16:33:58 +0200713 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300714 return $CI->form_validation->set_checkbox($field, $value, $default);
715 }
716 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
717 {
718 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 }
Andrey Andreeve8a23a52013-09-13 18:29:29 +0300720 elseif (is_array($input) && in_array($value, $input, TRUE))
721 {
722 return ' checked="checked"';
723 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000724
Andrey Andreevae50f552013-09-13 16:17:41 +0300725 return ($input === $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 }
727}
728
729// ------------------------------------------------------------------------
730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731if ( ! function_exists('set_radio'))
732{
Timothy Warren01b129a2012-04-27 11:36:50 -0400733 /**
734 * Set Radio
735 *
736 * Let's you set the selected value of a radio field via info in the POST array.
737 * If Form Validation is active it retrieves the info from the validation class
738 *
Andrey Andreevae50f552013-09-13 16:17:41 +0300739 * @param string $field
740 * @param string $value
741 * @param bool $default
Timothy Warren01b129a2012-04-27 11:36:50 -0400742 * @return string
743 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 function set_radio($field = '', $value = '', $default = FALSE)
745 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300746 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000747
Andrey Andreevae50f552013-09-13 16:17:41 +0300748 if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 {
Andrey Andreevae50f552013-09-13 16:17:41 +0300750 return $CI->form_validation->set_radio($field, $value, $default);
751 }
752 elseif (($input = $CI->input->post($field, FALSE)) === NULL)
753 {
754 return ($default === TRUE) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
756
Andrey Andreevae50f552013-09-13 16:17:41 +0300757 return ($input === $value) ? ' checked="checked"' : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
759}
760
761// ------------------------------------------------------------------------
762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763if ( ! function_exists('form_error'))
764{
Timothy Warren01b129a2012-04-27 11:36:50 -0400765 /**
766 * Form Error
767 *
768 * Returns the error for a specific form field. This is a helper for the
769 * form validation class.
770 *
771 * @param string
772 * @param string
773 * @param string
774 * @return string
775 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 function form_error($field = '', $prefix = '', $suffix = '')
777 {
778 if (FALSE === ($OBJ =& _get_validation_object()))
779 {
780 return '';
781 }
782
783 return $OBJ->error($field, $prefix, $suffix);
784 }
785}
786
787// ------------------------------------------------------------------------
788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789if ( ! function_exists('validation_errors'))
790{
Timothy Warren01b129a2012-04-27 11:36:50 -0400791 /**
792 * Validation Error String
793 *
794 * Returns all the errors associated with a form submission. This is a helper
795 * function for the form validation class.
796 *
797 * @param string
798 * @param string
799 * @return string
800 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 function validation_errors($prefix = '', $suffix = '')
802 {
803 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500804 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 return '';
806 }
807
808 return $OBJ->error_string($prefix, $suffix);
809 }
810}
811
812// ------------------------------------------------------------------------
813
Derek Allard2067d1a2008-11-13 22:59:24 +0000814if ( ! function_exists('_parse_form_attributes'))
815{
Timothy Warren01b129a2012-04-27 11:36:50 -0400816 /**
817 * Parse the form attributes
818 *
819 * Helper function used by some of the form helpers
820 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200821 * @param array $attributes List of attributes
822 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400823 * @return string
824 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 function _parse_form_attributes($attributes, $default)
826 {
827 if (is_array($attributes))
828 {
829 foreach ($default as $key => $val)
830 {
831 if (isset($attributes[$key]))
832 {
833 $default[$key] = $attributes[$key];
834 unset($attributes[$key]);
835 }
836 }
837
838 if (count($attributes) > 0)
839 {
840 $default = array_merge($default, $attributes);
841 }
842 }
843
844 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846 foreach ($default as $key => $val)
847 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100848 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200850 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300852 elseif ($key === 'name' && ! strlen($default['name']))
853 {
854 continue;
855 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000856
Andrey Andreev93a83c72012-03-26 21:24:02 +0300857 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 }
859
860 return $att;
861 }
862}
863
864// ------------------------------------------------------------------------
865
Derek Allard2067d1a2008-11-13 22:59:24 +0000866if ( ! function_exists('_attributes_to_string'))
867{
Timothy Warren01b129a2012-04-27 11:36:50 -0400868 /**
869 * Attributes To String
870 *
871 * Helper function used by some of the form helpers
872 *
873 * @param mixed
Timothy Warren01b129a2012-04-27 11:36:50 -0400874 * @return string
875 */
vlakoffea19bc42013-07-27 10:07:43 +0200876 function _attributes_to_string($attributes)
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 {
vlakoffbb8b0892013-07-28 22:35:04 +0200878 if (empty($attributes))
879 {
880 return '';
881 }
882
vlakoffea19bc42013-07-27 10:07:43 +0200883 if (is_object($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000884 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300885 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 }
887
vlakoffea19bc42013-07-27 10:07:43 +0200888 if (is_array($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 {
Derek Allard3241d732009-09-17 12:17:45 +0000890 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000891
Derek Allard3241d732009-09-17 12:17:45 +0000892 foreach ($attributes as $key => $val)
893 {
894 $atts .= ' '.$key.'="'.$val.'"';
895 }
896
897 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000898 }
vlakoffea19bc42013-07-27 10:07:43 +0200899
vlakofff7464752013-07-28 22:23:21 +0200900 if (is_string($attributes))
901 {
vlakoffbb8b0892013-07-28 22:35:04 +0200902 return ' '.$attributes;
vlakofff7464752013-07-28 22:23:21 +0200903 }
904
vlakoffea19bc42013-07-27 10:07:43 +0200905 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 }
907}
908
909// ------------------------------------------------------------------------
910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911if ( ! function_exists('_get_validation_object'))
912{
Timothy Warren01b129a2012-04-27 11:36:50 -0400913 /**
914 * Validation Object
915 *
916 * Determines what the form validation class was instantiated as, fetches
917 * the object and returns it.
918 *
919 * @return mixed
920 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 function &_get_validation_object()
922 {
923 $CI =& get_instance();
924
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500925 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200927
Andrey Andreev519f87a2013-07-23 17:16:10 +0300928 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000929 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500930 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
931 {
932 return $return;
933 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200934
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500935 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200937
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500938 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 }
940}
941
Derek Allard2067d1a2008-11-13 22:59:24 +0000942/* End of file form_helper.php */
Zachary Cardoza9f27a3e2013-03-23 21:59:20 -0700943/* Location: ./system/helpers/form_helper.php */