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 | 993925b | 2008-08-21 12:43:31 +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 = '';
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 114 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 115 | foreach ($name as $name => $value)
|
| 116 | {
|
Rick Ellis | f388511 | 2008-08-12 05:32:25 +0000 | [diff] [blame] | 117 | $form .= "\n";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 118 | $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
|
| 119 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 120 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 121 | return $form;
|
| 122 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 123 | }
|
| 124 |
|
| 125 | // ------------------------------------------------------------------------
|
| 126 |
|
| 127 | /**
|
| 128 | * Text Input Field
|
| 129 | *
|
| 130 | * @access public
|
| 131 | * @param mixed
|
| 132 | * @param string
|
| 133 | * @param string
|
| 134 | * @return string
|
| 135 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 136 | if ( ! function_exists('form_input'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 137 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 138 | function form_input($data = '', $value = '', $extra = '')
|
| 139 | {
|
Derek Allard | 3640a0b | 2008-06-19 07:46:22 +0000 | [diff] [blame] | 140 | $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 141 |
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 142 | return "<input ".parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 143 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 144 | }
|
| 145 |
|
| 146 | // ------------------------------------------------------------------------
|
| 147 |
|
| 148 | /**
|
| 149 | * Password Field
|
| 150 | *
|
| 151 | * Identical to the input function but adds the "password" type
|
| 152 | *
|
| 153 | * @access public
|
| 154 | * @param mixed
|
| 155 | * @param string
|
| 156 | * @param string
|
| 157 | * @return string
|
| 158 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 159 | if ( ! function_exists('form_password'))
|
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 | function form_password($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 162 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 163 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 164 | {
|
| 165 | $data = array('name' => $data);
|
| 166 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 167 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 168 | $data['type'] = 'password';
|
| 169 | return form_input($data, $value, $extra);
|
| 170 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 171 | }
|
| 172 |
|
| 173 | // ------------------------------------------------------------------------
|
| 174 |
|
| 175 | /**
|
| 176 | * Upload Field
|
| 177 | *
|
| 178 | * Identical to the input function but adds the "file" type
|
| 179 | *
|
| 180 | * @access public
|
| 181 | * @param mixed
|
| 182 | * @param string
|
| 183 | * @param string
|
| 184 | * @return string
|
| 185 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 186 | if ( ! function_exists('form_upload'))
|
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 | function form_upload($data = '', $value = '', $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 189 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 190 | if ( ! is_array($data))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 191 | {
|
| 192 | $data = array('name' => $data);
|
| 193 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 194 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 195 | $data['type'] = 'file';
|
| 196 | return form_input($data, $value, $extra);
|
| 197 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 198 | }
|
| 199 |
|
| 200 | // ------------------------------------------------------------------------
|
| 201 |
|
| 202 | /**
|
| 203 | * Textarea field
|
| 204 | *
|
| 205 | * @access public
|
| 206 | * @param mixed
|
| 207 | * @param string
|
| 208 | * @param string
|
| 209 | * @return string
|
| 210 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 211 | if ( ! function_exists('form_textarea'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 212 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 213 | function form_textarea($data = '', $value = '', $extra = '')
|
| 214 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 215 | $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 216 |
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame^] | 217 | if ( ! is_array($data) OR ! isset($data['value']))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 218 | {
|
| 219 | $val = $value;
|
| 220 | }
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame^] | 221 | else
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 222 | {
|
| 223 | $val = $data['value'];
|
| 224 | unset($data['value']); // textareas don't use the value attribute
|
| 225 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 226 |
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 227 | return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 228 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 229 | }
|
| 230 |
|
| 231 | // ------------------------------------------------------------------------
|
| 232 |
|
| 233 | /**
|
| 234 | * Drop-down Menu
|
| 235 | *
|
| 236 | * @access public
|
| 237 | * @param string
|
| 238 | * @param array
|
| 239 | * @param string
|
| 240 | * @param string
|
| 241 | * @return string
|
| 242 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 243 | if ( ! function_exists('form_dropdown'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 244 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 245 | function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 246 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 247 | if ( ! is_array($selected))
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 248 | {
|
| 249 | $selected = array($selected);
|
| 250 | }
|
Derek Allard | 4021b51 | 2008-01-04 22:26:12 +0000 | [diff] [blame] | 251 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 252 | if ($extra != '') $extra = ' '.$extra;
|
Derek Allard | f8f0570 | 2008-01-18 14:39:23 +0000 | [diff] [blame] | 253 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 254 | $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
|
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 | $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
|
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 | foreach ($options as $key => $val)
|
| 259 | {
|
| 260 | $key = (string) $key;
|
| 261 | $val = (string) $val;
|
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 | $sel = (in_array($key, $selected))?' selected="selected"':'';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 264 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 265 | $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
|
| 266 | }
|
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 | $form .= '</select>';
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 269 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 270 | return $form;
|
| 271 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 272 | }
|
| 273 |
|
| 274 | // ------------------------------------------------------------------------
|
| 275 |
|
| 276 | /**
|
| 277 | * Checkbox Field
|
| 278 | *
|
| 279 | * @access public
|
| 280 | * @param mixed
|
| 281 | * @param string
|
| 282 | * @param bool
|
| 283 | * @param string
|
| 284 | * @return string
|
| 285 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 286 | if ( ! function_exists('form_checkbox'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 287 | {
|
Derek Allard | de7320b | 2008-05-06 13:51:02 +0000 | [diff] [blame] | 288 | function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 289 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 290 | $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 291 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 292 | if (is_array($data) AND array_key_exists('checked', $data))
|
| 293 | {
|
| 294 | $checked = $data['checked'];
|
| 295 |
|
| 296 | if ($checked == FALSE)
|
| 297 | {
|
| 298 | unset($data['checked']);
|
| 299 | }
|
| 300 | else
|
| 301 | {
|
| 302 | $data['checked'] = 'checked';
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | if ($checked == TRUE)
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 307 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 308 | $defaults['checked'] = 'checked';
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 309 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 310 | else
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 311 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 312 | unset($defaults['checked']);
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 313 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 314 |
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 315 | return "<input ".parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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 Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 362 | return "<input ".parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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 Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 383 | return "<input ".parse_form_attributes($data, $defaults).$extra." />";
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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 |
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 410 | return "<button ".parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
|
Derek Allard | 707d0e0 | 2008-03-18 11:50:00 +0000 | [diff] [blame] | 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 | $fieldset = "<fieldset";
|
| 468 |
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame^] | 469 | $fieldset .= _attributes_to_string($attributes, FALSE);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 470 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 471 | $fieldset .= ">\n";
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 472 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 473 | if ($legend_text != '')
|
| 474 | {
|
| 475 | $fieldset .= "<legend>$legend_text</legend>\n";
|
| 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 | return $fieldset;
|
| 479 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 480 | }
|
| 481 |
|
| 482 | // ------------------------------------------------------------------------
|
| 483 |
|
| 484 | /**
|
| 485 | * Fieldset Close Tag
|
| 486 | *
|
| 487 | * @access public
|
| 488 | * @param string
|
| 489 | * @return string
|
| 490 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 491 | if ( ! function_exists('form_fieldset_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 492 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 493 | function form_fieldset_close($extra = '')
|
| 494 | {
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 495 | return "</fieldset>".$extra;
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 496 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 497 | }
|
| 498 |
|
| 499 | // ------------------------------------------------------------------------
|
| 500 |
|
| 501 | /**
|
| 502 | * Form Close Tag
|
| 503 | *
|
| 504 | * @access public
|
| 505 | * @param string
|
| 506 | * @return string
|
| 507 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 508 | if ( ! function_exists('form_close'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 509 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 510 | function form_close($extra = '')
|
| 511 | {
|
Derek Allard | c507418 | 2008-07-21 11:58:56 +0000 | [diff] [blame] | 512 | return "</form>".$extra;
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 513 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 514 | }
|
| 515 |
|
| 516 | // ------------------------------------------------------------------------
|
| 517 |
|
| 518 | /**
|
| 519 | * Form Prep
|
| 520 | *
|
| 521 | * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
|
| 522 | *
|
| 523 | * @access public
|
| 524 | * @param string
|
| 525 | * @return string
|
| 526 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 527 | if ( ! function_exists('form_prep'))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 528 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 529 | function form_prep($str = '')
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 530 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 531 | if ($str === '')
|
| 532 | {
|
| 533 | return '';
|
| 534 | }
|
| 535 |
|
| 536 | $temp = '__TEMP_AMPERSANDS__';
|
| 537 |
|
| 538 | // Replace entities to temporary markers so that
|
| 539 | // htmlspecialchars won't mess them up
|
| 540 | $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
|
| 541 | $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
|
| 542 |
|
| 543 | $str = htmlspecialchars($str);
|
| 544 |
|
| 545 | // In case htmlspecialchars misses these.
|
| 546 | $str = str_replace(array("'", '"'), array("'", """), $str);
|
| 547 |
|
| 548 | // Decode the temp markers back to entities
|
| 549 | $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
|
| 550 | $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
|
| 551 |
|
| 552 | return $str;
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 553 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 554 | }
|
| 555 |
|
| 556 | // ------------------------------------------------------------------------
|
| 557 |
|
| 558 | /**
|
| 559 | * Parse the form attributes
|
| 560 | *
|
| 561 | * Helper function used by some of the form helpers
|
| 562 | *
|
| 563 | * @access private
|
| 564 | * @param array
|
| 565 | * @param array
|
| 566 | * @return string
|
| 567 | */
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 568 | if ( ! function_exists('parse_form_attributes'))
|
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 | function parse_form_attributes($attributes, $default)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 571 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 572 | if (is_array($attributes))
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 573 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 574 | foreach ($default as $key => $val)
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 575 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 576 | if (isset($attributes[$key]))
|
| 577 | {
|
| 578 | $default[$key] = $attributes[$key];
|
| 579 | unset($attributes[$key]);
|
| 580 | }
|
| 581 | }
|
| 582 |
|
| 583 | if (count($attributes) > 0)
|
| 584 | {
|
| 585 | $default = array_merge($default, $attributes);
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 586 | }
|
| 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 = '';
|
| 590 | foreach ($default as $key => $val)
|
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 | if ($key == 'value')
|
| 593 | {
|
| 594 | $val = form_prep($val);
|
| 595 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 596 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 597 | $att .= $key . '="' . $val . '" ';
|
| 598 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 599 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 600 | return $att;
|
| 601 | }
|
Derek Allard | f3e8a35 | 2008-01-04 14:30:38 +0000 | [diff] [blame] | 602 | }
|
| 603 |
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 604 | // ------------------------------------------------------------------------
|
| 605 |
|
| 606 | /**
|
| 607 | * Attributes To String
|
| 608 | *
|
| 609 | * Helper function used by some of the form helpers
|
| 610 | *
|
| 611 | * @access private
|
| 612 | * @param mixed
|
| 613 | * @param bool
|
| 614 | * @return string
|
| 615 | */
|
| 616 | if ( ! function_exists('_attributes_to_string'))
|
| 617 | {
|
| 618 | function _attributes_to_string($attributes, $formtag = FALSE)
|
| 619 | {
|
| 620 | if (is_string($attributes) AND strlen($attributes) > 0)
|
| 621 | {
|
| 622 | if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
|
| 623 | {
|
| 624 | $attributes .= ' method="post"';
|
| 625 | }
|
Derek Allard | 993925b | 2008-08-21 12:43:31 +0000 | [diff] [blame^] | 626 |
|
| 627 | return ' '.$attributes;
|
Derek Allard | 1e6ab99 | 2008-06-06 11:37:34 +0000 | [diff] [blame] | 628 | }
|
| 629 |
|
| 630 | if (is_object($attributes) AND count($attributes) > 0)
|
| 631 | {
|
| 632 | $attributes = (array)$attributes;
|
| 633 | }
|
| 634 |
|
| 635 | if (is_array($attributes) AND count($attributes) > 0)
|
| 636 | {
|
| 637 | $atts = '';
|
| 638 |
|
| 639 | if ( ! isset($attributes['method']) AND $formtag === TRUE)
|
| 640 | {
|
| 641 | $atts .= ' method="post"';
|
| 642 | }
|
| 643 |
|
| 644 | foreach ($attributes as $key => $val)
|
| 645 | {
|
| 646 | $atts .= ' '.$key.'="'.$val.'"';
|
| 647 | }
|
| 648 |
|
| 649 | return $atts;
|
| 650 | }
|
| 651 | }
|
| 652 | }
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 653 |
|
| 654 | /* End of file form_helper.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 655 | /* Location: ./system/helpers/form_helper.php */ |