Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * CodeIgniter Form Helpers
|
| 20 | *
|
| 21 | * @package CodeIgniter
|
| 22 | * @subpackage Helpers
|
| 23 | * @category Helpers
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 24 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/helpers/form_helper.html
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 26 | */
|
| 27 |
|
| 28 | // ------------------------------------------------------------------------
|
| 29 |
|
| 30 | /**
|
| 31 | * Form Declaration
|
| 32 | *
|
| 33 | * Creates the opening portion of the form.
|
| 34 | *
|
| 35 | * @access public
|
| 36 | * @param string the URI segments of the form destination
|
| 37 | * @param array a key/value pair of attributes
|
| 38 | * @param array a key/value pair hidden data
|
| 39 | * @return string
|
| 40 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 41 | if ( ! function_exists('form_open'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 42 | {
|
Derek Allard | 96537fd | 2008-06-06 13:41:53 +0000 | [diff] [blame] | 43 | function form_open($action = '', $attributes = '', $hidden = array())
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 44 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 45 | $CI =& get_instance();
|
| 46 |
|
Derek Allard | 96537fd | 2008-06-06 13:41:53 +0000 | [diff] [blame] | 47 | if ($attributes == '')
|
| 48 | {
|
| 49 | $attributes = 'method="post"';
|
| 50 | }
|
| 51 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 52 | $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
|
| 53 |
|
| 54 | $form = '<form action="'.$action.'"';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 55 |
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 56 | $form .= _attributes_to_string($attributes, TRUE);
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 57 |
|
| 58 | $form .= '>';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 59 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 60 | if (is_array($hidden) AND count($hidden > 0))
|
| 61 | {
|
| 62 | $form .= form_hidden($hidden);
|
| 63 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 64 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 65 | return $form;
|
| 66 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 67 | }
|
| 68 |
|
| 69 | // ------------------------------------------------------------------------
|
| 70 |
|
| 71 | /**
|
| 72 | * Form Declaration - Multipart type
|
| 73 | *
|
| 74 | * Creates the opening portion of the form, but with "multipart/form-data".
|
| 75 | *
|
| 76 | * @access public
|
| 77 | * @param string the URI segments of the form destination
|
| 78 | * @param array a key/value pair of attributes
|
| 79 | * @param array a key/value pair hidden data
|
| 80 | * @return string
|
| 81 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 82 | if ( ! function_exists('form_open_multipart'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 83 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 84 | function form_open_multipart($action, $attributes = array(), $hidden = array())
|
| 85 | {
|
| 86 | $attributes['enctype'] = 'multipart/form-data';
|
| 87 | return form_open($action, $attributes, $hidden);
|
| 88 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 89 | }
|
| 90 |
|
| 91 | // ------------------------------------------------------------------------
|
| 92 |
|
| 93 | /**
|
| 94 | * Hidden Input Field
|
| 95 | *
|
| 96 | * Generates hidden fields. You can pass a simple key/value string or an associative
|
| 97 | * array with multiple values.
|
| 98 | *
|
| 99 | * @access public
|
| 100 | * @param mixed
|
| 101 | * @param string
|
| 102 | * @return string
|
| 103 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 104 | if ( ! function_exists('form_hidden'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 105 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 106 | function form_hidden($name, $value = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 107 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 108 | if ( ! is_array($name))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 109 | {
|
| 110 | return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 111 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 112 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 113 | $form = '';
|
| 114 | foreach ($name as $name => $value)
|
| 115 | {
|
| 116 | $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 117 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 118 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 119 | return $form;
|
| 120 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 121 | }
|
| 122 |
|
| 123 | // ------------------------------------------------------------------------
|
| 124 |
|
| 125 | /**
|
| 126 | * Text Input Field
|
| 127 | *
|
| 128 | * @access public
|
| 129 | * @param mixed
|
| 130 | * @param string
|
| 131 | * @param string
|
| 132 | * @return string
|
| 133 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 134 | if ( ! function_exists('form_input'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 135 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 136 | function form_input($data = '', $value = '', $extra = '')
|
| 137 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 138 | $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 139 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 140 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 141 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 142 | }
|
| 143 |
|
| 144 | // ------------------------------------------------------------------------
|
| 145 |
|
| 146 | /**
|
| 147 | * Password Field
|
| 148 | *
|
| 149 | * Identical to the input function but adds the "password" type
|
| 150 | *
|
| 151 | * @access public
|
| 152 | * @param mixed
|
| 153 | * @param string
|
| 154 | * @param string
|
| 155 | * @return string
|
| 156 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 157 | if ( ! function_exists('form_password'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 158 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 159 | function form_password($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 160 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 161 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 162 | {
|
| 163 | $data = array('name' => $data);
|
| 164 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 165 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 166 | $data['type'] = 'password';
|
| 167 | return form_input($data, $value, $extra);
|
| 168 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 169 | }
|
| 170 |
|
| 171 | // ------------------------------------------------------------------------
|
| 172 |
|
| 173 | /**
|
| 174 | * Upload Field
|
| 175 | *
|
| 176 | * Identical to the input function but adds the "file" type
|
| 177 | *
|
| 178 | * @access public
|
| 179 | * @param mixed
|
| 180 | * @param string
|
| 181 | * @param string
|
| 182 | * @return string
|
| 183 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 184 | if ( ! function_exists('form_upload'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 185 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 186 | function form_upload($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 187 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 188 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 189 | {
|
| 190 | $data = array('name' => $data);
|
| 191 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 192 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 193 | $data['type'] = 'file';
|
| 194 | return form_input($data, $value, $extra);
|
| 195 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 196 | }
|
| 197 |
|
| 198 | // ------------------------------------------------------------------------
|
| 199 |
|
| 200 | /**
|
| 201 | * Textarea field
|
| 202 | *
|
| 203 | * @access public
|
| 204 | * @param mixed
|
| 205 | * @param string
|
| 206 | * @param string
|
| 207 | * @return string
|
| 208 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 209 | if ( ! function_exists('form_textarea'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 210 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 211 | function form_textarea($data = '', $value = '', $extra = '')
|
| 212 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 213 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 214 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 215 | if ( ! is_array($data) OR ! isset($data['value']))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 216 | {
|
| 217 | $val = $value;
|
| 218 | }
|
| 219 | else
|
| 220 | {
|
| 221 | $val = $data['value'];
|
| 222 | unset($data['value']); // textareas don't use the value attribute
|
| 223 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 224 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 225 | return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
|
| 226 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 227 | }
|
| 228 |
|
| 229 | // ------------------------------------------------------------------------
|
| 230 |
|
| 231 | /**
|
| 232 | * Drop-down Menu
|
| 233 | *
|
| 234 | * @access public
|
| 235 | * @param string
|
| 236 | * @param array
|
| 237 | * @param string
|
| 238 | * @param string
|
| 239 | * @return string
|
| 240 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 241 | if ( ! function_exists('form_dropdown'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 242 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 243 | function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 244 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 245 | if ( ! is_array($selected))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 246 | {
|
| 247 | $selected = array($selected);
|
| 248 | }
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 249 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 250 | if ($extra != '') $extra = ' '.$extra;
|
Derek Allard | f8f0570 | 2008-01-18 14:39:23 +0000 | [diff] [blame] | 251 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 252 | $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 253 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 254 | $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 255 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 256 | foreach ($options as $key => $val)
|
| 257 | {
|
| 258 | $key = (string) $key;
|
| 259 | $val = (string) $val;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 260 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 261 | $sel = (in_array($key, $selected))?' selected="selected"':'';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 262 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 263 | $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
|
| 264 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 265 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 266 | $form .= '</select>';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 267 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 268 | return $form;
|
| 269 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 270 | }
|
| 271 |
|
| 272 | // ------------------------------------------------------------------------
|
| 273 |
|
| 274 | /**
|
| 275 | * Checkbox Field
|
| 276 | *
|
| 277 | * @access public
|
| 278 | * @param mixed
|
| 279 | * @param string
|
| 280 | * @param bool
|
| 281 | * @param string
|
| 282 | * @return string
|
| 283 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 284 | if ( ! function_exists('form_checkbox'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 285 | {
|
Derek Allard | de7320b | 2008-05-06 13:51:02 +0000 | [diff] [blame] | 286 | function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 287 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 288 | $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 289 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 290 | if (is_array($data) AND array_key_exists('checked', $data))
|
| 291 | {
|
| 292 | $checked = $data['checked'];
|
| 293 |
|
| 294 | if ($checked == FALSE)
|
| 295 | {
|
| 296 | unset($data['checked']);
|
| 297 | }
|
| 298 | else
|
| 299 | {
|
| 300 | $data['checked'] = 'checked';
|
| 301 | }
|
| 302 | }
|
| 303 |
|
| 304 | if ($checked == TRUE)
|
| 305 | $defaults['checked'] = 'checked';
|
| 306 | else
|
| 307 | unset($defaults['checked']);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 308 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 309 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 310 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 311 | }
|
| 312 |
|
| 313 | // ------------------------------------------------------------------------
|
| 314 |
|
| 315 | /**
|
| 316 | * Radio Button
|
| 317 | *
|
| 318 | * @access public
|
| 319 | * @param mixed
|
| 320 | * @param string
|
| 321 | * @param bool
|
| 322 | * @param string
|
| 323 | * @return string
|
| 324 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 325 | if ( ! function_exists('form_radio'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 326 | {
|
Derek Allard | de7320b | 2008-05-06 13:51:02 +0000 | [diff] [blame] | 327 | function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 328 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 329 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 330 | {
|
| 331 | $data = array('name' => $data);
|
| 332 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 333 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 334 | $data['type'] = 'radio';
|
| 335 | return form_checkbox($data, $value, $checked, $extra);
|
| 336 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 337 | }
|
| 338 |
|
| 339 | // ------------------------------------------------------------------------
|
| 340 |
|
| 341 | /**
|
| 342 | * Submit Button
|
| 343 | *
|
| 344 | * @access public
|
| 345 | * @param mixed
|
| 346 | * @param string
|
| 347 | * @param string
|
| 348 | * @return string
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 349 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 350 | if ( ! function_exists('form_submit'))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 351 | {
|
| 352 | function form_submit($data = '', $value = '', $extra = '')
|
| 353 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 354 | $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 355 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 356 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 357 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 358 | }
|
| 359 |
|
| 360 | // ------------------------------------------------------------------------
|
| 361 |
|
| 362 | /**
|
| 363 | * Reset Button
|
| 364 | *
|
| 365 | * @access public
|
| 366 | * @param mixed
|
| 367 | * @param string
|
| 368 | * @param string
|
| 369 | * @return string
|
| 370 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 371 | if ( ! function_exists('form_reset'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 372 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 373 | function form_reset($data = '', $value = '', $extra = '')
|
| 374 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 375 | $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 376 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 377 | return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
|
| 378 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 379 | }
|
| 380 |
|
| 381 | // ------------------------------------------------------------------------
|
| 382 |
|
| 383 | /**
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 384 | * Form Button
|
| 385 | *
|
| 386 | * @access public
|
| 387 | * @param mixed
|
| 388 | * @param string
|
| 389 | * @param string
|
| 390 | * @return string
|
| 391 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 392 | if ( ! function_exists('form_button'))
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 393 | {
|
| 394 | function form_button($data = '', $content = '', $extra = '')
|
| 395 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 396 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'submit');
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 397 |
|
| 398 | if ( is_array($data) AND isset($data['content']))
|
| 399 | {
|
| 400 | $content = $data['content'];
|
| 401 | unset($data['content']); // content is not an attribute
|
| 402 | }
|
| 403 |
|
| 404 | return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>\n";
|
| 405 | }
|
| 406 | }
|
| 407 |
|
| 408 | // ------------------------------------------------------------------------
|
| 409 |
|
| 410 | /**
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 411 | * Form Label Tag
|
| 412 | *
|
| 413 | * @access public
|
| 414 | * @param string The text to appear onscreen
|
| 415 | * @param string The id the label applies to
|
| 416 | * @param string Additional attributes
|
| 417 | * @return string
|
| 418 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 419 | if ( ! function_exists('form_label'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 420 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 421 | function form_label($label_text = '', $id = '', $attributes = array())
|
| 422 | {
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 423 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 424 | $label = '<label';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 425 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 426 | if ($id != '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 427 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 428 | $label .= " for=\"$id\"";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 429 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 430 |
|
| 431 | if (is_array($attributes) AND count($attributes) > 0)
|
| 432 | {
|
| 433 | foreach ($attributes as $key => $val)
|
| 434 | {
|
| 435 | $label .= ' '.$key.'="'.$val.'"';
|
| 436 | }
|
| 437 | }
|
| 438 |
|
| 439 | $label .= ">$label_text</label>";
|
| 440 |
|
| 441 | return $label;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 442 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 443 | }
|
| 444 |
|
| 445 | // ------------------------------------------------------------------------
|
| 446 | /**
|
| 447 | * Fieldset Tag
|
| 448 | *
|
| 449 | * Used to produce <fieldset><legend>text</legend>. To close fieldset
|
| 450 | * use form_fieldset_close()
|
| 451 | *
|
| 452 | * @access public
|
| 453 | * @param string The legend text
|
| 454 | * @param string Additional attributes
|
| 455 | * @return string
|
| 456 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 457 | if ( ! function_exists('form_fieldset'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 458 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 459 | function form_fieldset($legend_text = '', $attributes = array())
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 460 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 461 | $fieldset = "<fieldset";
|
| 462 |
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 463 | $fieldset .= _attributes_to_string($attributes, FALSE);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 464 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 465 | $fieldset .= ">\n";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 466 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 467 | if ($legend_text != '')
|
| 468 | {
|
| 469 | $fieldset .= "<legend>$legend_text</legend>\n";
|
| 470 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 471 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 472 | return $fieldset;
|
| 473 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 474 | }
|
| 475 |
|
| 476 | // ------------------------------------------------------------------------
|
| 477 |
|
| 478 | /**
|
| 479 | * Fieldset Close Tag
|
| 480 | *
|
| 481 | * @access public
|
| 482 | * @param string
|
| 483 | * @return string
|
| 484 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 485 | if ( ! function_exists('form_fieldset_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 486 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 487 | function form_fieldset_close($extra = '')
|
| 488 | {
|
| 489 | return "</fieldset>\n".$extra;
|
| 490 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 491 | }
|
| 492 |
|
| 493 | // ------------------------------------------------------------------------
|
| 494 |
|
| 495 | /**
|
| 496 | * Form Close Tag
|
| 497 | *
|
| 498 | * @access public
|
| 499 | * @param string
|
| 500 | * @return string
|
| 501 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 502 | if ( ! function_exists('form_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 503 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 504 | function form_close($extra = '')
|
| 505 | {
|
| 506 | return "</form>\n".$extra;
|
| 507 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 508 | }
|
| 509 |
|
| 510 | // ------------------------------------------------------------------------
|
| 511 |
|
| 512 | /**
|
| 513 | * Form Prep
|
| 514 | *
|
| 515 | * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
|
| 516 | *
|
| 517 | * @access public
|
| 518 | * @param string
|
| 519 | * @return string
|
| 520 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 521 | if ( ! function_exists('form_prep'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 522 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 523 | function form_prep($str = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 524 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 525 | if ($str === '')
|
| 526 | {
|
| 527 | return '';
|
| 528 | }
|
| 529 |
|
| 530 | $temp = '__TEMP_AMPERSANDS__';
|
| 531 |
|
| 532 | // Replace entities to temporary markers so that
|
| 533 | // htmlspecialchars won't mess them up
|
| 534 | $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
|
| 535 | $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
|
| 536 |
|
| 537 | $str = htmlspecialchars($str);
|
| 538 |
|
| 539 | // In case htmlspecialchars misses these.
|
| 540 | $str = str_replace(array("'", '"'), array("'", """), $str);
|
| 541 |
|
| 542 | // Decode the temp markers back to entities
|
| 543 | $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
|
| 544 | $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
|
| 545 |
|
| 546 | return $str;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 547 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 548 | }
|
| 549 |
|
| 550 | // ------------------------------------------------------------------------
|
| 551 |
|
| 552 | /**
|
| 553 | * Parse the form attributes
|
| 554 | *
|
| 555 | * Helper function used by some of the form helpers
|
| 556 | *
|
| 557 | * @access private
|
| 558 | * @param array
|
| 559 | * @param array
|
| 560 | * @return string
|
| 561 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 562 | if ( ! function_exists('parse_form_attributes'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 563 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 564 | function parse_form_attributes($attributes, $default)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 565 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 566 | if (is_array($attributes))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 567 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 568 | foreach ($default as $key => $val)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 569 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 570 | if (isset($attributes[$key]))
|
| 571 | {
|
| 572 | $default[$key] = $attributes[$key];
|
| 573 | unset($attributes[$key]);
|
| 574 | }
|
| 575 | }
|
| 576 |
|
| 577 | if (count($attributes) > 0)
|
| 578 | {
|
| 579 | $default = array_merge($default, $attributes);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 580 | }
|
| 581 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 582 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 583 | $att = '';
|
| 584 | foreach ($default as $key => $val)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 585 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 586 | if ($key == 'value')
|
| 587 | {
|
| 588 | $val = form_prep($val);
|
| 589 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 590 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 591 | $att .= $key . '="' . $val . '" ';
|
| 592 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 593 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 594 | return $att;
|
| 595 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 596 | }
|
| 597 |
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 598 | // ------------------------------------------------------------------------
|
| 599 |
|
| 600 | /**
|
| 601 | * Attributes To String
|
| 602 | *
|
| 603 | * Helper function used by some of the form helpers
|
| 604 | *
|
| 605 | * @access private
|
| 606 | * @param mixed
|
| 607 | * @param bool
|
| 608 | * @return string
|
| 609 | */
|
| 610 | if ( ! function_exists('_attributes_to_string'))
|
| 611 | {
|
| 612 | function _attributes_to_string($attributes, $formtag = FALSE)
|
| 613 | {
|
| 614 | if (is_string($attributes) AND strlen($attributes) > 0)
|
| 615 | {
|
| 616 | if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
|
| 617 | {
|
| 618 | $attributes .= ' method="post"';
|
| 619 | }
|
| 620 |
|
| 621 | return ' '.$attributes;
|
| 622 | }
|
| 623 |
|
| 624 | if (is_object($attributes) AND count($attributes) > 0)
|
| 625 | {
|
| 626 | $attributes = (array)$attributes;
|
| 627 | }
|
| 628 |
|
| 629 | if (is_array($attributes) AND count($attributes) > 0)
|
| 630 | {
|
| 631 | $atts = '';
|
| 632 |
|
| 633 | if ( ! isset($attributes['method']) AND $formtag === TRUE)
|
| 634 | {
|
| 635 | $atts .= ' method="post"';
|
| 636 | }
|
| 637 |
|
| 638 | foreach ($attributes as $key => $val)
|
| 639 | {
|
| 640 | $atts .= ' '.$key.'="'.$val.'"';
|
| 641 | }
|
| 642 |
|
| 643 | return $atts;
|
| 644 | }
|
| 645 | }
|
| 646 | }
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 647 |
|
| 648 | /* End of file form_helper.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 649 | /* Location: ./system/helpers/form_helper.php */ |