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