blob: 5323097949073cf3557432bd2dd9281a757ebd69 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Form Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Form Declaration
32 *
33 * Creates the opening portion of the form.
34 *
35 * @access public
36 * @param string the URI segments of the form destination
37 * @param array a key/value pair of attributes
38 * @param array a key/value pair hidden data
39 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020040 */
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('form_open'))
42{
43 function form_open($action = '', $attributes = '', $hidden = array())
44 {
45 $CI =& get_instance();
46
47 if ($attributes == '')
48 {
Derek Allard3241d732009-09-17 12:17:45 +000049 $attributes = 'method="post"';
Derek Allard2067d1a2008-11-13 22:59:24 +000050 }
51
Phil Sturgeon133beaf2011-03-10 16:38:32 +000052 if ($action && strpos($action, '://') === FALSE)
53 {
Phil Sturgeon52e73182011-03-11 10:17:01 +000054 $action = $CI->config->site_url($action);
Phil Sturgeon133beaf2011-03-10 16:38:32 +000055 }
Derek Allard2067d1a2008-11-13 22:59:24 +000056
57 $form = '<form action="'.$action.'"';
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 $form .= _attributes_to_string($attributes, TRUE);
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 $form .= '>';
62
Derek Allard958543a2010-07-22 14:10:26 -040063 // CSRF
64 if ($CI->config->item('csrf_protection') === TRUE)
65 {
Greg Aker28b425a2010-09-15 11:43:23 -050066 $hidden[$CI->security->csrf_token_name] = $CI->security->csrf_hash;
67 }
68
69 if (is_array($hidden) AND count($hidden) > 0)
70 {
71 $form .= sprintf("\n<div class=\"hidden\">%s</div>", form_hidden($hidden));
Derek Allard958543a2010-07-22 14:10:26 -040072 }
73
Derek Allard2067d1a2008-11-13 22:59:24 +000074 return $form;
75 }
76}
77
78// ------------------------------------------------------------------------
79
80/**
81 * Form Declaration - Multipart type
82 *
83 * Creates the opening portion of the form, but with "multipart/form-data".
84 *
85 * @access public
86 * @param string the URI segments of the form destination
87 * @param array a key/value pair of attributes
88 * @param array a key/value pair hidden data
89 * @return string
90 */
91if ( ! function_exists('form_open_multipart'))
92{
93 function form_open_multipart($action, $attributes = array(), $hidden = array())
94 {
Derek Allard33d4b6a2010-01-17 07:23:00 +000095 if (is_string($attributes))
96 {
97 $attributes .= ' enctype="multipart/form-data"';
98 }
99 else
100 {
101 $attributes['enctype'] = 'multipart/form-data';
102 }
103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 return form_open($action, $attributes, $hidden);
105 }
106}
107
108// ------------------------------------------------------------------------
109
110/**
111 * Hidden Input Field
112 *
113 * Generates hidden fields. You can pass a simple key/value string or an associative
114 * array with multiple values.
115 *
116 * @access public
117 * @param mixed
118 * @param string
119 * @return string
120 */
121if ( ! function_exists('form_hidden'))
122{
Robin Sowell57fe4102009-03-26 18:58:46 +0000123 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000125 static $form;
126
127 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000129 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131
Robin Sowell57fe4102009-03-26 18:58:46 +0000132 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000134 foreach ($name as $key => $val)
135 {
136 form_hidden($key, $val, TRUE);
137 }
138 return $form;
139 }
140
141 if ( ! is_array($value))
142 {
Derek Jones01a9b102009-07-17 18:30:36 +0000143 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000144 }
145 else
146 {
147 foreach ($value as $k => $v)
148 {
Barry Mienydd671972010-10-04 16:33:58 +0200149 $k = (is_int($k)) ? '' : $k;
Robin Sowell57fe4102009-03-26 18:58:46 +0000150 form_hidden($name.'['.$k.']', $v, TRUE);
151 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 return $form;
155 }
156}
157
158// ------------------------------------------------------------------------
159
160/**
161 * Text Input Field
162 *
163 * @access public
164 * @param mixed
165 * @param string
166 * @param string
167 * @return string
168 */
169if ( ! function_exists('form_input'))
170{
171 function form_input($data = '', $value = '', $extra = '')
172 {
173 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
174
175 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
176 }
177}
178
179// ------------------------------------------------------------------------
180
181/**
182 * Password Field
183 *
184 * Identical to the input function but adds the "password" type
185 *
186 * @access public
187 * @param mixed
188 * @param string
189 * @param string
190 * @return string
191 */
192if ( ! function_exists('form_password'))
193{
194 function form_password($data = '', $value = '', $extra = '')
195 {
196 if ( ! is_array($data))
197 {
198 $data = array('name' => $data);
199 }
200
201 $data['type'] = 'password';
202 return form_input($data, $value, $extra);
203 }
204}
205
206// ------------------------------------------------------------------------
207
208/**
209 * Upload Field
210 *
211 * Identical to the input function but adds the "file" type
212 *
213 * @access public
214 * @param mixed
215 * @param string
216 * @param string
217 * @return string
218 */
219if ( ! function_exists('form_upload'))
220{
221 function form_upload($data = '', $value = '', $extra = '')
222 {
223 if ( ! is_array($data))
224 {
225 $data = array('name' => $data);
226 }
227
228 $data['type'] = 'file';
229 return form_input($data, $value, $extra);
230 }
231}
232
233// ------------------------------------------------------------------------
234
235/**
236 * Textarea field
237 *
238 * @access public
239 * @param mixed
240 * @param string
241 * @param string
242 * @return string
243 */
244if ( ! function_exists('form_textarea'))
245{
246 function form_textarea($data = '', $value = '', $extra = '')
247 {
248 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
249
250 if ( ! is_array($data) OR ! isset($data['value']))
251 {
252 $val = $value;
253 }
254 else
255 {
Barry Mienydd671972010-10-04 16:33:58 +0200256 $val = $data['value'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 unset($data['value']); // textareas don't use the value attribute
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Jones01a9b102009-07-17 18:30:36 +0000260 $name = (is_array($data)) ? $data['name'] : $data;
261 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263}
264
265// ------------------------------------------------------------------------
266
267/**
Derek Jones26399292009-04-08 16:14:17 +0000268 * Multi-select menu
269 *
270 * @access public
271 * @param string
272 * @param array
273 * @param mixed
274 * @param string
275 * @return type
276 */
Derek Jones68788d52010-03-05 10:11:31 -0600277if ( ! function_exists('form_multiselect'))
Derek Jones26399292009-04-08 16:14:17 +0000278{
279 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
280 {
281 if ( ! strpos($extra, 'multiple'))
282 {
283 $extra .= ' multiple="multiple"';
284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Jones26399292009-04-08 16:14:17 +0000286 return form_dropdown($name, $options, $selected, $extra);
287 }
288}
289
290// --------------------------------------------------------------------
291
292/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 * Drop-down Menu
294 *
295 * @access public
296 * @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 {
306 if ( ! is_array($selected))
307 {
308 $selected = array($selected);
309 }
310
311 // If no selected state was submitted we will attempt to set it automatically
312 if (count($selected) === 0)
313 {
314 // If the form name appears in the $_POST array we have a winner!
315 if (isset($_POST[$name]))
316 {
317 $selected = array($_POST[$name]);
318 }
319 }
320
321 if ($extra != '') $extra = ' '.$extra;
322
323 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
324
325 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 foreach ($options as $key => $val)
328 {
329 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000330
Derek Jones68788d52010-03-05 10:11:31 -0600331 if (is_array($val) && ! empty($val))
Derek Allard78a5fc92009-02-05 16:34:35 +0000332 {
333 $form .= '<optgroup label="'.$key.'">'."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000334
Derek Allard78a5fc92009-02-05 16:34:35 +0000335 foreach ($val as $optgroup_key => $optgroup_val)
336 {
Derek Allard86a840c2009-02-05 17:31:58 +0000337 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000338
339 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
340 }
341
342 $form .= '</optgroup>'."\n";
343 }
344 else
345 {
346 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
347
348 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
349 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351
352 $form .= '</select>';
353
354 return $form;
355 }
356}
357
358// ------------------------------------------------------------------------
359
360/**
361 * Checkbox Field
362 *
363 * @access public
364 * @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 {
374 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
375
376 if (is_array($data) AND array_key_exists('checked', $data))
377 {
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
399 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
400 }
401}
402
403// ------------------------------------------------------------------------
404
405/**
406 * Radio Button
407 *
408 * @access public
409 * @param mixed
410 * @param string
411 * @param bool
412 * @param string
413 * @return string
414 */
415if ( ! function_exists('form_radio'))
416{
417 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
418 {
419 if ( ! is_array($data))
Barry Mienydd671972010-10-04 16:33:58 +0200420 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 $data = array('name' => $data);
422 }
423
424 $data['type'] = 'radio';
425 return form_checkbox($data, $value, $checked, $extra);
426 }
427}
428
429// ------------------------------------------------------------------------
430
431/**
432 * Submit Button
433 *
434 * @access public
435 * @param mixed
436 * @param string
437 * @param string
438 * @return string
439 */
440if ( ! function_exists('form_submit'))
Barry Mienydd671972010-10-04 16:33:58 +0200441{
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 function form_submit($data = '', $value = '', $extra = '')
443 {
444 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
445
446 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
447 }
448}
449
450// ------------------------------------------------------------------------
451
452/**
453 * Reset Button
454 *
455 * @access public
456 * @param mixed
457 * @param string
458 * @param string
459 * @return string
460 */
461if ( ! function_exists('form_reset'))
462{
463 function form_reset($data = '', $value = '', $extra = '')
464 {
465 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
466
467 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
468 }
469}
470
471// ------------------------------------------------------------------------
472
473/**
474 * Form Button
475 *
476 * @access public
477 * @param mixed
478 * @param string
479 * @param string
480 * @return string
481 */
482if ( ! function_exists('form_button'))
483{
484 function form_button($data = '', $content = '', $extra = '')
485 {
Derek Allard904094a2009-02-10 14:00:34 +0000486 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
488 if ( is_array($data) AND isset($data['content']))
489 {
490 $content = $data['content'];
491 unset($data['content']); // content is not an attribute
492 }
493
494 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
495 }
496}
497
498// ------------------------------------------------------------------------
499
500/**
501 * Form Label Tag
502 *
503 * @access public
504 * @param string The text to appear onscreen
505 * @param string The id the label applies to
506 * @param string Additional attributes
507 * @return string
508 */
509if ( ! function_exists('form_label'))
510{
511 function form_label($label_text = '', $id = '', $attributes = array())
512 {
513
514 $label = '<label';
515
516 if ($id != '')
517 {
Barry Mienydd671972010-10-04 16:33:58 +0200518 $label .= " for=\"$id\"";
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
520
521 if (is_array($attributes) AND count($attributes) > 0)
522 {
523 foreach ($attributes as $key => $val)
524 {
525 $label .= ' '.$key.'="'.$val.'"';
526 }
527 }
528
529 $label .= ">$label_text</label>";
530
531 return $label;
532 }
533}
534
535// ------------------------------------------------------------------------
536/**
537 * Fieldset Tag
538 *
539 * Used to produce <fieldset><legend>text</legend>. To close fieldset
540 * 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 {
551 $fieldset = "<fieldset";
552
553 $fieldset .= _attributes_to_string($attributes, FALSE);
554
555 $fieldset .= ">\n";
556
557 if ($legend_text != '')
558 {
559 $fieldset .= "<legend>$legend_text</legend>\n";
560 }
561
562 return $fieldset;
563 }
564}
565
566// ------------------------------------------------------------------------
567
568/**
569 * Fieldset Close Tag
570 *
571 * @access public
572 * @param string
573 * @return string
574 */
575if ( ! function_exists('form_fieldset_close'))
576{
577 function form_fieldset_close($extra = '')
578 {
579 return "</fieldset>".$extra;
580 }
581}
582
583// ------------------------------------------------------------------------
584
585/**
586 * Form Close Tag
587 *
588 * @access public
589 * @param string
590 * @return string
591 */
592if ( ! function_exists('form_close'))
593{
594 function form_close($extra = '')
595 {
596 return "</form>".$extra;
597 }
598}
599
600// ------------------------------------------------------------------------
601
602/**
603 * Form Prep
604 *
605 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
606 *
607 * @access public
608 * @param string
609 * @return string
610 */
611if ( ! function_exists('form_prep'))
612{
Derek Jones01a9b102009-07-17 18:30:36 +0000613 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
Derek Jones01a9b102009-07-17 18:30:36 +0000615 static $prepped_fields = array();
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 // if the field name is an array we do this recursively
618 if (is_array($str))
619 {
620 foreach ($str as $key => $val)
621 {
622 $str[$key] = form_prep($val);
623 }
624
625 return $str;
626 }
627
628 if ($str === '')
629 {
630 return '';
631 }
632
Derek Jones3eb9fac2009-07-17 20:25:13 +0000633 // we've already prepped a field with this name
634 // @todo need to figure out a way to namespace this so
635 // that we know the *exact* field and not just one with
636 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000637 if (isset($prepped_fields[$field_name]))
638 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000639 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000640 }
Barry Mienydd671972010-10-04 16:33:58 +0200641
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 $str = htmlspecialchars($str);
643
644 // In case htmlspecialchars misses these.
645 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
646
Derek Jones01a9b102009-07-17 18:30:36 +0000647 if ($field_name != '')
648 {
Derek Jones68788d52010-03-05 10:11:31 -0600649 $prepped_fields[$field_name] = $field_name;
Derek Jones01a9b102009-07-17 18:30:36 +0000650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 return $str;
653 }
654}
655
656// ------------------------------------------------------------------------
657
658/**
659 * Form Value
660 *
661 * Grabs a value from the POST array for the specified field so you can
662 * re-populate an input field or textarea. If Form Validation
663 * is active it retrieves the info from the validation class
664 *
665 * @access public
666 * @param string
667 * @return mixed
668 */
669if ( ! function_exists('set_value'))
670{
671 function set_value($field = '', $default = '')
672 {
673 if (FALSE === ($OBJ =& _get_validation_object()))
674 {
675 if ( ! isset($_POST[$field]))
676 {
677 return $default;
678 }
679
Derek Jones01a9b102009-07-17 18:30:36 +0000680 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682
Derek Jones01a9b102009-07-17 18:30:36 +0000683 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 }
685}
686
687// ------------------------------------------------------------------------
688
689/**
690 * Set Select
691 *
692 * Let's you set the selected value of a <select> menu via data in the POST array.
693 * If Form Validation is active it retrieves the info from the validation class
694 *
695 * @access public
696 * @param string
697 * @param string
698 * @param bool
699 * @return string
700 */
701if ( ! function_exists('set_select'))
702{
703 function set_select($field = '', $value = '', $default = FALSE)
704 {
705 $OBJ =& _get_validation_object();
706
707 if ($OBJ === FALSE)
708 {
709 if ( ! isset($_POST[$field]))
710 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000711 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000712 {
713 return ' selected="selected"';
714 }
715 return '';
716 }
717
718 $field = $_POST[$field];
719
720 if (is_array($field))
721 {
722 if ( ! in_array($value, $field))
723 {
724 return '';
725 }
726 }
727 else
728 {
729 if (($field == '' OR $value == '') OR ($field != $value))
730 {
731 return '';
732 }
733 }
734
735 return ' selected="selected"';
736 }
737
738 return $OBJ->set_select($field, $value, $default);
739 }
740}
741
742// ------------------------------------------------------------------------
743
744/**
745 * Set Checkbox
746 *
747 * Let's you set the selected value of a checkbox via the value in the POST array.
748 * If Form Validation is active it retrieves the info from the validation class
749 *
750 * @access public
751 * @param string
752 * @param string
753 * @param bool
754 * @return string
755 */
756if ( ! function_exists('set_checkbox'))
757{
758 function set_checkbox($field = '', $value = '', $default = FALSE)
759 {
760 $OBJ =& _get_validation_object();
761
762 if ($OBJ === FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200763 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 if ( ! isset($_POST[$field]))
765 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000766 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
768 return ' checked="checked"';
769 }
770 return '';
771 }
772
773 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200774
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 if (is_array($field))
776 {
777 if ( ! in_array($value, $field))
778 {
779 return '';
780 }
781 }
782 else
783 {
784 if (($field == '' OR $value == '') OR ($field != $value))
785 {
786 return '';
787 }
788 }
789
790 return ' checked="checked"';
791 }
792
793 return $OBJ->set_checkbox($field, $value, $default);
794 }
795}
796
797// ------------------------------------------------------------------------
798
799/**
800 * Set Radio
801 *
802 * Let's you set the selected value of a radio field via info in the POST array.
803 * If Form Validation is active it retrieves the info from the validation class
804 *
805 * @access public
806 * @param string
807 * @param string
808 * @param bool
809 * @return string
810 */
811if ( ! function_exists('set_radio'))
812{
813 function set_radio($field = '', $value = '', $default = FALSE)
814 {
815 $OBJ =& _get_validation_object();
816
817 if ($OBJ === FALSE)
818 {
819 if ( ! isset($_POST[$field]))
820 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000821 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000822 {
823 return ' checked="checked"';
824 }
825 return '';
826 }
827
828 $field = $_POST[$field];
Barry Mienydd671972010-10-04 16:33:58 +0200829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830 if (is_array($field))
831 {
832 if ( ! in_array($value, $field))
833 {
834 return '';
835 }
836 }
837 else
838 {
839 if (($field == '' OR $value == '') OR ($field != $value))
840 {
841 return '';
842 }
843 }
844
845 return ' checked="checked"';
846 }
847
848 return $OBJ->set_radio($field, $value, $default);
849 }
850}
851
852// ------------------------------------------------------------------------
853
854/**
855 * Form Error
856 *
857 * Returns the error for a specific form field. This is a helper for the
858 * form validation class.
859 *
860 * @access public
861 * @param string
862 * @param string
863 * @param string
864 * @return string
865 */
866if ( ! function_exists('form_error'))
867{
868 function form_error($field = '', $prefix = '', $suffix = '')
869 {
870 if (FALSE === ($OBJ =& _get_validation_object()))
871 {
872 return '';
873 }
874
875 return $OBJ->error($field, $prefix, $suffix);
876 }
877}
878
879// ------------------------------------------------------------------------
880
881/**
882 * Validation Error String
883 *
884 * Returns all the errors associated with a form submission. This is a helper
885 * function for the form validation class.
886 *
887 * @access public
888 * @param string
889 * @param string
890 * @return string
891 */
892if ( ! function_exists('validation_errors'))
893{
894 function validation_errors($prefix = '', $suffix = '')
895 {
896 if (FALSE === ($OBJ =& _get_validation_object()))
897 {
898 return '';
899 }
900
901 return $OBJ->error_string($prefix, $suffix);
902 }
903}
904
905// ------------------------------------------------------------------------
906
907/**
908 * Parse the form attributes
909 *
910 * Helper function used by some of the form helpers
911 *
912 * @access private
913 * @param array
914 * @param array
915 * @return string
916 */
917if ( ! function_exists('_parse_form_attributes'))
918{
919 function _parse_form_attributes($attributes, $default)
920 {
921 if (is_array($attributes))
922 {
923 foreach ($default as $key => $val)
924 {
925 if (isset($attributes[$key]))
926 {
927 $default[$key] = $attributes[$key];
928 unset($attributes[$key]);
929 }
930 }
931
932 if (count($attributes) > 0)
933 {
934 $default = array_merge($default, $attributes);
935 }
936 }
937
938 $att = '';
Barry Mienydd671972010-10-04 16:33:58 +0200939
Derek Allard2067d1a2008-11-13 22:59:24 +0000940 foreach ($default as $key => $val)
941 {
942 if ($key == 'value')
943 {
Derek Jones01a9b102009-07-17 18:30:36 +0000944 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000945 }
946
947 $att .= $key . '="' . $val . '" ';
948 }
949
950 return $att;
951 }
952}
953
954// ------------------------------------------------------------------------
955
956/**
957 * Attributes To String
958 *
959 * Helper function used by some of the form helpers
960 *
961 * @access private
962 * @param mixed
963 * @param bool
964 * @return string
965 */
966if ( ! function_exists('_attributes_to_string'))
967{
968 function _attributes_to_string($attributes, $formtag = FALSE)
969 {
970 if (is_string($attributes) AND strlen($attributes) > 0)
971 {
972 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
973 {
974 $attributes .= ' method="post"';
975 }
976
Derek Allard3241d732009-09-17 12:17:45 +0000977 if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE)
978 {
979 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
980 }
981
Derek Allard2067d1a2008-11-13 22:59:24 +0000982 return ' '.$attributes;
983 }
Barry Mienydd671972010-10-04 16:33:58 +0200984
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 if (is_object($attributes) AND count($attributes) > 0)
986 {
987 $attributes = (array)$attributes;
988 }
989
990 if (is_array($attributes) AND count($attributes) > 0)
991 {
Derek Allard3241d732009-09-17 12:17:45 +0000992 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000993
Derek Allard3241d732009-09-17 12:17:45 +0000994 if ( ! isset($attributes['method']) AND $formtag === TRUE)
995 {
996 $atts .= ' method="post"';
997 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000998
Derek Allard3241d732009-09-17 12:17:45 +0000999 if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE)
1000 {
1001 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
1002 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001003
Derek Allard3241d732009-09-17 12:17:45 +00001004 foreach ($attributes as $key => $val)
1005 {
1006 $atts .= ' '.$key.'="'.$val.'"';
1007 }
1008
1009 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001010 }
1011 }
1012}
1013
1014// ------------------------------------------------------------------------
1015
1016/**
1017 * Validation Object
1018 *
1019 * Determines what the form validation class was instantiated as, fetches
1020 * the object and returns it.
1021 *
1022 * @access private
1023 * @return mixed
1024 */
1025if ( ! function_exists('_get_validation_object'))
1026{
1027 function &_get_validation_object()
1028 {
1029 $CI =& get_instance();
1030
1031 // We set this as a variable since we're returning by reference
1032 $return = FALSE;
1033
1034 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
1035 {
1036 return $return;
1037 }
1038
1039 $object = $CI->load->_ci_classes['form_validation'];
1040
1041 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1042 {
1043 return $return;
1044 }
1045
1046 return $CI->$object;
1047 }
1048}
1049
1050
1051/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001052/* Location: ./system/helpers/form_helper.php */