From ea19bc4f8fea2a7b6d0b1d85c279369ec8fce06e Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sat, 27 Jul 2013 10:07:43 +0200 Subject: Form helper: refactor form_open() and _attributes_to_string() --- system/helpers/form_helper.php | 56 ++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 38 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 7f4276bc7..f28296c2e 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -50,21 +50,20 @@ if ( ! function_exists('form_open')) * @param array a key/value pair hidden data * @return string */ - function form_open($action = '', $attributes = '', $hidden = array()) + function form_open($action = '', $attributes = array(), $hidden = array()) { $CI =& get_instance(); - if (empty($attributes)) - { - $attributes = 'method="post"'; - } - elseif (is_array($attributes) && ! isset($attributes['method'])) + $attributes = _attributes_to_string($attributes); + + if (stripos($attributes, 'method=') === FALSE) { - $attributes['method'] = 'post'; + $attributes .= ' method="post"'; } - elseif (stripos($attributes, 'method=') === FALSE) + + if (stripos($attributes, 'accept-charset=') === FALSE) { - $attributes .= ' method="post"'; + $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; } // If an action is not a full URL then turn it into one @@ -78,7 +77,7 @@ if ( ! function_exists('form_open')) $action = $CI->config->site_url($CI->uri->uri_string()); } - $form = '
\n"; + $form = '\n"; // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR stripos($form, 'method="get"'))) @@ -550,12 +549,12 @@ if ( ! function_exists('form_fieldset')) * use form_fieldset_close() * * @param string The legend text - * @param string Additional attributes + * @param array Additional attributes * @return string */ function form_fieldset($legend_text = '', $attributes = array()) { - $fieldset = '\n"; + $fieldset = '\n"; if ($legend_text !== '') { return $fieldset.''.$legend_text."\n"; @@ -928,45 +927,24 @@ if ( ! function_exists('_attributes_to_string')) * Helper function used by some of the form helpers * * @param mixed - * @param bool * @return string */ - function _attributes_to_string($attributes, $formtag = FALSE) + function _attributes_to_string($attributes) { - if (is_string($attributes) && strlen($attributes) > 0) + if (is_string($attributes)) { - if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE) - { - $attributes .= ' method="post"'; - } - - if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE) - { - $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; - } - - return ' '.$attributes; + return ($attributes === '' ? '' : ' '.$attributes); } - if (is_object($attributes) && count($attributes) > 0) + if (is_object($attributes)) { $attributes = (array) $attributes; } - if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0)) + if (is_array($attributes)) { $atts = ''; - if ( ! isset($attributes['method']) && $formtag === TRUE) - { - $atts .= ' method="post"'; - } - - if ( ! isset($attributes['accept-charset']) && $formtag === TRUE) - { - $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"'; - } - foreach ($attributes as $key => $val) { $atts .= ' '.$key.'="'.$val.'"'; @@ -974,6 +952,8 @@ if ( ! function_exists('_attributes_to_string')) return $atts; } + + return FALSE; } } -- cgit v1.2.3-24-g4f1b