1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
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
16 // ------------------------------------------------------------------------
19 * CodeIgniter Form Helpers
21 * @package CodeIgniter
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
28 // ------------------------------------------------------------------------
33 * Creates the opening portion of the form.
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
41 if ( ! function_exists('form_open'))
43 function form_open($action = '', $attributes = '', $hidden = array())
45 $CI =& get_instance();
47 if ($attributes == '')
49 $attributes = 'method="post"';
52 $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
54 $form = '<form action="'.$action.'"';
56 $form .= _attributes_to_string($attributes, TRUE);
60 if (is_array($hidden) AND count($hidden) > 0)
62 $form .= form_hidden($hidden);
69 // ------------------------------------------------------------------------
72 * Form Declaration - Multipart type
74 * Creates the opening portion of the form, but with "multipart/form-data".
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
82 if ( ! function_exists('form_open_multipart'))
84 function form_open_multipart($action, $attributes = array(), $hidden = array())
86 $attributes['enctype'] = 'multipart/form-data';
87 return form_open($action, $attributes, $hidden);
91 // ------------------------------------------------------------------------
96 * Generates hidden fields. You can pass a simple key/value string or an associative
97 * array with multiple values.
104 if ( ! function_exists('form_hidden'))
106 function form_hidden($name, $value = '')
108 if ( ! is_array($name))
110 return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
115 foreach ($name as $name => $value)
118 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
125 // ------------------------------------------------------------------------
136 if ( ! function_exists('form_input'))
138 function form_input($data = '', $value = '', $extra = '')
140 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
142 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
146 // ------------------------------------------------------------------------
151 * Identical to the input function but adds the "password" type
159 if ( ! function_exists('form_password'))
161 function form_password($data = '', $value = '', $extra = '')
163 if ( ! is_array($data))
165 $data = array('name' => $data);
168 $data['type'] = 'password';
169 return form_input($data, $value, $extra);
173 // ------------------------------------------------------------------------
178 * Identical to the input function but adds the "file" type
186 if ( ! function_exists('form_upload'))
188 function form_upload($data = '', $value = '', $extra = '')
190 if ( ! is_array($data))
192 $data = array('name' => $data);
195 $data['type'] = 'file';
196 return form_input($data, $value, $extra);
200 // ------------------------------------------------------------------------
211 if ( ! function_exists('form_textarea'))
213 function form_textarea($data = '', $value = '', $extra = '')
215 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
217 if ( ! is_array($data) OR ! isset($data['value']))
223 $val = $data['value'];
224 unset($data['value']); // textareas don't use the value attribute
227 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>";
231 // ------------------------------------------------------------------------
243 if ( ! function_exists('form_dropdown'))
245 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
247 if ( ! is_array($selected))
249 $selected = array($selected);
252 // If no selected state was submitted we will attempt to set it automatically
253 if (count($selected) === 0)
255 // If the form name appears in the $_POST array we have a winner!
256 if (isset($_POST[$name]))
258 $selected = array($_POST[$name]);
262 if ($extra != '') $extra = ' '.$extra;
264 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
266 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
268 foreach ($options as $key => $val)
270 $key = (string) $key;
271 $val = (string) $val;
273 $sel = (in_array($key, $selected))?' selected="selected"':'';
275 $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
278 $form .= '</select>';
284 // ------------------------------------------------------------------------
296 if ( ! function_exists('form_checkbox'))
298 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
300 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
302 if (is_array($data) AND array_key_exists('checked', $data))
304 $checked = $data['checked'];
306 if ($checked == FALSE)
308 unset($data['checked']);
312 $data['checked'] = 'checked';
316 if ($checked == TRUE)
318 $defaults['checked'] = 'checked';
322 unset($defaults['checked']);
325 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
329 // ------------------------------------------------------------------------
341 if ( ! function_exists('form_radio'))
343 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
345 if ( ! is_array($data))
347 $data = array('name' => $data);
350 $data['type'] = 'radio';
351 return form_checkbox($data, $value, $checked, $extra);
355 // ------------------------------------------------------------------------
366 if ( ! function_exists('form_submit'))
368 function form_submit($data = '', $value = '', $extra = '')
370 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
372 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
376 // ------------------------------------------------------------------------
387 if ( ! function_exists('form_reset'))
389 function form_reset($data = '', $value = '', $extra = '')
391 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
393 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
397 // ------------------------------------------------------------------------
408 if ( ! function_exists('form_button'))
410 function form_button($data = '', $content = '', $extra = '')
412 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
414 if ( is_array($data) AND isset($data['content']))
416 $content = $data['content'];
417 unset($data['content']); // content is not an attribute
420 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
424 // ------------------------------------------------------------------------
430 * @param string The text to appear onscreen
431 * @param string The id the label applies to
432 * @param string Additional attributes
435 if ( ! function_exists('form_label'))
437 function form_label($label_text = '', $id = '', $attributes = array())
444 $label .= " for=\"$id\"";
447 if (is_array($attributes) AND count($attributes) > 0)
449 foreach ($attributes as $key => $val)
451 $label .= ' '.$key.'="'.$val.'"';
455 $label .= ">$label_text</label>";
461 // ------------------------------------------------------------------------
465 * Used to produce <fieldset><legend>text</legend>. To close fieldset
466 * use form_fieldset_close()
469 * @param string The legend text
470 * @param string Additional attributes
473 if ( ! function_exists('form_fieldset'))
475 function form_fieldset($legend_text = '', $attributes = array())
477 $fieldset = "<fieldset";
479 $fieldset .= _attributes_to_string($attributes, FALSE);
483 if ($legend_text != '')
485 $fieldset .= "<legend>$legend_text</legend>\n";
492 // ------------------------------------------------------------------------
501 if ( ! function_exists('form_fieldset_close'))
503 function form_fieldset_close($extra = '')
505 return "</fieldset>".$extra;
509 // ------------------------------------------------------------------------
518 if ( ! function_exists('form_close'))
520 function form_close($extra = '')
522 return "</form>".$extra;
526 // ------------------------------------------------------------------------
531 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
537 if ( ! function_exists('form_prep'))
539 function form_prep($str = '')
541 // if the field name is an array we do this recursively
544 foreach ($str as $key => $val)
546 $str[$key] = form_prep($val);
557 $temp = '__TEMP_AMPERSANDS__';
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);
564 $str = htmlspecialchars($str);
566 // In case htmlspecialchars misses these.
567 $str = str_replace(array("'", '"'), array("'", """), $str);
569 // Decode the temp markers back to entities
570 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
571 $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
577 // ------------------------------------------------------------------------
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
590 if ( ! function_exists('set_value'))
592 function set_value($field = '', $default = '')
594 if (FALSE === ($OBJ =& _get_validation_object()))
596 if ( ! isset($_POST[$field]))
601 return form_prep($_POST[$field]);
604 return form_prep($OBJ->set_value($field, $default));
608 // ------------------------------------------------------------------------
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
622 if ( ! function_exists('set_select'))
624 function set_select($field = '', $value = '', $default = FALSE)
626 $OBJ =& _get_validation_object();
630 if ( ! isset($_POST[$field]))
632 if (count($_POST) === 0)
634 return ' selected="selected"';
639 $field = $_POST[$field];
641 if (is_array($field))
643 if ( ! in_array($value, $field))
650 if (($field == '' OR $value == '') OR ($field != $value))
656 return ' selected="selected"';
659 return $OBJ->set_select($field, $value, $default);
663 // ------------------------------------------------------------------------
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
677 if ( ! function_exists('set_checkbox'))
679 function set_checkbox($field = '', $value = '', $default = FALSE)
681 $OBJ =& _get_validation_object();
685 if ( ! isset($_POST[$field]))
687 if (count($_POST) === 0)
689 return ' checked="checked"';
694 $field = $_POST[$field];
696 if (is_array($field))
698 if ( ! in_array($value, $field))
705 if (($field == '' OR $value == '') OR ($field != $value))
711 return ' checked="checked"';
714 return $OBJ->set_checkbox($field, $value, $default);
718 // ------------------------------------------------------------------------
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
732 if ( ! function_exists('set_radio'))
734 function set_radio($field = '', $value = '', $default = FALSE)
736 $OBJ =& _get_validation_object();
740 if ( ! isset($_POST[$field]))
742 if (count($_POST) === 0)
744 return ' checked="checked"';
749 $field = $_POST[$field];
751 if (is_array($field))
753 if ( ! in_array($value, $field))
760 if (($field == '' OR $value == '') OR ($field != $value))
766 return ' checked="checked"';
769 return $OBJ->set_radio($field, $value, $default);
773 // ------------------------------------------------------------------------
778 * Returns the error for a specific form field. This is a helper for the
779 * form validation class.
787 if ( ! function_exists('form_error'))
789 function form_error($field = '', $prefix = '', $suffix = '')
791 if (FALSE === ($OBJ =& _get_validation_object()))
796 return $OBJ->error($field, $prefix, $suffix);
800 // ------------------------------------------------------------------------
803 * Validation Error String
805 * Returns all the errors associated with a form submission. This is a helper
806 * function for the form validation class.
813 if ( ! function_exists('validation_errors'))
815 function validation_errors($prefix = '', $suffix = '')
817 if (FALSE === ($OBJ =& _get_validation_object()))
822 return $OBJ->error_string($prefix, $suffix);
826 // ------------------------------------------------------------------------
829 * Parse the form attributes
831 * Helper function used by some of the form helpers
838 if ( ! function_exists('_parse_form_attributes'))
840 function _parse_form_attributes($attributes, $default)
842 if (is_array($attributes))
844 foreach ($default as $key => $val)
846 if (isset($attributes[$key]))
848 $default[$key] = $attributes[$key];
849 unset($attributes[$key]);
853 if (count($attributes) > 0)
855 $default = array_merge($default, $attributes);
861 foreach ($default as $key => $val)
865 $val = form_prep($val);
868 $att .= $key . '="' . $val . '" ';
875 // ------------------------------------------------------------------------
878 * Attributes To String
880 * Helper function used by some of the form helpers
887 if ( ! function_exists('_attributes_to_string'))
889 function _attributes_to_string($attributes, $formtag = FALSE)
891 if (is_string($attributes) AND strlen($attributes) > 0)
893 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
895 $attributes .= ' method="post"';
898 return ' '.$attributes;
901 if (is_object($attributes) AND count($attributes) > 0)
903 $attributes = (array)$attributes;
906 if (is_array($attributes) AND count($attributes) > 0)
910 if ( ! isset($attributes['method']) AND $formtag === TRUE)
912 $atts .= ' method="post"';
915 foreach ($attributes as $key => $val)
917 $atts .= ' '.$key.'="'.$val.'"';
925 // ------------------------------------------------------------------------
930 * Determines what the form validation class was instantiated as, fetches
931 * the object and returns it.
936 if ( ! function_exists('_get_validation_object'))
938 function &_get_validation_object()
940 $CI =& get_instance();
942 // We set this as a variable since we're returning by reference
945 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
950 $object = $CI->load->_ci_classes['form_validation'];
952 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
962 /* End of file form_helper.php */
963 /* Location: ./system/helpers/form_helper.php */