blob: ada822860c2e5f1c9753f4059011c0edcfcf72ac [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
39/**
40 * Form Declaration
41 *
42 * Creates the opening portion of the form.
43 *
Derek Allard2067d1a2008-11-13 22:59:24 +000044 * @param string the URI segments of the form destination
45 * @param array a key/value pair of attributes
46 * @param array a key/value pair hidden data
47 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020048 */
Derek Allard2067d1a2008-11-13 22:59:24 +000049if ( ! function_exists('form_open'))
50{
51 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
88/**
89 * Form Declaration - Multipart type
90 *
91 * Creates the opening portion of the form, but with "multipart/form-data".
92 *
Derek Allard2067d1a2008-11-13 22:59:24 +000093 * @param string the URI segments of the form destination
94 * @param array a key/value pair of attributes
95 * @param array a key/value pair hidden data
96 * @return string
97 */
98if ( ! function_exists('form_open_multipart'))
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
117/**
118 * Hidden Input Field
119 *
Andrey Andreev93a83c72012-03-26 21:24:02 +0300120 * Generates hidden fields. You can pass a simple key/value string or
121 * an associative array with multiple values.
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param mixed
124 * @param string
125 * @return string
126 */
127if ( ! function_exists('form_hidden'))
128{
Robin Sowell57fe4102009-03-26 18:58:46 +0000129 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000131 static $form;
132
133 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000135 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
137
Robin Sowell57fe4102009-03-26 18:58:46 +0000138 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000140 foreach ($name as $key => $val)
141 {
142 form_hidden($key, $val, TRUE);
143 }
144 return $form;
145 }
146
147 if ( ! is_array($value))
148 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200149 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000150 }
151 else
152 {
153 foreach ($value as $k => $v)
154 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300155 $k = is_int($k) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000156 form_hidden($name.'['.$k.']', $v, TRUE);
157 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 }
159
160 return $form;
161 }
162}
163
164// ------------------------------------------------------------------------
165
166/**
167 * Text Input Field
168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @param mixed
170 * @param string
171 * @param string
172 * @return string
173 */
174if ( ! function_exists('form_input'))
175{
176 function form_input($data = '', $value = '', $extra = '')
177 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300178 $defaults = array('type' => 'text', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200180 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182}
183
184// ------------------------------------------------------------------------
185
186/**
187 * Password Field
188 *
189 * Identical to the input function but adds the "password" type
190 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @param mixed
192 * @param string
193 * @param string
194 * @return string
195 */
196if ( ! function_exists('form_password'))
197{
198 function form_password($data = '', $value = '', $extra = '')
199 {
200 if ( ! is_array($data))
201 {
202 $data = array('name' => $data);
203 }
204
205 $data['type'] = 'password';
206 return form_input($data, $value, $extra);
207 }
208}
209
210// ------------------------------------------------------------------------
211
212/**
213 * Upload Field
214 *
215 * Identical to the input function but adds the "file" type
216 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @param mixed
218 * @param string
219 * @param string
220 * @return string
221 */
222if ( ! function_exists('form_upload'))
223{
224 function form_upload($data = '', $value = '', $extra = '')
225 {
226 if ( ! is_array($data))
227 {
228 $data = array('name' => $data);
229 }
230
231 $data['type'] = 'file';
232 return form_input($data, $value, $extra);
233 }
234}
235
236// ------------------------------------------------------------------------
237
238/**
239 * Textarea field
240 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * @param mixed
242 * @param string
243 * @param string
244 * @return string
245 */
246if ( ! function_exists('form_textarea'))
247{
248 function form_textarea($data = '', $value = '', $extra = '')
249 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300250 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000251
252 if ( ! is_array($data) OR ! isset($data['value']))
253 {
254 $val = $value;
255 }
256 else
257 {
Barry Mienydd671972010-10-04 16:33:58 +0200258 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 unset($data['value']); // textareas don't use the value attribute
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Andrey Andreev93a83c72012-03-26 21:24:02 +0300262 $name = is_array($data) ? $data['name'] : $data;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200263 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, $name)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265}
266
267// ------------------------------------------------------------------------
268
269/**
Derek Jones26399292009-04-08 16:14:17 +0000270 * Multi-select menu
271 *
Derek Jones26399292009-04-08 16:14:17 +0000272 * @param string
273 * @param array
274 * @param mixed
275 * @param string
Andrey Andreev93a83c72012-03-26 21:24:02 +0300276 * @return string
Derek Jones26399292009-04-08 16:14:17 +0000277 */
Derek Jones68788d52010-03-05 10:11:31 -0600278if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000279{
280 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
281 {
282 if ( ! strpos($extra, 'multiple'))
283 {
284 $extra .= ' multiple="multiple"';
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Jones26399292009-04-08 16:14:17 +0000287 return form_dropdown($name, $options, $selected, $extra);
288 }
289}
290
291// --------------------------------------------------------------------
292
293/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * Drop-down Menu
295 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @param string
297 * @param array
298 * @param string
299 * @param string
300 * @return string
301 */
302if ( ! function_exists('form_dropdown'))
303{
304 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
305 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300306 // If name is really an array then we'll call the function again using the array
307 if (is_array($name) && isset($name['name']))
308 {
309 isset($name['options']) OR $name['options'] = array();
310 isset($name['selected']) OR $name['selected'] = array();
311 isset($name['extra']) OR $name['extra'] = array();
312
313 return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']);
314 }
315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 if ( ! is_array($selected))
317 {
318 $selected = array($selected);
319 }
320
321 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200322 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200324 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326
327 if ($extra != '') $extra = ' '.$extra;
328
329 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
330
331 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 foreach ($options as $key => $val)
334 {
335 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
Derek Jones68788d52010-03-05 10:11:31 -0600337 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000338 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200339 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000340
Derek Allard78a5fc92009-02-05 16:34:35 +0000341 foreach ($val as $optgroup_key => $optgroup_val)
342 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300343 $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000344 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
345 }
346
Andrey Andreev93a83c72012-03-26 21:24:02 +0300347 $form .= "</optgroup>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000348 }
349 else
350 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200351 $form .= '<option value="'.$key.'"'.(in_array($key, $selected) ? ' selected="selected"' : '').'>'.(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000352 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
Andrey Andreev93a83c72012-03-26 21:24:02 +0300355 return $form."</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357}
358
359// ------------------------------------------------------------------------
360
361/**
362 * Checkbox Field
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @param mixed
365 * @param string
366 * @param bool
367 * @param string
368 * @return string
369 */
370if ( ! function_exists('form_checkbox'))
371{
372 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
373 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300374 $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Derek Allard2067d1a2008-11-13 22:59:24 +0000375
Andrey Andreev93a83c72012-03-26 21:24:02 +0300376 if (is_array($data) && array_key_exists('checked', $data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $checked = $data['checked'];
379
380 if ($checked == FALSE)
381 {
382 unset($data['checked']);
383 }
384 else
385 {
386 $data['checked'] = 'checked';
387 }
388 }
389
390 if ($checked == TRUE)
391 {
392 $defaults['checked'] = 'checked';
393 }
394 else
395 {
396 unset($defaults['checked']);
397 }
398
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200399 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401}
402
403// ------------------------------------------------------------------------
404
405/**
406 * Radio Button
407 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 * @param mixed
409 * @param string
410 * @param bool
411 * @param string
412 * @return string
413 */
414if ( ! function_exists('form_radio'))
415{
416 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
417 {
418 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200419 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 $data = array('name' => $data);
421 }
422
423 $data['type'] = 'radio';
424 return form_checkbox($data, $value, $checked, $extra);
425 }
426}
427
428// ------------------------------------------------------------------------
429
430/**
431 * Submit Button
432 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 * @param mixed
434 * @param string
435 * @param string
436 * @return string
437 */
438if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200439{
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 function form_submit($data = '', $value = '', $extra = '')
441 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300442 $defaults = array('type' => 'submit', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200443 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445}
446
447// ------------------------------------------------------------------------
448
449/**
450 * Reset Button
451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @param mixed
453 * @param string
454 * @param string
455 * @return string
456 */
457if ( ! function_exists('form_reset'))
458{
459 function form_reset($data = '', $value = '', $extra = '')
460 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300461 $defaults = array('type' => 'reset', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200462 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464}
465
466// ------------------------------------------------------------------------
467
468/**
469 * Form Button
470 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @param mixed
472 * @param string
473 * @param string
474 * @return string
475 */
476if ( ! function_exists('form_button'))
477{
478 function form_button($data = '', $content = '', $extra = '')
479 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300480 $defaults = array('name' => ( ! is_array($data) ? $data : ''), 'type' => 'button');
481 if (is_array($data) && isset($data['content']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 $content = $data['content'];
484 unset($data['content']); // content is not an attribute
485 }
486
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200487 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489}
490
491// ------------------------------------------------------------------------
492
493/**
494 * Form Label Tag
495 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @param string The text to appear onscreen
497 * @param string The id the label applies to
498 * @param string Additional attributes
499 * @return string
500 */
501if ( ! function_exists('form_label'))
502{
503 function form_label($label_text = '', $id = '', $attributes = array())
504 {
505
506 $label = '<label';
507
508 if ($id != '')
509 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300510 $label .= ' for="'.$id.'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
512
Andrey Andreev93a83c72012-03-26 21:24:02 +0300513 if (is_array($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
515 foreach ($attributes as $key => $val)
516 {
517 $label .= ' '.$key.'="'.$val.'"';
518 }
519 }
520
Andrey Andreev93a83c72012-03-26 21:24:02 +0300521 return $label.'>'.$label_text.'</label>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
523}
524
525// ------------------------------------------------------------------------
526/**
527 * Fieldset Tag
528 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500529 * Used to produce <fieldset><legend>text</legend>. To close fieldset
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * use form_fieldset_close()
531 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @param string The legend text
533 * @param string Additional attributes
534 * @return string
535 */
536if ( ! function_exists('form_fieldset'))
537{
538 function form_fieldset($legend_text = '', $attributes = array())
539 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200540 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 if ($legend_text != '')
542 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300543 return $fieldset.'<legend>'.$legend_text."</legend>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
545
546 return $fieldset;
547 }
548}
549
550// ------------------------------------------------------------------------
551
552/**
553 * Fieldset Close Tag
554 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 * @param string
556 * @return string
557 */
558if ( ! function_exists('form_fieldset_close'))
559{
560 function form_fieldset_close($extra = '')
561 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300562 return '</fieldset>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
564}
565
566// ------------------------------------------------------------------------
567
568/**
569 * Form Close Tag
570 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @param string
572 * @return string
573 */
574if ( ! function_exists('form_close'))
575{
576 function form_close($extra = '')
577 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300578 return '</form>'.$extra;
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 }
580}
581
582// ------------------------------------------------------------------------
583
584/**
585 * Form Prep
586 *
587 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
588 *
589 * @access public
590 * @param string
591 * @return string
592 */
593if ( ! function_exists('form_prep'))
594{
Derek Jones01a9b102009-07-17 18:30:36 +0000595 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 {
Derek Jones01a9b102009-07-17 18:30:36 +0000597 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // if the field name is an array we do this recursively
600 if (is_array($str))
601 {
602 foreach ($str as $key => $val)
603 {
604 $str[$key] = form_prep($val);
605 }
606
607 return $str;
608 }
609
610 if ($str === '')
611 {
612 return '';
613 }
614
Derek Jones3eb9fac2009-07-17 20:25:13 +0000615 // we've already prepped a field with this name
616 // @todo need to figure out a way to namespace this so
617 // that we know the *exact* field and not just one with
618 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000619 if (isset($prepped_fields[$field_name]))
620 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000621 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000622 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000623
Derek Jones01a9b102009-07-17 18:30:36 +0000624 if ($field_name != '')
625 {
Derek Jones68788d52010-03-05 10:11:31 -0600626 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200629 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
631}
632
633// ------------------------------------------------------------------------
634
635/**
636 * Form Value
637 *
638 * Grabs a value from the POST array for the specified field so you can
Andrey Andreev93a83c72012-03-26 21:24:02 +0300639 * re-populate an input field or textarea. If Form Validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * is active it retrieves the info from the validation class
641 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @param string
643 * @return mixed
644 */
645if ( ! function_exists('set_value'))
646{
647 function set_value($field = '', $default = '')
648 {
649 if (FALSE === ($OBJ =& _get_validation_object()))
650 {
651 if ( ! isset($_POST[$field]))
652 {
653 return $default;
654 }
655
Derek Jones01a9b102009-07-17 18:30:36 +0000656 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
658
Derek Jones01a9b102009-07-17 18:30:36 +0000659 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661}
662
663// ------------------------------------------------------------------------
664
665/**
666 * Set Select
667 *
668 * Let's you set the selected value of a <select> menu via data in the POST array.
669 * If Form Validation is active it retrieves the info from the validation class
670 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @param string
672 * @param string
673 * @param bool
674 * @return string
675 */
676if ( ! function_exists('set_select'))
677{
678 function set_select($field = '', $value = '', $default = FALSE)
679 {
680 $OBJ =& _get_validation_object();
681
682 if ($OBJ === FALSE)
683 {
684 if ( ! isset($_POST[$field]))
685 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300686 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 return ' selected="selected"';
689 }
690 return '';
691 }
692
693 $field = $_POST[$field];
694
695 if (is_array($field))
696 {
697 if ( ! in_array($value, $field))
698 {
699 return '';
700 }
701 }
Andrey Andreev93a83c72012-03-26 21:24:02 +0300702 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000703 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300704 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000705 }
706
707 return ' selected="selected"';
708 }
709
710 return $OBJ->set_select($field, $value, $default);
711 }
712}
713
714// ------------------------------------------------------------------------
715
716/**
717 * Set Checkbox
718 *
719 * Let's you set the selected value of a checkbox via the value in the POST array.
720 * If Form Validation is active it retrieves the info from the validation class
721 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 * @param string
723 * @param string
724 * @param bool
725 * @return string
726 */
727if ( ! function_exists('set_checkbox'))
728{
729 function set_checkbox($field = '', $value = '', $default = FALSE)
730 {
731 $OBJ =& _get_validation_object();
732
733 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200734 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 if ( ! isset($_POST[$field]))
736 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300737 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 {
739 return ' checked="checked"';
740 }
741 return '';
742 }
743
744 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200745
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 if (is_array($field))
747 {
748 if ( ! in_array($value, $field))
749 {
750 return '';
751 }
752 }
Andrey Andreev93a83c72012-03-26 21:24:02 +0300753 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300755 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 }
757
758 return ' checked="checked"';
759 }
760
761 return $OBJ->set_checkbox($field, $value, $default);
762 }
763}
764
765// ------------------------------------------------------------------------
766
767/**
768 * Set Radio
769 *
770 * Let's you set the selected value of a radio field via info in the POST array.
771 * If Form Validation is active it retrieves the info from the validation class
772 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 * @param string
774 * @param string
775 * @param bool
776 * @return string
777 */
778if ( ! function_exists('set_radio'))
779{
780 function set_radio($field = '', $value = '', $default = FALSE)
781 {
782 $OBJ =& _get_validation_object();
783
784 if ($OBJ === FALSE)
785 {
786 if ( ! isset($_POST[$field]))
787 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300788 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
790 return ' checked="checked"';
791 }
792 return '';
793 }
794
795 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 if (is_array($field))
798 {
799 if ( ! in_array($value, $field))
800 {
801 return '';
802 }
803 }
804 else
805 {
806 if (($field == '' OR $value == '') OR ($field != $value))
807 {
808 return '';
809 }
810 }
811
812 return ' checked="checked"';
813 }
814
815 return $OBJ->set_radio($field, $value, $default);
816 }
817}
818
819// ------------------------------------------------------------------------
820
821/**
822 * Form Error
823 *
Andrey Andreev93a83c72012-03-26 21:24:02 +0300824 * Returns the error for a specific form field. This is a helper for the
Derek Allard2067d1a2008-11-13 22:59:24 +0000825 * form validation class.
826 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000827 * @param string
828 * @param string
829 * @param string
830 * @return string
831 */
832if ( ! function_exists('form_error'))
833{
834 function form_error($field = '', $prefix = '', $suffix = '')
835 {
836 if (FALSE === ($OBJ =& _get_validation_object()))
837 {
838 return '';
839 }
840
841 return $OBJ->error($field, $prefix, $suffix);
842 }
843}
844
845// ------------------------------------------------------------------------
846
847/**
848 * Validation Error String
849 *
Andrey Andreev93a83c72012-03-26 21:24:02 +0300850 * Returns all the errors associated with a form submission. This is a helper
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 * function for the form validation class.
852 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 * @param string
854 * @param string
855 * @return string
856 */
857if ( ! function_exists('validation_errors'))
858{
859 function validation_errors($prefix = '', $suffix = '')
860 {
861 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500862 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 return '';
864 }
865
866 return $OBJ->error_string($prefix, $suffix);
867 }
868}
869
870// ------------------------------------------------------------------------
871
872/**
873 * Parse the form attributes
874 *
875 * Helper function used by some of the form helpers
876 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000877 * @param array
878 * @param array
879 * @return string
880 */
881if ( ! function_exists('_parse_form_attributes'))
882{
883 function _parse_form_attributes($attributes, $default)
884 {
885 if (is_array($attributes))
886 {
887 foreach ($default as $key => $val)
888 {
889 if (isset($attributes[$key]))
890 {
891 $default[$key] = $attributes[$key];
892 unset($attributes[$key]);
893 }
894 }
895
896 if (count($attributes) > 0)
897 {
898 $default = array_merge($default, $attributes);
899 }
900 }
901
902 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200903
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 foreach ($default as $key => $val)
905 {
906 if ($key == 'value')
907 {
Derek Jones01a9b102009-07-17 18:30:36 +0000908 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 }
910
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
920/**
921 * Attributes To String
922 *
923 * Helper function used by some of the form helpers
924 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000925 * @param mixed
926 * @param bool
927 * @return string
928 */
929if ( ! function_exists('_attributes_to_string'))
930{
931 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 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300935 if ($formtag == TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
937 $attributes .= ' method="post"';
938 }
939
Andrey Andreev93a83c72012-03-26 21:24:02 +0300940 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
979/**
980 * Validation Object
981 *
982 * Determines what the form validation class was instantiated as, fetches
983 * the object and returns it.
984 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 * @return mixed
986 */
987if ( ! function_exists('_get_validation_object'))
988{
989 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 */