summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/helpers/form_helper.php524
1 files changed, 523 insertions, 1 deletions
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php
index a23d4c6d2..e591eb74c 100644
--- a/system/helpers/form_helper.php
+++ b/system/helpers/form_helper.php
@@ -1 +1,523 @@
-<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter * * An open source application development framework for PHP 4.3.2 or newer * * @package CodeIgniter * @author Rick Ellis * @copyright Copyright (c) 2006, EllisLab, Inc. * @license http://www.codeigniter.com/user_guide/license.html * @link http://www.codeigniter.com * @since Version 1.0 * @filesource */ // ------------------------------------------------------------------------ /** * CodeIgniter Form Helpers * * @package CodeIgniter * @subpackage Helpers * @category Helpers * @author Rick Ellis * @link http://www.codeigniter.com/user_guide/helpers/form_helper.html */ // ------------------------------------------------------------------------ /** * Form Declaration * * Creates the opening portion of the form. * * @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($action = '', $attributes = array(), $hidden = array()) { $CI =& get_instance(); $form = '<form action="'.$CI->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 '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />'; } $form = ''; foreach ($name as $name => $value) { $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />'; } 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 "<input ".parse_form_attributes($data, $defaults).$extra." />\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'); if ( ! is_array($data) OR ! isset($data['value'])) { $val = $value; } else { $val = $data['value']; unset($data['value']); // textareas don't use the value attribute } return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\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 = '<select name="'.$name.'"'.$extra.">\n"; foreach ($options as $key => $val) { $key = (string) $key; $val = (string) $val; $sel = ($selected != $key) ? '' : ' selected="selected"'; $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n"; } $form .= '</select>'; 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']); } else { $data['checked'] = 'checked'; } } if ($checked == TRUE) $defaults['checked'] = 'checked'; else unset($defaults['checked']); return "<input ".parse_form_attributes($data, $defaults).$extra." />\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 "<input ".parse_form_attributes($data, $defaults).$extra." />\n"; } // ------------------------------------------------------------------------ /** * Reset Button * * @access public * @param mixed * @param string * @param string * @return string */ function form_reset($data = '', $value = '', $extra = '') { $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); return "<input ".parse_form_attributes($data, $defaults).$extra." />\n"; } // ------------------------------------------------------------------------ /** * Form Close Tag * * @access public * @param string * @return string */ function form_close($extra = '') { return "</form>\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("&#39;", "&quot;"), $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
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://www.codeigniter.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Form Helpers
+ *
+ * @package CodeIgniter
+ * @subpackage Helpers
+ * @category Helpers
+ * @author Rick Ellis
+ * @link http://www.codeigniter.com/user_guide/helpers/form_helper.html
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Form Declaration
+ *
+ * Creates the opening portion of the form.
+ *
+ * @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($action = '', $attributes = array(), $hidden = array())
+{
+ $CI =& get_instance();
+
+ $form = '<form action="'.$CI->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 '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
+ }
+
+ $form = '';
+ foreach ($name as $name => $value)
+ {
+ $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
+ }
+
+ 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 "<input ".parse_form_attributes($data, $defaults).$extra." />\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');
+
+ if ( ! is_array($data) OR ! isset($data['value']))
+ {
+ $val = $value;
+ }
+ else
+ {
+ $val = $data['value'];
+ unset($data['value']); // textareas don't use the value attribute
+ }
+
+ return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\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 = '<select name="'.$name.'"'.$extra.">\n";
+
+ foreach ($options as $key => $val)
+ {
+ $key = (string) $key;
+ $val = (string) $val;
+
+ $sel = ($selected != $key) ? '' : ' selected="selected"';
+
+ $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
+ }
+
+ $form .= '</select>';
+
+ 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']);
+ }
+ else
+ {
+ $data['checked'] = 'checked';
+ }
+ }
+
+ if ($checked == TRUE)
+ $defaults['checked'] = 'checked';
+ else
+ unset($defaults['checked']);
+
+ return "<input ".parse_form_attributes($data, $defaults).$extra." />\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 "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * Reset Button
+ *
+ * @access public
+ * @param mixed
+ * @param string
+ * @param string
+ * @return string
+ */
+function form_reset($data = '', $value = '', $extra = '')
+{
+ $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
+
+ return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * Form Label Tag
+ *
+ * @access public
+ * @param string The text to appear onscreen
+ * @param string The id the label applies to
+ * @param string Additional attributes
+ * @return string
+ */
+function form_label($label_text = '', $id = '', $attributes = array())
+{
+
+ $label = '<label';
+
+ if ($id != '')
+ {
+ $label .= " for=\"$id\"";
+ }
+
+ if (is_array($attributes) AND count($attributes) > 0)
+ {
+ foreach ($attributes as $key => $val)
+ {
+ $label .= ' '.$key.'="'.$val.'"';
+ }
+ }
+
+ $label .= ">$label_text</label>";
+
+ return $label;
+}
+
+// ------------------------------------------------------------------------
+/**
+ * Fieldset Tag
+ *
+ * Used to produce <fieldset><legend>text</legend>. To close fieldset
+ * use form_fieldset_close()
+ *
+ * @access public
+ * @param string The legend text
+ * @param string Additional attributes
+ * @return string
+ */
+function form_fieldset($legend_text = '', $attributes = array())
+{
+
+ $fieldset = "<fieldset";
+
+ if (is_array($attributes) AND count($attributes) > 0)
+ {
+ foreach ($attributes as $key => $val)
+ {
+ $fieldset .= ' '.$key.'="'.$val.'"';
+ }
+ }
+
+ $fieldset .= ">\n";
+
+ if ($legend_text != '')
+ {
+ $fieldset .= "<legend>$legend_text</legend>\n";
+ }
+
+
+
+ return $fieldset;
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * Fieldset Close Tag
+ *
+ * @access public
+ * @param string
+ * @return string
+ */
+function form_fieldset_close($extra = '')
+{
+ return "</fieldset>\n".$extra;
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * Form Close Tag
+ *
+ * @access public
+ * @param string
+ * @return string
+ */
+function form_close($extra = '')
+{
+ return "</form>\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("&#39;", "&quot;"), $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