blob: 01d0ea76d8bb006f655efef7f49f50926850eb8a [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
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
10 * @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{
106 function form_hidden($name, $value = '')
107 {
108 if ( ! is_array($name))
109 {
110 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
111 }
112
113 $form = '';
114
115 foreach ($name as $name => $value)
116 {
117 $form .= "\n";
118 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
119 }
120
121 return $form;
122 }
123}
124
125// ------------------------------------------------------------------------
126
127/**
128 * Text Input Field
129 *
130 * @access public
131 * @param mixed
132 * @param string
133 * @param string
134 * @return string
135 */
136if ( ! function_exists('form_input'))
137{
138 function form_input($data = '', $value = '', $extra = '')
139 {
140 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
141
142 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
143 }
144}
145
146// ------------------------------------------------------------------------
147
148/**
149 * Password Field
150 *
151 * Identical to the input function but adds the "password" type
152 *
153 * @access public
154 * @param mixed
155 * @param string
156 * @param string
157 * @return string
158 */
159if ( ! function_exists('form_password'))
160{
161 function form_password($data = '', $value = '', $extra = '')
162 {
163 if ( ! is_array($data))
164 {
165 $data = array('name' => $data);
166 }
167
168 $data['type'] = 'password';
169 return form_input($data, $value, $extra);
170 }
171}
172
173// ------------------------------------------------------------------------
174
175/**
176 * Upload Field
177 *
178 * Identical to the input function but adds the "file" type
179 *
180 * @access public
181 * @param mixed
182 * @param string
183 * @param string
184 * @return string
185 */
186if ( ! function_exists('form_upload'))
187{
188 function form_upload($data = '', $value = '', $extra = '')
189 {
190 if ( ! is_array($data))
191 {
192 $data = array('name' => $data);
193 }
194
195 $data['type'] = 'file';
196 return form_input($data, $value, $extra);
197 }
198}
199
200// ------------------------------------------------------------------------
201
202/**
203 * Textarea field
204 *
205 * @access public
206 * @param mixed
207 * @param string
208 * @param string
209 * @return string
210 */
211if ( ! function_exists('form_textarea'))
212{
213 function form_textarea($data = '', $value = '', $extra = '')
214 {
215 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
216
217 if ( ! is_array($data) OR ! isset($data['value']))
218 {
219 $val = $value;
220 }
221 else
222 {
223 $val = $data['value'];
224 unset($data['value']); // textareas don't use the value attribute
225 }
226
227 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>";
228 }
229}
230
231// ------------------------------------------------------------------------
232
233/**
234 * Drop-down Menu
235 *
236 * @access public
237 * @param string
238 * @param array
239 * @param string
240 * @param string
241 * @return string
242 */
243if ( ! function_exists('form_dropdown'))
244{
245 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
246 {
247 if ( ! is_array($selected))
248 {
249 $selected = array($selected);
250 }
251
252 // If no selected state was submitted we will attempt to set it automatically
253 if (count($selected) === 0)
254 {
255 // If the form name appears in the $_POST array we have a winner!
256 if (isset($_POST[$name]))
257 {
258 $selected = array($_POST[$name]);
259 }
260 }
261
262 if ($extra != '') $extra = ' '.$extra;
263
264 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
265
266 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
267
268 foreach ($options as $key => $val)
269 {
270 $key = (string) $key;
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
Derek Allard78a5fc92009-02-05 16:34:35 +0000272 if (is_array($val))
273 {
274 $form .= '<optgroup label="'.$key.'">'."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000275
Derek Allard78a5fc92009-02-05 16:34:35 +0000276 foreach ($val as $optgroup_key => $optgroup_val)
277 {
278 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
279
280 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
281 }
282
283 $form .= '</optgroup>'."\n";
284 }
285 else
286 {
287 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
288
289 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 $form .= '</select>';
294
295 return $form;
296 }
297}
298
299// ------------------------------------------------------------------------
300
301/**
302 * Checkbox Field
303 *
304 * @access public
305 * @param mixed
306 * @param string
307 * @param bool
308 * @param string
309 * @return string
310 */
311if ( ! function_exists('form_checkbox'))
312{
313 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
314 {
315 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
316
317 if (is_array($data) AND array_key_exists('checked', $data))
318 {
319 $checked = $data['checked'];
320
321 if ($checked == FALSE)
322 {
323 unset($data['checked']);
324 }
325 else
326 {
327 $data['checked'] = 'checked';
328 }
329 }
330
331 if ($checked == TRUE)
332 {
333 $defaults['checked'] = 'checked';
334 }
335 else
336 {
337 unset($defaults['checked']);
338 }
339
340 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
341 }
342}
343
344// ------------------------------------------------------------------------
345
346/**
347 * Radio Button
348 *
349 * @access public
350 * @param mixed
351 * @param string
352 * @param bool
353 * @param string
354 * @return string
355 */
356if ( ! function_exists('form_radio'))
357{
358 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
359 {
360 if ( ! is_array($data))
361 {
362 $data = array('name' => $data);
363 }
364
365 $data['type'] = 'radio';
366 return form_checkbox($data, $value, $checked, $extra);
367 }
368}
369
370// ------------------------------------------------------------------------
371
372/**
373 * Submit Button
374 *
375 * @access public
376 * @param mixed
377 * @param string
378 * @param string
379 * @return string
380 */
381if ( ! function_exists('form_submit'))
382{
383 function form_submit($data = '', $value = '', $extra = '')
384 {
385 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
386
387 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
388 }
389}
390
391// ------------------------------------------------------------------------
392
393/**
394 * Reset Button
395 *
396 * @access public
397 * @param mixed
398 * @param string
399 * @param string
400 * @return string
401 */
402if ( ! function_exists('form_reset'))
403{
404 function form_reset($data = '', $value = '', $extra = '')
405 {
406 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
407
408 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
409 }
410}
411
412// ------------------------------------------------------------------------
413
414/**
415 * Form Button
416 *
417 * @access public
418 * @param mixed
419 * @param string
420 * @param string
421 * @return string
422 */
423if ( ! function_exists('form_button'))
424{
425 function form_button($data = '', $content = '', $extra = '')
426 {
427 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
428
429 if ( is_array($data) AND isset($data['content']))
430 {
431 $content = $data['content'];
432 unset($data['content']); // content is not an attribute
433 }
434
435 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
436 }
437}
438
439// ------------------------------------------------------------------------
440
441/**
442 * Form Label Tag
443 *
444 * @access public
445 * @param string The text to appear onscreen
446 * @param string The id the label applies to
447 * @param string Additional attributes
448 * @return string
449 */
450if ( ! function_exists('form_label'))
451{
452 function form_label($label_text = '', $id = '', $attributes = array())
453 {
454
455 $label = '<label';
456
457 if ($id != '')
458 {
459 $label .= " for=\"$id\"";
460 }
461
462 if (is_array($attributes) AND count($attributes) > 0)
463 {
464 foreach ($attributes as $key => $val)
465 {
466 $label .= ' '.$key.'="'.$val.'"';
467 }
468 }
469
470 $label .= ">$label_text</label>";
471
472 return $label;
473 }
474}
475
476// ------------------------------------------------------------------------
477/**
478 * Fieldset Tag
479 *
480 * Used to produce <fieldset><legend>text</legend>. To close fieldset
481 * use form_fieldset_close()
482 *
483 * @access public
484 * @param string The legend text
485 * @param string Additional attributes
486 * @return string
487 */
488if ( ! function_exists('form_fieldset'))
489{
490 function form_fieldset($legend_text = '', $attributes = array())
491 {
492 $fieldset = "<fieldset";
493
494 $fieldset .= _attributes_to_string($attributes, FALSE);
495
496 $fieldset .= ">\n";
497
498 if ($legend_text != '')
499 {
500 $fieldset .= "<legend>$legend_text</legend>\n";
501 }
502
503 return $fieldset;
504 }
505}
506
507// ------------------------------------------------------------------------
508
509/**
510 * Fieldset Close Tag
511 *
512 * @access public
513 * @param string
514 * @return string
515 */
516if ( ! function_exists('form_fieldset_close'))
517{
518 function form_fieldset_close($extra = '')
519 {
520 return "</fieldset>".$extra;
521 }
522}
523
524// ------------------------------------------------------------------------
525
526/**
527 * Form Close Tag
528 *
529 * @access public
530 * @param string
531 * @return string
532 */
533if ( ! function_exists('form_close'))
534{
535 function form_close($extra = '')
536 {
537 return "</form>".$extra;
538 }
539}
540
541// ------------------------------------------------------------------------
542
543/**
544 * Form Prep
545 *
546 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
547 *
548 * @access public
549 * @param string
550 * @return string
551 */
552if ( ! function_exists('form_prep'))
553{
554 function form_prep($str = '')
555 {
556 // if the field name is an array we do this recursively
557 if (is_array($str))
558 {
559 foreach ($str as $key => $val)
560 {
561 $str[$key] = form_prep($val);
562 }
563
564 return $str;
565 }
566
567 if ($str === '')
568 {
569 return '';
570 }
571
572 $temp = '__TEMP_AMPERSANDS__';
573
574 // Replace entities to temporary markers so that
575 // htmlspecialchars won't mess them up
576 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
577 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
578
579 $str = htmlspecialchars($str);
580
581 // In case htmlspecialchars misses these.
582 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
583
584 // Decode the temp markers back to entities
585 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
586 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
587
588 return $str;
589 }
590}
591
592// ------------------------------------------------------------------------
593
594/**
595 * Form Value
596 *
597 * Grabs a value from the POST array for the specified field so you can
598 * re-populate an input field or textarea. If Form Validation
599 * is active it retrieves the info from the validation class
600 *
601 * @access public
602 * @param string
603 * @return mixed
604 */
605if ( ! function_exists('set_value'))
606{
607 function set_value($field = '', $default = '')
608 {
609 if (FALSE === ($OBJ =& _get_validation_object()))
610 {
611 if ( ! isset($_POST[$field]))
612 {
613 return $default;
614 }
615
616 return form_prep($_POST[$field]);
617 }
618
619 return form_prep($OBJ->set_value($field, $default));
620 }
621}
622
623// ------------------------------------------------------------------------
624
625/**
626 * Set Select
627 *
628 * Let's you set the selected value of a <select> menu via data in the POST array.
629 * If Form Validation is active it retrieves the info from the validation class
630 *
631 * @access public
632 * @param string
633 * @param string
634 * @param bool
635 * @return string
636 */
637if ( ! function_exists('set_select'))
638{
639 function set_select($field = '', $value = '', $default = FALSE)
640 {
641 $OBJ =& _get_validation_object();
642
643 if ($OBJ === FALSE)
644 {
645 if ( ! isset($_POST[$field]))
646 {
647 if (count($_POST) === 0)
648 {
649 return ' selected="selected"';
650 }
651 return '';
652 }
653
654 $field = $_POST[$field];
655
656 if (is_array($field))
657 {
658 if ( ! in_array($value, $field))
659 {
660 return '';
661 }
662 }
663 else
664 {
665 if (($field == '' OR $value == '') OR ($field != $value))
666 {
667 return '';
668 }
669 }
670
671 return ' selected="selected"';
672 }
673
674 return $OBJ->set_select($field, $value, $default);
675 }
676}
677
678// ------------------------------------------------------------------------
679
680/**
681 * Set Checkbox
682 *
683 * Let's you set the selected value of a checkbox via the value 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_checkbox'))
693{
694 function set_checkbox($field = '', $value = '', $default = FALSE)
695 {
696 $OBJ =& _get_validation_object();
697
698 if ($OBJ === FALSE)
699 {
700 if ( ! isset($_POST[$field]))
701 {
702 if (count($_POST) === 0)
703 {
704 return ' checked="checked"';
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 ' checked="checked"';
727 }
728
729 return $OBJ->set_checkbox($field, $value, $default);
730 }
731}
732
733// ------------------------------------------------------------------------
734
735/**
736 * Set Radio
737 *
738 * Let's you set the selected value of a radio field via info 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_radio'))
748{
749 function set_radio($field = '', $value = '', $default = FALSE)
750 {
751 $OBJ =& _get_validation_object();
752
753 if ($OBJ === FALSE)
754 {
755 if ( ! isset($_POST[$field]))
756 {
757 if (count($_POST) === 0)
758 {
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_radio($field, $value, $default);
785 }
786}
787
788// ------------------------------------------------------------------------
789
790/**
791 * Form Error
792 *
793 * Returns the error for a specific form field. This is a helper for the
794 * form validation class.
795 *
796 * @access public
797 * @param string
798 * @param string
799 * @param string
800 * @return string
801 */
802if ( ! function_exists('form_error'))
803{
804 function form_error($field = '', $prefix = '', $suffix = '')
805 {
806 if (FALSE === ($OBJ =& _get_validation_object()))
807 {
808 return '';
809 }
810
811 return $OBJ->error($field, $prefix, $suffix);
812 }
813}
814
815// ------------------------------------------------------------------------
816
817/**
818 * Validation Error String
819 *
820 * Returns all the errors associated with a form submission. This is a helper
821 * function for the form validation class.
822 *
823 * @access public
824 * @param string
825 * @param string
826 * @return string
827 */
828if ( ! function_exists('validation_errors'))
829{
830 function validation_errors($prefix = '', $suffix = '')
831 {
832 if (FALSE === ($OBJ =& _get_validation_object()))
833 {
834 return '';
835 }
836
837 return $OBJ->error_string($prefix, $suffix);
838 }
839}
840
841// ------------------------------------------------------------------------
842
843/**
844 * Parse the form attributes
845 *
846 * Helper function used by some of the form helpers
847 *
848 * @access private
849 * @param array
850 * @param array
851 * @return string
852 */
853if ( ! function_exists('_parse_form_attributes'))
854{
855 function _parse_form_attributes($attributes, $default)
856 {
857 if (is_array($attributes))
858 {
859 foreach ($default as $key => $val)
860 {
861 if (isset($attributes[$key]))
862 {
863 $default[$key] = $attributes[$key];
864 unset($attributes[$key]);
865 }
866 }
867
868 if (count($attributes) > 0)
869 {
870 $default = array_merge($default, $attributes);
871 }
872 }
873
874 $att = '';
875
876 foreach ($default as $key => $val)
877 {
878 if ($key == 'value')
879 {
880 $val = form_prep($val);
881 }
882
883 $att .= $key . '="' . $val . '" ';
884 }
885
886 return $att;
887 }
888}
889
890// ------------------------------------------------------------------------
891
892/**
893 * Attributes To String
894 *
895 * Helper function used by some of the form helpers
896 *
897 * @access private
898 * @param mixed
899 * @param bool
900 * @return string
901 */
902if ( ! function_exists('_attributes_to_string'))
903{
904 function _attributes_to_string($attributes, $formtag = FALSE)
905 {
906 if (is_string($attributes) AND strlen($attributes) > 0)
907 {
908 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
909 {
910 $attributes .= ' method="post"';
911 }
912
913 return ' '.$attributes;
914 }
915
916 if (is_object($attributes) AND count($attributes) > 0)
917 {
918 $attributes = (array)$attributes;
919 }
920
921 if (is_array($attributes) AND count($attributes) > 0)
922 {
923 $atts = '';
924
925 if ( ! isset($attributes['method']) AND $formtag === TRUE)
926 {
927 $atts .= ' method="post"';
928 }
929
930 foreach ($attributes as $key => $val)
931 {
932 $atts .= ' '.$key.'="'.$val.'"';
933 }
934
935 return $atts;
936 }
937 }
938}
939
940// ------------------------------------------------------------------------
941
942/**
943 * Validation Object
944 *
945 * Determines what the form validation class was instantiated as, fetches
946 * the object and returns it.
947 *
948 * @access private
949 * @return mixed
950 */
951if ( ! function_exists('_get_validation_object'))
952{
953 function &_get_validation_object()
954 {
955 $CI =& get_instance();
956
957 // We set this as a variable since we're returning by reference
958 $return = FALSE;
959
960 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
961 {
962 return $return;
963 }
964
965 $object = $CI->load->_ci_classes['form_validation'];
966
967 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
968 {
969 return $return;
970 }
971
972 return $CI->$object;
973 }
974}
975
976
977/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000978/* Location: ./system/helpers/form_helper.php */