summaryrefslogtreecommitdiffstats
path: root/system/helpers/form_helper.php
diff options
context:
space:
mode:
authoradmin <devnull@localhost>2006-08-25 19:25:49 +0200
committeradmin <devnull@localhost>2006-08-25 19:25:49 +0200
commitb0dd10f8171945e0c1f3527dd1e9d18b043e01a7 (patch)
treec3583ba09e72217683c4304f4690df6ce39ba731 /system/helpers/form_helper.php
Initial Import
Diffstat (limited to 'system/helpers/form_helper.php')
-rw-r--r--system/helpers/form_helper.php382
1 files changed, 382 insertions, 0 deletions
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php
new file mode 100644
index 000000000..f82ad01a0
--- /dev/null
+++ b/system/helpers/form_helper.php
@@ -0,0 +1,382 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * Code Igniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author Rick Ellis
+ * @copyright Copyright (c) 2006, pMachine, Inc.
+ * @license http://www.codeignitor.com/user_guide/license.html
+ * @link http://www.codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Code Igniter 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())
+{
+ $obj =& get_instance();
+
+ $form = '<form method="post" action="'.$obj->config->site_url($action).'"';
+
+ 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');
+
+ $val = (( ! is_array($data) OR ! isset($data['value'])) ? $value : $data['value']);
+
+ return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * Dropdown 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)
+ {
+ $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 (isset($data['checked']))
+ {
+ $checked = $data['checked'];
+
+ if ($checked == FALSE)
+ unset($data['checked']);
+ }
+
+ if ($checked == TRUE)
+ $defaults['checked'] = ' 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";
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * 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 '';
+ }
+
+ return str_replace(array("'", '"'), array("&#39;", "&quot;"), htmlspecialchars($str));
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * Parse the form attributes
+ *
+ * Helper function used by some of the form helpers
+ *
+ * @access private
+ * @param array
+ * @parm 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