blob: f343b6c7162b59ee0d8ee027713b0c38aaaf201b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
Andrey Andreevc5536aa2012-11-01 17:33:58 +020025 * @filesource
Derek Allard2067d1a2008-11-13 22:59:24 +000026 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Form Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('form_open'))
42{
Timothy Warren01b129a2012-04-27 11:36:50 -040043 /**
44 * Form Declaration
45 *
46 * Creates the opening portion of the form.
47 *
48 * @param string the URI segments of the form destination
49 * @param array a key/value pair of attributes
50 * @param array a key/value pair hidden data
51 * @return string
52 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053 function form_open($action = '', $attributes = '', $hidden = array())
54 {
55 $CI =& get_instance();
56
Alex Bilbie773ccc32012-06-02 11:11:08 +010057 if ($attributes === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
Derek Allard3241d732009-09-17 12:17:45 +000059 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
61
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010062 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000063 if ($action && strpos($action, '://') === FALSE)
64 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000065 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000066 }
Andrey Andreev3c32a112012-06-16 18:05:34 +030067 elseif ( ! $action)
68 {
69 // If no action is provided then set to the current url
70 $action = $CI->config->site_url($CI->uri->uri_string());
71 }
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010072
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020073 $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n";
Barry Mienydd671972010-10-04 16:33:58 +020074
Andrey Andreev93a83c72012-03-26 21:24:02 +030075 // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
76 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 -040077 {
Greg Aker1f6f0ab2011-04-07 18:24:53 -050078 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
Greg Aker28b425a2010-09-15 11:43:23 -050079 }
80
Andrey Andreev93a83c72012-03-26 21:24:02 +030081 if (is_array($hidden) && count($hidden) > 0)
Greg Aker28b425a2010-09-15 11:43:23 -050082 {
Andrey Andreev3c32a112012-06-16 18:05:34 +030083 $form .= '<div style="display:none;">'.form_hidden($hidden).'</div>';
Derek Allard958543a2010-07-22 14:10:26 -040084 }
85
Derek Allard2067d1a2008-11-13 22:59:24 +000086 return $form;
87 }
88}
89
90// ------------------------------------------------------------------------
91
Derek Allard2067d1a2008-11-13 22:59:24 +000092if ( ! function_exists('form_open_multipart'))
93{
Timothy Warren01b129a2012-04-27 11:36:50 -040094 /**
95 * Form Declaration - Multipart type
96 *
97 * Creates the opening portion of the form, but with "multipart/form-data".
98 *
99 * @param string the URI segments of the form destination
100 * @param array a key/value pair of attributes
101 * @param array a key/value pair hidden data
102 * @return string
103 */
Ben Edmunds98963b32011-08-20 14:17:16 -0500104 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000106 if (is_string($attributes))
107 {
108 $attributes .= ' enctype="multipart/form-data"';
109 }
110 else
111 {
112 $attributes['enctype'] = 'multipart/form-data';
113 }
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 return form_open($action, $attributes, $hidden);
116 }
117}
118
119// ------------------------------------------------------------------------
120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121if ( ! function_exists('form_hidden'))
122{
Timothy Warren01b129a2012-04-27 11:36:50 -0400123 /**
124 * Hidden Input Field
125 *
126 * Generates hidden fields. You can pass a simple key/value string or
127 * an associative array with multiple values.
128 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200129 * @param mixed $name Field name
130 * @param string $value Field value
131 * @param bool $recursing
Timothy Warren01b129a2012-04-27 11:36:50 -0400132 * @return string
133 */
Robin Sowell57fe4102009-03-26 18:58:46 +0000134 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000136 static $form;
137
138 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000140 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
Robin Sowell57fe4102009-03-26 18:58:46 +0000143 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000145 foreach ($name as $key => $val)
146 {
147 form_hidden($key, $val, TRUE);
148 }
149 return $form;
150 }
151
152 if ( ! is_array($value))
153 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200154 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000155 }
156 else
157 {
158 foreach ($value as $k => $v)
159 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300160 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000161 form_hidden($name.'['.$k.']', $v, TRUE);
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 return $form;
166 }
167}
168
169// ------------------------------------------------------------------------
170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171if ( ! function_exists('form_input'))
172{
Timothy Warren01b129a2012-04-27 11:36:50 -0400173 /**
174 * Text Input Field
175 *
176 * @param mixed
177 * @param string
178 * @param string
179 * @return string
180 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 function form_input($data = '', $value = '', $extra = '')
182 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300183 $defaults = array('type' => 'text', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000184
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200185 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
187}
188
189// ------------------------------------------------------------------------
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191if ( ! function_exists('form_password'))
192{
Timothy Warren01b129a2012-04-27 11:36:50 -0400193 /**
194 * Password Field
195 *
196 * Identical to the input function but adds the "password" type
197 *
198 * @param mixed
199 * @param string
200 * @param string
201 * @return string
202 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 function form_password($data = '', $value = '', $extra = '')
204 {
205 if ( ! is_array($data))
206 {
207 $data = array('name' => $data);
208 }
209
210 $data['type'] = 'password';
211 return form_input($data, $value, $extra);
212 }
213}
214
215// ------------------------------------------------------------------------
216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217if ( ! function_exists('form_upload'))
218{
Timothy Warren01b129a2012-04-27 11:36:50 -0400219 /**
220 * Upload Field
221 *
222 * Identical to the input function but adds the "file" type
223 *
224 * @param mixed
225 * @param string
226 * @param string
227 * @return string
228 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 function form_upload($data = '', $value = '', $extra = '')
230 {
231 if ( ! is_array($data))
232 {
233 $data = array('name' => $data);
234 }
235
236 $data['type'] = 'file';
237 return form_input($data, $value, $extra);
238 }
239}
240
241// ------------------------------------------------------------------------
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243if ( ! function_exists('form_textarea'))
244{
Timothy Warren01b129a2012-04-27 11:36:50 -0400245 /**
246 * Textarea field
247 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200248 * @param mixed $data
249 * @param string $value
250 * @param string $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400251 * @return string
252 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 function form_textarea($data = '', $value = '', $extra = '')
254 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300255 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000256
257 if ( ! is_array($data) OR ! isset($data['value']))
258 {
259 $val = $value;
260 }
261 else
262 {
Barry Mienydd671972010-10-04 16:33:58 +0200263 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 unset($data['value']); // textareas don't use the value attribute
265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Andrey Andreev93a83c72012-03-26 21:24:02 +0300267 $name = is_array($data) ? $data['name'] : $data;
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200268 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, TRUE)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
270}
271
272// ------------------------------------------------------------------------
273
Derek Jones68788d52010-03-05 10:11:31 -0600274if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000275{
Timothy Warren01b129a2012-04-27 11:36:50 -0400276 /**
277 * Multi-select menu
278 *
279 * @param string
280 * @param array
281 * @param mixed
282 * @param string
283 * @return string
284 */
Derek Jones26399292009-04-08 16:14:17 +0000285 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
286 {
287 if ( ! strpos($extra, 'multiple'))
288 {
289 $extra .= ' multiple="multiple"';
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Jones26399292009-04-08 16:14:17 +0000292 return form_dropdown($name, $options, $selected, $extra);
293 }
294}
295
296// --------------------------------------------------------------------
297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298if ( ! function_exists('form_dropdown'))
299{
Timothy Warren01b129a2012-04-27 11:36:50 -0400300 /**
301 * Drop-down Menu
302 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200303 * @param mixed $name
304 * @param mixed $options
305 * @param mixed $selected
306 * @param mixed $extra
Timothy Warren01b129a2012-04-27 11:36:50 -0400307 * @return string
308 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
310 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300311 // If name is really an array then we'll call the function again using the array
312 if (is_array($name) && isset($name['name']))
313 {
314 isset($name['options']) OR $name['options'] = array();
315 isset($name['selected']) OR $name['selected'] = array();
316 isset($name['extra']) OR $name['extra'] = array();
317
318 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
319 }
320
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300321 is_array($selected) OR $selected = array($selected);
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
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
Michiel Vugteveen9640a032012-06-06 20:20:27 +0200329 if ($extra != '')
330 {
331 $extra = ' '.$extra;
332 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
334 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
335
336 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 foreach ($options as $key => $val)
339 {
340 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000341
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300342 if (is_array($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000343 {
Andrey Andreev6b114ae2012-07-13 12:05:52 +0300344 if (empty($val))
345 {
346 continue;
347 }
348
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200349 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
Derek Allard78a5fc92009-02-05 16:34:35 +0000351 foreach ($val as $optgroup_key => $optgroup_val)
352 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300353 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200354 $form .= '<option value="'.form_prep($optgroup_key).'"'.$sel.'>'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300355 .(string) $optgroup_val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000356 }
357
Andrey Andreev93a83c72012-03-26 21:24:02 +0300358 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000359 }
360 else
361 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200362 $form .= '<option value="'.form_prep($key).'"'
Andrey Andreev582ebcb2012-10-27 00:52:15 +0300363 .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
364 .(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000365 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367
Andrey Andreev93a83c72012-03-26 21:24:02 +0300368 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370}
371
372// ------------------------------------------------------------------------
373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374if ( ! function_exists('form_checkbox'))
375{
Timothy Warren01b129a2012-04-27 11:36:50 -0400376 /**
377 * Checkbox Field
378 *
379 * @param mixed
380 * @param string
381 * @param bool
382 * @param string
383 * @return string
384 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
386 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300387 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000388
Andrey Andreev93a83c72012-03-26 21:24:02 +0300389 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
391 $checked = $data['checked'];
392
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200393 if ($checked == FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
395 unset($data['checked']);
396 }
397 else
398 {
399 $data['checked'] = 'checked';
400 }
401 }
402
Michiel Vugteveen910ff7a2012-06-06 20:03:14 +0200403 if ($checked == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 $defaults['checked'] = 'checked';
406 }
407 else
408 {
409 unset($defaults['checked']);
410 }
411
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200412 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414}
415
416// ------------------------------------------------------------------------
417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418if ( ! function_exists('form_radio'))
419{
Timothy Warren01b129a2012-04-27 11:36:50 -0400420 /**
421 * Radio Button
422 *
423 * @param mixed
424 * @param string
425 * @param bool
426 * @param string
427 * @return string
428 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
430 {
431 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200432 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 $data = array('name' => $data);
434 }
435
436 $data['type'] = 'radio';
437 return form_checkbox($data, $value, $checked, $extra);
438 }
439}
440
441// ------------------------------------------------------------------------
442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200444{
Timothy Warren01b129a2012-04-27 11:36:50 -0400445 /**
446 * Submit Button
447 *
448 * @param mixed
449 * @param string
450 * @param string
451 * @return string
452 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 function form_submit($data = '', $value = '', $extra = '')
454 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300455 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200456 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458}
459
460// ------------------------------------------------------------------------
461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462if ( ! function_exists('form_reset'))
463{
Timothy Warren01b129a2012-04-27 11:36:50 -0400464 /**
465 * Reset Button
466 *
467 * @param mixed
468 * @param string
469 * @param string
470 * @return string
471 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 function form_reset($data = '', $value = '', $extra = '')
473 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300474 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200475 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477}
478
479// ------------------------------------------------------------------------
480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481if ( ! function_exists('form_button'))
482{
Timothy Warren01b129a2012-04-27 11:36:50 -0400483 /**
484 * Form Button
485 *
486 * @param mixed
487 * @param string
488 * @param string
489 * @return string
490 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 function form_button($data = '', $content = '', $extra = '')
492 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300493 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
494 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 $content = $data['content'];
497 unset($data['content']); // content is not an attribute
498 }
499
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200500 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502}
503
504// ------------------------------------------------------------------------
505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506if ( ! function_exists('form_label'))
507{
Timothy Warren01b129a2012-04-27 11:36:50 -0400508 /**
509 * Form Label Tag
510 *
511 * @param string The text to appear onscreen
512 * @param string The id the label applies to
513 * @param string Additional attributes
514 * @return string
515 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 function form_label($label_text = '', $id = '', $attributes = array())
517 {
518
519 $label = '<label';
520
Alex Bilbie773ccc32012-06-02 11:11:08 +0100521 if ($id !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300523 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525
Andrey Andreev93a83c72012-03-26 21:24:02 +0300526 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 foreach ($attributes as $key => $val)
529 {
530 $label .= ' '.$key.'="'.$val.'"';
531 }
532 }
533
Andrey Andreev93a83c72012-03-26 21:24:02 +0300534 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 }
536}
537
538// ------------------------------------------------------------------------
Timothy Warren01b129a2012-04-27 11:36:50 -0400539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540if ( ! function_exists('form_fieldset'))
541{
Timothy Warren01b129a2012-04-27 11:36:50 -0400542 /**
543 * Fieldset Tag
544 *
545 * Used to produce <fieldset><legend>text</legend>. To close fieldset
546 * use form_fieldset_close()
547 *
548 * @param string The legend text
549 * @param string Additional attributes
550 * @return string
551 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 function form_fieldset($legend_text = '', $attributes = array())
553 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200554 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Alex Bilbie773ccc32012-06-02 11:11:08 +0100555 if ($legend_text !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300557 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 }
559
560 return $fieldset;
561 }
562}
563
564// ------------------------------------------------------------------------
565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566if ( ! function_exists('form_fieldset_close'))
567{
Timothy Warren01b129a2012-04-27 11:36:50 -0400568 /**
569 * Fieldset Close Tag
570 *
571 * @param string
572 * @return string
573 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 function form_fieldset_close($extra = '')
575 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300576 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 }
578}
579
580// ------------------------------------------------------------------------
581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582if ( ! function_exists('form_close'))
583{
Timothy Warren01b129a2012-04-27 11:36:50 -0400584 /**
585 * Form Close Tag
586 *
587 * @param string
588 * @return string
589 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 function form_close($extra = '')
591 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300592 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594}
595
596// ------------------------------------------------------------------------
597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598if ( ! function_exists('form_prep'))
599{
Timothy Warren01b129a2012-04-27 11:36:50 -0400600 /**
601 * Form Prep
602 *
603 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
604 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200605 * @param string|string[] $str Value to escape
606 * @param bool $is_textarea Whether we're escaping for a textarea element
607 * @return string|string[] Escaped values
Timothy Warren01b129a2012-04-27 11:36:50 -0400608 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200609 function form_prep($str = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200611 if (is_array($str))
612 {
613 foreach (array_keys($str) as $key)
614 {
615 $str[$key] = form_prep($str[$key], $is_textarea);
616 }
617
618 return $str;
619 }
620
621 if ($is_textarea === TRUE)
622 {
623 return str_replace(array('<', '>'), array('&lt;', '&gt;'), stripslashes($str));
624 }
625
Andrey Andreev075f6fa2012-11-01 15:18:44 +0200626 return str_replace(array("'", '"'), array('&#39;', '&quot;'), stripslashes($str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
628}
629
630// ------------------------------------------------------------------------
631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632if ( ! function_exists('set_value'))
633{
Timothy Warren01b129a2012-04-27 11:36:50 -0400634 /**
635 * Form Value
636 *
637 * Grabs a value from the POST array for the specified field so you can
638 * re-populate an input field or textarea. If Form Validation
639 * is active it retrieves the info from the validation class
640 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200641 * @param string $field Field name
642 * @param string $default Default value
643 * @param bool $is_textarea Whether the field is a textarea element
644 * @return string
Timothy Warren01b129a2012-04-27 11:36:50 -0400645 */
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200646 function set_value($field = '', $default = '', $is_textarea = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
648 if (FALSE === ($OBJ =& _get_validation_object()))
649 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200650 return isset($_POST[$field])
651 ? form_prep($_POST[$field], $is_textarea)
652 : form_prep($default, $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 }
654
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200655 return form_prep($OBJ->set_value($field, $default), $is_textarea);
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 }
657}
658
659// ------------------------------------------------------------------------
660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661if ( ! function_exists('set_select'))
662{
Timothy Warren01b129a2012-04-27 11:36:50 -0400663 /**
664 * Set Select
665 *
666 * Let's you set the selected value of a <select> menu via data in the POST array.
667 * If Form Validation is active it retrieves the info from the validation class
668 *
669 * @param string
670 * @param string
671 * @param bool
672 * @return string
673 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 function set_select($field = '', $value = '', $default = FALSE)
675 {
676 $OBJ =& _get_validation_object();
677
678 if ($OBJ === FALSE)
679 {
680 if ( ! isset($_POST[$field]))
681 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100682 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 return ' selected="selected"';
685 }
686 return '';
687 }
688
689 $field = $_POST[$field];
690
691 if (is_array($field))
692 {
693 if ( ! in_array($value, $field))
694 {
695 return '';
696 }
697 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200698 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300700 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 }
702
703 return ' selected="selected"';
704 }
705
706 return $OBJ->set_select($field, $value, $default);
707 }
708}
709
710// ------------------------------------------------------------------------
711
Derek Allard2067d1a2008-11-13 22:59:24 +0000712if ( ! function_exists('set_checkbox'))
713{
Timothy Warren01b129a2012-04-27 11:36:50 -0400714 /**
715 * Set Checkbox
716 *
717 * Let's you set the selected value of a checkbox via the value in the POST array.
718 * If Form Validation is active it retrieves the info from the validation class
719 *
720 * @param string
721 * @param string
722 * @param bool
723 * @return string
724 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000725 function set_checkbox($field = '', $value = '', $default = FALSE)
726 {
727 $OBJ =& _get_validation_object();
728
729 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200730 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 if ( ! isset($_POST[$field]))
732 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100733 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 {
735 return ' checked="checked"';
736 }
737 return '';
738 }
739
740 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200741
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 if (is_array($field))
743 {
744 if ( ! in_array($value, $field))
745 {
746 return '';
747 }
748 }
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200749 elseif (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000750 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300751 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000752 }
753
754 return ' checked="checked"';
755 }
756
757 return $OBJ->set_checkbox($field, $value, $default);
758 }
759}
760
761// ------------------------------------------------------------------------
762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763if ( ! function_exists('set_radio'))
764{
Timothy Warren01b129a2012-04-27 11:36:50 -0400765 /**
766 * Set Radio
767 *
768 * Let's you set the selected value of a radio field via info in the POST array.
769 * If Form Validation is active it retrieves the info from the validation class
770 *
771 * @param string
772 * @param string
773 * @param bool
774 * @return string
775 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 function set_radio($field = '', $value = '', $default = FALSE)
777 {
778 $OBJ =& _get_validation_object();
779
780 if ($OBJ === FALSE)
781 {
782 if ( ! isset($_POST[$field]))
783 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100784 if (count($_POST) === 0 && $default === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 {
786 return ' checked="checked"';
787 }
788 return '';
789 }
790
791 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200792
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 if (is_array($field))
794 {
795 if ( ! in_array($value, $field))
796 {
797 return '';
798 }
799 }
800 else
801 {
Michiel Vugteveena6f34232012-06-07 10:14:29 +0200802 if (($field == '' OR $value == '') OR $field !== $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 {
804 return '';
805 }
806 }
807
808 return ' checked="checked"';
809 }
810
811 return $OBJ->set_radio($field, $value, $default);
812 }
813}
814
815// ------------------------------------------------------------------------
816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817if ( ! function_exists('form_error'))
818{
Timothy Warren01b129a2012-04-27 11:36:50 -0400819 /**
820 * Form Error
821 *
822 * Returns the error for a specific form field. This is a helper for the
823 * form validation class.
824 *
825 * @param string
826 * @param string
827 * @param string
828 * @return string
829 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 function form_error($field = '', $prefix = '', $suffix = '')
831 {
832 if (FALSE === ($OBJ =& _get_validation_object()))
833 {
834 return '';
835 }
836
837 return $OBJ->error($field, $prefix, $suffix);
838 }
839}
840
841// ------------------------------------------------------------------------
842
Derek Allard2067d1a2008-11-13 22:59:24 +0000843if ( ! function_exists('validation_errors'))
844{
Timothy Warren01b129a2012-04-27 11:36:50 -0400845 /**
846 * Validation Error String
847 *
848 * Returns all the errors associated with a form submission. This is a helper
849 * function for the form validation class.
850 *
851 * @param string
852 * @param string
853 * @return string
854 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000855 function validation_errors($prefix = '', $suffix = '')
856 {
857 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500858 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 return '';
860 }
861
862 return $OBJ->error_string($prefix, $suffix);
863 }
864}
865
866// ------------------------------------------------------------------------
867
Derek Allard2067d1a2008-11-13 22:59:24 +0000868if ( ! function_exists('_parse_form_attributes'))
869{
Timothy Warren01b129a2012-04-27 11:36:50 -0400870 /**
871 * Parse the form attributes
872 *
873 * Helper function used by some of the form helpers
874 *
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200875 * @param array $attributes List of attributes
876 * @param array $default Default values
Timothy Warren01b129a2012-04-27 11:36:50 -0400877 * @return string
878 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000879 function _parse_form_attributes($attributes, $default)
880 {
881 if (is_array($attributes))
882 {
883 foreach ($default as $key => $val)
884 {
885 if (isset($attributes[$key]))
886 {
887 $default[$key] = $attributes[$key];
888 unset($attributes[$key]);
889 }
890 }
891
892 if (count($attributes) > 0)
893 {
894 $default = array_merge($default, $attributes);
895 }
896 }
897
898 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200899
Derek Allard2067d1a2008-11-13 22:59:24 +0000900 foreach ($default as $key => $val)
901 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100902 if ($key === 'value')
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200904 $val = form_prep($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 }
Andrey Andreev60b97142012-10-25 16:59:17 +0300906 elseif ($key === 'name' && ! strlen($default['name']))
907 {
908 continue;
909 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000910
Andrey Andreev93a83c72012-03-26 21:24:02 +0300911 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 }
913
914 return $att;
915 }
916}
917
918// ------------------------------------------------------------------------
919
Derek Allard2067d1a2008-11-13 22:59:24 +0000920if ( ! function_exists('_attributes_to_string'))
921{
Timothy Warren01b129a2012-04-27 11:36:50 -0400922 /**
923 * Attributes To String
924 *
925 * Helper function used by some of the form helpers
926 *
927 * @param mixed
928 * @param bool
929 * @return string
930 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000931 function _attributes_to_string($attributes, $formtag = FALSE)
932 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300933 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100935 if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
937 $attributes .= ' method="post"';
938 }
939
Alex Bilbie773ccc32012-06-02 11:11:08 +0100940 if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000941 {
942 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
943 }
944
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200945 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Andrey Andreev93a83c72012-03-26 21:24:02 +0300948 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300950 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 }
952
Andrey Andreev93a83c72012-03-26 21:24:02 +0300953 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000954 {
Derek Allard3241d732009-09-17 12:17:45 +0000955 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000956
Andrey Andreev93a83c72012-03-26 21:24:02 +0300957 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000958 {
959 $atts .= ' method="post"';
960 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000961
Andrey Andreev93a83c72012-03-26 21:24:02 +0300962 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000963 {
964 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
965 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000966
Derek Allard3241d732009-09-17 12:17:45 +0000967 foreach ($attributes as $key => $val)
968 {
969 $atts .= ' '.$key.'="'.$val.'"';
970 }
971
972 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 }
974 }
975}
976
977// ------------------------------------------------------------------------
978
Derek Allard2067d1a2008-11-13 22:59:24 +0000979if ( ! function_exists('_get_validation_object'))
980{
Timothy Warren01b129a2012-04-27 11:36:50 -0400981 /**
982 * Validation Object
983 *
984 * Determines what the form validation class was instantiated as, fetches
985 * the object and returns it.
986 *
987 * @return mixed
988 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000989 function &_get_validation_object()
990 {
991 $CI =& get_instance();
992
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500993 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200995
Greg Akerc6d918a2011-04-22 10:17:32 -0500996 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500998 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
999 {
1000 return $return;
1001 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001002
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001003 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001004 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001005
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001006 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 }
1008}
1009
Derek Allard2067d1a2008-11-13 22:59:24 +00001010/* End of file form_helper.php */
Andrey Andreev93a83c72012-03-26 21:24:02 +03001011/* Location: ./system/helpers/form_helper.php */