blob: 41097218758d4a2343b7ea165ffe545d17e8ace3 [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
Alex Bilbie773ccc32012-06-02 11:11:08 +0100328 if ($extra !== '') $extra = ' '.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000329
330 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
331
332 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 foreach ($options as $key => $val)
335 {
336 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000337
Derek Jones68788d52010-03-05 10:11:31 -0600338 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000339 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200340 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000341
Derek Allard78a5fc92009-02-05 16:34:35 +0000342 foreach ($val as $optgroup_key => $optgroup_val)
343 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300344 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000345 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
346 }
347
Andrey Andreev93a83c72012-03-26 21:24:02 +0300348 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000349 }
350 else
351 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200352 $form .= '<option value="'.$key.'"'.(in_array($key, $selected) ? ' selected="selected"' : '').'>'.(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000353 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
355
Andrey Andreev93a83c72012-03-26 21:24:02 +0300356 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
358}
359
360// ------------------------------------------------------------------------
361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362if ( ! function_exists('form_checkbox'))
363{
Timothy Warren01b129a2012-04-27 11:36:50 -0400364 /**
365 * Checkbox Field
366 *
367 * @param mixed
368 * @param string
369 * @param bool
370 * @param string
371 * @return string
372 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
374 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300375 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376
Andrey Andreev93a83c72012-03-26 21:24:02 +0300377 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
379 $checked = $data['checked'];
380
Alex Bilbie773ccc32012-06-02 11:11:08 +0100381 if ($checked === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
383 unset($data['checked']);
384 }
385 else
386 {
387 $data['checked'] = 'checked';
388 }
389 }
390
Alex Bilbie773ccc32012-06-02 11:11:08 +0100391 if ($checked === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 $defaults['checked'] = 'checked';
394 }
395 else
396 {
397 unset($defaults['checked']);
398 }
399
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200400 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402}
403
404// ------------------------------------------------------------------------
405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406if ( ! function_exists('form_radio'))
407{
Timothy Warren01b129a2012-04-27 11:36:50 -0400408 /**
409 * Radio Button
410 *
411 * @param mixed
412 * @param string
413 * @param bool
414 * @param string
415 * @return string
416 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
418 {
419 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200420 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 $data = array('name' => $data);
422 }
423
424 $data['type'] = 'radio';
425 return form_checkbox($data, $value, $checked, $extra);
426 }
427}
428
429// ------------------------------------------------------------------------
430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200432{
Timothy Warren01b129a2012-04-27 11:36:50 -0400433 /**
434 * Submit Button
435 *
436 * @param mixed
437 * @param string
438 * @param string
439 * @return string
440 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 function form_submit($data = '', $value = '', $extra = '')
442 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300443 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200444 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446}
447
448// ------------------------------------------------------------------------
449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450if ( ! function_exists('form_reset'))
451{
Timothy Warren01b129a2012-04-27 11:36:50 -0400452 /**
453 * Reset Button
454 *
455 * @param mixed
456 * @param string
457 * @param string
458 * @return string
459 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 function form_reset($data = '', $value = '', $extra = '')
461 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300462 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200463 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
465}
466
467// ------------------------------------------------------------------------
468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469if ( ! function_exists('form_button'))
470{
Timothy Warren01b129a2012-04-27 11:36:50 -0400471 /**
472 * Form Button
473 *
474 * @param mixed
475 * @param string
476 * @param string
477 * @return string
478 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 function form_button($data = '', $content = '', $extra = '')
480 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300481 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
482 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 $content = $data['content'];
485 unset($data['content']); // content is not an attribute
486 }
487
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200488 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490}
491
492// ------------------------------------------------------------------------
493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494if ( ! function_exists('form_label'))
495{
Timothy Warren01b129a2012-04-27 11:36:50 -0400496 /**
497 * Form Label Tag
498 *
499 * @param string The text to appear onscreen
500 * @param string The id the label applies to
501 * @param string Additional attributes
502 * @return string
503 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 function form_label($label_text = '', $id = '', $attributes = array())
505 {
506
507 $label = '<label';
508
Alex Bilbie773ccc32012-06-02 11:11:08 +0100509 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300511 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
Andrey Andreev93a83c72012-03-26 21:24:02 +0300514 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 foreach ($attributes as $key => $val)
517 {
518 $label .= ' '.$key.'="'.$val.'"';
519 }
520 }
521
Andrey Andreev93a83c72012-03-26 21:24:02 +0300522 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524}
525
526// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528if ( ! function_exists('form_fieldset'))
529{
Timothy Warren01b129a2012-04-27 11:36:50 -0400530 /**
531 * Fieldset Tag
532 *
533 * Used to produce <fieldset><legend>text</legend>. To close fieldset
534 * use form_fieldset_close()
535 *
536 * @param string The legend text
537 * @param string Additional attributes
538 * @return string
539 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 function form_fieldset($legend_text = '', $attributes = array())
541 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200542 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100543 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300545 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 }
547
548 return $fieldset;
549 }
550}
551
552// ------------------------------------------------------------------------
553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554if ( ! function_exists('form_fieldset_close'))
555{
Timothy Warren01b129a2012-04-27 11:36:50 -0400556 /**
557 * Fieldset Close Tag
558 *
559 * @param string
560 * @return string
561 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 function form_fieldset_close($extra = '')
563 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300564 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566}
567
568// ------------------------------------------------------------------------
569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570if ( ! function_exists('form_close'))
571{
Timothy Warren01b129a2012-04-27 11:36:50 -0400572 /**
573 * Form Close Tag
574 *
575 * @param string
576 * @return string
577 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 function form_close($extra = '')
579 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300580 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
582}
583
584// ------------------------------------------------------------------------
585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586if ( ! function_exists('form_prep'))
587{
Timothy Warren01b129a2012-04-27 11:36:50 -0400588 /**
589 * Form Prep
590 *
591 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
592 *
593 * @param string
594 * @param string
595 * @return string
596 */
Derek Jones01a9b102009-07-17 18:30:36 +0000597 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Derek Jones01a9b102009-07-17 18:30:36 +0000599 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 // if the field name is an array we do this recursively
602 if (is_array($str))
603 {
604 foreach ($str as $key => $val)
605 {
606 $str[$key] = form_prep($val);
607 }
608
609 return $str;
610 }
611
612 if ($str === '')
613 {
614 return '';
615 }
616
Derek Jones3eb9fac2009-07-17 20:25:13 +0000617 // we've already prepped a field with this name
618 // @todo need to figure out a way to namespace this so
619 // that we know the *exact* field and not just one with
620 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000621 if (isset($prepped_fields[$field_name]))
622 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000623 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000624 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000625
Alex Bilbie773ccc32012-06-02 11:11:08 +0100626 if ($field_name !== '')
Derek Jones01a9b102009-07-17 18:30:36 +0000627 {
Derek Jones68788d52010-03-05 10:11:31 -0600628 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200631 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
633}
634
635// ------------------------------------------------------------------------
636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637if ( ! function_exists('set_value'))
638{
Timothy Warren01b129a2012-04-27 11:36:50 -0400639 /**
640 * Form Value
641 *
642 * Grabs a value from the POST array for the specified field so you can
643 * re-populate an input field or textarea. If Form Validation
644 * is active it retrieves the info from the validation class
645 *
646 * @param string
647 * @param string
648 * @return mixed
649 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 function set_value($field = '', $default = '')
651 {
652 if (FALSE === ($OBJ =& _get_validation_object()))
653 {
654 if ( ! isset($_POST[$field]))
655 {
656 return $default;
657 }
658
Derek Jones01a9b102009-07-17 18:30:36 +0000659 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661
Derek Jones01a9b102009-07-17 18:30:36 +0000662 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664}
665
666// ------------------------------------------------------------------------
667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668if ( ! function_exists('set_select'))
669{
Timothy Warren01b129a2012-04-27 11:36:50 -0400670 /**
671 * Set Select
672 *
673 * Let's you set the selected value of a <select> menu via data in the POST array.
674 * If Form Validation is active it retrieves the info from the validation class
675 *
676 * @param string
677 * @param string
678 * @param bool
679 * @return string
680 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 function set_select($field = '', $value = '', $default = FALSE)
682 {
683 $OBJ =& _get_validation_object();
684
685 if ($OBJ === FALSE)
686 {
687 if ( ! isset($_POST[$field]))
688 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100689 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
691 return ' selected="selected"';
692 }
693 return '';
694 }
695
696 $field = $_POST[$field];
697
698 if (is_array($field))
699 {
700 if ( ! in_array($value, $field))
701 {
702 return '';
703 }
704 }
Alex Bilbie773ccc32012-06-02 11:11:08 +0100705 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300707 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000708 }
709
710 return ' selected="selected"';
711 }
712
713 return $OBJ->set_select($field, $value, $default);
714 }
715}
716
717// ------------------------------------------------------------------------
718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719if ( ! function_exists('set_checkbox'))
720{
Timothy Warren01b129a2012-04-27 11:36:50 -0400721 /**
722 * Set Checkbox
723 *
724 * Let's you set the selected value of a checkbox via the value in the POST array.
725 * If Form Validation is active it retrieves the info from the validation class
726 *
727 * @param string
728 * @param string
729 * @param bool
730 * @return string
731 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 function set_checkbox($field = '', $value = '', $default = FALSE)
733 {
734 $OBJ =& _get_validation_object();
735
736 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200737 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 if ( ! isset($_POST[$field]))
739 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100740 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000741 {
742 return ' checked="checked"';
743 }
744 return '';
745 }
746
747 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 if (is_array($field))
750 {
751 if ( ! in_array($value, $field))
752 {
753 return '';
754 }
755 }
Alex Bilbie773ccc32012-06-02 11:11:08 +0100756 elseif (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300758 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 }
760
761 return ' checked="checked"';
762 }
763
764 return $OBJ->set_checkbox($field, $value, $default);
765 }
766}
767
768// ------------------------------------------------------------------------
769
Derek Allard2067d1a2008-11-13 22:59:24 +0000770if ( ! function_exists('set_radio'))
771{
Timothy Warren01b129a2012-04-27 11:36:50 -0400772 /**
773 * Set Radio
774 *
775 * Let's you set the selected value of a radio field via info in the POST array.
776 * If Form Validation is active it retrieves the info from the validation class
777 *
778 * @param string
779 * @param string
780 * @param bool
781 * @return string
782 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 function set_radio($field = '', $value = '', $default = FALSE)
784 {
785 $OBJ =& _get_validation_object();
786
787 if ($OBJ === FALSE)
788 {
789 if ( ! isset($_POST[$field]))
790 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100791 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 {
793 return ' checked="checked"';
794 }
795 return '';
796 }
797
798 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200799
Derek Allard2067d1a2008-11-13 22:59:24 +0000800 if (is_array($field))
801 {
802 if ( ! in_array($value, $field))
803 {
804 return '';
805 }
806 }
807 else
808 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100809 if (($field === '' OR $value === '') OR ($field !== $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 {
811 return '';
812 }
813 }
814
815 return ' checked="checked"';
816 }
817
818 return $OBJ->set_radio($field, $value, $default);
819 }
820}
821
822// ------------------------------------------------------------------------
823
Timothy Warren01b129a2012-04-27 11:36:50 -0400824
Derek Allard2067d1a2008-11-13 22:59:24 +0000825if ( ! function_exists('form_error'))
826{
Timothy Warren01b129a2012-04-27 11:36:50 -0400827 /**
828 * Form Error
829 *
830 * Returns the error for a specific form field. This is a helper for the
831 * form validation class.
832 *
833 * @param string
834 * @param string
835 * @param string
836 * @return string
837 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 function form_error($field = '', $prefix = '', $suffix = '')
839 {
840 if (FALSE === ($OBJ =& _get_validation_object()))
841 {
842 return '';
843 }
844
845 return $OBJ->error($field, $prefix, $suffix);
846 }
847}
848
849// ------------------------------------------------------------------------
850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851if ( ! function_exists('validation_errors'))
852{
Timothy Warren01b129a2012-04-27 11:36:50 -0400853 /**
854 * Validation Error String
855 *
856 * Returns all the errors associated with a form submission. This is a helper
857 * function for the form validation class.
858 *
859 * @param string
860 * @param string
861 * @return string
862 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 function validation_errors($prefix = '', $suffix = '')
864 {
865 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500866 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 return '';
868 }
869
870 return $OBJ->error_string($prefix, $suffix);
871 }
872}
873
874// ------------------------------------------------------------------------
875
Derek Allard2067d1a2008-11-13 22:59:24 +0000876if ( ! function_exists('_parse_form_attributes'))
877{
Timothy Warren01b129a2012-04-27 11:36:50 -0400878 /**
879 * Parse the form attributes
880 *
881 * Helper function used by some of the form helpers
882 *
883 * @param array
884 * @param array
885 * @return string
886 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 function _parse_form_attributes($attributes, $default)
888 {
889 if (is_array($attributes))
890 {
891 foreach ($default as $key => $val)
892 {
893 if (isset($attributes[$key]))
894 {
895 $default[$key] = $attributes[$key];
896 unset($attributes[$key]);
897 }
898 }
899
900 if (count($attributes) > 0)
901 {
902 $default = array_merge($default, $attributes);
903 }
904 }
905
906 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200907
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 foreach ($default as $key => $val)
909 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100910 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
Derek Jones01a9b102009-07-17 18:30:36 +0000912 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 }
914
Andrey Andreev93a83c72012-03-26 21:24:02 +0300915 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 }
917
918 return $att;
919 }
920}
921
922// ------------------------------------------------------------------------
923
Derek Allard2067d1a2008-11-13 22:59:24 +0000924if ( ! function_exists('_attributes_to_string'))
925{
Timothy Warren01b129a2012-04-27 11:36:50 -0400926 /**
927 * Attributes To String
928 *
929 * Helper function used by some of the form helpers
930 *
931 * @param mixed
932 * @param bool
933 * @return string
934 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 function _attributes_to_string($attributes, $formtag = FALSE)
936 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300937 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000938 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100939 if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 {
941 $attributes .= ' method="post"';
942 }
943
Alex Bilbie773ccc32012-06-02 11:11:08 +0100944 if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000945 {
946 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
947 }
948
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200949 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
Barry Mienydd671972010-10-04 16:33:58 +0200951
Andrey Andreev93a83c72012-03-26 21:24:02 +0300952 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300954 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
956
Andrey Andreev93a83c72012-03-26 21:24:02 +0300957 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Derek Allard3241d732009-09-17 12:17:45 +0000959 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
Andrey Andreev93a83c72012-03-26 21:24:02 +0300961 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000962 {
963 $atts .= ' method="post"';
964 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
Andrey Andreev93a83c72012-03-26 21:24:02 +0300966 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000967 {
968 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
969 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000970
Derek Allard3241d732009-09-17 12:17:45 +0000971 foreach ($attributes as $key => $val)
972 {
973 $atts .= ' '.$key.'="'.$val.'"';
974 }
975
976 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 }
978 }
979}
980
981// ------------------------------------------------------------------------
982
Derek Allard2067d1a2008-11-13 22:59:24 +0000983if ( ! function_exists('_get_validation_object'))
984{
Timothy Warren01b129a2012-04-27 11:36:50 -0400985 /**
986 * Validation Object
987 *
988 * Determines what the form validation class was instantiated as, fetches
989 * the object and returns it.
990 *
991 * @return mixed
992 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 function &_get_validation_object()
994 {
995 $CI =& get_instance();
996
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500997 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200999
Greg Akerc6d918a2011-04-22 10:17:32 -05001000 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001001 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001002 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1003 {
1004 return $return;
1005 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001006
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001007 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001009
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001010 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 }
1012}
1013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014/* End of file form_helper.php */
Andrey Andreev93a83c72012-03-26 21:24:02 +03001015/* Location: ./system/helpers/form_helper.php */