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