blob: 0c5d550375050756b643c912e5ab7a17609704b8 [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 }
Andrey Andreev3c32a112012-06-16 18:05:34 +030065 elseif ( ! $action)
66 {
67 // If no action is provided then set to the current url
68 $action = $CI->config->site_url($CI->uri->uri_string());
69 }
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010070
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020071 $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n";
Barry Mienydd671972010-10-04 16:33:58 +020072
Andrey Andreev93a83c72012-03-26 21:24:02 +030073 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
74 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 -040075 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050076 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050077 }
78
Andrey Andreev93a83c72012-03-26 21:24:02 +030079 if (is_array($hidden) && count($hidden) > 0)
Greg Aker28b425a2010-09-15 11:43:23 -050080 {
Andrey Andreev3c32a112012-06-16 18:05:34 +030081 $form .= '<div style="display:none;">'.form_hidden($hidden).'</div>';
Derek Allard958543a2010-07-22 14:10:26 -040082 }
83
Derek Allard2067d1a2008-11-13 22:59:24 +000084 return $form;
85 }
86}
87
88// ------------------------------------------------------------------------
89
Derek Allard2067d1a2008-11-13 22:59:24 +000090if ( ! function_exists('form_open_multipart'))
91{
Timothy Warren01b129a2012-04-27 11:36:50 -040092 /**
93 * Form Declaration - Multipart type
94 *
95 * Creates the opening portion of the form, but with "multipart/form-data".
96 *
97 * @param string the URI segments of the form destination
98 * @param array a key/value pair of attributes
99 * @param array a key/value pair hidden data
100 * @return string
101 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500102 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000104 if (is_string($attributes))
105 {
106 $attributes .= ' enctype="multipart/form-data"';
107 }
108 else
109 {
110 $attributes['enctype'] = 'multipart/form-data';
111 }
112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 return form_open($action, $attributes, $hidden);
114 }
115}
116
117// ------------------------------------------------------------------------
118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119if ( ! function_exists('form_hidden'))
120{
Timothy Warren01b129a2012-04-27 11:36:50 -0400121 /**
122 * Hidden Input Field
123 *
124 * Generates hidden fields. You can pass a simple key/value string or
125 * an associative array with multiple values.
126 *
127 * @param mixed
128 * @param string
129 * @param bool
130 * @return string
131 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000132 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000134 static $form;
135
136 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000138 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
Robin Sowell57fe4102009-03-26 18:58:46 +0000141 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000143 foreach ($name as $key => $val)
144 {
145 form_hidden($key, $val, TRUE);
146 }
147 return $form;
148 }
149
150 if ( ! is_array($value))
151 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200152 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000153 }
154 else
155 {
156 foreach ($value as $k => $v)
157 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300158 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000159 form_hidden($name.'['.$k.']', $v, TRUE);
160 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162
163 return $form;
164 }
165}
166
167// ------------------------------------------------------------------------
168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169if ( ! function_exists('form_input'))
170{
Timothy Warren01b129a2012-04-27 11:36:50 -0400171 /**
172 * Text Input Field
173 *
174 * @param mixed
175 * @param string
176 * @param string
177 * @return string
178 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 function form_input($data = '', $value = '', $extra = '')
180 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300181 $defaults = array('type' => 'text', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200183 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
185}
186
187// ------------------------------------------------------------------------
188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189if ( ! function_exists('form_password'))
190{
Timothy Warren01b129a2012-04-27 11:36:50 -0400191 /**
192 * Password Field
193 *
194 * Identical to the input function but adds the "password" type
195 *
196 * @param mixed
197 * @param string
198 * @param string
199 * @return string
200 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 function form_password($data = '', $value = '', $extra = '')
202 {
203 if ( ! is_array($data))
204 {
205 $data = array('name' => $data);
206 }
207
208 $data['type'] = 'password';
209 return form_input($data, $value, $extra);
210 }
211}
212
213// ------------------------------------------------------------------------
214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215if ( ! function_exists('form_upload'))
216{
Timothy Warren01b129a2012-04-27 11:36:50 -0400217 /**
218 * Upload Field
219 *
220 * Identical to the input function but adds the "file" type
221 *
222 * @param mixed
223 * @param string
224 * @param string
225 * @return string
226 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 function form_upload($data = '', $value = '', $extra = '')
228 {
229 if ( ! is_array($data))
230 {
231 $data = array('name' => $data);
232 }
233
234 $data['type'] = 'file';
235 return form_input($data, $value, $extra);
236 }
237}
238
239// ------------------------------------------------------------------------
240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241if ( ! function_exists('form_textarea'))
242{
Timothy Warren01b129a2012-04-27 11:36:50 -0400243 /**
244 * Textarea field
245 *
246 * @param mixed
247 * @param string
248 * @param string
249 * @return string
250 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 function form_textarea($data = '', $value = '', $extra = '')
252 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300253 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000254
255 if ( ! is_array($data) OR ! isset($data['value']))
256 {
257 $val = $value;
258 }
259 else
260 {
Barry Mienydd671972010-10-04 16:33:58 +0200261 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 unset($data['value']); // textareas don't use the value attribute
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Andrey Andreev93a83c72012-03-26 21:24:02 +0300265 $name = is_array($data) ? $data['name'] : $data;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200266 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, $name)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268}
269
270// ------------------------------------------------------------------------
271
Derek Jones68788d52010-03-05 10:11:31 -0600272if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000273{
Timothy Warren01b129a2012-04-27 11:36:50 -0400274 /**
275 * Multi-select menu
276 *
277 * @param string
278 * @param array
279 * @param mixed
280 * @param string
281 * @return string
282 */
Derek Jones26399292009-04-08 16:14:17 +0000283 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
284 {
285 if ( ! strpos($extra, 'multiple'))
286 {
287 $extra .= ' multiple="multiple"';
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jones26399292009-04-08 16:14:17 +0000290 return form_dropdown($name, $options, $selected, $extra);
291 }
292}
293
294// --------------------------------------------------------------------
295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296if ( ! function_exists('form_dropdown'))
297{
Timothy Warren01b129a2012-04-27 11:36:50 -0400298 /**
299 * Drop-down Menu
300 *
301 * @param string
302 * @param array
303 * @param string
304 * @param string
305 * @return string
306 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
308 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300309 // If name is really an array then we'll call the function again using the array
310 if (is_array($name) && isset($name['name']))
311 {
312 isset($name['options']) OR $name['options'] = array();
313 isset($name['selected']) OR $name['selected'] = array();
314 isset($name['extra']) OR $name['extra'] = array();
315
316 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
317 }
318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 if ( ! is_array($selected))
320 {
321 $selected = array($selected);
322 }
323
324 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200325 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200327 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200330 if ($extra != '')
331 {
332 $extra = ' '.$extra;
333 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000334
335 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
336
337 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 foreach ($options as $key => $val)
340 {
341 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000342
Derek Jones68788d52010-03-05 10:11:31 -0600343 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000344 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200345 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000346
Derek Allard78a5fc92009-02-05 16:34:35 +0000347 foreach ($val as $optgroup_key => $optgroup_val)
348 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300349 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000350 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
351 }
352
Andrey Andreev93a83c72012-03-26 21:24:02 +0300353 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000354 }
355 else
356 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200357 $form .= '<option value="'.$key.'"'.(in_array($key, $selected) ? ' selected="selected"' : '').'>'.(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000358 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360
Andrey Andreev93a83c72012-03-26 21:24:02 +0300361 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
363}
364
365// ------------------------------------------------------------------------
366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367if ( ! function_exists('form_checkbox'))
368{
Timothy Warren01b129a2012-04-27 11:36:50 -0400369 /**
370 * Checkbox Field
371 *
372 * @param mixed
373 * @param string
374 * @param bool
375 * @param string
376 * @return string
377 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
379 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300380 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000381
Andrey Andreev93a83c72012-03-26 21:24:02 +0300382 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $checked = $data['checked'];
385
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200386 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 unset($data['checked']);
389 }
390 else
391 {
392 $data['checked'] = 'checked';
393 }
394 }
395
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200396 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 $defaults['checked'] = 'checked';
399 }
400 else
401 {
402 unset($defaults['checked']);
403 }
404
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200405 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407}
408
409// ------------------------------------------------------------------------
410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411if ( ! function_exists('form_radio'))
412{
Timothy Warren01b129a2012-04-27 11:36:50 -0400413 /**
414 * Radio Button
415 *
416 * @param mixed
417 * @param string
418 * @param bool
419 * @param string
420 * @return string
421 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
423 {
424 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200425 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 $data = array('name' => $data);
427 }
428
429 $data['type'] = 'radio';
430 return form_checkbox($data, $value, $checked, $extra);
431 }
432}
433
434// ------------------------------------------------------------------------
435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200437{
Timothy Warren01b129a2012-04-27 11:36:50 -0400438 /**
439 * Submit Button
440 *
441 * @param mixed
442 * @param string
443 * @param string
444 * @return string
445 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 function form_submit($data = '', $value = '', $extra = '')
447 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300448 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200449 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451}
452
453// ------------------------------------------------------------------------
454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455if ( ! function_exists('form_reset'))
456{
Timothy Warren01b129a2012-04-27 11:36:50 -0400457 /**
458 * Reset Button
459 *
460 * @param mixed
461 * @param string
462 * @param string
463 * @return string
464 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 function form_reset($data = '', $value = '', $extra = '')
466 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300467 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200468 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470}
471
472// ------------------------------------------------------------------------
473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474if ( ! function_exists('form_button'))
475{
Timothy Warren01b129a2012-04-27 11:36:50 -0400476 /**
477 * Form Button
478 *
479 * @param mixed
480 * @param string
481 * @param string
482 * @return string
483 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 function form_button($data = '', $content = '', $extra = '')
485 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300486 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
487 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 $content = $data['content'];
490 unset($data['content']); // content is not an attribute
491 }
492
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200493 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495}
496
497// ------------------------------------------------------------------------
498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499if ( ! function_exists('form_label'))
500{
Timothy Warren01b129a2012-04-27 11:36:50 -0400501 /**
502 * Form Label Tag
503 *
504 * @param string The text to appear onscreen
505 * @param string The id the label applies to
506 * @param string Additional attributes
507 * @return string
508 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 function form_label($label_text = '', $id = '', $attributes = array())
510 {
511
512 $label = '<label';
513
Alex Bilbie773ccc32012-06-02 11:11:08 +0100514 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300516 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518
Andrey Andreev93a83c72012-03-26 21:24:02 +0300519 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 foreach ($attributes as $key => $val)
522 {
523 $label .= ' '.$key.'="'.$val.'"';
524 }
525 }
526
Andrey Andreev93a83c72012-03-26 21:24:02 +0300527 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
529}
530
531// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533if ( ! function_exists('form_fieldset'))
534{
Timothy Warren01b129a2012-04-27 11:36:50 -0400535 /**
536 * Fieldset Tag
537 *
538 * Used to produce <fieldset><legend>text</legend>. To close fieldset
539 * use form_fieldset_close()
540 *
541 * @param string The legend text
542 * @param string Additional attributes
543 * @return string
544 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 function form_fieldset($legend_text = '', $attributes = array())
546 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200547 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100548 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300550 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552
553 return $fieldset;
554 }
555}
556
557// ------------------------------------------------------------------------
558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559if ( ! function_exists('form_fieldset_close'))
560{
Timothy Warren01b129a2012-04-27 11:36:50 -0400561 /**
562 * Fieldset Close Tag
563 *
564 * @param string
565 * @return string
566 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 function form_fieldset_close($extra = '')
568 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300569 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
571}
572
573// ------------------------------------------------------------------------
574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575if ( ! function_exists('form_close'))
576{
Timothy Warren01b129a2012-04-27 11:36:50 -0400577 /**
578 * Form Close Tag
579 *
580 * @param string
581 * @return string
582 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 function form_close($extra = '')
584 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300585 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 }
587}
588
589// ------------------------------------------------------------------------
590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591if ( ! function_exists('form_prep'))
592{
Timothy Warren01b129a2012-04-27 11:36:50 -0400593 /**
594 * Form Prep
595 *
596 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
597 *
598 * @param string
599 * @param string
600 * @return string
601 */
Derek Jones01a9b102009-07-17 18:30:36 +0000602 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Derek Jones01a9b102009-07-17 18:30:36 +0000604 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 // if the field name is an array we do this recursively
607 if (is_array($str))
608 {
609 foreach ($str as $key => $val)
610 {
611 $str[$key] = form_prep($val);
612 }
613
614 return $str;
615 }
616
617 if ($str === '')
618 {
619 return '';
620 }
621
Derek Jones3eb9fac2009-07-17 20:25:13 +0000622 // we've already prepped a field with this name
623 // @todo need to figure out a way to namespace this so
624 // that we know the *exact* field and not just one with
625 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000626 if (isset($prepped_fields[$field_name]))
627 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000628 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000629 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000630
Alex Bilbie773ccc32012-06-02 11:11:08 +0100631 if ($field_name !== '')
Derek Jones01a9b102009-07-17 18:30:36 +0000632 {
Derek Jones68788d52010-03-05 10:11:31 -0600633 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200636 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 }
638}
639
640// ------------------------------------------------------------------------
641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642if ( ! function_exists('set_value'))
643{
Timothy Warren01b129a2012-04-27 11:36:50 -0400644 /**
645 * Form Value
646 *
647 * Grabs a value from the POST array for the specified field so you can
648 * re-populate an input field or textarea. If Form Validation
649 * is active it retrieves the info from the validation class
650 *
651 * @param string
652 * @param string
653 * @return mixed
654 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 function set_value($field = '', $default = '')
656 {
657 if (FALSE === ($OBJ =& _get_validation_object()))
658 {
659 if ( ! isset($_POST[$field]))
660 {
661 return $default;
662 }
663
Derek Jones01a9b102009-07-17 18:30:36 +0000664 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
666
Derek Jones01a9b102009-07-17 18:30:36 +0000667 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 }
669}
670
671// ------------------------------------------------------------------------
672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673if ( ! function_exists('set_select'))
674{
Timothy Warren01b129a2012-04-27 11:36:50 -0400675 /**
676 * Set Select
677 *
678 * Let's you set the selected value of a <select> menu via data in the POST array.
679 * If Form Validation is active it retrieves the info from the validation class
680 *
681 * @param string
682 * @param string
683 * @param bool
684 * @return string
685 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 function set_select($field = '', $value = '', $default = FALSE)
687 {
688 $OBJ =& _get_validation_object();
689
690 if ($OBJ === FALSE)
691 {
692 if ( ! isset($_POST[$field]))
693 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100694 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 {
696 return ' selected="selected"';
697 }
698 return '';
699 }
700
701 $field = $_POST[$field];
702
703 if (is_array($field))
704 {
705 if ( ! in_array($value, $field))
706 {
707 return '';
708 }
709 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200710 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300712 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 }
714
715 return ' selected="selected"';
716 }
717
718 return $OBJ->set_select($field, $value, $default);
719 }
720}
721
722// ------------------------------------------------------------------------
723
Derek Allard2067d1a2008-11-13 22:59:24 +0000724if ( ! function_exists('set_checkbox'))
725{
Timothy Warren01b129a2012-04-27 11:36:50 -0400726 /**
727 * Set Checkbox
728 *
729 * Let's you set the selected value of a checkbox via the value in the POST array.
730 * If Form Validation is active it retrieves the info from the validation class
731 *
732 * @param string
733 * @param string
734 * @param bool
735 * @return string
736 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 function set_checkbox($field = '', $value = '', $default = FALSE)
738 {
739 $OBJ =& _get_validation_object();
740
741 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200742 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 if ( ! isset($_POST[$field]))
744 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100745 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
747 return ' checked="checked"';
748 }
749 return '';
750 }
751
752 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200753
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 if (is_array($field))
755 {
756 if ( ! in_array($value, $field))
757 {
758 return '';
759 }
760 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200761 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300763 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 }
765
766 return ' checked="checked"';
767 }
768
769 return $OBJ->set_checkbox($field, $value, $default);
770 }
771}
772
773// ------------------------------------------------------------------------
774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775if ( ! function_exists('set_radio'))
776{
Timothy Warren01b129a2012-04-27 11:36:50 -0400777 /**
778 * Set Radio
779 *
780 * Let's you set the selected value of a radio field via info in the POST array.
781 * If Form Validation is active it retrieves the info from the validation class
782 *
783 * @param string
784 * @param string
785 * @param bool
786 * @return string
787 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 function set_radio($field = '', $value = '', $default = FALSE)
789 {
790 $OBJ =& _get_validation_object();
791
792 if ($OBJ === FALSE)
793 {
794 if ( ! isset($_POST[$field]))
795 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100796 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
798 return ' checked="checked"';
799 }
800 return '';
801 }
802
803 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 if (is_array($field))
806 {
807 if ( ! in_array($value, $field))
808 {
809 return '';
810 }
811 }
812 else
813 {
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200814 if (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 {
816 return '';
817 }
818 }
819
820 return ' checked="checked"';
821 }
822
823 return $OBJ->set_radio($field, $value, $default);
824 }
825}
826
827// ------------------------------------------------------------------------
828
Timothy Warren01b129a2012-04-27 11:36:50 -0400829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830if ( ! function_exists('form_error'))
831{
Timothy Warren01b129a2012-04-27 11:36:50 -0400832 /**
833 * Form Error
834 *
835 * Returns the error for a specific form field. This is a helper for the
836 * form validation class.
837 *
838 * @param string
839 * @param string
840 * @param string
841 * @return string
842 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 function form_error($field = '', $prefix = '', $suffix = '')
844 {
845 if (FALSE === ($OBJ =& _get_validation_object()))
846 {
847 return '';
848 }
849
850 return $OBJ->error($field, $prefix, $suffix);
851 }
852}
853
854// ------------------------------------------------------------------------
855
Derek Allard2067d1a2008-11-13 22:59:24 +0000856if ( ! function_exists('validation_errors'))
857{
Timothy Warren01b129a2012-04-27 11:36:50 -0400858 /**
859 * Validation Error String
860 *
861 * Returns all the errors associated with a form submission. This is a helper
862 * function for the form validation class.
863 *
864 * @param string
865 * @param string
866 * @return string
867 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 function validation_errors($prefix = '', $suffix = '')
869 {
870 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500871 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000872 return '';
873 }
874
875 return $OBJ->error_string($prefix, $suffix);
876 }
877}
878
879// ------------------------------------------------------------------------
880
Derek Allard2067d1a2008-11-13 22:59:24 +0000881if ( ! function_exists('_parse_form_attributes'))
882{
Timothy Warren01b129a2012-04-27 11:36:50 -0400883 /**
884 * Parse the form attributes
885 *
886 * Helper function used by some of the form helpers
887 *
888 * @param array
889 * @param array
890 * @return string
891 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000892 function _parse_form_attributes($attributes, $default)
893 {
894 if (is_array($attributes))
895 {
896 foreach ($default as $key => $val)
897 {
898 if (isset($attributes[$key]))
899 {
900 $default[$key] = $attributes[$key];
901 unset($attributes[$key]);
902 }
903 }
904
905 if (count($attributes) > 0)
906 {
907 $default = array_merge($default, $attributes);
908 }
909 }
910
911 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 foreach ($default as $key => $val)
914 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100915 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 {
Derek Jones01a9b102009-07-17 18:30:36 +0000917 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000918 }
919
Andrey Andreev93a83c72012-03-26 21:24:02 +0300920 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 }
922
923 return $att;
924 }
925}
926
927// ------------------------------------------------------------------------
928
Derek Allard2067d1a2008-11-13 22:59:24 +0000929if ( ! function_exists('_attributes_to_string'))
930{
Timothy Warren01b129a2012-04-27 11:36:50 -0400931 /**
932 * Attributes To String
933 *
934 * Helper function used by some of the form helpers
935 *
936 * @param mixed
937 * @param bool
938 * @return string
939 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 function _attributes_to_string($attributes, $formtag = FALSE)
941 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300942 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100944 if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 {
946 $attributes .= ' method="post"';
947 }
948
Alex Bilbie773ccc32012-06-02 11:11:08 +0100949 if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000950 {
951 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
952 }
953
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200954 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 }
Barry Mienydd671972010-10-04 16:33:58 +0200956
Andrey Andreev93a83c72012-03-26 21:24:02 +0300957 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300959 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 }
961
Andrey Andreev93a83c72012-03-26 21:24:02 +0300962 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Derek Allard3241d732009-09-17 12:17:45 +0000964 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
Andrey Andreev93a83c72012-03-26 21:24:02 +0300966 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000967 {
968 $atts .= ' method="post"';
969 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000970
Andrey Andreev93a83c72012-03-26 21:24:02 +0300971 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000972 {
973 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
974 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000975
Derek Allard3241d732009-09-17 12:17:45 +0000976 foreach ($attributes as $key => $val)
977 {
978 $atts .= ' '.$key.'="'.$val.'"';
979 }
980
981 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 }
983 }
984}
985
986// ------------------------------------------------------------------------
987
Derek Allard2067d1a2008-11-13 22:59:24 +0000988if ( ! function_exists('_get_validation_object'))
989{
Timothy Warren01b129a2012-04-27 11:36:50 -0400990 /**
991 * Validation Object
992 *
993 * Determines what the form validation class was instantiated as, fetches
994 * the object and returns it.
995 *
996 * @return mixed
997 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 function &_get_validation_object()
999 {
1000 $CI =& get_instance();
1001
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001002 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001004
Greg Akerc6d918a2011-04-22 10:17:32 -05001005 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001007 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1008 {
1009 return $return;
1010 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001011
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001012 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001014
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001015 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016 }
1017}
1018
Derek Allard2067d1a2008-11-13 22:59:24 +00001019/* End of file form_helper.php */
Andrey Andreev93a83c72012-03-26 21:24:02 +03001020/* Location: ./system/helpers/form_helper.php */