blob: 7f4276bc712774bbde6daa34263caeea2df5235b [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 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053 function form_open($action = '', $attributes = '', $hidden = array())
54 {
55 $CI =& get_instance();
56
Andrey Andreev122ca9b2013-07-26 18:16:26 +030057 if (empty($attributes))
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
Derek Allard3241d732009-09-17 12:17:45 +000059 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
Andrey Andreev122ca9b2013-07-26 18:16:26 +030061 elseif (is_array($attributes) && ! isset($attributes['method']))
62 {
63 $attributes['method'] = 'post';
64 }
65 elseif (stripos($attributes, 'method=') === FALSE)
66 {
67 $attributes .= ' method="post"';
68 }
Derek Allard2067d1a2008-11-13 22:59:24 +000069
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010070 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000071 if ($action && strpos($action, '://') === FALSE)
72 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000073 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000074 }
Andrey Andreev3c32a112012-06-16 18:05:34 +030075 elseif ( ! $action)
76 {
77 // If no action is provided then set to the current url
78 $action = $CI->config->site_url($CI->uri->uri_string());
79 }
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010080
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020081 $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n";
Barry Mienydd671972010-10-04 16:33:58 +020082
Andrey Andreev93a83c72012-03-26 21:24:02 +030083 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
Andrey Andreev122ca9b2013-07-26 18:16:26 +030084 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 -040085 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050086 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050087 }
88
Andrey Andreev93a83c72012-03-26 21:24:02 +030089 if (is_array($hidden) && count($hidden) > 0)
Greg Aker28b425a2010-09-15 11:43:23 -050090 {
Andrey Andreev3c32a112012-06-16 18:05:34 +030091 $form .= '<div style="display:none;">'.form_hidden($hidden).'</div>';
Derek Allard958543a2010-07-22 14:10:26 -040092 }
93
Derek Allard2067d1a2008-11-13 22:59:24 +000094 return $form;
95 }
96}
97
98// ------------------------------------------------------------------------
99
Derek Allard2067d1a2008-11-13 22:59:24 +0000100if ( ! function_exists('form_open_multipart'))
101{
Timothy Warren01b129a2012-04-27 11:36:50 -0400102 /**
103 * Form Declaration - Multipart type
104 *
105 * Creates the opening portion of the form, but with "multipart/form-data".
106 *
107 * @param string the URI segments of the form destination
108 * @param array a key/value pair of attributes
109 * @param array a key/value pair hidden data
110 * @return string
111 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500112 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000114 if (is_string($attributes))
115 {
116 $attributes .= ' enctype="multipart/form-data"';
117 }
118 else
119 {
120 $attributes['enctype'] = 'multipart/form-data';
121 }
122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 return form_open($action, $attributes, $hidden);
124 }
125}
126
127// ------------------------------------------------------------------------
128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129if ( ! function_exists('form_hidden'))
130{
Timothy Warren01b129a2012-04-27 11:36:50 -0400131 /**
132 * Hidden Input Field
133 *
134 * Generates hidden fields. You can pass a simple key/value string or
135 * an associative array with multiple values.
136 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200137 * @param mixed $name Field name
138 * @param string $value Field value
139 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400140 * @return string
141 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000142 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000144 static $form;
145
146 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000148 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
Robin Sowell57fe4102009-03-26 18:58:46 +0000151 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000153 foreach ($name as $key => $val)
154 {
155 form_hidden($key, $val, TRUE);
156 }
157 return $form;
158 }
159
160 if ( ! is_array($value))
161 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200162 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000163 }
164 else
165 {
166 foreach ($value as $k => $v)
167 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300168 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000169 form_hidden($name.'['.$k.']', $v, TRUE);
170 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172
173 return $form;
174 }
175}
176
177// ------------------------------------------------------------------------
178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179if ( ! function_exists('form_input'))
180{
Timothy Warren01b129a2012-04-27 11:36:50 -0400181 /**
182 * Text Input Field
183 *
184 * @param mixed
185 * @param string
186 * @param string
187 * @return string
188 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 function form_input($data = '', $value = '', $extra = '')
190 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300191 $defaults = array('type' => 'text', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200193 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
195}
196
197// ------------------------------------------------------------------------
198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199if ( ! function_exists('form_password'))
200{
Timothy Warren01b129a2012-04-27 11:36:50 -0400201 /**
202 * Password Field
203 *
204 * Identical to the input function but adds the "password" type
205 *
206 * @param mixed
207 * @param string
208 * @param string
209 * @return string
210 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 function form_password($data = '', $value = '', $extra = '')
212 {
213 if ( ! is_array($data))
214 {
215 $data = array('name' => $data);
216 }
217
218 $data['type'] = 'password';
219 return form_input($data, $value, $extra);
220 }
221}
222
223// ------------------------------------------------------------------------
224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225if ( ! function_exists('form_upload'))
226{
Timothy Warren01b129a2012-04-27 11:36:50 -0400227 /**
228 * Upload Field
229 *
230 * Identical to the input function but adds the "file" type
231 *
232 * @param mixed
233 * @param string
234 * @param string
235 * @return string
236 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 function form_upload($data = '', $value = '', $extra = '')
238 {
Bo-Yi Wu06ddcf02013-02-18 08:52:05 +0800239 $defaults = array('type' => 'file', 'name' => '');
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200240 is_array($data) OR $data = array('name' => $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 $data['type'] = 'file';
Andrey Andreev99ba3a22013-02-15 22:42:22 +0200242 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244}
245
246// ------------------------------------------------------------------------
247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248if ( ! function_exists('form_textarea'))
249{
Timothy Warren01b129a2012-04-27 11:36:50 -0400250 /**
251 * Textarea field
252 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200253 * @param mixed $data
254 * @param string $value
255 * @param string $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400256 * @return string
257 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 function form_textarea($data = '', $value = '', $extra = '')
259 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300260 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
262 if ( ! is_array($data) OR ! isset($data['value']))
263 {
264 $val = $value;
265 }
266 else
267 {
Barry Mienydd671972010-10-04 16:33:58 +0200268 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 unset($data['value']); // textareas don't use the value attribute
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200272 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274}
275
276// ------------------------------------------------------------------------
277
Derek Jones68788d52010-03-05 10:11:31 -0600278if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000279{
Timothy Warren01b129a2012-04-27 11:36:50 -0400280 /**
281 * Multi-select menu
282 *
283 * @param string
284 * @param array
285 * @param mixed
286 * @param string
287 * @return string
288 */
Derek Jones26399292009-04-08 16:14:17 +0000289 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
290 {
291 if ( ! strpos($extra, 'multiple'))
292 {
293 $extra .= ' multiple="multiple"';
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Jones26399292009-04-08 16:14:17 +0000296 return form_dropdown($name, $options, $selected, $extra);
297 }
298}
299
300// --------------------------------------------------------------------
301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302if ( ! function_exists('form_dropdown'))
303{
Timothy Warren01b129a2012-04-27 11:36:50 -0400304 /**
305 * Drop-down Menu
306 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200307 * @param mixed $name
308 * @param mixed $options
309 * @param mixed $selected
310 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400311 * @return string
312 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
314 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300315 // If name is really an array then we'll call the function again using the array
316 if (is_array($name) && isset($name['name']))
317 {
318 isset($name['options']) OR $name['options'] = array();
319 isset($name['selected']) OR $name['selected'] = array();
320 isset($name['extra']) OR $name['extra'] = array();
321
322 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
323 }
324
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300325 is_array($selected) OR $selected = array($selected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000326
327 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200328 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200330 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
332
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200333 if ($extra != '')
334 {
335 $extra = ' '.$extra;
336 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000337
338 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
339
340 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 foreach ($options as $key => $val)
343 {
344 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300346 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000347 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300348 if (empty($val))
349 {
350 continue;
351 }
352
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200353 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000354
Derek Allard78a5fc92009-02-05 16:34:35 +0000355 foreach ($val as $optgroup_key => $optgroup_val)
356 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300357 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200358 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300359 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000360 }
361
Andrey Andreev93a83c72012-03-26 21:24:02 +0300362 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000363 }
364 else
365 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200366 $form .= '<option value="'.form_prep($key).'"'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300367 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
368 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000369 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
Andrey Andreev93a83c72012-03-26 21:24:02 +0300372 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374}
375
376// ------------------------------------------------------------------------
377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378if ( ! function_exists('form_checkbox'))
379{
Timothy Warren01b129a2012-04-27 11:36:50 -0400380 /**
381 * Checkbox Field
382 *
383 * @param mixed
384 * @param string
385 * @param bool
386 * @param string
387 * @return string
388 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
390 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300391 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000392
Andrey Andreev93a83c72012-03-26 21:24:02 +0300393 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 $checked = $data['checked'];
396
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200397 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 unset($data['checked']);
400 }
401 else
402 {
403 $data['checked'] = 'checked';
404 }
405 }
406
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200407 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 $defaults['checked'] = 'checked';
410 }
411 else
412 {
413 unset($defaults['checked']);
414 }
415
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200416 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418}
419
420// ------------------------------------------------------------------------
421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422if ( ! function_exists('form_radio'))
423{
Timothy Warren01b129a2012-04-27 11:36:50 -0400424 /**
425 * Radio Button
426 *
427 * @param mixed
428 * @param string
429 * @param bool
430 * @param string
431 * @return string
432 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
434 {
435 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200436 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 $data = array('name' => $data);
438 }
439
440 $data['type'] = 'radio';
441 return form_checkbox($data, $value, $checked, $extra);
442 }
443}
444
445// ------------------------------------------------------------------------
446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200448{
Timothy Warren01b129a2012-04-27 11:36:50 -0400449 /**
450 * Submit Button
451 *
452 * @param mixed
453 * @param string
454 * @param string
455 * @return string
456 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 function form_submit($data = '', $value = '', $extra = '')
458 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300459 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200460 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462}
463
464// ------------------------------------------------------------------------
465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466if ( ! function_exists('form_reset'))
467{
Timothy Warren01b129a2012-04-27 11:36:50 -0400468 /**
469 * Reset Button
470 *
471 * @param mixed
472 * @param string
473 * @param string
474 * @return string
475 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 function form_reset($data = '', $value = '', $extra = '')
477 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300478 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200479 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
481}
482
483// ------------------------------------------------------------------------
484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485if ( ! function_exists('form_button'))
486{
Timothy Warren01b129a2012-04-27 11:36:50 -0400487 /**
488 * Form Button
489 *
490 * @param mixed
491 * @param string
492 * @param string
493 * @return string
494 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 function form_button($data = '', $content = '', $extra = '')
496 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300497 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
498 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 $content = $data['content'];
501 unset($data['content']); // content is not an attribute
502 }
503
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200504 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
506}
507
508// ------------------------------------------------------------------------
509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510if ( ! function_exists('form_label'))
511{
Timothy Warren01b129a2012-04-27 11:36:50 -0400512 /**
513 * Form Label Tag
514 *
515 * @param string The text to appear onscreen
516 * @param string The id the label applies to
517 * @param string Additional attributes
518 * @return string
519 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 function form_label($label_text = '', $id = '', $attributes = array())
521 {
522
523 $label = '<label';
524
Alex Bilbie773ccc32012-06-02 11:11:08 +0100525 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300527 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529
Andrey Andreev93a83c72012-03-26 21:24:02 +0300530 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 {
532 foreach ($attributes as $key => $val)
533 {
534 $label .= ' '.$key.'="'.$val.'"';
535 }
536 }
537
Andrey Andreev93a83c72012-03-26 21:24:02 +0300538 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540}
541
542// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544if ( ! function_exists('form_fieldset'))
545{
Timothy Warren01b129a2012-04-27 11:36:50 -0400546 /**
547 * Fieldset Tag
548 *
549 * Used to produce <fieldset><legend>text</legend>. To close fieldset
550 * use form_fieldset_close()
551 *
552 * @param string The legend text
553 * @param string Additional attributes
554 * @return string
555 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 function form_fieldset($legend_text = '', $attributes = array())
557 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200558 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100559 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300561 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
563
564 return $fieldset;
565 }
566}
567
568// ------------------------------------------------------------------------
569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570if ( ! function_exists('form_fieldset_close'))
571{
Timothy Warren01b129a2012-04-27 11:36:50 -0400572 /**
573 * Fieldset Close Tag
574 *
575 * @param string
576 * @return string
577 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 function form_fieldset_close($extra = '')
579 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300580 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
582}
583
584// ------------------------------------------------------------------------
585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586if ( ! function_exists('form_close'))
587{
Timothy Warren01b129a2012-04-27 11:36:50 -0400588 /**
589 * Form Close Tag
590 *
591 * @param string
592 * @return string
593 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 function form_close($extra = '')
595 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300596 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
598}
599
600// ------------------------------------------------------------------------
601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602if ( ! function_exists('form_prep'))
603{
Timothy Warren01b129a2012-04-27 11:36:50 -0400604 /**
605 * Form Prep
606 *
607 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
608 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200609 * @param string|string[] $str Value to escape
610 * @param bool $is_textarea Whether we're escaping for a textarea element
611 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400612 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200613 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200615 if (is_array($str))
616 {
617 foreach (array_keys($str) as $key)
618 {
619 $str[$key] = form_prep($str[$key], $is_textarea);
620 }
621
622 return $str;
623 }
624
625 if ($is_textarea === TRUE)
626 {
627 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
628 }
629
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200630 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 }
632}
633
634// ------------------------------------------------------------------------
635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636if ( ! function_exists('set_value'))
637{
Timothy Warren01b129a2012-04-27 11:36:50 -0400638 /**
639 * Form Value
640 *
641 * Grabs a value from the POST array for the specified field so you can
642 * re-populate an input field or textarea. If Form Validation
643 * is active it retrieves the info from the validation class
644 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200645 * @param string $field Field name
646 * @param string $default Default value
647 * @param bool $is_textarea Whether the field is a textarea element
648 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400649 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200650 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
nisheeth-barthwal77236e02013-03-25 23:42:36 +0530652 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +0000653
nisheeth-barthwal47ea5a82013-03-26 18:57:28 +0530654 $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
655 ? $CI->form_validation->set_value($field, $default)
656 : $CI->input->post($field, FALSE);
657
658 return form_prep($value === NULL ? $default : $value, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660}
661
662// ------------------------------------------------------------------------
663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664if ( ! function_exists('set_select'))
665{
Timothy Warren01b129a2012-04-27 11:36:50 -0400666 /**
667 * Set Select
668 *
669 * Let's you set the selected value of a <select> menu via data in the POST array.
670 * If Form Validation is active it retrieves the info from the validation class
671 *
672 * @param string
673 * @param string
674 * @param bool
675 * @return string
676 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 function set_select($field = '', $value = '', $default = FALSE)
678 {
679 $OBJ =& _get_validation_object();
680
681 if ($OBJ === FALSE)
682 {
683 if ( ! isset($_POST[$field]))
684 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100685 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 return ' selected="selected"';
688 }
689 return '';
690 }
691
692 $field = $_POST[$field];
693
694 if (is_array($field))
695 {
696 if ( ! in_array($value, $field))
697 {
698 return '';
699 }
700 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200701 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300703 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
705
706 return ' selected="selected"';
707 }
708
709 return $OBJ->set_select($field, $value, $default);
710 }
711}
712
713// ------------------------------------------------------------------------
714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715if ( ! function_exists('set_checkbox'))
716{
Timothy Warren01b129a2012-04-27 11:36:50 -0400717 /**
718 * Set Checkbox
719 *
720 * Let's you set the selected value of a checkbox via the value in the POST array.
721 * If Form Validation is active it retrieves the info from the validation class
722 *
723 * @param string
724 * @param string
725 * @param bool
726 * @return string
727 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 function set_checkbox($field = '', $value = '', $default = FALSE)
729 {
730 $OBJ =& _get_validation_object();
731
732 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200733 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 if ( ! isset($_POST[$field]))
735 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100736 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 {
738 return ' checked="checked"';
739 }
740 return '';
741 }
742
743 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 if (is_array($field))
746 {
747 if ( ! in_array($value, $field))
748 {
749 return '';
750 }
751 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200752 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300754 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
756
757 return ' checked="checked"';
758 }
759
760 return $OBJ->set_checkbox($field, $value, $default);
761 }
762}
763
764// ------------------------------------------------------------------------
765
Derek Allard2067d1a2008-11-13 22:59:24 +0000766if ( ! function_exists('set_radio'))
767{
Timothy Warren01b129a2012-04-27 11:36:50 -0400768 /**
769 * Set Radio
770 *
771 * Let's you set the selected value of a radio field via info in the POST array.
772 * If Form Validation is active it retrieves the info from the validation class
773 *
774 * @param string
775 * @param string
776 * @param bool
777 * @return string
778 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 function set_radio($field = '', $value = '', $default = FALSE)
780 {
781 $OBJ =& _get_validation_object();
782
783 if ($OBJ === FALSE)
784 {
785 if ( ! isset($_POST[$field]))
786 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100787 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
789 return ' checked="checked"';
790 }
791 return '';
792 }
793
794 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 if (is_array($field))
797 {
798 if ( ! in_array($value, $field))
799 {
800 return '';
801 }
802 }
803 else
804 {
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200805 if (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 {
807 return '';
808 }
809 }
810
811 return ' checked="checked"';
812 }
813
814 return $OBJ->set_radio($field, $value, $default);
815 }
816}
817
818// ------------------------------------------------------------------------
819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820if ( ! function_exists('form_error'))
821{
Timothy Warren01b129a2012-04-27 11:36:50 -0400822 /**
823 * Form Error
824 *
825 * Returns the error for a specific form field. This is a helper for the
826 * form validation class.
827 *
828 * @param string
829 * @param string
830 * @param string
831 * @return string
832 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000833 function form_error($field = '', $prefix = '', $suffix = '')
834 {
835 if (FALSE === ($OBJ =& _get_validation_object()))
836 {
837 return '';
838 }
839
840 return $OBJ->error($field, $prefix, $suffix);
841 }
842}
843
844// ------------------------------------------------------------------------
845
Derek Allard2067d1a2008-11-13 22:59:24 +0000846if ( ! function_exists('validation_errors'))
847{
Timothy Warren01b129a2012-04-27 11:36:50 -0400848 /**
849 * Validation Error String
850 *
851 * Returns all the errors associated with a form submission. This is a helper
852 * function for the form validation class.
853 *
854 * @param string
855 * @param string
856 * @return string
857 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 function validation_errors($prefix = '', $suffix = '')
859 {
860 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500861 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 return '';
863 }
864
865 return $OBJ->error_string($prefix, $suffix);
866 }
867}
868
869// ------------------------------------------------------------------------
870
Derek Allard2067d1a2008-11-13 22:59:24 +0000871if ( ! function_exists('_parse_form_attributes'))
872{
Timothy Warren01b129a2012-04-27 11:36:50 -0400873 /**
874 * Parse the form attributes
875 *
876 * Helper function used by some of the form helpers
877 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200878 * @param array $attributes List of attributes
879 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400880 * @return string
881 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000882 function _parse_form_attributes($attributes, $default)
883 {
884 if (is_array($attributes))
885 {
886 foreach ($default as $key => $val)
887 {
888 if (isset($attributes[$key]))
889 {
890 $default[$key] = $attributes[$key];
891 unset($attributes[$key]);
892 }
893 }
894
895 if (count($attributes) > 0)
896 {
897 $default = array_merge($default, $attributes);
898 }
899 }
900
901 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 foreach ($default as $key => $val)
904 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100905 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000906 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200907 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300909 elseif ($key === 'name' && ! strlen($default['name']))
910 {
911 continue;
912 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000913
Andrey Andreev93a83c72012-03-26 21:24:02 +0300914 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 }
916
917 return $att;
918 }
919}
920
921// ------------------------------------------------------------------------
922
Derek Allard2067d1a2008-11-13 22:59:24 +0000923if ( ! function_exists('_attributes_to_string'))
924{
Timothy Warren01b129a2012-04-27 11:36:50 -0400925 /**
926 * Attributes To String
927 *
928 * Helper function used by some of the form helpers
929 *
930 * @param mixed
931 * @param bool
932 * @return string
933 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 function _attributes_to_string($attributes, $formtag = FALSE)
935 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300936 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100938 if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
940 $attributes .= ' method="post"';
941 }
942
Alex Bilbie773ccc32012-06-02 11:11:08 +0100943 if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000944 {
945 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
946 }
947
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200948 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 }
Barry Mienydd671972010-10-04 16:33:58 +0200950
Andrey Andreev93a83c72012-03-26 21:24:02 +0300951 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000952 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300953 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 }
955
Andrey Andreev93a83c72012-03-26 21:24:02 +0300956 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
Derek Allard3241d732009-09-17 12:17:45 +0000958 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000959
Andrey Andreev93a83c72012-03-26 21:24:02 +0300960 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000961 {
962 $atts .= ' method="post"';
963 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000964
Andrey Andreev93a83c72012-03-26 21:24:02 +0300965 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000966 {
967 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
968 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000969
Derek Allard3241d732009-09-17 12:17:45 +0000970 foreach ($attributes as $key => $val)
971 {
972 $atts .= ' '.$key.'="'.$val.'"';
973 }
974
975 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 }
977 }
978}
979
980// ------------------------------------------------------------------------
981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982if ( ! function_exists('_get_validation_object'))
983{
Timothy Warren01b129a2012-04-27 11:36:50 -0400984 /**
985 * Validation Object
986 *
987 * Determines what the form validation class was instantiated as, fetches
988 * the object and returns it.
989 *
990 * @return mixed
991 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000992 function &_get_validation_object()
993 {
994 $CI =& get_instance();
995
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500996 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200998
Andrey Andreev519f87a2013-07-23 17:16:10 +0300999 if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001001 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1002 {
1003 return $return;
1004 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001005
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001006 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001008
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001009 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
1011}
1012
Derek Allard2067d1a2008-11-13 22:59:24 +00001013/* End of file form_helper.php */
Zachary Cardoza9f27a3e2013-03-23 21:59:20 -07001014/* Location: ./system/helpers/form_helper.php */