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