blob: bed2cb29794b3ab9df057d7c046c7c5abec519dc [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Form Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Form Declaration
44 *
45 * Creates the opening portion of the form.
46 *
47 * @access public
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
Barry Mienydd671972010-10-04 16:33:58 +020052 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053if ( ! function_exists('form_open'))
54{
55 function form_open($action = '', $attributes = '', $hidden = array())
56 {
57 $CI =& get_instance();
58
59 if ($attributes == '')
60 {
Derek Allard3241d732009-09-17 12:17:45 +000061 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000062 }
63
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010064 // If an action is not a full URL then turn it into one
Phil Sturgeon133beaf2011-03-10 16:38:32 +000065 if ($action && strpos($action, '://') === FALSE)
66 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000067 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000068 }
Derek Allard2067d1a2008-11-13 22:59:24 +000069
Phil Sturgeon9d0e6172011-04-02 12:43:55 +010070 // If no action is provided then set to the current url
71 $action OR $action = $CI->config->site_url($CI->uri->uri_string());
72
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 Andreev8bf6bb62012-01-06 16:11:04 +020075 // 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 AND ! (strpos($action, $CI->config->site_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
81 if (is_array($hidden) AND count($hidden) > 0)
82 {
Greg Aker9ce43852011-04-19 12:58:52 -050083 $form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
Derek Allard958543a2010-07-22 14:10:26 -040084 }
85
Derek Allard2067d1a2008-11-13 22:59:24 +000086 return $form;
87 }
88}
89
90// ------------------------------------------------------------------------
91
92/**
93 * Form Declaration - Multipart type
94 *
95 * Creates the opening portion of the form, but with "multipart/form-data".
96 *
97 * @access public
98 * @param string the URI segments of the form destination
99 * @param array a key/value pair of attributes
100 * @param array a key/value pair hidden data
101 * @return string
102 */
103if ( ! function_exists('form_open_multipart'))
104{
Ben Edmunds98963b32011-08-20 14:17:16 -0500105 function form_open_multipart($action = '', $attributes = array(), $hidden = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
Derek Allard33d4b6a2010-01-17 07:23:00 +0000107 if (is_string($attributes))
108 {
109 $attributes .= ' enctype="multipart/form-data"';
110 }
111 else
112 {
113 $attributes['enctype'] = 'multipart/form-data';
114 }
115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 return form_open($action, $attributes, $hidden);
117 }
118}
119
120// ------------------------------------------------------------------------
121
122/**
123 * Hidden Input Field
124 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500125 * Generates hidden fields. You can pass a simple key/value string or an associative
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * array with multiple values.
127 *
128 * @access public
129 * @param mixed
130 * @param string
131 * @return string
132 */
133if ( ! function_exists('form_hidden'))
134{
Robin Sowell57fe4102009-03-26 18:58:46 +0000135 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000137 static $form;
138
139 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000141 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
143
Robin Sowell57fe4102009-03-26 18:58:46 +0000144 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000146 foreach ($name as $key => $val)
147 {
148 form_hidden($key, $val, TRUE);
149 }
150 return $form;
151 }
152
153 if ( ! is_array($value))
154 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200155 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name)."\" />\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000156 }
157 else
158 {
159 foreach ($value as $k => $v)
160 {
Barry Mienydd671972010-10-04 16:33:58 +0200161 $k = (is_int($k)) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000162 form_hidden($name.'['.$k.']', $v, TRUE);
163 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 }
165
166 return $form;
167 }
168}
169
170// ------------------------------------------------------------------------
171
172/**
173 * Text Input Field
174 *
175 * @access public
176 * @param mixed
177 * @param string
178 * @param string
179 * @return string
180 */
181if ( ! function_exists('form_input'))
182{
183 function form_input($data = '', $value = '', $extra = '')
184 {
185 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
186
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200187 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189}
190
191// ------------------------------------------------------------------------
192
193/**
194 * Password Field
195 *
196 * Identical to the input function but adds the "password" type
197 *
198 * @access public
199 * @param mixed
200 * @param string
201 * @param string
202 * @return string
203 */
204if ( ! function_exists('form_password'))
205{
206 function form_password($data = '', $value = '', $extra = '')
207 {
208 if ( ! is_array($data))
209 {
210 $data = array('name' => $data);
211 }
212
213 $data['type'] = 'password';
214 return form_input($data, $value, $extra);
215 }
216}
217
218// ------------------------------------------------------------------------
219
220/**
221 * Upload Field
222 *
223 * Identical to the input function but adds the "file" type
224 *
225 * @access public
226 * @param mixed
227 * @param string
228 * @param string
229 * @return string
230 */
231if ( ! function_exists('form_upload'))
232{
233 function form_upload($data = '', $value = '', $extra = '')
234 {
235 if ( ! is_array($data))
236 {
237 $data = array('name' => $data);
238 }
239
240 $data['type'] = 'file';
241 return form_input($data, $value, $extra);
242 }
243}
244
245// ------------------------------------------------------------------------
246
247/**
248 * Textarea field
249 *
250 * @access public
251 * @param mixed
252 * @param string
253 * @param string
254 * @return string
255 */
256if ( ! function_exists('form_textarea'))
257{
258 function form_textarea($data = '', $value = '', $extra = '')
259 {
Phil Sturgeon7de31602011-08-13 10:26:22 -0600260 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '40', 'rows' => '10');
Derek Allard2067d1a2008-11-13 22:59:24 +0000261
262 if ( ! is_array($data) OR ! isset($data['value']))
263 {
264 $val = $value;
265 }
266 else
267 {
Barry Mienydd671972010-10-04 16:33:58 +0200268 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 unset($data['value']); // textareas don't use the value attribute
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Jones01a9b102009-07-17 18:30:36 +0000272 $name = (is_array($data)) ? $data['name'] : $data;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200273 return '<textarea '._parse_form_attributes($data, $defaults).$extra.'>'.form_prep($val, $name)."</textarea>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275}
276
277// ------------------------------------------------------------------------
278
279/**
Derek Jones26399292009-04-08 16:14:17 +0000280 * Multi-select menu
281 *
282 * @access public
283 * @param string
284 * @param array
285 * @param mixed
286 * @param string
287 * @return type
288 */
Derek Jones68788d52010-03-05 10:11:31 -0600289if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000290{
291 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
292 {
293 if ( ! strpos($extra, 'multiple'))
294 {
295 $extra .= ' multiple="multiple"';
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jones26399292009-04-08 16:14:17 +0000298 return form_dropdown($name, $options, $selected, $extra);
299 }
300}
301
302// --------------------------------------------------------------------
303
304/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * Drop-down Menu
306 *
307 * @access public
308 * @param string
309 * @param array
310 * @param string
311 * @param string
312 * @return string
313 */
314if ( ! function_exists('form_dropdown'))
315{
316 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
317 {
318 if ( ! is_array($selected))
319 {
320 $selected = array($selected);
321 }
322
323 // If no selected state was submitted we will attempt to set it automatically
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200324 if (count($selected) === 0 && isset($_POST[$name]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200326 $selected = array($_POST[$name]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328
329 if ($extra != '') $extra = ' '.$extra;
330
331 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
332
333 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 foreach ($options as $key => $val)
336 {
337 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338
Derek Jones68788d52010-03-05 10:11:31 -0600339 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000340 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200341 $form .= '<optgroup label="'.$key."\">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000342
Derek Allard78a5fc92009-02-05 16:34:35 +0000343 foreach ($val as $optgroup_key => $optgroup_val)
344 {
Derek Allard86a840c2009-02-05 17:31:58 +0000345 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000346 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
347 }
348
349 $form .= '</optgroup>'."\n";
350 }
351 else
352 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200353 $form .= '<option value="'.$key.'"'.(in_array($key, $selected) ? ' selected="selected"' : '').'>'.(string) $val."</option>\n";
Derek Allard78a5fc92009-02-05 16:34:35 +0000354 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200357 $form .= "</select>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000358
359 return $form;
360 }
361}
362
363// ------------------------------------------------------------------------
364
365/**
366 * Checkbox Field
367 *
368 * @access public
369 * @param mixed
370 * @param string
371 * @param bool
372 * @param string
373 * @return string
374 */
375if ( ! function_exists('form_checkbox'))
376{
377 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
378 {
379 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
380
381 if (is_array($data) AND array_key_exists('checked', $data))
382 {
383 $checked = $data['checked'];
384
385 if ($checked == FALSE)
386 {
387 unset($data['checked']);
388 }
389 else
390 {
391 $data['checked'] = 'checked';
392 }
393 }
394
395 if ($checked == TRUE)
396 {
397 $defaults['checked'] = 'checked';
398 }
399 else
400 {
401 unset($defaults['checked']);
402 }
403
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200404 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406}
407
408// ------------------------------------------------------------------------
409
410/**
411 * Radio Button
412 *
413 * @access public
414 * @param mixed
415 * @param string
416 * @param bool
417 * @param string
418 * @return string
419 */
420if ( ! function_exists('form_radio'))
421{
422 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
423 {
424 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200425 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 $data = array('name' => $data);
427 }
428
429 $data['type'] = 'radio';
430 return form_checkbox($data, $value, $checked, $extra);
431 }
432}
433
434// ------------------------------------------------------------------------
435
436/**
437 * Submit Button
438 *
439 * @access public
440 * @param mixed
441 * @param string
442 * @param string
443 * @return string
444 */
445if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200446{
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 function form_submit($data = '', $value = '', $extra = '')
448 {
449 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200450 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452}
453
454// ------------------------------------------------------------------------
455
456/**
457 * Reset Button
458 *
459 * @access public
460 * @param mixed
461 * @param string
462 * @param string
463 * @return string
464 */
465if ( ! function_exists('form_reset'))
466{
467 function form_reset($data = '', $value = '', $extra = '')
468 {
469 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200470 return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472}
473
474// ------------------------------------------------------------------------
475
476/**
477 * Form Button
478 *
479 * @access public
480 * @param mixed
481 * @param string
482 * @param string
483 * @return string
484 */
485if ( ! function_exists('form_button'))
486{
487 function form_button($data = '', $content = '', $extra = '')
488 {
Derek Allard904094a2009-02-10 14:00:34 +0000489 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 if ( is_array($data) AND isset($data['content']))
491 {
492 $content = $data['content'];
493 unset($data['content']); // content is not an attribute
494 }
495
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200496 return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498}
499
500// ------------------------------------------------------------------------
501
502/**
503 * Form Label Tag
504 *
505 * @access public
506 * @param string The text to appear onscreen
507 * @param string The id the label applies to
508 * @param string Additional attributes
509 * @return string
510 */
511if ( ! function_exists('form_label'))
512{
513 function form_label($label_text = '', $id = '', $attributes = array())
514 {
515
516 $label = '<label';
517
518 if ($id != '')
519 {
Barry Mienydd671972010-10-04 16:33:58 +0200520 $label .= " for=\"$id\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
522
523 if (is_array($attributes) AND count($attributes) > 0)
524 {
525 foreach ($attributes as $key => $val)
526 {
527 $label .= ' '.$key.'="'.$val.'"';
528 }
529 }
530
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200531 return $label .= ">$label_text</label>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 }
533}
534
535// ------------------------------------------------------------------------
536/**
537 * Fieldset Tag
538 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500539 * Used to produce <fieldset><legend>text</legend>. To close fieldset
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * use form_fieldset_close()
541 *
542 * @access public
543 * @param string The legend text
544 * @param string Additional attributes
545 * @return string
546 */
547if ( ! function_exists('form_fieldset'))
548{
549 function form_fieldset($legend_text = '', $attributes = array())
550 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200551 $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 if ($legend_text != '')
553 {
554 $fieldset .= "<legend>$legend_text</legend>\n";
555 }
556
557 return $fieldset;
558 }
559}
560
561// ------------------------------------------------------------------------
562
563/**
564 * Fieldset Close Tag
565 *
566 * @access public
567 * @param string
568 * @return string
569 */
570if ( ! function_exists('form_fieldset_close'))
571{
572 function form_fieldset_close($extra = '')
573 {
574 return "</fieldset>".$extra;
575 }
576}
577
578// ------------------------------------------------------------------------
579
580/**
581 * Form Close Tag
582 *
583 * @access public
584 * @param string
585 * @return string
586 */
587if ( ! function_exists('form_close'))
588{
589 function form_close($extra = '')
590 {
591 return "</form>".$extra;
592 }
593}
594
595// ------------------------------------------------------------------------
596
597/**
598 * Form Prep
599 *
600 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
601 *
602 * @access public
603 * @param string
604 * @return string
605 */
606if ( ! function_exists('form_prep'))
607{
Derek Jones01a9b102009-07-17 18:30:36 +0000608 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Derek Jones01a9b102009-07-17 18:30:36 +0000610 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // if the field name is an array we do this recursively
613 if (is_array($str))
614 {
615 foreach ($str as $key => $val)
616 {
617 $str[$key] = form_prep($val);
618 }
619
620 return $str;
621 }
622
623 if ($str === '')
624 {
625 return '';
626 }
627
Derek Jones3eb9fac2009-07-17 20:25:13 +0000628 // we've already prepped a field with this name
629 // @todo need to figure out a way to namespace this so
630 // that we know the *exact* field and not just one with
631 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000632 if (isset($prepped_fields[$field_name]))
633 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000634 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000635 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000636
Derek Jones01a9b102009-07-17 18:30:36 +0000637 if ($field_name != '')
638 {
Derek Jones68788d52010-03-05 10:11:31 -0600639 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200642 return html_escape($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 }
644}
645
646// ------------------------------------------------------------------------
647
648/**
649 * Form Value
650 *
651 * Grabs a value from the POST array for the specified field so you can
Derek Jones37f4b9c2011-07-01 17:56:50 -0500652 * re-populate an input field or textarea. If Form Validation
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 * is active it retrieves the info from the validation class
654 *
655 * @access public
656 * @param string
657 * @return mixed
658 */
659if ( ! function_exists('set_value'))
660{
661 function set_value($field = '', $default = '')
662 {
663 if (FALSE === ($OBJ =& _get_validation_object()))
664 {
665 if ( ! isset($_POST[$field]))
666 {
667 return $default;
668 }
669
Derek Jones01a9b102009-07-17 18:30:36 +0000670 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
672
Derek Jones01a9b102009-07-17 18:30:36 +0000673 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 }
675}
676
677// ------------------------------------------------------------------------
678
679/**
680 * Set Select
681 *
682 * Let's you set the selected value of a <select> menu via data in the POST array.
683 * If Form Validation is active it retrieves the info from the validation class
684 *
685 * @access public
686 * @param string
687 * @param string
688 * @param bool
689 * @return string
690 */
691if ( ! function_exists('set_select'))
692{
693 function set_select($field = '', $value = '', $default = FALSE)
694 {
695 $OBJ =& _get_validation_object();
696
697 if ($OBJ === FALSE)
698 {
699 if ( ! isset($_POST[$field]))
700 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000701 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 {
703 return ' selected="selected"';
704 }
705 return '';
706 }
707
708 $field = $_POST[$field];
709
710 if (is_array($field))
711 {
712 if ( ! in_array($value, $field))
713 {
714 return '';
715 }
716 }
717 else
718 {
719 if (($field == '' OR $value == '') OR ($field != $value))
720 {
721 return '';
722 }
723 }
724
725 return ' selected="selected"';
726 }
727
728 return $OBJ->set_select($field, $value, $default);
729 }
730}
731
732// ------------------------------------------------------------------------
733
734/**
735 * Set Checkbox
736 *
737 * Let's you set the selected value of a checkbox via the value in the POST array.
738 * If Form Validation is active it retrieves the info from the validation class
739 *
740 * @access public
741 * @param string
742 * @param string
743 * @param bool
744 * @return string
745 */
746if ( ! function_exists('set_checkbox'))
747{
748 function set_checkbox($field = '', $value = '', $default = FALSE)
749 {
750 $OBJ =& _get_validation_object();
751
752 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200753 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000754 if ( ! isset($_POST[$field]))
755 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000756 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 {
758 return ' checked="checked"';
759 }
760 return '';
761 }
762
763 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 if (is_array($field))
766 {
767 if ( ! in_array($value, $field))
768 {
769 return '';
770 }
771 }
772 else
773 {
774 if (($field == '' OR $value == '') OR ($field != $value))
775 {
776 return '';
777 }
778 }
779
780 return ' checked="checked"';
781 }
782
783 return $OBJ->set_checkbox($field, $value, $default);
784 }
785}
786
787// ------------------------------------------------------------------------
788
789/**
790 * Set Radio
791 *
792 * Let's you set the selected value of a radio field via info in the POST array.
793 * If Form Validation is active it retrieves the info from the validation class
794 *
795 * @access public
796 * @param string
797 * @param string
798 * @param bool
799 * @return string
800 */
801if ( ! function_exists('set_radio'))
802{
803 function set_radio($field = '', $value = '', $default = FALSE)
804 {
805 $OBJ =& _get_validation_object();
806
807 if ($OBJ === FALSE)
808 {
809 if ( ! isset($_POST[$field]))
810 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000811 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 {
813 return ' checked="checked"';
814 }
815 return '';
816 }
817
818 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200819
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 if (is_array($field))
821 {
822 if ( ! in_array($value, $field))
823 {
824 return '';
825 }
826 }
827 else
828 {
829 if (($field == '' OR $value == '') OR ($field != $value))
830 {
831 return '';
832 }
833 }
834
835 return ' checked="checked"';
836 }
837
838 return $OBJ->set_radio($field, $value, $default);
839 }
840}
841
842// ------------------------------------------------------------------------
843
844/**
845 * Form Error
846 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500847 * Returns the error for a specific form field. This is a helper for the
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 * form validation class.
849 *
850 * @access public
851 * @param string
852 * @param string
853 * @param string
854 * @return string
855 */
856if ( ! function_exists('form_error'))
857{
858 function form_error($field = '', $prefix = '', $suffix = '')
859 {
860 if (FALSE === ($OBJ =& _get_validation_object()))
861 {
862 return '';
863 }
864
865 return $OBJ->error($field, $prefix, $suffix);
866 }
867}
868
869// ------------------------------------------------------------------------
870
871/**
872 * Validation Error String
873 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500874 * Returns all the errors associated with a form submission. This is a helper
Derek Allard2067d1a2008-11-13 22:59:24 +0000875 * function for the form validation class.
876 *
877 * @access public
878 * @param string
879 * @param string
880 * @return string
881 */
882if ( ! function_exists('validation_errors'))
883{
884 function validation_errors($prefix = '', $suffix = '')
885 {
886 if (FALSE === ($OBJ =& _get_validation_object()))
Greg Akerc83bea62011-04-23 12:12:57 -0500887 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000888 return '';
889 }
890
891 return $OBJ->error_string($prefix, $suffix);
892 }
893}
894
895// ------------------------------------------------------------------------
896
897/**
898 * Parse the form attributes
899 *
900 * Helper function used by some of the form helpers
901 *
902 * @access private
903 * @param array
904 * @param array
905 * @return string
906 */
907if ( ! function_exists('_parse_form_attributes'))
908{
909 function _parse_form_attributes($attributes, $default)
910 {
911 if (is_array($attributes))
912 {
913 foreach ($default as $key => $val)
914 {
915 if (isset($attributes[$key]))
916 {
917 $default[$key] = $attributes[$key];
918 unset($attributes[$key]);
919 }
920 }
921
922 if (count($attributes) > 0)
923 {
924 $default = array_merge($default, $attributes);
925 }
926 }
927
928 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 foreach ($default as $key => $val)
931 {
932 if ($key == 'value')
933 {
Derek Jones01a9b102009-07-17 18:30:36 +0000934 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 }
936
937 $att .= $key . '="' . $val . '" ';
938 }
939
940 return $att;
941 }
942}
943
944// ------------------------------------------------------------------------
945
946/**
947 * Attributes To String
948 *
949 * Helper function used by some of the form helpers
950 *
951 * @access private
952 * @param mixed
953 * @param bool
954 * @return string
955 */
956if ( ! function_exists('_attributes_to_string'))
957{
958 function _attributes_to_string($attributes, $formtag = FALSE)
959 {
960 if (is_string($attributes) AND strlen($attributes) > 0)
961 {
962 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
963 {
964 $attributes .= ' method="post"';
965 }
966
Derek Allard3241d732009-09-17 12:17:45 +0000967 if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE)
968 {
969 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
970 }
971
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200972 return ' '.$attributes;
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 }
Barry Mienydd671972010-10-04 16:33:58 +0200974
Derek Allard2067d1a2008-11-13 22:59:24 +0000975 if (is_object($attributes) AND count($attributes) > 0)
976 {
977 $attributes = (array)$attributes;
978 }
979
Joel Kallman79c1c462011-12-18 19:25:45 -0500980 if (is_array($attributes) AND ($formtag === TRUE OR count($attributes) > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000981 {
Derek Allard3241d732009-09-17 12:17:45 +0000982 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000983
Derek Allard3241d732009-09-17 12:17:45 +0000984 if ( ! isset($attributes['method']) AND $formtag === TRUE)
985 {
986 $atts .= ' method="post"';
987 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000988
Derek Allard3241d732009-09-17 12:17:45 +0000989 if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE)
990 {
991 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
992 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000993
Derek Allard3241d732009-09-17 12:17:45 +0000994 foreach ($attributes as $key => $val)
995 {
996 $atts .= ' '.$key.'="'.$val.'"';
997 }
998
999 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 }
1001 }
1002}
1003
1004// ------------------------------------------------------------------------
1005
1006/**
1007 * Validation Object
1008 *
1009 * Determines what the form validation class was instantiated as, fetches
1010 * the object and returns it.
1011 *
1012 * @access private
1013 * @return mixed
1014 */
1015if ( ! function_exists('_get_validation_object'))
1016{
1017 function &_get_validation_object()
1018 {
1019 $CI =& get_instance();
1020
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001021 // We set this as a variable since we're returning by reference.
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 $return = FALSE;
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001023
Greg Akerc6d918a2011-04-22 10:17:32 -05001024 if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
Derek Allard2067d1a2008-11-13 22:59:24 +00001025 {
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001026 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1027 {
1028 return $return;
1029 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001030
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001031 return $CI->$object;
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001033
Greg Aker0c9ee4a2011-04-20 09:40:17 -05001034 return $return;
Derek Allard2067d1a2008-11-13 22:59:24 +00001035 }
1036}
1037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038/* End of file form_helper.php */
patwork64e35cd2011-04-08 15:25:31 +02001039/* Location: ./system/helpers/form_helper.php */