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