blob: c002c6fc0e49c427e7f46a3f821ca73e738d4b82 [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;
271 $val = (string) $val;
272
273 $sel = (in_array($key, $selected))?' selected="selected"':'';
274
275 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
276 }
277
278 $form .= '</select>';
279
280 return $form;
281 }
282}
283
284// ------------------------------------------------------------------------
285
286/**
287 * Checkbox Field
288 *
289 * @access public
290 * @param mixed
291 * @param string
292 * @param bool
293 * @param string
294 * @return string
295 */
296if ( ! function_exists('form_checkbox'))
297{
298 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
299 {
300 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
301
302 if (is_array($data) AND array_key_exists('checked', $data))
303 {
304 $checked = $data['checked'];
305
306 if ($checked == FALSE)
307 {
308 unset($data['checked']);
309 }
310 else
311 {
312 $data['checked'] = 'checked';
313 }
314 }
315
316 if ($checked == TRUE)
317 {
318 $defaults['checked'] = 'checked';
319 }
320 else
321 {
322 unset($defaults['checked']);
323 }
324
325 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
326 }
327}
328
329// ------------------------------------------------------------------------
330
331/**
332 * Radio Button
333 *
334 * @access public
335 * @param mixed
336 * @param string
337 * @param bool
338 * @param string
339 * @return string
340 */
341if ( ! function_exists('form_radio'))
342{
343 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
344 {
345 if ( ! is_array($data))
346 {
347 $data = array('name' => $data);
348 }
349
350 $data['type'] = 'radio';
351 return form_checkbox($data, $value, $checked, $extra);
352 }
353}
354
355// ------------------------------------------------------------------------
356
357/**
358 * Submit Button
359 *
360 * @access public
361 * @param mixed
362 * @param string
363 * @param string
364 * @return string
365 */
366if ( ! function_exists('form_submit'))
367{
368 function form_submit($data = '', $value = '', $extra = '')
369 {
370 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
371
372 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
373 }
374}
375
376// ------------------------------------------------------------------------
377
378/**
379 * Reset Button
380 *
381 * @access public
382 * @param mixed
383 * @param string
384 * @param string
385 * @return string
386 */
387if ( ! function_exists('form_reset'))
388{
389 function form_reset($data = '', $value = '', $extra = '')
390 {
391 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
392
393 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
394 }
395}
396
397// ------------------------------------------------------------------------
398
399/**
400 * Form Button
401 *
402 * @access public
403 * @param mixed
404 * @param string
405 * @param string
406 * @return string
407 */
408if ( ! function_exists('form_button'))
409{
410 function form_button($data = '', $content = '', $extra = '')
411 {
412 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
413
414 if ( is_array($data) AND isset($data['content']))
415 {
416 $content = $data['content'];
417 unset($data['content']); // content is not an attribute
418 }
419
420 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
421 }
422}
423
424// ------------------------------------------------------------------------
425
426/**
427 * Form Label Tag
428 *
429 * @access public
430 * @param string The text to appear onscreen
431 * @param string The id the label applies to
432 * @param string Additional attributes
433 * @return string
434 */
435if ( ! function_exists('form_label'))
436{
437 function form_label($label_text = '', $id = '', $attributes = array())
438 {
439
440 $label = '<label';
441
442 if ($id != '')
443 {
444 $label .= " for=\"$id\"";
445 }
446
447 if (is_array($attributes) AND count($attributes) > 0)
448 {
449 foreach ($attributes as $key => $val)
450 {
451 $label .= ' '.$key.'="'.$val.'"';
452 }
453 }
454
455 $label .= ">$label_text</label>";
456
457 return $label;
458 }
459}
460
461// ------------------------------------------------------------------------
462/**
463 * Fieldset Tag
464 *
465 * Used to produce <fieldset><legend>text</legend>. To close fieldset
466 * use form_fieldset_close()
467 *
468 * @access public
469 * @param string The legend text
470 * @param string Additional attributes
471 * @return string
472 */
473if ( ! function_exists('form_fieldset'))
474{
475 function form_fieldset($legend_text = '', $attributes = array())
476 {
477 $fieldset = "<fieldset";
478
479 $fieldset .= _attributes_to_string($attributes, FALSE);
480
481 $fieldset .= ">\n";
482
483 if ($legend_text != '')
484 {
485 $fieldset .= "<legend>$legend_text</legend>\n";
486 }
487
488 return $fieldset;
489 }
490}
491
492// ------------------------------------------------------------------------
493
494/**
495 * Fieldset Close Tag
496 *
497 * @access public
498 * @param string
499 * @return string
500 */
501if ( ! function_exists('form_fieldset_close'))
502{
503 function form_fieldset_close($extra = '')
504 {
505 return "</fieldset>".$extra;
506 }
507}
508
509// ------------------------------------------------------------------------
510
511/**
512 * Form Close Tag
513 *
514 * @access public
515 * @param string
516 * @return string
517 */
518if ( ! function_exists('form_close'))
519{
520 function form_close($extra = '')
521 {
522 return "</form>".$extra;
523 }
524}
525
526// ------------------------------------------------------------------------
527
528/**
529 * Form Prep
530 *
531 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
532 *
533 * @access public
534 * @param string
535 * @return string
536 */
537if ( ! function_exists('form_prep'))
538{
539 function form_prep($str = '')
540 {
541 // if the field name is an array we do this recursively
542 if (is_array($str))
543 {
544 foreach ($str as $key => $val)
545 {
546 $str[$key] = form_prep($val);
547 }
548
549 return $str;
550 }
551
552 if ($str === '')
553 {
554 return '';
555 }
556
557 $temp = '__TEMP_AMPERSANDS__';
558
559 // Replace entities to temporary markers so that
560 // htmlspecialchars won't mess them up
561 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
562 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
563
564 $str = htmlspecialchars($str);
565
566 // In case htmlspecialchars misses these.
567 $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
568
569 // Decode the temp markers back to entities
570 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
571 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
572
573 return $str;
574 }
575}
576
577// ------------------------------------------------------------------------
578
579/**
580 * Form Value
581 *
582 * Grabs a value from the POST array for the specified field so you can
583 * re-populate an input field or textarea. If Form Validation
584 * is active it retrieves the info from the validation class
585 *
586 * @access public
587 * @param string
588 * @return mixed
589 */
590if ( ! function_exists('set_value'))
591{
592 function set_value($field = '', $default = '')
593 {
594 if (FALSE === ($OBJ =& _get_validation_object()))
595 {
596 if ( ! isset($_POST[$field]))
597 {
598 return $default;
599 }
600
601 return form_prep($_POST[$field]);
602 }
603
604 return form_prep($OBJ->set_value($field, $default));
605 }
606}
607
608// ------------------------------------------------------------------------
609
610/**
611 * Set Select
612 *
613 * Let's you set the selected value of a <select> menu via data in the POST array.
614 * If Form Validation is active it retrieves the info from the validation class
615 *
616 * @access public
617 * @param string
618 * @param string
619 * @param bool
620 * @return string
621 */
622if ( ! function_exists('set_select'))
623{
624 function set_select($field = '', $value = '', $default = FALSE)
625 {
626 $OBJ =& _get_validation_object();
627
628 if ($OBJ === FALSE)
629 {
630 if ( ! isset($_POST[$field]))
631 {
632 if (count($_POST) === 0)
633 {
634 return ' selected="selected"';
635 }
636 return '';
637 }
638
639 $field = $_POST[$field];
640
641 if (is_array($field))
642 {
643 if ( ! in_array($value, $field))
644 {
645 return '';
646 }
647 }
648 else
649 {
650 if (($field == '' OR $value == '') OR ($field != $value))
651 {
652 return '';
653 }
654 }
655
656 return ' selected="selected"';
657 }
658
659 return $OBJ->set_select($field, $value, $default);
660 }
661}
662
663// ------------------------------------------------------------------------
664
665/**
666 * Set Checkbox
667 *
668 * Let's you set the selected value of a checkbox via the value in the POST array.
669 * If Form Validation is active it retrieves the info from the validation class
670 *
671 * @access public
672 * @param string
673 * @param string
674 * @param bool
675 * @return string
676 */
677if ( ! function_exists('set_checkbox'))
678{
679 function set_checkbox($field = '', $value = '', $default = FALSE)
680 {
681 $OBJ =& _get_validation_object();
682
683 if ($OBJ === FALSE)
684 {
685 if ( ! isset($_POST[$field]))
686 {
687 if (count($_POST) === 0)
688 {
689 return ' checked="checked"';
690 }
691 return '';
692 }
693
694 $field = $_POST[$field];
695
696 if (is_array($field))
697 {
698 if ( ! in_array($value, $field))
699 {
700 return '';
701 }
702 }
703 else
704 {
705 if (($field == '' OR $value == '') OR ($field != $value))
706 {
707 return '';
708 }
709 }
710
711 return ' checked="checked"';
712 }
713
714 return $OBJ->set_checkbox($field, $value, $default);
715 }
716}
717
718// ------------------------------------------------------------------------
719
720/**
721 * Set Radio
722 *
723 * Let's you set the selected value of a radio field via info in the POST array.
724 * If Form Validation is active it retrieves the info from the validation class
725 *
726 * @access public
727 * @param string
728 * @param string
729 * @param bool
730 * @return string
731 */
732if ( ! function_exists('set_radio'))
733{
734 function set_radio($field = '', $value = '', $default = FALSE)
735 {
736 $OBJ =& _get_validation_object();
737
738 if ($OBJ === FALSE)
739 {
740 if ( ! isset($_POST[$field]))
741 {
742 if (count($_POST) === 0)
743 {
744 return ' checked="checked"';
745 }
746 return '';
747 }
748
749 $field = $_POST[$field];
750
751 if (is_array($field))
752 {
753 if ( ! in_array($value, $field))
754 {
755 return '';
756 }
757 }
758 else
759 {
760 if (($field == '' OR $value == '') OR ($field != $value))
761 {
762 return '';
763 }
764 }
765
766 return ' checked="checked"';
767 }
768
769 return $OBJ->set_radio($field, $value, $default);
770 }
771}
772
773// ------------------------------------------------------------------------
774
775/**
776 * Form Error
777 *
778 * Returns the error for a specific form field. This is a helper for the
779 * form validation class.
780 *
781 * @access public
782 * @param string
783 * @param string
784 * @param string
785 * @return string
786 */
787if ( ! function_exists('form_error'))
788{
789 function form_error($field = '', $prefix = '', $suffix = '')
790 {
791 if (FALSE === ($OBJ =& _get_validation_object()))
792 {
793 return '';
794 }
795
796 return $OBJ->error($field, $prefix, $suffix);
797 }
798}
799
800// ------------------------------------------------------------------------
801
802/**
803 * Validation Error String
804 *
805 * Returns all the errors associated with a form submission. This is a helper
806 * function for the form validation class.
807 *
808 * @access public
809 * @param string
810 * @param string
811 * @return string
812 */
813if ( ! function_exists('validation_errors'))
814{
815 function validation_errors($prefix = '', $suffix = '')
816 {
817 if (FALSE === ($OBJ =& _get_validation_object()))
818 {
819 return '';
820 }
821
822 return $OBJ->error_string($prefix, $suffix);
823 }
824}
825
826// ------------------------------------------------------------------------
827
828/**
829 * Parse the form attributes
830 *
831 * Helper function used by some of the form helpers
832 *
833 * @access private
834 * @param array
835 * @param array
836 * @return string
837 */
838if ( ! function_exists('_parse_form_attributes'))
839{
840 function _parse_form_attributes($attributes, $default)
841 {
842 if (is_array($attributes))
843 {
844 foreach ($default as $key => $val)
845 {
846 if (isset($attributes[$key]))
847 {
848 $default[$key] = $attributes[$key];
849 unset($attributes[$key]);
850 }
851 }
852
853 if (count($attributes) > 0)
854 {
855 $default = array_merge($default, $attributes);
856 }
857 }
858
859 $att = '';
860
861 foreach ($default as $key => $val)
862 {
863 if ($key == 'value')
864 {
865 $val = form_prep($val);
866 }
867
868 $att .= $key . '="' . $val . '" ';
869 }
870
871 return $att;
872 }
873}
874
875// ------------------------------------------------------------------------
876
877/**
878 * Attributes To String
879 *
880 * Helper function used by some of the form helpers
881 *
882 * @access private
883 * @param mixed
884 * @param bool
885 * @return string
886 */
887if ( ! function_exists('_attributes_to_string'))
888{
889 function _attributes_to_string($attributes, $formtag = FALSE)
890 {
891 if (is_string($attributes) AND strlen($attributes) > 0)
892 {
893 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
894 {
895 $attributes .= ' method="post"';
896 }
897
898 return ' '.$attributes;
899 }
900
901 if (is_object($attributes) AND count($attributes) > 0)
902 {
903 $attributes = (array)$attributes;
904 }
905
906 if (is_array($attributes) AND count($attributes) > 0)
907 {
908 $atts = '';
909
910 if ( ! isset($attributes['method']) AND $formtag === TRUE)
911 {
912 $atts .= ' method="post"';
913 }
914
915 foreach ($attributes as $key => $val)
916 {
917 $atts .= ' '.$key.'="'.$val.'"';
918 }
919
920 return $atts;
921 }
922 }
923}
924
925// ------------------------------------------------------------------------
926
927/**
928 * Validation Object
929 *
930 * Determines what the form validation class was instantiated as, fetches
931 * the object and returns it.
932 *
933 * @access private
934 * @return mixed
935 */
936if ( ! function_exists('_get_validation_object'))
937{
938 function &_get_validation_object()
939 {
940 $CI =& get_instance();
941
942 // We set this as a variable since we're returning by reference
943 $return = FALSE;
944
945 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
946 {
947 return $return;
948 }
949
950 $object = $CI->load->_ci_classes['form_validation'];
951
952 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
953 {
954 return $return;
955 }
956
957 return $CI->$object;
958 }
959}
960
961
962/* End of file form_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000963/* Location: ./system/helpers/form_helper.php */