config->site_url($action);
}
elseif ( ! $action)
{
// If no action is provided then set to the current url
$action = $CI->config->site_url($CI->uri->uri_string());
}
$form = '
'.$extra;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('form_prep'))
{
/**
* Form Prep
*
* Formats text so that it can be safely placed in a form field in the event it has HTML tags.
*
* @todo Remove in version 3.1+.
* @deprecated 3.0.0 This function has been broken for a long time
* and is now just an alias for html_escape(). It's
* second argument is ignored.
* @param string $str = ''
* @param string $field_name = ''
* @return string
*/
function form_prep($str = '', $field_name = '')
{
return html_escape($str);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_value'))
{
/**
* Form Value
*
* Grabs a value from the POST array for the specified field so you can
* re-populate an input field or textarea. If Form Validation
* is active it retrieves the info from the validation class
*
* @param string
* @param string
* @return mixed
*/
function set_value($field = '', $default = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
if ( ! isset($_POST[$field]))
{
return html_escape($default);
}
return html_escape($_POST[$field]);
}
return html_escape($OBJ->set_value($field, $default));
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_select'))
{
/**
* Set Select
*
* Let's you set the selected value of a menu via data in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*
* @param string
* @param string
* @param bool
* @return string
*/
function set_select($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 && $default === TRUE)
{
return ' selected="selected"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
elseif (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
return ' selected="selected"';
}
return $OBJ->set_select($field, $value, $default);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_checkbox'))
{
/**
* Set Checkbox
*
* Let's you set the selected value of a checkbox via the value in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*
* @param string
* @param string
* @param bool
* @return string
*/
function set_checkbox($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 && $default === TRUE)
{
return ' checked="checked"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
elseif (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
return ' checked="checked"';
}
return $OBJ->set_checkbox($field, $value, $default);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_radio'))
{
/**
* Set Radio
*
* Let's you set the selected value of a radio field via info in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*
* @param string
* @param string
* @param bool
* @return string
*/
function set_radio($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 && $default === TRUE)
{
return ' checked="checked"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
else
{
if (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
}
return ' checked="checked"';
}
return $OBJ->set_radio($field, $value, $default);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('form_error'))
{
/**
* Form Error
*
* Returns the error for a specific form field. This is a helper for the
* form validation class.
*
* @param string
* @param string
* @param string
* @return string
*/
function form_error($field = '', $prefix = '', $suffix = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
return '';
}
return $OBJ->error($field, $prefix, $suffix);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('validation_errors'))
{
/**
* Validation Error String
*
* Returns all the errors associated with a form submission. This is a helper
* function for the form validation class.
*
* @param string
* @param string
* @return string
*/
function validation_errors($prefix = '', $suffix = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
return '';
}
return $OBJ->error_string($prefix, $suffix);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_parse_form_attributes'))
{
/**
* Parse the form attributes
*
* Helper function used by some of the form helpers
*
* @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 = html_escape($val);
}
elseif ($key === 'name' && ! strlen($default['name']))
{
continue;
}
$att .= $key.'="'.$val.'" ';
}
return $att;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_attributes_to_string'))
{
/**
* 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)
{
if (is_string($attributes) && strlen($attributes) > 0)
{
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;
}
if (is_object($attributes) && count($attributes) > 0)
{
$attributes = (array) $attributes;
}
if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
{
$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.'"';
}
return $atts;
}
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_get_validation_object'))
{
/**
* Validation Object
*
* Determines what the form validation class was instantiated as, fetches
* the object and returns it.
*
* @return mixed
*/
function &_get_validation_object()
{
$CI =& get_instance();
// We set this as a variable since we're returning by reference.
$return = FALSE;
if (FALSE !== ($object = $CI->load->is_loaded('form_validation')))
{
if ( ! isset($CI->$object) OR ! is_object($CI->$object))
{
return $return;
}
return $CI->$object;
}
return $return;
}
}
/* End of file form_helper.php */
/* Location: ./system/helpers/form_helper.php */