blob: 40c7d8823ef10cc07cb2c40cf8daa525bbfb4683 [file] [log] [blame]
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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
Derek Allard2067d1a2008-11-13 22:59:24 +000025 */
26
Derek Allard2067d1a2008-11-13 22:59:24 +000027/**
28 * CodeIgniter Form Helpers
29 *
30 * @package CodeIgniter
31 * @subpackage Helpers
32 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050033 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
35 */
36
37// ------------------------------------------------------------------------
38
Derek Allard2067d1a2008-11-13 22:59:24 +000039if ( ! function_exists('form_open'))
40{
Timothy Warren01b129a2012-04-27 11:36:50 -040041 /**
42 * Form Declaration
43 *
44 * Creates the opening portion of the form.
45 *
46 * @param string the URI segments of the form destination
47 * @param array a key/value pair of attributes
48 * @param array a key/value pair hidden data
49 * @return string
50 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051 function form_open($action = '', $attributes = '', $hidden = array())
52 {
53 $CI =& get_instance();
54
Alex Bilbie773ccc32012-06-02 11:11:08 +010055 if ($attributes === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
Derek Allard3241d732009-09-17 12:17:45 +000057 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000058 }
59
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010060 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000061 if ($action && strpos($action, '://') === FALSE)
62 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000063 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000064 }
Derek Allard2067d1a2008-11-13 22:59:24 +000065
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010066 // If no action is provided then set to the current url
67 $action OR $action = $CI->config->site_url($CI->uri->uri_string());
68
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020069 $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n";
Barry Mienydd671972010-10-04 16:33:58 +020070
Andrey Andreev93a83c72012-03-26 21:24:02 +030071 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
72 if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"')))
Derek Allard958543a2010-07-22 14:10:26 -040073 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050074 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050075 }
76
Andrey Andreev93a83c72012-03-26 21:24:02 +030077 if (is_array($hidden) && count($hidden) > 0)
Greg Aker28b425a2010-09-15 11:43:23 -050078 {
Andrey Andreev93a83c72012-03-26 21:24:02 +030079 $form .= sprintf('<div style="display:none;">%s</div>', form_hidden($hidden));
Derek Allard958543a2010-07-22 14:10:26 -040080 }
81
Derek Allard2067d1a2008-11-13 22:59:24 +000082 return $form;
83 }
84}
85
86// ------------------------------------------------------------------------
87
Derek Allard2067d1a2008-11-13 22:59:24 +000088if ( ! function_exists('form_open_multipart'))
89{
Timothy Warren01b129a2012-04-27 11:36:50 -040090 /**
91 * Form Declaration - Multipart type
92 *
93 * Creates the opening portion of the form, but with "multipart/form-data".
94 *
95 * @param string the URI segments of the form destination
96 * @param array a key/value pair of attributes
97 * @param array a key/value pair hidden data
98 * @return string
99 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500100 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000102 if (is_string($attributes))
103 {
104 $attributes .= ' enctype="multipart/form-data"';
105 }
106 else
107 {
108 $attributes['enctype'] = 'multipart/form-data';
109 }
110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 return form_open($action, $attributes, $hidden);
112 }
113}
114
115// ------------------------------------------------------------------------
116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117if ( ! function_exists('form_hidden'))
118{
Timothy Warren01b129a2012-04-27 11:36:50 -0400119 /**
120 * Hidden Input Field
121 *
122 * Generates hidden fields. You can pass a simple key/value string or
123 * an associative array with multiple values.
124 *
125 * @param mixed
126 * @param string
127 * @param bool
128 * @return string
129 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000130 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000132 static $form;
133
134 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000136 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138
Robin Sowell57fe4102009-03-26 18:58:46 +0000139 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000141 foreach ($name as $key => $val)
142 {
143 form_hidden($key, $val, TRUE);
144 }
145 return $form;
146 }
147
148 if ( ! is_array($value))
149 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200150 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000151 }
152 else
153 {
154 foreach ($value as $k => $v)
155 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300156 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000157 form_hidden($name.'['.$k.']', $v, TRUE);
158 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
160
161 return $form;
162 }
163}
164
165// ------------------------------------------------------------------------
166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167if ( ! function_exists('form_input'))
168{
Timothy Warren01b129a2012-04-27 11:36:50 -0400169 /**
170 * Text Input Field
171 *
172 * @param mixed
173 * @param string
174 * @param string
175 * @return string
176 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 function form_input($data = '', $value = '', $extra = '')
178 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300179 $defaults = array('type' => 'text', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200181 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183}
184
185// ------------------------------------------------------------------------
186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187if ( ! function_exists('form_password'))
188{
Timothy Warren01b129a2012-04-27 11:36:50 -0400189 /**
190 * Password Field
191 *
192 * Identical to the input function but adds the "password" type
193 *
194 * @param mixed
195 * @param string
196 * @param string
197 * @return string
198 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 function form_password($data = '', $value = '', $extra = '')
200 {
201 if ( ! is_array($data))
202 {
203 $data = array('name' => $data);
204 }
205
206 $data['type'] = 'password';
207 return form_input($data, $value, $extra);
208 }
209}
210
211// ------------------------------------------------------------------------
212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213if ( ! function_exists('form_upload'))
214{
Timothy Warren01b129a2012-04-27 11:36:50 -0400215 /**
216 * Upload Field
217 *
218 * Identical to the input function but adds the "file" type
219 *
220 * @param mixed
221 * @param string
222 * @param string
223 * @return string
224 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 function form_upload($data = '', $value = '', $extra = '')
226 {
227 if ( ! is_array($data))
228 {
229 $data = array('name' => $data);
230 }
231
232 $data['type'] = 'file';
233 return form_input($data, $value, $extra);
234 }
235}
236
237// ------------------------------------------------------------------------
238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239if ( ! function_exists('form_textarea'))
240{
Timothy Warren01b129a2012-04-27 11:36:50 -0400241 /**
242 * Textarea field
243 *
244 * @param mixed
245 * @param string
246 * @param string
247 * @return string
248 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 function form_textarea($data = '', $value = '', $extra = '')
250 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300251 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000252
253 if ( ! is_array($data) OR ! isset($data['value']))
254 {
255 $val = $value;
256 }
257 else
258 {
Barry Mienydd671972010-10-04 16:33:58 +0200259 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 unset($data['value']); // textareas don't use the value attribute
261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Andrey Andreev93a83c72012-03-26 21:24:02 +0300263 $name = is_array($data) ? $data['name'] : $data;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200264 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, $name)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
266}
267
268// ------------------------------------------------------------------------
269
Derek Jones68788d52010-03-05 10:11:31 -0600270if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000271{
Timothy Warren01b129a2012-04-27 11:36:50 -0400272 /**
273 * Multi-select menu
274 *
275 * @param string
276 * @param array
277 * @param mixed
278 * @param string
279 * @return string
280 */
Derek Jones26399292009-04-08 16:14:17 +0000281 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
282 {
283 if ( ! strpos($extra, 'multiple'))
284 {
285 $extra .= ' multiple="multiple"';
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Jones26399292009-04-08 16:14:17 +0000288 return form_dropdown($name, $options, $selected, $extra);
289 }
290}
291
292// --------------------------------------------------------------------
293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294if ( ! function_exists('form_dropdown'))
295{
Timothy Warren01b129a2012-04-27 11:36:50 -0400296 /**
297 * Drop-down Menu
298 *
299 * @param string
300 * @param array
301 * @param string
302 * @param string
303 * @return string
304 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
306 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300307 // If name is really an array then we'll call the function again using the array
308 if (is_array($name) && isset($name['name']))
309 {
310 isset($name['options']) OR $name['options'] = array();
311 isset($name['selected']) OR $name['selected'] = array();
312 isset($name['extra']) OR $name['extra'] = array();
313
314 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
315 }
316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 if ( ! is_array($selected))
318 {
319 $selected = array($selected);
320 }
321
322 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200323 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200325 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200328 if ($extra != '')
329 {
330 $extra = ' '.$extra;
331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
333 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
334
335 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 foreach ($options as $key => $val)
338 {
339 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340
Derek Jones68788d52010-03-05 10:11:31 -0600341 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000342 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200343 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000344
Derek Allard78a5fc92009-02-05 16:34:35 +0000345 foreach ($val as $optgroup_key => $optgroup_val)
346 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300347 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000348 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
349 }
350
Andrey Andreev93a83c72012-03-26 21:24:02 +0300351 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000352 }
353 else
354 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200355 $form .= '<option value="'.$key.'"'.(in_array($key, $selected) ? ' selected="selected"' : '').'>'.(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000356 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358
Andrey Andreev93a83c72012-03-26 21:24:02 +0300359 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361}
362
363// ------------------------------------------------------------------------
364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365if ( ! function_exists('form_checkbox'))
366{
Timothy Warren01b129a2012-04-27 11:36:50 -0400367 /**
368 * Checkbox Field
369 *
370 * @param mixed
371 * @param string
372 * @param bool
373 * @param string
374 * @return string
375 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
377 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300378 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000379
Andrey Andreev93a83c72012-03-26 21:24:02 +0300380 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 $checked = $data['checked'];
383
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200384 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 unset($data['checked']);
387 }
388 else
389 {
390 $data['checked'] = 'checked';
391 }
392 }
393
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200394 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 $defaults['checked'] = 'checked';
397 }
398 else
399 {
400 unset($defaults['checked']);
401 }
402
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200403 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405}
406
407// ------------------------------------------------------------------------
408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409if ( ! function_exists('form_radio'))
410{
Timothy Warren01b129a2012-04-27 11:36:50 -0400411 /**
412 * Radio Button
413 *
414 * @param mixed
415 * @param string
416 * @param bool
417 * @param string
418 * @return string
419 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
421 {
422 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200423 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 $data = array('name' => $data);
425 }
426
427 $data['type'] = 'radio';
428 return form_checkbox($data, $value, $checked, $extra);
429 }
430}
431
432// ------------------------------------------------------------------------
433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200435{
Timothy Warren01b129a2012-04-27 11:36:50 -0400436 /**
437 * Submit Button
438 *
439 * @param mixed
440 * @param string
441 * @param string
442 * @return string
443 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 function form_submit($data = '', $value = '', $extra = '')
445 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300446 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200447 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449}
450
451// ------------------------------------------------------------------------
452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453if ( ! function_exists('form_reset'))
454{
Timothy Warren01b129a2012-04-27 11:36:50 -0400455 /**
456 * Reset Button
457 *
458 * @param mixed
459 * @param string
460 * @param string
461 * @return string
462 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 function form_reset($data = '', $value = '', $extra = '')
464 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300465 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200466 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468}
469
470// ------------------------------------------------------------------------
471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472if ( ! function_exists('form_button'))
473{
Timothy Warren01b129a2012-04-27 11:36:50 -0400474 /**
475 * Form Button
476 *
477 * @param mixed
478 * @param string
479 * @param string
480 * @return string
481 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 function form_button($data = '', $content = '', $extra = '')
483 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300484 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
485 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 $content = $data['content'];
488 unset($data['content']); // content is not an attribute
489 }
490
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200491 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493}
494
495// ------------------------------------------------------------------------
496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497if ( ! function_exists('form_label'))
498{
Timothy Warren01b129a2012-04-27 11:36:50 -0400499 /**
500 * Form Label Tag
501 *
502 * @param string The text to appear onscreen
503 * @param string The id the label applies to
504 * @param string Additional attributes
505 * @return string
506 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 function form_label($label_text = '', $id = '', $attributes = array())
508 {
509
510 $label = '<label';
511
Alex Bilbie773ccc32012-06-02 11:11:08 +0100512 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300514 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
516
Andrey Andreev93a83c72012-03-26 21:24:02 +0300517 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 foreach ($attributes as $key => $val)
520 {
521 $label .= ' '.$key.'="'.$val.'"';
522 }
523 }
524
Andrey Andreev93a83c72012-03-26 21:24:02 +0300525 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
527}
528
529// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531if ( ! function_exists('form_fieldset'))
532{
Timothy Warren01b129a2012-04-27 11:36:50 -0400533 /**
534 * Fieldset Tag
535 *
536 * Used to produce <fieldset><legend>text</legend>. To close fieldset
537 * use form_fieldset_close()
538 *
539 * @param string The legend text
540 * @param string Additional attributes
541 * @return string
542 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 function form_fieldset($legend_text = '', $attributes = array())
544 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200545 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100546 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300548 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 }
550
551 return $fieldset;
552 }
553}
554
555// ------------------------------------------------------------------------
556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557if ( ! function_exists('form_fieldset_close'))
558{
Timothy Warren01b129a2012-04-27 11:36:50 -0400559 /**
560 * Fieldset Close Tag
561 *
562 * @param string
563 * @return string
564 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 function form_fieldset_close($extra = '')
566 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300567 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
569}
570
571// ------------------------------------------------------------------------
572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573if ( ! function_exists('form_close'))
574{
Timothy Warren01b129a2012-04-27 11:36:50 -0400575 /**
576 * Form Close Tag
577 *
578 * @param string
579 * @return string
580 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 function form_close($extra = '')
582 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300583 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
585}
586
587// ------------------------------------------------------------------------
588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589if ( ! function_exists('form_prep'))
590{
Timothy Warren01b129a2012-04-27 11:36:50 -0400591 /**
592 * Form Prep
593 *
594 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
595 *
596 * @param string
597 * @param string
598 * @return string
599 */
Derek Jones01a9b102009-07-17 18:30:36 +0000600 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Derek Jones01a9b102009-07-17 18:30:36 +0000602 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // if the field name is an array we do this recursively
605 if (is_array($str))
606 {
607 foreach ($str as $key => $val)
608 {
609 $str[$key] = form_prep($val);
610 }
611
612 return $str;
613 }
614
615 if ($str === '')
616 {
617 return '';
618 }
619
Derek Jones3eb9fac2009-07-17 20:25:13 +0000620 // we've already prepped a field with this name
621 // @todo need to figure out a way to namespace this so
622 // that we know the *exact* field and not just one with
623 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000624 if (isset($prepped_fields[$field_name]))
625 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000626 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000627 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000628
Alex Bilbie773ccc32012-06-02 11:11:08 +0100629 if ($field_name !== '')
Derek Jones01a9b102009-07-17 18:30:36 +0000630 {
Derek Jones68788d52010-03-05 10:11:31 -0600631 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200634 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
636}
637
638// ------------------------------------------------------------------------
639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640if ( ! function_exists('set_value'))
641{
Timothy Warren01b129a2012-04-27 11:36:50 -0400642 /**
643 * Form Value
644 *
645 * Grabs a value from the POST array for the specified field so you can
646 * re-populate an input field or textarea. If Form Validation
647 * is active it retrieves the info from the validation class
648 *
649 * @param string
650 * @param string
651 * @return mixed
652 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 function set_value($field = '', $default = '')
654 {
655 if (FALSE === ($OBJ =& _get_validation_object()))
656 {
657 if ( ! isset($_POST[$field]))
658 {
659 return $default;
660 }
661
Derek Jones01a9b102009-07-17 18:30:36 +0000662 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664
Derek Jones01a9b102009-07-17 18:30:36 +0000665 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 }
667}
668
669// ------------------------------------------------------------------------
670
Derek Allard2067d1a2008-11-13 22:59:24 +0000671if ( ! function_exists('set_select'))
672{
Timothy Warren01b129a2012-04-27 11:36:50 -0400673 /**
674 * Set Select
675 *
676 * Let's you set the selected value of a <select> menu via data in the POST array.
677 * If Form Validation is active it retrieves the info from the validation class
678 *
679 * @param string
680 * @param string
681 * @param bool
682 * @return string
683 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 function set_select($field = '', $value = '', $default = FALSE)
685 {
686 $OBJ =& _get_validation_object();
687
688 if ($OBJ === FALSE)
689 {
690 if ( ! isset($_POST[$field]))
691 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100692 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
694 return ' selected="selected"';
695 }
696 return '';
697 }
698
699 $field = $_POST[$field];
700
701 if (is_array($field))
702 {
703 if ( ! in_array($value, $field))
704 {
705 return '';
706 }
707 }
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200708 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300710 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 }
712
713 return ' selected="selected"';
714 }
715
716 return $OBJ->set_select($field, $value, $default);
717 }
718}
719
720// ------------------------------------------------------------------------
721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722if ( ! function_exists('set_checkbox'))
723{
Timothy Warren01b129a2012-04-27 11:36:50 -0400724 /**
725 * Set Checkbox
726 *
727 * Let's you set the selected value of a checkbox via the value in the POST array.
728 * If Form Validation is active it retrieves the info from the validation class
729 *
730 * @param string
731 * @param string
732 * @param bool
733 * @return string
734 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 function set_checkbox($field = '', $value = '', $default = FALSE)
736 {
737 $OBJ =& _get_validation_object();
738
739 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200740 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 if ( ! isset($_POST[$field]))
742 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100743 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 {
745 return ' checked="checked"';
746 }
747 return '';
748 }
749
750 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200751
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 if (is_array($field))
753 {
754 if ( ! in_array($value, $field))
755 {
756 return '';
757 }
758 }
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200759 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300761 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763
764 return ' checked="checked"';
765 }
766
767 return $OBJ->set_checkbox($field, $value, $default);
768 }
769}
770
771// ------------------------------------------------------------------------
772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773if ( ! function_exists('set_radio'))
774{
Timothy Warren01b129a2012-04-27 11:36:50 -0400775 /**
776 * Set Radio
777 *
778 * Let's you set the selected value of a radio field via info in the POST array.
779 * If Form Validation is active it retrieves the info from the validation class
780 *
781 * @param string
782 * @param string
783 * @param bool
784 * @return string
785 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 function set_radio($field = '', $value = '', $default = FALSE)
787 {
788 $OBJ =& _get_validation_object();
789
790 if ($OBJ === FALSE)
791 {
792 if ( ! isset($_POST[$field]))
793 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100794 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
796 return ' checked="checked"';
797 }
798 return '';
799 }
800
801 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 if (is_array($field))
804 {
805 if ( ! in_array($value, $field))
806 {
807 return '';
808 }
809 }
810 else
811 {
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200812 if (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 {
814 return '';
815 }
816 }
817
818 return ' checked="checked"';
819 }
820
821 return $OBJ->set_radio($field, $value, $default);
822 }
823}
824
825// ------------------------------------------------------------------------
826
Timothy Warren01b129a2012-04-27 11:36:50 -0400827
Derek Allard2067d1a2008-11-13 22:59:24 +0000828if ( ! function_exists('form_error'))
829{
Timothy Warren01b129a2012-04-27 11:36:50 -0400830 /**
831 * Form Error
832 *
833 * Returns the error for a specific form field. This is a helper for the
834 * form validation class.
835 *
836 * @param string
837 * @param string
838 * @param string
839 * @return string
840 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 function form_error($field = '', $prefix = '', $suffix = '')
842 {
843 if (FALSE === ($OBJ =& _get_validation_object()))
844 {
845 return '';
846 }
847
848 return $OBJ->error($field, $prefix, $suffix);
849 }
850}
851
852// ------------------------------------------------------------------------
853
Derek Allard2067d1a2008-11-13 22:59:24 +0000854if ( ! function_exists('validation_errors'))
855{
Timothy Warren01b129a2012-04-27 11:36:50 -0400856 /**
857 * Validation Error String
858 *
859 * Returns all the errors associated with a form submission. This is a helper
860 * function for the form validation class.
861 *
862 * @param string
863 * @param string
864 * @return string
865 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 function validation_errors($prefix = '', $suffix = '')
867 {
868 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500869 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000870 return '';
871 }
872
873 return $OBJ->error_string($prefix, $suffix);
874 }
875}
876
877// ------------------------------------------------------------------------
878
Derek Allard2067d1a2008-11-13 22:59:24 +0000879if ( ! function_exists('_parse_form_attributes'))
880{
Timothy Warren01b129a2012-04-27 11:36:50 -0400881 /**
882 * Parse the form attributes
883 *
884 * Helper function used by some of the form helpers
885 *
886 * @param array
887 * @param array
888 * @return string
889 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000890 function _parse_form_attributes($attributes, $default)
891 {
892 if (is_array($attributes))
893 {
894 foreach ($default as $key => $val)
895 {
896 if (isset($attributes[$key]))
897 {
898 $default[$key] = $attributes[$key];
899 unset($attributes[$key]);
900 }
901 }
902
903 if (count($attributes) > 0)
904 {
905 $default = array_merge($default, $attributes);
906 }
907 }
908
909 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 foreach ($default as $key => $val)
912 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100913 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 {
Derek Jones01a9b102009-07-17 18:30:36 +0000915 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 }
917
Andrey Andreev93a83c72012-03-26 21:24:02 +0300918 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 }
920
921 return $att;
922 }
923}
924
925// ------------------------------------------------------------------------
926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927if ( ! function_exists('_attributes_to_string'))
928{
Timothy Warren01b129a2012-04-27 11:36:50 -0400929 /**
930 * Attributes To String
931 *
932 * Helper function used by some of the form helpers
933 *
934 * @param mixed
935 * @param bool
936 * @return string
937 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 function _attributes_to_string($attributes, $formtag = FALSE)
939 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300940 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100942 if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
944 $attributes .= ' method="post"';
945 }
946
Alex Bilbie773ccc32012-06-02 11:11:08 +0100947 if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000948 {
949 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
950 }
951
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200952 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 }
Barry Mienydd671972010-10-04 16:33:58 +0200954
Andrey Andreev93a83c72012-03-26 21:24:02 +0300955 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300957 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 }
959
Andrey Andreev93a83c72012-03-26 21:24:02 +0300960 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 {
Derek Allard3241d732009-09-17 12:17:45 +0000962 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000963
Andrey Andreev93a83c72012-03-26 21:24:02 +0300964 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000965 {
966 $atts .= ' method="post"';
967 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000968
Andrey Andreev93a83c72012-03-26 21:24:02 +0300969 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000970 {
971 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
972 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000973
Derek Allard3241d732009-09-17 12:17:45 +0000974 foreach ($attributes as $key => $val)
975 {
976 $atts .= ' '.$key.'="'.$val.'"';
977 }
978
979 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 }
981 }
982}
983
984// ------------------------------------------------------------------------
985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986if ( ! function_exists('_get_validation_object'))
987{
Timothy Warren01b129a2012-04-27 11:36:50 -0400988 /**
989 * Validation Object
990 *
991 * Determines what the form validation class was instantiated as, fetches
992 * the object and returns it.
993 *
994 * @return mixed
995 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 function &_get_validation_object()
997 {
998 $CI =& get_instance();
999
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001000 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001002
Greg Akerc6d918a2011-04-22 10:17:32 -05001003 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001005 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1006 {
1007 return $return;
1008 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001009
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001010 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001012
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001013 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 }
1015}
1016
Derek Allard2067d1a2008-11-13 22:59:24 +00001017/* End of file form_helper.php */
Andrey Andreev93a83c72012-03-26 21:24:02 +03001018/* Location: ./system/helpers/form_helper.php */