blob: b246d72f3a647ab4cb31b9c2c502896fd4d20745 [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
55 if ($attributes == '')
56 {
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
Timothy Warren01b129a2012-04-27 11:36:50 -0400187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188if ( ! function_exists('form_password'))
189{
Timothy Warren01b129a2012-04-27 11:36:50 -0400190 /**
191 * Password Field
192 *
193 * Identical to the input function but adds the "password" type
194 *
195 * @param mixed
196 * @param string
197 * @param string
198 * @return string
199 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 function form_password($data = '', $value = '', $extra = '')
201 {
202 if ( ! is_array($data))
203 {
204 $data = array('name' => $data);
205 }
206
207 $data['type'] = 'password';
208 return form_input($data, $value, $extra);
209 }
210}
211
212// ------------------------------------------------------------------------
213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214if ( ! function_exists('form_upload'))
215{
Timothy Warren01b129a2012-04-27 11:36:50 -0400216 /**
217 * Upload Field
218 *
219 * Identical to the input function but adds the "file" type
220 *
221 * @param mixed
222 * @param string
223 * @param string
224 * @return string
225 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 function form_upload($data = '', $value = '', $extra = '')
227 {
228 if ( ! is_array($data))
229 {
230 $data = array('name' => $data);
231 }
232
233 $data['type'] = 'file';
234 return form_input($data, $value, $extra);
235 }
236}
237
238// ------------------------------------------------------------------------
239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240if ( ! function_exists('form_textarea'))
241{
Timothy Warren01b129a2012-04-27 11:36:50 -0400242 /**
243 * Textarea field
244 *
245 * @param mixed
246 * @param string
247 * @param string
248 * @return string
249 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 function form_textarea($data = '', $value = '', $extra = '')
251 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300252 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000253
254 if ( ! is_array($data) OR ! isset($data['value']))
255 {
256 $val = $value;
257 }
258 else
259 {
Barry Mienydd671972010-10-04 16:33:58 +0200260 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 unset($data['value']); // textareas don't use the value attribute
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Andrey Andreev93a83c72012-03-26 21:24:02 +0300264 $name = is_array($data) ? $data['name'] : $data;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200265 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, $name)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
267}
268
269// ------------------------------------------------------------------------
270
Derek Jones68788d52010-03-05 10:11:31 -0600271if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000272{
Timothy Warren01b129a2012-04-27 11:36:50 -0400273 /**
274 * Multi-select menu
275 *
276 * @param string
277 * @param array
278 * @param mixed
279 * @param string
280 * @return string
281 */
Derek Jones26399292009-04-08 16:14:17 +0000282 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
283 {
284 if ( ! strpos($extra, 'multiple'))
285 {
286 $extra .= ' multiple="multiple"';
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Jones26399292009-04-08 16:14:17 +0000289 return form_dropdown($name, $options, $selected, $extra);
290 }
291}
292
293// --------------------------------------------------------------------
294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295if ( ! function_exists('form_dropdown'))
296{
Timothy Warren01b129a2012-04-27 11:36:50 -0400297 /**
298 * Drop-down Menu
299 *
300 * @param string
301 * @param array
302 * @param string
303 * @param string
304 * @return string
305 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
307 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300308 // If name is really an array then we'll call the function again using the array
309 if (is_array($name) && isset($name['name']))
310 {
311 isset($name['options']) OR $name['options'] = array();
312 isset($name['selected']) OR $name['selected'] = array();
313 isset($name['extra']) OR $name['extra'] = array();
314
315 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
316 }
317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 if ( ! is_array($selected))
319 {
320 $selected = array($selected);
321 }
322
323 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200324 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200326 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328
329 if ($extra != '') $extra = ' '.$extra;
330
331 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
332
333 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 foreach ($options as $key => $val)
336 {
337 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
Derek Jones68788d52010-03-05 10:11:31 -0600339 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000340 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200341 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000342
Derek Allard78a5fc92009-02-05 16:34:35 +0000343 foreach ($val as $optgroup_key => $optgroup_val)
344 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300345 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000346 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
347 }
348
Andrey Andreev93a83c72012-03-26 21:24:02 +0300349 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000350 }
351 else
352 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200353 $form .= '<option value="'.$key.'"'.(in_array($key, $selected) ? ' selected="selected"' : '').'>'.(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000354 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
Andrey Andreev93a83c72012-03-26 21:24:02 +0300357 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
359}
360
361// ------------------------------------------------------------------------
362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363if ( ! function_exists('form_checkbox'))
364{
Timothy Warren01b129a2012-04-27 11:36:50 -0400365 /**
366 * Checkbox Field
367 *
368 * @param mixed
369 * @param string
370 * @param bool
371 * @param string
372 * @return string
373 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
375 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300376 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000377
Andrey Andreev93a83c72012-03-26 21:24:02 +0300378 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $checked = $data['checked'];
381
382 if ($checked == FALSE)
383 {
384 unset($data['checked']);
385 }
386 else
387 {
388 $data['checked'] = 'checked';
389 }
390 }
391
392 if ($checked == TRUE)
393 {
394 $defaults['checked'] = 'checked';
395 }
396 else
397 {
398 unset($defaults['checked']);
399 }
400
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200401 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403}
404
405// ------------------------------------------------------------------------
406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407if ( ! function_exists('form_radio'))
408{
Timothy Warren01b129a2012-04-27 11:36:50 -0400409 /**
410 * Radio Button
411 *
412 * @param mixed
413 * @param string
414 * @param bool
415 * @param string
416 * @return string
417 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
419 {
420 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200421 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 $data = array('name' => $data);
423 }
424
425 $data['type'] = 'radio';
426 return form_checkbox($data, $value, $checked, $extra);
427 }
428}
429
430// ------------------------------------------------------------------------
431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200433{
Timothy Warren01b129a2012-04-27 11:36:50 -0400434 /**
435 * Submit Button
436 *
437 * @param mixed
438 * @param string
439 * @param string
440 * @return string
441 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 function form_submit($data = '', $value = '', $extra = '')
443 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300444 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200445 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447}
448
449// ------------------------------------------------------------------------
450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451if ( ! function_exists('form_reset'))
452{
Timothy Warren01b129a2012-04-27 11:36:50 -0400453 /**
454 * Reset Button
455 *
456 * @param mixed
457 * @param string
458 * @param string
459 * @return string
460 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 function form_reset($data = '', $value = '', $extra = '')
462 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300463 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200464 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
466}
467
468// ------------------------------------------------------------------------
469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470if ( ! function_exists('form_button'))
471{
Timothy Warren01b129a2012-04-27 11:36:50 -0400472 /**
473 * Form Button
474 *
475 * @param mixed
476 * @param string
477 * @param string
478 * @return string
479 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 function form_button($data = '', $content = '', $extra = '')
481 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300482 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
483 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 $content = $data['content'];
486 unset($data['content']); // content is not an attribute
487 }
488
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200489 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491}
492
493// ------------------------------------------------------------------------
494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495if ( ! function_exists('form_label'))
496{
Timothy Warren01b129a2012-04-27 11:36:50 -0400497 /**
498 * Form Label Tag
499 *
500 * @param string The text to appear onscreen
501 * @param string The id the label applies to
502 * @param string Additional attributes
503 * @return string
504 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 function form_label($label_text = '', $id = '', $attributes = array())
506 {
507
508 $label = '<label';
509
510 if ($id != '')
511 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300512 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
514
Andrey Andreev93a83c72012-03-26 21:24:02 +0300515 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 foreach ($attributes as $key => $val)
518 {
519 $label .= ' '.$key.'="'.$val.'"';
520 }
521 }
522
Andrey Andreev93a83c72012-03-26 21:24:02 +0300523 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525}
526
527// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529if ( ! function_exists('form_fieldset'))
530{
Timothy Warren01b129a2012-04-27 11:36:50 -0400531 /**
532 * Fieldset Tag
533 *
534 * Used to produce <fieldset><legend>text</legend>. To close fieldset
535 * use form_fieldset_close()
536 *
537 * @param string The legend text
538 * @param string Additional attributes
539 * @return string
540 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 function form_fieldset($legend_text = '', $attributes = array())
542 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200543 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 if ($legend_text != '')
545 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300546 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548
549 return $fieldset;
550 }
551}
552
553// ------------------------------------------------------------------------
554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555if ( ! function_exists('form_fieldset_close'))
556{
Timothy Warren01b129a2012-04-27 11:36:50 -0400557 /**
558 * Fieldset Close Tag
559 *
560 * @param string
561 * @return string
562 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 function form_fieldset_close($extra = '')
564 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300565 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
567}
568
569// ------------------------------------------------------------------------
570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571if ( ! function_exists('form_close'))
572{
Timothy Warren01b129a2012-04-27 11:36:50 -0400573 /**
574 * Form Close Tag
575 *
576 * @param string
577 * @return string
578 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 function form_close($extra = '')
580 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300581 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 }
583}
584
585// ------------------------------------------------------------------------
586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587if ( ! function_exists('form_prep'))
588{
Timothy Warren01b129a2012-04-27 11:36:50 -0400589 /**
590 * Form Prep
591 *
592 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
593 *
594 * @param string
595 * @param string
596 * @return string
597 */
Derek Jones01a9b102009-07-17 18:30:36 +0000598 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 {
Derek Jones01a9b102009-07-17 18:30:36 +0000600 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 // if the field name is an array we do this recursively
603 if (is_array($str))
604 {
605 foreach ($str as $key => $val)
606 {
607 $str[$key] = form_prep($val);
608 }
609
610 return $str;
611 }
612
613 if ($str === '')
614 {
615 return '';
616 }
617
Derek Jones3eb9fac2009-07-17 20:25:13 +0000618 // we've already prepped a field with this name
619 // @todo need to figure out a way to namespace this so
620 // that we know the *exact* field and not just one with
621 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000622 if (isset($prepped_fields[$field_name]))
623 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000624 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000625 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000626
Derek Jones01a9b102009-07-17 18:30:36 +0000627 if ($field_name != '')
628 {
Derek Jones68788d52010-03-05 10:11:31 -0600629 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200632 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
634}
635
636// ------------------------------------------------------------------------
637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638if ( ! function_exists('set_value'))
639{
Timothy Warren01b129a2012-04-27 11:36:50 -0400640 /**
641 * Form Value
642 *
643 * Grabs a value from the POST array for the specified field so you can
644 * re-populate an input field or textarea. If Form Validation
645 * is active it retrieves the info from the validation class
646 *
647 * @param string
648 * @param string
649 * @return mixed
650 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 function set_value($field = '', $default = '')
652 {
653 if (FALSE === ($OBJ =& _get_validation_object()))
654 {
655 if ( ! isset($_POST[$field]))
656 {
657 return $default;
658 }
659
Derek Jones01a9b102009-07-17 18:30:36 +0000660 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 }
662
Derek Jones01a9b102009-07-17 18:30:36 +0000663 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665}
666
667// ------------------------------------------------------------------------
668
Derek Allard2067d1a2008-11-13 22:59:24 +0000669if ( ! function_exists('set_select'))
670{
Timothy Warren01b129a2012-04-27 11:36:50 -0400671 /**
672 * Set Select
673 *
674 * Let's you set the selected value of a <select> menu via data in the POST array.
675 * If Form Validation is active it retrieves the info from the validation class
676 *
677 * @param string
678 * @param string
679 * @param bool
680 * @return string
681 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 function set_select($field = '', $value = '', $default = FALSE)
683 {
684 $OBJ =& _get_validation_object();
685
686 if ($OBJ === FALSE)
687 {
688 if ( ! isset($_POST[$field]))
689 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300690 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 return ' selected="selected"';
693 }
694 return '';
695 }
696
697 $field = $_POST[$field];
698
699 if (is_array($field))
700 {
701 if ( ! in_array($value, $field))
702 {
703 return '';
704 }
705 }
Andrey Andreev93a83c72012-03-26 21:24:02 +0300706 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300708 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 }
710
711 return ' selected="selected"';
712 }
713
714 return $OBJ->set_select($field, $value, $default);
715 }
716}
717
718// ------------------------------------------------------------------------
719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720if ( ! function_exists('set_checkbox'))
721{
Timothy Warren01b129a2012-04-27 11:36:50 -0400722 /**
723 * Set Checkbox
724 *
725 * Let's you set the selected value of a checkbox via the value in the POST array.
726 * If Form Validation is active it retrieves the info from the validation class
727 *
728 * @param string
729 * @param string
730 * @param bool
731 * @return string
732 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000733 function set_checkbox($field = '', $value = '', $default = FALSE)
734 {
735 $OBJ =& _get_validation_object();
736
737 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200738 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000739 if ( ! isset($_POST[$field]))
740 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300741 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 {
743 return ' checked="checked"';
744 }
745 return '';
746 }
747
748 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200749
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 if (is_array($field))
751 {
752 if ( ! in_array($value, $field))
753 {
754 return '';
755 }
756 }
Andrey Andreev93a83c72012-03-26 21:24:02 +0300757 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300759 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 }
761
762 return ' checked="checked"';
763 }
764
765 return $OBJ->set_checkbox($field, $value, $default);
766 }
767}
768
769// ------------------------------------------------------------------------
770
Derek Allard2067d1a2008-11-13 22:59:24 +0000771if ( ! function_exists('set_radio'))
772{
Timothy Warren01b129a2012-04-27 11:36:50 -0400773 /**
774 * Set Radio
775 *
776 * Let's you set the selected value of a radio field via info in the POST array.
777 * If Form Validation is active it retrieves the info from the validation class
778 *
779 * @param string
780 * @param string
781 * @param bool
782 * @return string
783 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000784 function set_radio($field = '', $value = '', $default = FALSE)
785 {
786 $OBJ =& _get_validation_object();
787
788 if ($OBJ === FALSE)
789 {
790 if ( ! isset($_POST[$field]))
791 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300792 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 {
794 return ' checked="checked"';
795 }
796 return '';
797 }
798
799 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 if (is_array($field))
802 {
803 if ( ! in_array($value, $field))
804 {
805 return '';
806 }
807 }
808 else
809 {
810 if (($field == '' OR $value == '') OR ($field != $value))
811 {
812 return '';
813 }
814 }
815
816 return ' checked="checked"';
817 }
818
819 return $OBJ->set_radio($field, $value, $default);
820 }
821}
822
823// ------------------------------------------------------------------------
824
Timothy Warren01b129a2012-04-27 11:36:50 -0400825
Derek Allard2067d1a2008-11-13 22:59:24 +0000826if ( ! function_exists('form_error'))
827{
Timothy Warren01b129a2012-04-27 11:36:50 -0400828 /**
829 * Form Error
830 *
831 * Returns the error for a specific form field. This is a helper for the
832 * form validation class.
833 *
834 * @param string
835 * @param string
836 * @param string
837 * @return string
838 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 function form_error($field = '', $prefix = '', $suffix = '')
840 {
841 if (FALSE === ($OBJ =& _get_validation_object()))
842 {
843 return '';
844 }
845
846 return $OBJ->error($field, $prefix, $suffix);
847 }
848}
849
850// ------------------------------------------------------------------------
851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852if ( ! function_exists('validation_errors'))
853{
Timothy Warren01b129a2012-04-27 11:36:50 -0400854 /**
855 * Validation Error String
856 *
857 * Returns all the errors associated with a form submission. This is a helper
858 * function for the form validation class.
859 *
860 * @param string
861 * @param string
862 * @return string
863 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 function validation_errors($prefix = '', $suffix = '')
865 {
866 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500867 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 return '';
869 }
870
871 return $OBJ->error_string($prefix, $suffix);
872 }
873}
874
875// ------------------------------------------------------------------------
876
Derek Allard2067d1a2008-11-13 22:59:24 +0000877if ( ! function_exists('_parse_form_attributes'))
878{
Timothy Warren01b129a2012-04-27 11:36:50 -0400879 /**
880 * Parse the form attributes
881 *
882 * Helper function used by some of the form helpers
883 *
884 * @param array
885 * @param array
886 * @return string
887 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 function _parse_form_attributes($attributes, $default)
889 {
890 if (is_array($attributes))
891 {
892 foreach ($default as $key => $val)
893 {
894 if (isset($attributes[$key]))
895 {
896 $default[$key] = $attributes[$key];
897 unset($attributes[$key]);
898 }
899 }
900
901 if (count($attributes) > 0)
902 {
903 $default = array_merge($default, $attributes);
904 }
905 }
906
907 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200908
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 foreach ($default as $key => $val)
910 {
911 if ($key == 'value')
912 {
Derek Jones01a9b102009-07-17 18:30:36 +0000913 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000914 }
915
Andrey Andreev93a83c72012-03-26 21:24:02 +0300916 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000917 }
918
919 return $att;
920 }
921}
922
923// ------------------------------------------------------------------------
924
Derek Allard2067d1a2008-11-13 22:59:24 +0000925if ( ! function_exists('_attributes_to_string'))
926{
Timothy Warren01b129a2012-04-27 11:36:50 -0400927 /**
928 * Attributes To String
929 *
930 * Helper function used by some of the form helpers
931 *
932 * @param mixed
933 * @param bool
934 * @return string
935 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 function _attributes_to_string($attributes, $formtag = FALSE)
937 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300938 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000939 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300940 if ($formtag == TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 {
942 $attributes .= ' method="post"';
943 }
944
Andrey Andreev93a83c72012-03-26 21:24:02 +0300945 if ($formtag == TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000946 {
947 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
948 }
949
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200950 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Andrey Andreev93a83c72012-03-26 21:24:02 +0300953 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300955 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000956 }
957
Andrey Andreev93a83c72012-03-26 21:24:02 +0300958 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
Derek Allard3241d732009-09-17 12:17:45 +0000960 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000961
Andrey Andreev93a83c72012-03-26 21:24:02 +0300962 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000963 {
964 $atts .= ' method="post"';
965 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000966
Andrey Andreev93a83c72012-03-26 21:24:02 +0300967 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000968 {
969 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
970 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000971
Derek Allard3241d732009-09-17 12:17:45 +0000972 foreach ($attributes as $key => $val)
973 {
974 $atts .= ' '.$key.'="'.$val.'"';
975 }
976
977 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000978 }
979 }
980}
981
982// ------------------------------------------------------------------------
983
Derek Allard2067d1a2008-11-13 22:59:24 +0000984if ( ! function_exists('_get_validation_object'))
985{
Timothy Warren01b129a2012-04-27 11:36:50 -0400986 /**
987 * Validation Object
988 *
989 * Determines what the form validation class was instantiated as, fetches
990 * the object and returns it.
991 *
992 * @return mixed
993 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 function &_get_validation_object()
995 {
996 $CI =& get_instance();
997
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500998 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001000
Greg Akerc6d918a2011-04-22 10:17:32 -05001001 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001003 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1004 {
1005 return $return;
1006 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001007
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001008 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001010
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001011 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 }
1013}
1014
Derek Allard2067d1a2008-11-13 22:59:24 +00001015/* End of file form_helper.php */
Andrey Andreev93a83c72012-03-26 21:24:02 +03001016/* Location: ./system/helpers/form_helper.php */