blob: 4c229ae9faaa7f391a1e96ffb971790c0923bda4 [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 Jonesfc395a12009-04-22 14:15:09 +00009 * @copyright Copyright (c) 2008 - 2009, 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 {
49 $attributes = 'method="post"';
50 }
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 {
86 $attributes['enctype'] = 'multipart/form-data';
87 return form_open($action, $attributes, $hidden);
88 }
89}
90
91// ------------------------------------------------------------------------
92
93/**
94 * Hidden Input Field
95 *
96 * Generates hidden fields. You can pass a simple key/value string or an associative
97 * array with multiple values.
98 *
99 * @access public
100 * @param mixed
101 * @param string
102 * @return string
103 */
104if ( ! function_exists('form_hidden'))
105{
Robin Sowell57fe4102009-03-26 18:58:46 +0000106 function form_hidden($name, $value = '', $recursing = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000108 static $form;
109
110 if ($recursing === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000112 $form = "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114
Robin Sowell57fe4102009-03-26 18:58:46 +0000115 if (is_array($name))
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Robin Sowell57fe4102009-03-26 18:58:46 +0000117 foreach ($name as $key => $val)
118 {
119 form_hidden($key, $val, TRUE);
120 }
121 return $form;
122 }
123
124 if ( ! is_array($value))
125 {
Derek Jones01a9b102009-07-17 18:30:36 +0000126 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000127 }
128 else
129 {
130 foreach ($value as $k => $v)
131 {
132 $k = (is_int($k)) ? '' : $k;
133 form_hidden($name.'['.$k.']', $v, TRUE);
134 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
136
137 return $form;
138 }
139}
140
141// ------------------------------------------------------------------------
142
143/**
144 * Text Input Field
145 *
146 * @access public
147 * @param mixed
148 * @param string
149 * @param string
150 * @return string
151 */
152if ( ! function_exists('form_input'))
153{
154 function form_input($data = '', $value = '', $extra = '')
155 {
156 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
157
158 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
159 }
160}
161
162// ------------------------------------------------------------------------
163
164/**
165 * Password Field
166 *
167 * Identical to the input function but adds the "password" type
168 *
169 * @access public
170 * @param mixed
171 * @param string
172 * @param string
173 * @return string
174 */
175if ( ! function_exists('form_password'))
176{
177 function form_password($data = '', $value = '', $extra = '')
178 {
179 if ( ! is_array($data))
180 {
181 $data = array('name' => $data);
182 }
183
184 $data['type'] = 'password';
185 return form_input($data, $value, $extra);
186 }
187}
188
189// ------------------------------------------------------------------------
190
191/**
192 * Upload Field
193 *
194 * Identical to the input function but adds the "file" type
195 *
196 * @access public
197 * @param mixed
198 * @param string
199 * @param string
200 * @return string
201 */
202if ( ! function_exists('form_upload'))
203{
204 function form_upload($data = '', $value = '', $extra = '')
205 {
206 if ( ! is_array($data))
207 {
208 $data = array('name' => $data);
209 }
210
211 $data['type'] = 'file';
212 return form_input($data, $value, $extra);
213 }
214}
215
216// ------------------------------------------------------------------------
217
218/**
219 * Textarea field
220 *
221 * @access public
222 * @param mixed
223 * @param string
224 * @param string
225 * @return string
226 */
227if ( ! function_exists('form_textarea'))
228{
229 function form_textarea($data = '', $value = '', $extra = '')
230 {
231 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
232
233 if ( ! is_array($data) OR ! isset($data['value']))
234 {
235 $val = $value;
236 }
237 else
238 {
239 $val = $data['value'];
240 unset($data['value']); // textareas don't use the value attribute
241 }
Derek Jones01a9b102009-07-17 18:30:36 +0000242
243 $name = (is_array($data)) ? $data['name'] : $data;
244 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246}
247
248// ------------------------------------------------------------------------
249
250/**
Derek Jones26399292009-04-08 16:14:17 +0000251 * Multi-select menu
252 *
253 * @access public
254 * @param string
255 * @param array
256 * @param mixed
257 * @param string
258 * @return type
259 */
260if (! function_exists('form_multiselect'))
261{
262 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
263 {
264 if ( ! strpos($extra, 'multiple'))
265 {
266 $extra .= ' multiple="multiple"';
267 }
Derek Jones01a9b102009-07-17 18:30:36 +0000268
Derek Jones26399292009-04-08 16:14:17 +0000269 return form_dropdown($name, $options, $selected, $extra);
270 }
271}
272
273// --------------------------------------------------------------------
274
275/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * Drop-down Menu
277 *
278 * @access public
279 * @param string
280 * @param array
281 * @param string
282 * @param string
283 * @return string
284 */
285if ( ! function_exists('form_dropdown'))
286{
287 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
288 {
289 if ( ! is_array($selected))
290 {
291 $selected = array($selected);
292 }
293
294 // If no selected state was submitted we will attempt to set it automatically
295 if (count($selected) === 0)
296 {
297 // If the form name appears in the $_POST array we have a winner!
298 if (isset($_POST[$name]))
299 {
300 $selected = array($_POST[$name]);
301 }
302 }
303
304 if ($extra != '') $extra = ' '.$extra;
305
306 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
307
308 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
Robin Sowell57fe4102009-03-26 18:58:46 +0000309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 foreach ($options as $key => $val)
311 {
312 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000313
Derek Allard78a5fc92009-02-05 16:34:35 +0000314 if (is_array($val))
315 {
316 $form .= '<optgroup label="'.$key.'">'."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000317
Derek Allard78a5fc92009-02-05 16:34:35 +0000318 foreach ($val as $optgroup_key => $optgroup_val)
319 {
Derek Allard86a840c2009-02-05 17:31:58 +0000320 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
Derek Allard78a5fc92009-02-05 16:34:35 +0000321
322 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
323 }
324
325 $form .= '</optgroup>'."\n";
326 }
327 else
328 {
329 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
330
331 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
332 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334
335 $form .= '</select>';
336
337 return $form;
338 }
339}
340
341// ------------------------------------------------------------------------
342
343/**
344 * Checkbox Field
345 *
346 * @access public
347 * @param mixed
348 * @param string
349 * @param bool
350 * @param string
351 * @return string
352 */
353if ( ! function_exists('form_checkbox'))
354{
355 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
356 {
357 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
358
359 if (is_array($data) AND array_key_exists('checked', $data))
360 {
361 $checked = $data['checked'];
362
363 if ($checked == FALSE)
364 {
365 unset($data['checked']);
366 }
367 else
368 {
369 $data['checked'] = 'checked';
370 }
371 }
372
373 if ($checked == TRUE)
374 {
375 $defaults['checked'] = 'checked';
376 }
377 else
378 {
379 unset($defaults['checked']);
380 }
381
382 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
383 }
384}
385
386// ------------------------------------------------------------------------
387
388/**
389 * Radio Button
390 *
391 * @access public
392 * @param mixed
393 * @param string
394 * @param bool
395 * @param string
396 * @return string
397 */
398if ( ! function_exists('form_radio'))
399{
400 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
401 {
402 if ( ! is_array($data))
403 {
404 $data = array('name' => $data);
405 }
406
407 $data['type'] = 'radio';
408 return form_checkbox($data, $value, $checked, $extra);
409 }
410}
411
412// ------------------------------------------------------------------------
413
414/**
415 * Submit Button
416 *
417 * @access public
418 * @param mixed
419 * @param string
420 * @param string
421 * @return string
422 */
423if ( ! function_exists('form_submit'))
424{
425 function form_submit($data = '', $value = '', $extra = '')
426 {
427 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
428
429 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
430 }
431}
432
433// ------------------------------------------------------------------------
434
435/**
436 * Reset Button
437 *
438 * @access public
439 * @param mixed
440 * @param string
441 * @param string
442 * @return string
443 */
444if ( ! function_exists('form_reset'))
445{
446 function form_reset($data = '', $value = '', $extra = '')
447 {
448 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
449
450 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
451 }
452}
453
454// ------------------------------------------------------------------------
455
456/**
457 * Form Button
458 *
459 * @access public
460 * @param mixed
461 * @param string
462 * @param string
463 * @return string
464 */
465if ( ! function_exists('form_button'))
466{
467 function form_button($data = '', $content = '', $extra = '')
468 {
Derek Allard904094a2009-02-10 14:00:34 +0000469 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
471 if ( is_array($data) AND isset($data['content']))
472 {
473 $content = $data['content'];
474 unset($data['content']); // content is not an attribute
475 }
476
477 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
478 }
479}
480
481// ------------------------------------------------------------------------
482
483/**
484 * Form Label Tag
485 *
486 * @access public
487 * @param string The text to appear onscreen
488 * @param string The id the label applies to
489 * @param string Additional attributes
490 * @return string
491 */
492if ( ! function_exists('form_label'))
493{
494 function form_label($label_text = '', $id = '', $attributes = array())
495 {
496
497 $label = '<label';
498
499 if ($id != '')
500 {
501 $label .= " for=\"$id\"";
502 }
503
504 if (is_array($attributes) AND count($attributes) > 0)
505 {
506 foreach ($attributes as $key => $val)
507 {
508 $label .= ' '.$key.'="'.$val.'"';
509 }
510 }
511
512 $label .= ">$label_text</label>";
513
514 return $label;
515 }
516}
517
518// ------------------------------------------------------------------------
519/**
520 * Fieldset Tag
521 *
522 * Used to produce <fieldset><legend>text</legend>. To close fieldset
523 * use form_fieldset_close()
524 *
525 * @access public
526 * @param string The legend text
527 * @param string Additional attributes
528 * @return string
529 */
530if ( ! function_exists('form_fieldset'))
531{
532 function form_fieldset($legend_text = '', $attributes = array())
533 {
534 $fieldset = "<fieldset";
535
536 $fieldset .= _attributes_to_string($attributes, FALSE);
537
538 $fieldset .= ">\n";
539
540 if ($legend_text != '')
541 {
542 $fieldset .= "<legend>$legend_text</legend>\n";
543 }
544
545 return $fieldset;
546 }
547}
548
549// ------------------------------------------------------------------------
550
551/**
552 * Fieldset Close Tag
553 *
554 * @access public
555 * @param string
556 * @return string
557 */
558if ( ! function_exists('form_fieldset_close'))
559{
560 function form_fieldset_close($extra = '')
561 {
562 return "</fieldset>".$extra;
563 }
564}
565
566// ------------------------------------------------------------------------
567
568/**
569 * Form Close Tag
570 *
571 * @access public
572 * @param string
573 * @return string
574 */
575if ( ! function_exists('form_close'))
576{
577 function form_close($extra = '')
578 {
579 return "</form>".$extra;
580 }
581}
582
583// ------------------------------------------------------------------------
584
585/**
586 * Form Prep
587 *
588 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
589 *
590 * @access public
591 * @param string
592 * @return string
593 */
594if ( ! function_exists('form_prep'))
595{
Derek Jones01a9b102009-07-17 18:30:36 +0000596 function form_prep($str = '', $field_name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
Derek Jones01a9b102009-07-17 18:30:36 +0000598 static $prepped_fields = array();
599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 // if the field name is an array we do this recursively
601 if (is_array($str))
602 {
603 foreach ($str as $key => $val)
604 {
605 $str[$key] = form_prep($val);
606 }
607
608 return $str;
609 }
610
611 if ($str === '')
612 {
613 return '';
614 }
615
Derek Jones01a9b102009-07-17 18:30:36 +0000616 if (isset($prepped_fields[$field_name]))
617 {
618 return $prepped_fields[$field_name];
619 }
620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 $str = htmlspecialchars($str);
622
623 // In case htmlspecialchars misses these.
624 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
625
Derek Jones01a9b102009-07-17 18:30:36 +0000626 if ($field_name != '')
627 {
628 $prepped_fields[$field_name] = $str;
629 }
630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 return $str;
632 }
633}
634
635// ------------------------------------------------------------------------
636
637/**
638 * Form Value
639 *
640 * Grabs a value from the POST array for the specified field so you can
641 * re-populate an input field or textarea. If Form Validation
642 * is active it retrieves the info from the validation class
643 *
644 * @access public
645 * @param string
646 * @return mixed
647 */
648if ( ! function_exists('set_value'))
649{
650 function set_value($field = '', $default = '')
651 {
652 if (FALSE === ($OBJ =& _get_validation_object()))
653 {
654 if ( ! isset($_POST[$field]))
655 {
656 return $default;
657 }
658
Derek Jones01a9b102009-07-17 18:30:36 +0000659 return form_prep($_POST[$field], $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661
Derek Jones01a9b102009-07-17 18:30:36 +0000662 return form_prep($OBJ->set_value($field, $default), $field);
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 }
664}
665
666// ------------------------------------------------------------------------
667
668/**
669 * Set Select
670 *
671 * Let's you set the selected value of a <select> menu via data in the POST array.
672 * If Form Validation is active it retrieves the info from the validation class
673 *
674 * @access public
675 * @param string
676 * @param string
677 * @param bool
678 * @return string
679 */
680if ( ! function_exists('set_select'))
681{
682 function set_select($field = '', $value = '', $default = FALSE)
683 {
684 $OBJ =& _get_validation_object();
685
686 if ($OBJ === FALSE)
687 {
688 if ( ! isset($_POST[$field]))
689 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000690 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 {
692 return ' selected="selected"';
693 }
694 return '';
695 }
696
697 $field = $_POST[$field];
698
699 if (is_array($field))
700 {
701 if ( ! in_array($value, $field))
702 {
703 return '';
704 }
705 }
706 else
707 {
708 if (($field == '' OR $value == '') OR ($field != $value))
709 {
710 return '';
711 }
712 }
713
714 return ' selected="selected"';
715 }
716
717 return $OBJ->set_select($field, $value, $default);
718 }
719}
720
721// ------------------------------------------------------------------------
722
723/**
724 * Set Checkbox
725 *
726 * Let's you set the selected value of a checkbox via the value in the POST array.
727 * If Form Validation is active it retrieves the info from the validation class
728 *
729 * @access public
730 * @param string
731 * @param string
732 * @param bool
733 * @return string
734 */
735if ( ! function_exists('set_checkbox'))
736{
737 function set_checkbox($field = '', $value = '', $default = FALSE)
738 {
739 $OBJ =& _get_validation_object();
740
741 if ($OBJ === FALSE)
742 {
743 if ( ! isset($_POST[$field]))
744 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000745 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
747 return ' checked="checked"';
748 }
749 return '';
750 }
751
752 $field = $_POST[$field];
753
754 if (is_array($field))
755 {
756 if ( ! in_array($value, $field))
757 {
758 return '';
759 }
760 }
761 else
762 {
763 if (($field == '' OR $value == '') OR ($field != $value))
764 {
765 return '';
766 }
767 }
768
769 return ' checked="checked"';
770 }
771
772 return $OBJ->set_checkbox($field, $value, $default);
773 }
774}
775
776// ------------------------------------------------------------------------
777
778/**
779 * Set Radio
780 *
781 * Let's you set the selected value of a radio field via info in the POST array.
782 * If Form Validation is active it retrieves the info from the validation class
783 *
784 * @access public
785 * @param string
786 * @param string
787 * @param bool
788 * @return string
789 */
790if ( ! function_exists('set_radio'))
791{
792 function set_radio($field = '', $value = '', $default = FALSE)
793 {
794 $OBJ =& _get_validation_object();
795
796 if ($OBJ === FALSE)
797 {
798 if ( ! isset($_POST[$field]))
799 {
Rick Ellis28e5c8f2009-03-09 22:36:48 +0000800 if (count($_POST) === 0 AND $default == TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
802 return ' checked="checked"';
803 }
804 return '';
805 }
806
807 $field = $_POST[$field];
808
809 if (is_array($field))
810 {
811 if ( ! in_array($value, $field))
812 {
813 return '';
814 }
815 }
816 else
817 {
818 if (($field == '' OR $value == '') OR ($field != $value))
819 {
820 return '';
821 }
822 }
823
824 return ' checked="checked"';
825 }
826
827 return $OBJ->set_radio($field, $value, $default);
828 }
829}
830
831// ------------------------------------------------------------------------
832
833/**
834 * Form Error
835 *
836 * Returns the error for a specific form field. This is a helper for the
837 * form validation class.
838 *
839 * @access public
840 * @param string
841 * @param string
842 * @param string
843 * @return string
844 */
845if ( ! function_exists('form_error'))
846{
847 function form_error($field = '', $prefix = '', $suffix = '')
848 {
849 if (FALSE === ($OBJ =& _get_validation_object()))
850 {
851 return '';
852 }
853
854 return $OBJ->error($field, $prefix, $suffix);
855 }
856}
857
858// ------------------------------------------------------------------------
859
860/**
861 * Validation Error String
862 *
863 * Returns all the errors associated with a form submission. This is a helper
864 * function for the form validation class.
865 *
866 * @access public
867 * @param string
868 * @param string
869 * @return string
870 */
871if ( ! function_exists('validation_errors'))
872{
873 function validation_errors($prefix = '', $suffix = '')
874 {
875 if (FALSE === ($OBJ =& _get_validation_object()))
876 {
877 return '';
878 }
879
880 return $OBJ->error_string($prefix, $suffix);
881 }
882}
883
884// ------------------------------------------------------------------------
885
886/**
887 * Parse the form attributes
888 *
889 * Helper function used by some of the form helpers
890 *
891 * @access private
892 * @param array
893 * @param array
894 * @return string
895 */
896if ( ! function_exists('_parse_form_attributes'))
897{
898 function _parse_form_attributes($attributes, $default)
899 {
900 if (is_array($attributes))
901 {
902 foreach ($default as $key => $val)
903 {
904 if (isset($attributes[$key]))
905 {
906 $default[$key] = $attributes[$key];
907 unset($attributes[$key]);
908 }
909 }
910
911 if (count($attributes) > 0)
912 {
913 $default = array_merge($default, $attributes);
914 }
915 }
916
917 $att = '';
Derek Jones01a9b102009-07-17 18:30:36 +0000918
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 foreach ($default as $key => $val)
920 {
921 if ($key == 'value')
922 {
Derek Jones01a9b102009-07-17 18:30:36 +0000923 $val = form_prep($val, $default['name']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000924 }
925
926 $att .= $key . '="' . $val . '" ';
927 }
928
929 return $att;
930 }
931}
932
933// ------------------------------------------------------------------------
934
935/**
936 * Attributes To String
937 *
938 * Helper function used by some of the form helpers
939 *
940 * @access private
941 * @param mixed
942 * @param bool
943 * @return string
944 */
945if ( ! function_exists('_attributes_to_string'))
946{
947 function _attributes_to_string($attributes, $formtag = FALSE)
948 {
949 if (is_string($attributes) AND strlen($attributes) > 0)
950 {
951 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
952 {
953 $attributes .= ' method="post"';
954 }
955
956 return ' '.$attributes;
957 }
958
959 if (is_object($attributes) AND count($attributes) > 0)
960 {
961 $attributes = (array)$attributes;
962 }
963
964 if (is_array($attributes) AND count($attributes) > 0)
965 {
966 $atts = '';
967
968 if ( ! isset($attributes['method']) AND $formtag === TRUE)
969 {
970 $atts .= ' method="post"';
971 }
972
973 foreach ($attributes as $key => $val)
974 {
975 $atts .= ' '.$key.'="'.$val.'"';
976 }
977
978 return $atts;
979 }
980 }
981}
982
983// ------------------------------------------------------------------------
984
985/**
986 * Validation Object
987 *
988 * Determines what the form validation class was instantiated as, fetches
989 * the object and returns it.
990 *
991 * @access private
992 * @return mixed
993 */
994if ( ! function_exists('_get_validation_object'))
995{
996 function &_get_validation_object()
997 {
998 $CI =& get_instance();
999
1000 // We set this as a variable since we're returning by reference
1001 $return = FALSE;
1002
1003 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
1004 {
1005 return $return;
1006 }
1007
1008 $object = $CI->load->_ci_classes['form_validation'];
1009
1010 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1011 {
1012 return $return;
1013 }
1014
1015 return $CI->$object;
1016 }
1017}
1018
1019
1020/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001021/* Location: ./system/helpers/form_helper.php */