blob: e5b4876081ce7c307226f1fdda25cf652b63f541 [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 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @param string
590 * @return string
591 */
592if ( ! function_exists('form_prep'))
593{
Derek Jones01a9b102009-07-17 18:30:36 +0000594 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 {
Derek Jones01a9b102009-07-17 18:30:36 +0000596 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // if the field name is an array we do this recursively
599 if (is_array($str))
600 {
601 foreach ($str as $key => $val)
602 {
603 $str[$key] = form_prep($val);
604 }
605
606 return $str;
607 }
608
609 if ($str === '')
610 {
611 return '';
612 }
613
Derek Jones3eb9fac2009-07-17 20:25:13 +0000614 // we've already prepped a field with this name
615 // @todo need to figure out a way to namespace this so
616 // that we know the *exact* field and not just one with
617 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000618 if (isset($prepped_fields[$field_name]))
619 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000620 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000621 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000622
Derek Jones01a9b102009-07-17 18:30:36 +0000623 if ($field_name != '')
624 {
Derek Jones68788d52010-03-05 10:11:31 -0600625 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200628 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
630}
631
632// ------------------------------------------------------------------------
633
634/**
635 * Form Value
636 *
637 * Grabs a value from the POST array for the specified field so you can
Andrey Andreev93a83c72012-03-26 21:24:02 +0300638 * re-populate an input field or textarea. If Form Validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 * is active it retrieves the info from the validation class
640 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 * @param string
642 * @return mixed
643 */
644if ( ! function_exists('set_value'))
645{
646 function set_value($field = '', $default = '')
647 {
648 if (FALSE === ($OBJ =& _get_validation_object()))
649 {
650 if ( ! isset($_POST[$field]))
651 {
652 return $default;
653 }
654
Derek Jones01a9b102009-07-17 18:30:36 +0000655 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 }
657
Derek Jones01a9b102009-07-17 18:30:36 +0000658 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660}
661
662// ------------------------------------------------------------------------
663
664/**
665 * Set Select
666 *
667 * Let's you set the selected value of a <select> menu via data in the POST array.
668 * If Form Validation is active it retrieves the info from the validation class
669 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * @param string
671 * @param string
672 * @param bool
673 * @return string
674 */
675if ( ! function_exists('set_select'))
676{
677 function set_select($field = '', $value = '', $default = FALSE)
678 {
679 $OBJ =& _get_validation_object();
680
681 if ($OBJ === FALSE)
682 {
683 if ( ! isset($_POST[$field]))
684 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300685 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 return ' selected="selected"';
688 }
689 return '';
690 }
691
692 $field = $_POST[$field];
693
694 if (is_array($field))
695 {
696 if ( ! in_array($value, $field))
697 {
698 return '';
699 }
700 }
Andrey Andreev93a83c72012-03-26 21:24:02 +0300701 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300703 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 }
705
706 return ' selected="selected"';
707 }
708
709 return $OBJ->set_select($field, $value, $default);
710 }
711}
712
713// ------------------------------------------------------------------------
714
715/**
716 * Set Checkbox
717 *
718 * Let's you set the selected value of a checkbox via the value in the POST array.
719 * If Form Validation is active it retrieves the info from the validation class
720 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000721 * @param string
722 * @param string
723 * @param bool
724 * @return string
725 */
726if ( ! function_exists('set_checkbox'))
727{
728 function set_checkbox($field = '', $value = '', $default = FALSE)
729 {
730 $OBJ =& _get_validation_object();
731
732 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200733 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 if ( ! isset($_POST[$field]))
735 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300736 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000737 {
738 return ' checked="checked"';
739 }
740 return '';
741 }
742
743 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200744
Derek Allard2067d1a2008-11-13 22:59:24 +0000745 if (is_array($field))
746 {
747 if ( ! in_array($value, $field))
748 {
749 return '';
750 }
751 }
Andrey Andreev93a83c72012-03-26 21:24:02 +0300752 elseif (($field == '' OR $value == '') OR ($field != $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300754 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 }
756
757 return ' checked="checked"';
758 }
759
760 return $OBJ->set_checkbox($field, $value, $default);
761 }
762}
763
764// ------------------------------------------------------------------------
765
766/**
767 * Set Radio
768 *
769 * Let's you set the selected value of a radio field via info in the POST array.
770 * If Form Validation is active it retrieves the info from the validation class
771 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 * @param string
773 * @param string
774 * @param bool
775 * @return string
776 */
777if ( ! function_exists('set_radio'))
778{
779 function set_radio($field = '', $value = '', $default = FALSE)
780 {
781 $OBJ =& _get_validation_object();
782
783 if ($OBJ === FALSE)
784 {
785 if ( ! isset($_POST[$field]))
786 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300787 if (count($_POST) === 0 && $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000788 {
789 return ' checked="checked"';
790 }
791 return '';
792 }
793
794 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200795
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 if (is_array($field))
797 {
798 if ( ! in_array($value, $field))
799 {
800 return '';
801 }
802 }
803 else
804 {
805 if (($field == '' OR $value == '') OR ($field != $value))
806 {
807 return '';
808 }
809 }
810
811 return ' checked="checked"';
812 }
813
814 return $OBJ->set_radio($field, $value, $default);
815 }
816}
817
818// ------------------------------------------------------------------------
819
820/**
821 * Form Error
822 *
Andrey Andreev93a83c72012-03-26 21:24:02 +0300823 * Returns the error for a specific form field. This is a helper for the
Derek Allard2067d1a2008-11-13 22:59:24 +0000824 * form validation class.
825 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 * @param string
827 * @param string
828 * @param string
829 * @return string
830 */
831if ( ! function_exists('form_error'))
832{
833 function form_error($field = '', $prefix = '', $suffix = '')
834 {
835 if (FALSE === ($OBJ =& _get_validation_object()))
836 {
837 return '';
838 }
839
840 return $OBJ->error($field, $prefix, $suffix);
841 }
842}
843
844// ------------------------------------------------------------------------
845
846/**
847 * Validation Error String
848 *
Andrey Andreev93a83c72012-03-26 21:24:02 +0300849 * Returns all the errors associated with a form submission. This is a helper
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 * function for the form validation class.
851 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 * @param string
853 * @param string
854 * @return string
855 */
856if ( ! function_exists('validation_errors'))
857{
858 function validation_errors($prefix = '', $suffix = '')
859 {
860 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500861 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 return '';
863 }
864
865 return $OBJ->error_string($prefix, $suffix);
866 }
867}
868
869// ------------------------------------------------------------------------
870
871/**
872 * Parse the form attributes
873 *
874 * Helper function used by some of the form helpers
875 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 * @param array
877 * @param array
878 * @return string
879 */
880if ( ! function_exists('_parse_form_attributes'))
881{
882 function _parse_form_attributes($attributes, $default)
883 {
884 if (is_array($attributes))
885 {
886 foreach ($default as $key => $val)
887 {
888 if (isset($attributes[$key]))
889 {
890 $default[$key] = $attributes[$key];
891 unset($attributes[$key]);
892 }
893 }
894
895 if (count($attributes) > 0)
896 {
897 $default = array_merge($default, $attributes);
898 }
899 }
900
901 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200902
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 foreach ($default as $key => $val)
904 {
905 if ($key == 'value')
906 {
Derek Jones01a9b102009-07-17 18:30:36 +0000907 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 }
909
Andrey Andreev93a83c72012-03-26 21:24:02 +0300910 $att .= $key.'="'.$val.'" ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 }
912
913 return $att;
914 }
915}
916
917// ------------------------------------------------------------------------
918
919/**
920 * Attributes To String
921 *
922 * Helper function used by some of the form helpers
923 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 * @param mixed
925 * @param bool
926 * @return string
927 */
928if ( ! function_exists('_attributes_to_string'))
929{
930 function _attributes_to_string($attributes, $formtag = FALSE)
931 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300932 if (is_string($attributes) && strlen($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000933 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300934 if ($formtag == TRUE && strpos($attributes, 'method=') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
936 $attributes .= ' method="post"';
937 }
938
Andrey Andreev93a83c72012-03-26 21:24:02 +0300939 if ($formtag == TRUE && strpos($attributes, 'accept-charset=') === FALSE)
Derek Allard3241d732009-09-17 12:17:45 +0000940 {
941 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
942 }
943
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200944 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Andrey Andreev93a83c72012-03-26 21:24:02 +0300947 if (is_object($attributes) && count($attributes) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 {
Andrey Andreev93a83c72012-03-26 21:24:02 +0300949 $attributes = (array) $attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000950 }
951
Andrey Andreev93a83c72012-03-26 21:24:02 +0300952 if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 {
Derek Allard3241d732009-09-17 12:17:45 +0000954 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000955
Andrey Andreev93a83c72012-03-26 21:24:02 +0300956 if ( ! isset($attributes['method']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000957 {
958 $atts .= ' method="post"';
959 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000960
Andrey Andreev93a83c72012-03-26 21:24:02 +0300961 if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
Derek Allard3241d732009-09-17 12:17:45 +0000962 {
963 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
964 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000965
Derek Allard3241d732009-09-17 12:17:45 +0000966 foreach ($attributes as $key => $val)
967 {
968 $atts .= ' '.$key.'="'.$val.'"';
969 }
970
971 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +0000972 }
973 }
974}
975
976// ------------------------------------------------------------------------
977
978/**
979 * Validation Object
980 *
981 * Determines what the form validation class was instantiated as, fetches
982 * the object and returns it.
983 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000984 * @return mixed
985 */
986if ( ! function_exists('_get_validation_object'))
987{
988 function &_get_validation_object()
989 {
990 $CI =& get_instance();
991
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500992 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200994
Greg Akerc6d918a2011-04-22 10:17:32 -0500995 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000996 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -0500997 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
998 {
999 return $return;
1000 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001001
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001002 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001004
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001005 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 }
1007}
1008
Derek Allard2067d1a2008-11-13 22:59:24 +00001009/* End of file form_helper.php */
Andrey Andreev93a83c72012-03-26 21:24:02 +03001010/* Location: ./system/helpers/form_helper.php */