From 17f4d73ff388dbcb4d9fbd4f79a0f9150ddc8c7e Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Sat, 27 Jan 2007 17:23:13 +0000 Subject: unset the value attribute in textarea --- system/helpers/form_helper.php | 806 +++++++++++++++++++++-------------------- 1 file changed, 404 insertions(+), 402 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 7d594d72c..8186d4ccd 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -1,403 +1,405 @@ -config->site_url($action).'"'; - - if ( ! isset($attributes['method'])) - { - $form .= ' method="post"'; - } - - if (is_array($attributes) AND count($attributes) > 0) - { - foreach ($attributes as $key => $val) - { - $form .= ' '.$key.'="'.$val.'"'; - } - } - - $form .= '>'; - - if (is_array($hidden) AND count($hidden > 0)) - { - $form .= form_hidden($hidden); - } - - return $form; -} - -// ------------------------------------------------------------------------ - -/** - * Form Declaration - Multipart type - * - * Creates the opening portion of the form, but with "multipart/form-data". - * - * @access public - * @param string the URI segments of the form destination - * @param array a key/value pair of attributes - * @param array a key/value pair hidden data - * @return string - */ -function form_open_multipart($action, $attributes = array(), $hidden = array()) -{ - $attributes['enctype'] = 'multipart/form-data'; - return form_open($action, $attributes, $hidden); -} - -// ------------------------------------------------------------------------ - -/** - * Hidden Input Field - * - * Generates hidden fields. You can pass a simple key/value string or an associative - * array with multiple values. - * - * @access public - * @param mixed - * @param string - * @return string - */ -function form_hidden($name, $value = '') -{ - if ( ! is_array($name)) - { - return ''; - } - - $form = ''; - foreach ($name as $name => $value) - { - $form .= ''; - } - - return $form; -} - -// ------------------------------------------------------------------------ - -/** - * Text Input Field - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_input($data = '', $value = '', $extra = '') -{ - $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50'); - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Password Field - * - * Identical to the input function but adds the "password" type - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_password($data = '', $value = '', $extra = '') -{ - if ( ! is_array($data)) - { - $data = array('name' => $data); - } - - $data['type'] = 'password'; - return form_input($data, $value, $extra); -} - -// ------------------------------------------------------------------------ - -/** - * Upload Field - * - * Identical to the input function but adds the "file" type - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_upload($data = '', $value = '', $extra = '') -{ - if ( ! is_array($data)) - { - $data = array('name' => $data); - } - - $data['type'] = 'file'; - return form_input($data, $value, $extra); -} - -// ------------------------------------------------------------------------ - -/** - * Textarea field - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_textarea($data = '', $value = '', $extra = '') -{ - $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12'); - - $val = (( ! is_array($data) OR ! isset($data['value'])) ? $value : $data['value']); - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Drop-down Menu - * - * @access public - * @param string - * @param array - * @param string - * @param string - * @return string - */ -function form_dropdown($name = '', $options = array(), $selected = '', $extra = '') -{ - if ($extra != '') $extra = ' '.$extra; - - $form = ''; - - return $form; -} - -// ------------------------------------------------------------------------ - -/** - * Checkbox Field - * - * @access public - * @param mixed - * @param string - * @param bool - * @param string - * @return string - */ -function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '') -{ - $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); - - if (is_array($data) AND array_key_exists('checked', $data)) - { - $checked = $data['checked']; - - if ($checked == FALSE) - unset($data['checked']); - } - - if ($checked == TRUE) - $defaults['checked'] = 'checked'; - else - unset($defaults['checked']); - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Radio Button - * - * @access public - * @param mixed - * @param string - * @param bool - * @param string - * @return string - */ -function form_radio($data = '', $value = '', $checked = TRUE, $extra = '') -{ - if ( ! is_array($data)) - { - $data = array('name' => $data); - } - - $data['type'] = 'radio'; - return form_checkbox($data, $value, $checked, $extra); -} - -// ------------------------------------------------------------------------ - -/** - * Submit Button - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_submit($data = '', $value = '', $extra = '') -{ - $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Form Close Tag - * - * @access public - * @param string - * @return string - */ -function form_close($extra = '') -{ - return "\n".$extra; -} - -// ------------------------------------------------------------------------ - -/** - * Form Prep - * - * Formats text so that it can be safely placed in a form field in the event it has HTML tags. - * - * @access public - * @param string - * @return string - */ -function form_prep($str = '') -{ - if ($str === '') - { - return ''; - } - - $temp = '__TEMP_AMPERSANDS__'; - - // Replace entities to temporary markers so that - // htmlspecialchars won't mess them up - $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str); - $str = preg_replace("/&(\w+);/", "$temp\\1;", $str); - - $str = htmlspecialchars($str); - - // In case htmlspecialchars misses these. - $str = str_replace(array("'", '"'), array("'", """), $str); - - // Decode the temp markers back to entities - $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); - $str = preg_replace("/$temp(\w+);/","&\\1;",$str); - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Parse the form attributes - * - * Helper function used by some of the form helpers - * - * @access private - * @param array - * @param array - * @return string - */ -function parse_form_attributes($attributes, $default) -{ - if (is_array($attributes)) - { - foreach ($default as $key => $val) - { - if (isset($attributes[$key])) - { - $default[$key] = $attributes[$key]; - unset($attributes[$key]); - } - } - - if (count($attributes) > 0) - { - $default = array_merge($default, $attributes); - } - } - - $att = ''; - foreach ($default as $key => $val) - { - if ($key == 'value') - { - $val = form_prep($val); - } - - $att .= $key . '="' . $val . '" '; - } - - return $att; -} - +config->site_url($action).'"'; + + if ( ! isset($attributes['method'])) + { + $form .= ' method="post"'; + } + + if (is_array($attributes) AND count($attributes) > 0) + { + foreach ($attributes as $key => $val) + { + $form .= ' '.$key.'="'.$val.'"'; + } + } + + $form .= '>'; + + if (is_array($hidden) AND count($hidden > 0)) + { + $form .= form_hidden($hidden); + } + + return $form; +} + +// ------------------------------------------------------------------------ + +/** + * Form Declaration - Multipart type + * + * Creates the opening portion of the form, but with "multipart/form-data". + * + * @access public + * @param string the URI segments of the form destination + * @param array a key/value pair of attributes + * @param array a key/value pair hidden data + * @return string + */ +function form_open_multipart($action, $attributes = array(), $hidden = array()) +{ + $attributes['enctype'] = 'multipart/form-data'; + return form_open($action, $attributes, $hidden); +} + +// ------------------------------------------------------------------------ + +/** + * Hidden Input Field + * + * Generates hidden fields. You can pass a simple key/value string or an associative + * array with multiple values. + * + * @access public + * @param mixed + * @param string + * @return string + */ +function form_hidden($name, $value = '') +{ + if ( ! is_array($name)) + { + return ''; + } + + $form = ''; + foreach ($name as $name => $value) + { + $form .= ''; + } + + return $form; +} + +// ------------------------------------------------------------------------ + +/** + * Text Input Field + * + * @access public + * @param mixed + * @param string + * @param string + * @return string + */ +function form_input($data = '', $value = '', $extra = '') +{ + $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50'); + + return "\n"; +} + +// ------------------------------------------------------------------------ + +/** + * Password Field + * + * Identical to the input function but adds the "password" type + * + * @access public + * @param mixed + * @param string + * @param string + * @return string + */ +function form_password($data = '', $value = '', $extra = '') +{ + if ( ! is_array($data)) + { + $data = array('name' => $data); + } + + $data['type'] = 'password'; + return form_input($data, $value, $extra); +} + +// ------------------------------------------------------------------------ + +/** + * Upload Field + * + * Identical to the input function but adds the "file" type + * + * @access public + * @param mixed + * @param string + * @param string + * @return string + */ +function form_upload($data = '', $value = '', $extra = '') +{ + if ( ! is_array($data)) + { + $data = array('name' => $data); + } + + $data['type'] = 'file'; + return form_input($data, $value, $extra); +} + +// ------------------------------------------------------------------------ + +/** + * Textarea field + * + * @access public + * @param mixed + * @param string + * @param string + * @return string + */ +function form_textarea($data = '', $value = '', $extra = '') +{ + $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12'); + + $val = (( ! is_array($data) OR ! isset($data['value'])) ? $value : $data['value']); + + unset ($data['value']); // textareas don't use the value attribute + + return "\n"; +} + +// ------------------------------------------------------------------------ + +/** + * Drop-down Menu + * + * @access public + * @param string + * @param array + * @param string + * @param string + * @return string + */ +function form_dropdown($name = '', $options = array(), $selected = '', $extra = '') +{ + if ($extra != '') $extra = ' '.$extra; + + $form = ''; + + return $form; +} + +// ------------------------------------------------------------------------ + +/** + * Checkbox Field + * + * @access public + * @param mixed + * @param string + * @param bool + * @param string + * @return string + */ +function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '') +{ + $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); + + if (is_array($data) AND array_key_exists('checked', $data)) + { + $checked = $data['checked']; + + if ($checked == FALSE) + unset($data['checked']); + } + + if ($checked == TRUE) + $defaults['checked'] = 'checked'; + else + unset($defaults['checked']); + + return "\n"; +} + +// ------------------------------------------------------------------------ + +/** + * Radio Button + * + * @access public + * @param mixed + * @param string + * @param bool + * @param string + * @return string + */ +function form_radio($data = '', $value = '', $checked = TRUE, $extra = '') +{ + if ( ! is_array($data)) + { + $data = array('name' => $data); + } + + $data['type'] = 'radio'; + return form_checkbox($data, $value, $checked, $extra); +} + +// ------------------------------------------------------------------------ + +/** + * Submit Button + * + * @access public + * @param mixed + * @param string + * @param string + * @return string + */ +function form_submit($data = '', $value = '', $extra = '') +{ + $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); + + return "\n"; +} + +// ------------------------------------------------------------------------ + +/** + * Form Close Tag + * + * @access public + * @param string + * @return string + */ +function form_close($extra = '') +{ + return "\n".$extra; +} + +// ------------------------------------------------------------------------ + +/** + * Form Prep + * + * Formats text so that it can be safely placed in a form field in the event it has HTML tags. + * + * @access public + * @param string + * @return string + */ +function form_prep($str = '') +{ + if ($str === '') + { + return ''; + } + + $temp = '__TEMP_AMPERSANDS__'; + + // Replace entities to temporary markers so that + // htmlspecialchars won't mess them up + $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str); + $str = preg_replace("/&(\w+);/", "$temp\\1;", $str); + + $str = htmlspecialchars($str); + + // In case htmlspecialchars misses these. + $str = str_replace(array("'", '"'), array("'", """), $str); + + // Decode the temp markers back to entities + $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); + $str = preg_replace("/$temp(\w+);/","&\\1;",$str); + + return $str; +} + +// ------------------------------------------------------------------------ + +/** + * Parse the form attributes + * + * Helper function used by some of the form helpers + * + * @access private + * @param array + * @param array + * @return string + */ +function parse_form_attributes($attributes, $default) +{ + if (is_array($attributes)) + { + foreach ($default as $key => $val) + { + if (isset($attributes[$key])) + { + $default[$key] = $attributes[$key]; + unset($attributes[$key]); + } + } + + if (count($attributes) > 0) + { + $default = array_merge($default, $attributes); + } + } + + $att = ''; + foreach ($default as $key => $val) + { + if ($key == 'value') + { + $val = form_prep($val); + } + + $att .= $key . '="' . $val . '" '; + } + + return $att; +} + ?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b