config->site_url($action) : $action;
$form = '
".$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
*/
if ( ! function_exists('form_prep'))
{
function form_prep($str = '')
{
// if the field name is an array we do this recursively
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = form_prep($val);
}
return $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;
}
}
// ------------------------------------------------------------------------
/**
* 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
*
* @access public
* @param string
* @return mixed
*/
if ( ! function_exists('set_value'))
{
function set_value($field = '', $default = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
if ( ! isset($_POST[$field]))
{
return $default;
}
return form_prep($_POST[$field]);
}
return form_prep($OBJ->set_value($field, $default));
}
}
// ------------------------------------------------------------------------
/**
* 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
*
* @access public
* @param string
* @param string
* @param bool
* @return string
*/
if ( ! function_exists('set_select'))
{
function set_select($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0)
{
return ' selected="selected"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
else
{
if (($field == '' OR $value == '') OR ($field != $value))
{
return '';
}
}
return ' selected="selected"';
}
return $OBJ->set_select($field, $value, $default);
}
}
// ------------------------------------------------------------------------
/**
* 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
*
* @access public
* @param string
* @param string
* @param bool
* @return string
*/
if ( ! function_exists('set_checkbox'))
{
function set_checkbox($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0)
{
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_checkbox($field, $value, $default);
}
}
// ------------------------------------------------------------------------
/**
* 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
*
* @access public
* @param string
* @param string
* @param bool
* @return string
*/
if ( ! function_exists('set_radio'))
{
function set_radio($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0)
{
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);
}
}
// ------------------------------------------------------------------------
/**
* Form Error
*
* Returns the error for a specific form field. This is a helper for the
* form validation class.
*
* @access public
* @param string
* @param string
* @param string
* @return string
*/
if ( ! function_exists('form_error'))
{
function form_error($field = '', $prefix = '', $suffix = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
return '';
}
return $OBJ->error($field, $prefix, $suffix);
}
}
// ------------------------------------------------------------------------
/**
* Validation Error String
*
* Returns all the errors associated with a form submission. This is a helper
* function for the form validation class.
*
* @access public
* @param string
* @param string
* @return string
*/
if ( ! function_exists('validation_errors'))
{
function validation_errors($prefix = '', $suffix = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
return '';
}
return $OBJ->error_string($prefix, $suffix);
}
}
// ------------------------------------------------------------------------
/**
* Parse the form attributes
*
* Helper function used by some of the form helpers
*
* @access private
* @param array
* @param array
* @return string
*/
if ( ! function_exists('_parse_form_attributes'))
{
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;
}
}
// ------------------------------------------------------------------------
/**
* Attributes To String
*
* Helper function used by some of the form helpers
*
* @access private
* @param mixed
* @param bool
* @return string
*/
if ( ! function_exists('_attributes_to_string'))
{
function _attributes_to_string($attributes, $formtag = FALSE)
{
if (is_string($attributes) AND strlen($attributes) > 0)
{
if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
{
$attributes .= ' method="post"';
}
return ' '.$attributes;
}
if (is_object($attributes) AND count($attributes) > 0)
{
$attributes = (array)$attributes;
}
if (is_array($attributes) AND count($attributes) > 0)
{
$atts = '';
if ( ! isset($attributes['method']) AND $formtag === TRUE)
{
$atts .= ' method="post"';
}
foreach ($attributes as $key => $val)
{
$atts .= ' '.$key.'="'.$val.'"';
}
return $atts;
}
}
}
// ------------------------------------------------------------------------
/**
* Validation Object
*
* Determines what the form validation class was instantiated as, fetches
* the object and returns it.
*
* @access private
* @return mixed
*/
if ( ! function_exists('_get_validation_object'))
{
function &_get_validation_object()
{
$CI =& get_instance();
// We set this as a variable since we're returning by reference
$return = FALSE;
if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
{
return $return;
}
$object = $CI->load->_ci_classes['form_validation'];
if ( ! isset($CI->$object) OR ! is_object($CI->$object))
{
return $return;
}
return $CI->$object;
}
}
/* End of file form_helper.php */
/* Location: ./system/helpers/form_helper.php */