blob: 60d2631e94ca7e8f7f69d1d4a4240d2c55e2fc2f [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 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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
40 */
41if ( ! 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
52 $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
53
54 $form = '<form action="'.$action.'"';
55
56 $form .= _attributes_to_string($attributes, TRUE);
57
58 $form .= '>';
59
60 if (is_array($hidden) AND count($hidden) > 0)
61 {
62 $form .= form_hidden($hidden);
63 }
64
65 return $form;
66 }
67}
68
Derek Allard3241d732009-09-17 12:17:45 +000069
Derek Allard2067d1a2008-11-13 22:59:24 +000070// ------------------------------------------------------------------------
71
72/**
73 * Form Declaration - Multipart type
74 *
75 * Creates the opening portion of the form, but with "multipart/form-data".
76 *
77 * @access public
78 * @param string the URI segments of the form destination
79 * @param array a key/value pair of attributes
80 * @param array a key/value pair hidden data
81 * @return string
82 */
83if ( ! function_exists('form_open_multipart'))
84{
85 function form_open_multipart($action, $attributes = array(), $hidden = array())
86 {
Derek Allard33d4b6a2010-01-17 07:23:00 +000087 if (is_string($attributes))
88 {
89 $attributes .= ' enctype="multipart/form-data"';
90 }
91 else
92 {
93 $attributes['enctype'] = 'multipart/form-data';
94 }
95
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return form_open($action, $attributes, $hidden);
97 }
98}
99
100// ------------------------------------------------------------------------
101
102/**
103 * Hidden Input Field
104 *
105 * Generates hidden fields. You can pass a simple key/value string or an associative
106 * array with multiple values.
107 *
108 * @access public
109 * @param mixed
110 * @param string
111 * @return string
112 */
113if ( ! function_exists('form_hidden'))
114{
Robin Sowell57fe4102009-03-26 18:58:46 +0000115 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000117 static $form;
118
119 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000121 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
123
Robin Sowell57fe4102009-03-26 18:58:46 +0000124 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000126 foreach ($name as $key => $val)
127 {
128 form_hidden($key, $val, TRUE);
129 }
130 return $form;
131 }
132
133 if ( ! is_array($value))
134 {
Derek Jones01a9b102009-07-17 18:30:36 +0000135 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000136 }
137 else
138 {
139 foreach ($value as $k => $v)
140 {
141 $k = (is_int($k)) ? '' : $k;
142 form_hidden($name.'['.$k.']', $v, TRUE);
143 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145
146 return $form;
147 }
148}
149
150// ------------------------------------------------------------------------
151
152/**
153 * Text Input Field
154 *
155 * @access public
156 * @param mixed
157 * @param string
158 * @param string
159 * @return string
160 */
161if ( ! function_exists('form_input'))
162{
163 function form_input($data = '', $value = '', $extra = '')
164 {
165 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
166
167 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
168 }
169}
170
171// ------------------------------------------------------------------------
172
173/**
174 * Password Field
175 *
176 * Identical to the input function but adds the "password" type
177 *
178 * @access public
179 * @param mixed
180 * @param string
181 * @param string
182 * @return string
183 */
184if ( ! function_exists('form_password'))
185{
186 function form_password($data = '', $value = '', $extra = '')
187 {
188 if ( ! is_array($data))
189 {
190 $data = array('name' => $data);
191 }
192
193 $data['type'] = 'password';
194 return form_input($data, $value, $extra);
195 }
196}
197
198// ------------------------------------------------------------------------
199
200/**
201 * Upload Field
202 *
203 * Identical to the input function but adds the "file" type
204 *
205 * @access public
206 * @param mixed
207 * @param string
208 * @param string
209 * @return string
210 */
211if ( ! function_exists('form_upload'))
212{
213 function form_upload($data = '', $value = '', $extra = '')
214 {
215 if ( ! is_array($data))
216 {
217 $data = array('name' => $data);
218 }
219
220 $data['type'] = 'file';
221 return form_input($data, $value, $extra);
222 }
223}
224
225// ------------------------------------------------------------------------
226
227/**
228 * Textarea field
229 *
230 * @access public
231 * @param mixed
232 * @param string
233 * @param string
234 * @return string
235 */
236if ( ! function_exists('form_textarea'))
237{
238 function form_textarea($data = '', $value = '', $extra = '')
239 {
240 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
241
242 if ( ! is_array($data) OR ! isset($data['value']))
243 {
244 $val = $value;
245 }
246 else
247 {
248 $val = $data['value'];
249 unset($data['value']); // textareas don't use the value attribute
250 }
Derek Jones01a9b102009-07-17 18:30:36 +0000251
252 $name = (is_array($data)) ? $data['name'] : $data;
253 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
255}
256
257// ------------------------------------------------------------------------
258
259/**
Derek Jones26399292009-04-08 16:14:17 +0000260 * Multi-select menu
261 *
262 * @access public
263 * @param string
264 * @param array
265 * @param mixed
266 * @param string
267 * @return type
268 */
269if (! function_exists('form_multiselect'))
270{
271 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
272 {
273 if ( ! strpos($extra, 'multiple'))
274 {
275 $extra .= ' multiple="multiple"';
276 }
Derek Jones01a9b102009-07-17 18:30:36 +0000277
Derek Jones26399292009-04-08 16:14:17 +0000278 return form_dropdown($name, $options, $selected, $extra);
279 }
280}
281
282// --------------------------------------------------------------------
283
284/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * Drop-down Menu
286 *
287 * @access public
288 * @param string
289 * @param array
290 * @param string
291 * @param string
292 * @return string
293 */
294if ( ! function_exists('form_dropdown'))
295{
296 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
297 {
298 if ( ! is_array($selected))
299 {
300 $selected = array($selected);
301 }
302
303 // If no selected state was submitted we will attempt to set it automatically
304 if (count($selected) === 0)
305 {
306 // If the form name appears in the $_POST array we have a winner!
307 if (isset($_POST[$name]))
308 {
309 $selected = array($_POST[$name]);
310 }
311 }
312
313 if ($extra != '') $extra = ' '.$extra;
314
315 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
316
317 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 foreach ($options as $key => $val)
320 {
321 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
Derek Allard78a5fc92009-02-05 16:34:35 +0000323 if (is_array($val))
324 {
325 $form .= '<optgroup label="'.$key.'">'."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000326
Derek Allard78a5fc92009-02-05 16:34:35 +0000327 foreach ($val as $optgroup_key => $optgroup_val)
328 {
Derek Allard86a840c2009-02-05 17:31:58 +0000329 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000330
331 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
332 }
333
334 $form .= '</optgroup>'."\n";
335 }
336 else
337 {
338 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
339
340 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
341 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
343
344 $form .= '</select>';
345
346 return $form;
347 }
348}
349
350// ------------------------------------------------------------------------
351
352/**
353 * Checkbox Field
354 *
355 * @access public
356 * @param mixed
357 * @param string
358 * @param bool
359 * @param string
360 * @return string
361 */
362if ( ! function_exists('form_checkbox'))
363{
364 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
365 {
366 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
367
368 if (is_array($data) AND array_key_exists('checked', $data))
369 {
370 $checked = $data['checked'];
371
372 if ($checked == FALSE)
373 {
374 unset($data['checked']);
375 }
376 else
377 {
378 $data['checked'] = 'checked';
379 }
380 }
381
382 if ($checked == TRUE)
383 {
384 $defaults['checked'] = 'checked';
385 }
386 else
387 {
388 unset($defaults['checked']);
389 }
390
391 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
392 }
393}
394
395// ------------------------------------------------------------------------
396
397/**
398 * Radio Button
399 *
400 * @access public
401 * @param mixed
402 * @param string
403 * @param bool
404 * @param string
405 * @return string
406 */
407if ( ! function_exists('form_radio'))
408{
409 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
410 {
411 if ( ! is_array($data))
412 {
413 $data = array('name' => $data);
414 }
415
416 $data['type'] = 'radio';
417 return form_checkbox($data, $value, $checked, $extra);
418 }
419}
420
421// ------------------------------------------------------------------------
422
423/**
424 * Submit Button
425 *
426 * @access public
427 * @param mixed
428 * @param string
429 * @param string
430 * @return string
431 */
432if ( ! function_exists('form_submit'))
433{
434 function form_submit($data = '', $value = '', $extra = '')
435 {
436 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
437
438 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
439 }
440}
441
442// ------------------------------------------------------------------------
443
444/**
445 * Reset Button
446 *
447 * @access public
448 * @param mixed
449 * @param string
450 * @param string
451 * @return string
452 */
453if ( ! function_exists('form_reset'))
454{
455 function form_reset($data = '', $value = '', $extra = '')
456 {
457 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
458
459 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
460 }
461}
462
463// ------------------------------------------------------------------------
464
465/**
466 * Form Button
467 *
468 * @access public
469 * @param mixed
470 * @param string
471 * @param string
472 * @return string
473 */
474if ( ! function_exists('form_button'))
475{
476 function form_button($data = '', $content = '', $extra = '')
477 {
Derek Allard904094a2009-02-10 14:00:34 +0000478 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
Derek Allard2067d1a2008-11-13 22:59:24 +0000479
480 if ( is_array($data) AND isset($data['content']))
481 {
482 $content = $data['content'];
483 unset($data['content']); // content is not an attribute
484 }
485
486 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
487 }
488}
489
490// ------------------------------------------------------------------------
491
492/**
493 * Form Label Tag
494 *
495 * @access public
496 * @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 {
510 $label .= " for=\"$id\"";
511 }
512
513 if (is_array($attributes) AND count($attributes) > 0)
514 {
515 foreach ($attributes as $key => $val)
516 {
517 $label .= ' '.$key.'="'.$val.'"';
518 }
519 }
520
521 $label .= ">$label_text</label>";
522
523 return $label;
524 }
525}
526
527// ------------------------------------------------------------------------
528/**
529 * Fieldset Tag
530 *
531 * Used to produce <fieldset><legend>text</legend>. To close fieldset
532 * use form_fieldset_close()
533 *
534 * @access public
535 * @param string The legend text
536 * @param string Additional attributes
537 * @return string
538 */
539if ( ! function_exists('form_fieldset'))
540{
541 function form_fieldset($legend_text = '', $attributes = array())
542 {
543 $fieldset = "<fieldset";
544
545 $fieldset .= _attributes_to_string($attributes, FALSE);
546
547 $fieldset .= ">\n";
548
549 if ($legend_text != '')
550 {
551 $fieldset .= "<legend>$legend_text</legend>\n";
552 }
553
554 return $fieldset;
555 }
556}
557
558// ------------------------------------------------------------------------
559
560/**
561 * Fieldset Close Tag
562 *
563 * @access public
564 * @param string
565 * @return string
566 */
567if ( ! function_exists('form_fieldset_close'))
568{
569 function form_fieldset_close($extra = '')
570 {
571 return "</fieldset>".$extra;
572 }
573}
574
575// ------------------------------------------------------------------------
576
577/**
578 * Form Close Tag
579 *
580 * @access public
581 * @param string
582 * @return string
583 */
584if ( ! function_exists('form_close'))
585{
586 function form_close($extra = '')
587 {
588 return "</form>".$extra;
589 }
590}
591
592// ------------------------------------------------------------------------
593
594/**
595 * Form Prep
596 *
597 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
598 *
599 * @access public
600 * @param string
601 * @return string
602 */
603if ( ! function_exists('form_prep'))
604{
Derek Jones01a9b102009-07-17 18:30:36 +0000605 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Derek Jones01a9b102009-07-17 18:30:36 +0000607 static $prepped_fields = array();
608
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 // if the field name is an array we do this recursively
610 if (is_array($str))
611 {
612 foreach ($str as $key => $val)
613 {
614 $str[$key] = form_prep($val);
615 }
616
617 return $str;
618 }
619
620 if ($str === '')
621 {
622 return '';
623 }
624
Derek Jones3eb9fac2009-07-17 20:25:13 +0000625 // we've already prepped a field with this name
626 // @todo need to figure out a way to namespace this so
627 // that we know the *exact* field and not just one with
628 // the same name
Derek Jones01a9b102009-07-17 18:30:36 +0000629 if (isset($prepped_fields[$field_name]))
630 {
Derek Jones3eb9fac2009-07-17 20:25:13 +0000631 return $str;
Derek Jones01a9b102009-07-17 18:30:36 +0000632 }
633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 $str = htmlspecialchars($str);
635
636 // In case htmlspecialchars misses these.
637 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
638
Derek Jones01a9b102009-07-17 18:30:36 +0000639 if ($field_name != '')
640 {
641 $prepped_fields[$field_name] = $str;
642 }
643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 return $str;
645 }
646}
647
648// ------------------------------------------------------------------------
649
650/**
651 * Form Value
652 *
653 * Grabs a value from the POST array for the specified field so you can
654 * re-populate an input field or textarea. If Form Validation
655 * is active it retrieves the info from the validation class
656 *
657 * @access public
658 * @param string
659 * @return mixed
660 */
661if ( ! function_exists('set_value'))
662{
663 function set_value($field = '', $default = '')
664 {
665 if (FALSE === ($OBJ =& _get_validation_object()))
666 {
667 if ( ! isset($_POST[$field]))
668 {
669 return $default;
670 }
671
Derek Jones01a9b102009-07-17 18:30:36 +0000672 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
674
Derek Jones01a9b102009-07-17 18:30:36 +0000675 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677}
678
679// ------------------------------------------------------------------------
680
681/**
682 * Set Select
683 *
684 * Let's you set the selected value of a <select> menu via data in the POST array.
685 * If Form Validation is active it retrieves the info from the validation class
686 *
687 * @access public
688 * @param string
689 * @param string
690 * @param bool
691 * @return string
692 */
693if ( ! function_exists('set_select'))
694{
695 function set_select($field = '', $value = '', $default = FALSE)
696 {
697 $OBJ =& _get_validation_object();
698
699 if ($OBJ === FALSE)
700 {
701 if ( ! isset($_POST[$field]))
702 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000703 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
705 return ' selected="selected"';
706 }
707 return '';
708 }
709
710 $field = $_POST[$field];
711
712 if (is_array($field))
713 {
714 if ( ! in_array($value, $field))
715 {
716 return '';
717 }
718 }
719 else
720 {
721 if (($field == '' OR $value == '') OR ($field != $value))
722 {
723 return '';
724 }
725 }
726
727 return ' selected="selected"';
728 }
729
730 return $OBJ->set_select($field, $value, $default);
731 }
732}
733
734// ------------------------------------------------------------------------
735
736/**
737 * Set Checkbox
738 *
739 * Let's you set the selected value of a checkbox via the value in the POST array.
740 * If Form Validation is active it retrieves the info from the validation class
741 *
742 * @access public
743 * @param string
744 * @param string
745 * @param bool
746 * @return string
747 */
748if ( ! function_exists('set_checkbox'))
749{
750 function set_checkbox($field = '', $value = '', $default = FALSE)
751 {
752 $OBJ =& _get_validation_object();
753
754 if ($OBJ === FALSE)
755 {
756 if ( ! isset($_POST[$field]))
757 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000758 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
760 return ' checked="checked"';
761 }
762 return '';
763 }
764
765 $field = $_POST[$field];
766
767 if (is_array($field))
768 {
769 if ( ! in_array($value, $field))
770 {
771 return '';
772 }
773 }
774 else
775 {
776 if (($field == '' OR $value == '') OR ($field != $value))
777 {
778 return '';
779 }
780 }
781
782 return ' checked="checked"';
783 }
784
785 return $OBJ->set_checkbox($field, $value, $default);
786 }
787}
788
789// ------------------------------------------------------------------------
790
791/**
792 * Set Radio
793 *
794 * Let's you set the selected value of a radio field via info in the POST array.
795 * If Form Validation is active it retrieves the info from the validation class
796 *
797 * @access public
798 * @param string
799 * @param string
800 * @param bool
801 * @return string
802 */
803if ( ! function_exists('set_radio'))
804{
805 function set_radio($field = '', $value = '', $default = FALSE)
806 {
807 $OBJ =& _get_validation_object();
808
809 if ($OBJ === FALSE)
810 {
811 if ( ! isset($_POST[$field]))
812 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000813 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 {
815 return ' checked="checked"';
816 }
817 return '';
818 }
819
820 $field = $_POST[$field];
821
822 if (is_array($field))
823 {
824 if ( ! in_array($value, $field))
825 {
826 return '';
827 }
828 }
829 else
830 {
831 if (($field == '' OR $value == '') OR ($field != $value))
832 {
833 return '';
834 }
835 }
836
837 return ' checked="checked"';
838 }
839
840 return $OBJ->set_radio($field, $value, $default);
841 }
842}
843
844// ------------------------------------------------------------------------
845
846/**
847 * Form Error
848 *
849 * Returns the error for a specific form field. This is a helper for the
850 * form validation class.
851 *
852 * @access public
853 * @param string
854 * @param string
855 * @param string
856 * @return string
857 */
858if ( ! function_exists('form_error'))
859{
860 function form_error($field = '', $prefix = '', $suffix = '')
861 {
862 if (FALSE === ($OBJ =& _get_validation_object()))
863 {
864 return '';
865 }
866
867 return $OBJ->error($field, $prefix, $suffix);
868 }
869}
870
871// ------------------------------------------------------------------------
872
873/**
874 * Validation Error String
875 *
876 * Returns all the errors associated with a form submission. This is a helper
877 * function for the form validation class.
878 *
879 * @access public
880 * @param string
881 * @param string
882 * @return string
883 */
884if ( ! function_exists('validation_errors'))
885{
886 function validation_errors($prefix = '', $suffix = '')
887 {
888 if (FALSE === ($OBJ =& _get_validation_object()))
889 {
890 return '';
891 }
892
893 return $OBJ->error_string($prefix, $suffix);
894 }
895}
896
897// ------------------------------------------------------------------------
898
899/**
900 * Parse the form attributes
901 *
902 * Helper function used by some of the form helpers
903 *
904 * @access private
905 * @param array
906 * @param array
907 * @return string
908 */
909if ( ! function_exists('_parse_form_attributes'))
910{
911 function _parse_form_attributes($attributes, $default)
912 {
913 if (is_array($attributes))
914 {
915 foreach ($default as $key => $val)
916 {
917 if (isset($attributes[$key]))
918 {
919 $default[$key] = $attributes[$key];
920 unset($attributes[$key]);
921 }
922 }
923
924 if (count($attributes) > 0)
925 {
926 $default = array_merge($default, $attributes);
927 }
928 }
929
930 $att = '';
Derek Jones01a9b102009-07-17 18:30:36 +0000931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 foreach ($default as $key => $val)
933 {
934 if ($key == 'value')
935 {
Derek Jones01a9b102009-07-17 18:30:36 +0000936 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000937 }
938
939 $att .= $key . '="' . $val . '" ';
940 }
941
942 return $att;
943 }
944}
945
946// ------------------------------------------------------------------------
947
948/**
949 * Attributes To String
950 *
951 * Helper function used by some of the form helpers
952 *
953 * @access private
954 * @param mixed
955 * @param bool
956 * @return string
957 */
958if ( ! function_exists('_attributes_to_string'))
959{
960 function _attributes_to_string($attributes, $formtag = FALSE)
961 {
962 if (is_string($attributes) AND strlen($attributes) > 0)
963 {
964 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
965 {
966 $attributes .= ' method="post"';
967 }
968
Derek Allard3241d732009-09-17 12:17:45 +0000969 if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE)
970 {
971 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
972 }
973
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 return ' '.$attributes;
975 }
976
977 if (is_object($attributes) AND count($attributes) > 0)
978 {
979 $attributes = (array)$attributes;
980 }
981
982 if (is_array($attributes) AND count($attributes) > 0)
983 {
Derek Allard3241d732009-09-17 12:17:45 +0000984 $atts = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000985
Derek Allard3241d732009-09-17 12:17:45 +0000986 if ( ! isset($attributes['method']) AND $formtag === TRUE)
987 {
988 $atts .= ' method="post"';
989 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000990
Derek Allard3241d732009-09-17 12:17:45 +0000991 if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE)
992 {
993 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
994 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000995
Derek Allard3241d732009-09-17 12:17:45 +0000996 foreach ($attributes as $key => $val)
997 {
998 $atts .= ' '.$key.'="'.$val.'"';
999 }
1000
1001 return $atts;
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 }
1003 }
1004}
1005
1006// ------------------------------------------------------------------------
1007
1008/**
1009 * Validation Object
1010 *
1011 * Determines what the form validation class was instantiated as, fetches
1012 * the object and returns it.
1013 *
1014 * @access private
1015 * @return mixed
1016 */
1017if ( ! function_exists('_get_validation_object'))
1018{
1019 function &_get_validation_object()
1020 {
1021 $CI =& get_instance();
1022
1023 // We set this as a variable since we're returning by reference
1024 $return = FALSE;
1025
1026 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
1027 {
1028 return $return;
1029 }
1030
1031 $object = $CI->load->_ci_classes['form_validation'];
1032
1033 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1034 {
1035 return $return;
1036 }
1037
1038 return $CI->$object;
1039 }
1040}
1041
1042
1043/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001044/* Location: ./system/helpers/form_helper.php */