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