| | 58 | /* |
| | 59 | * Method: open_multipart |
| | 60 | * Generates an opening HTML form tag that can be used for uploading files. |
| | 61 | * |
| | 62 | * Parameters: |
| | 63 | * action - form action attribute |
| | 64 | * attr - extra attributes |
| | 65 | * hidden - hidden fields to be created immediately after the form tag |
| | 66 | * |
| | 67 | * Returns: |
| | 68 | * An HTML form tag. |
| | 69 | */ |
| | 70 | public static function open_multipart($action = '', $attr = array(), $hidden = array()) |
| | 71 | { |
| | 72 | // Set multi-part form type |
| | 73 | $attr['enctype'] = 'multipart/form-data'; |
| | 74 | |
| | 75 | return self::open($action, $attr, $hidden); |
| | 76 | } |
| | 77 | |
| | 78 | /** |
| | 79 | * Hidden Input Field |
| | 80 | * |
| | 81 | * Generates hidden fields. You can pass a simple key/value string or an associative |
| | 82 | * array with multiple values. |
| | 83 | * |
| | 84 | * @access public |
| | 85 | * @param mixed |
| | 86 | * @param string |
| | 87 | * @return string |
| | 88 | */ |
| | 89 | /* |
| | 90 | * Method: open |
| | 91 | * Generates hidden form fields. |
| | 92 | * |
| | 93 | * Parameters: |
| | 94 | * data - input name (string) or key/value pairs (array) |
| | 95 | * value - input value, if using an input name |
| | 96 | * |
| | 97 | * Returns: |
| | 98 | * One or more HTML hidden form input tags. |
| | 99 | */ |
| | 100 | public static function hidden($data, $value = '') |
| | 101 | { |
| | 102 | if ( ! is_array($data)) |
| | 103 | { |
| | 104 | $data = array |
| | 105 | ( |
| | 106 | $data => $value |
| | 107 | ); |
| | 108 | } |
| | 109 | |
| | 110 | $input = ''; |
| | 111 | foreach($data as $name => $value) |
| | 112 | { |
| | 113 | $attr = array |
| | 114 | ( |
| | 115 | 'type' => 'hidden', |
| | 116 | 'name' => $name, |
| | 117 | 'value' => $value |
| | 118 | ); |
| | 119 | |
| | 120 | $input .= self::input($attr)."\n"; |
| | 121 | } |
| | 122 | |
| | 123 | return $input; |
| | 124 | } |
| | 125 | |
| | 126 | /* |
| | 127 | * Method: input |
| | 128 | * Creates an HTML form input tag. Defaults to a text type. |
| | 129 | * |
| | 130 | * Parameters: |
| | 131 | * data - input name or an array of HTML attributes |
| | 132 | * value - input value, when using a name |
| | 133 | * extra - a string to be attached to the end of the attributes |
| | 134 | * |
| | 135 | * Returns: |
| | 136 | * An HTML form input tag. |
| | 137 | */ |
| | 138 | public static function input($data = '', $value = '', $extra = '') |
| | 139 | { |
| | 140 | if ( ! is_array($data)) |
| | 141 | { |
| | 142 | $data = array('name' => $data); |
| | 143 | } |
| | 144 | |
| | 145 | // Type and value are required attributes |
| | 146 | $data += array |
| | 147 | ( |
| | 148 | 'type' => 'text', |
| | 149 | 'value' => $value |
| | 150 | ); |
| | 151 | |
| | 152 | // Form elements should have the same id as name |
| | 153 | if ( ! isset($data['id'])) |
| | 154 | { |
| | 155 | $data['id'] = $data['name']; |
| | 156 | } |
| | 157 | |
| | 158 | // For safe form data |
| | 159 | $data['value'] = html::specialchars($data['value']); |
| | 160 | |
| | 161 | return '<input'.self::attributes($data).$extra.' />'; |
| | 162 | } |
| | 163 | |
| | 164 | /* |
| | 165 | * Method: password |
| | 166 | * Creates a HTML form password input tag. |
| | 167 | * |
| | 168 | * Parameters: |
| | 169 | * data - input name or an array of HTML attributes |
| | 170 | * value - input value, when using a name |
| | 171 | * extra - a string to be attached to the end of the attributes |
| | 172 | * |
| | 173 | * Returns: |
| | 174 | * An HTML form password input tag. |
| | 175 | */ |
| | 176 | public static function password($data = '', $value = '', $extra = '') |
| | 177 | { |
| | 178 | if ( ! is_array($data)) |
| | 179 | { |
| | 180 | $data = array('name' => $data); |
| | 181 | } |
| | 182 | |
| | 183 | $data['type'] = 'password'; |
| | 184 | |
| | 185 | return self::input($data, $value, $extra); |
| | 186 | } |
| | 187 | |
| | 188 | /* |
| | 189 | * Method: input |
| | 190 | * Creates an HTML form upload input tag. |
| | 191 | * |
| | 192 | * Parameters: |
| | 193 | * data - input name or an array of HTML attributes |
| | 194 | * value - input value, when using a name |
| | 195 | * extra - a string to be attached to the end of the attributes |
| | 196 | * |
| | 197 | * Returns: |
| | 198 | * An HTML form upload tag. |
| | 199 | */ |
| | 200 | public static function upload($data = '', $value = '', $extra = '') |
| | 201 | { |
| | 202 | if ( ! is_array($data)) |
| | 203 | { |
| | 204 | $data = array('name' => $data); |
| | 205 | } |
| | 206 | |
| | 207 | $data['type'] = 'file'; |
| | 208 | |
| | 209 | return self::input($data, $value, $extra); |
| | 210 | } |
| | 211 | |
| | 212 | /* |
| | 213 | * Method: textarea |
| | 214 | * Creates an HTML form textarea tag. |
| | 215 | * |
| | 216 | * Parameters: |
| | 217 | * data - textarea name or an array of HTML attributes |
| | 218 | * value - textarea value, when using a name |
| | 219 | * extra - a string to be attached to the end of the attributes |
| | 220 | * |
| | 221 | * Returns: |
| | 222 | * An HTML form textarea tag. |
| | 223 | */ |
| | 224 | public static function textarea($data = '', $value = '', $extra = '') |
| | 225 | { |
| | 226 | if ( ! is_array($data)) |
| | 227 | { |
| | 228 | $data = array('name' => $data); |
| | 229 | } |
| | 230 | |
| | 231 | // Use the value from $data if possible, or use $value |
| | 232 | $value = isset($data['value']) ? $data['value'] : $value; |
| | 233 | |
| | 234 | // Value is not part of the attributes |
| | 235 | unset($data['value']); |
| | 236 | |
| | 237 | return '<textarea'.self::attributes($data).'>'.html::specialchars($value).'</textarea>'; |
| | 238 | } |
| | 239 | |
| | 240 | /* |
| | 241 | * Method: dropdown |
| | 242 | * Creates an HTML form select tag, or "dropdown menu". |
| | 243 | * |
| | 244 | * Parameters: |
| | 245 | * data - select name or an array of HTML attributes |
| | 246 | * options - select options, when using a name |
| | 247 | * selected - option key that should be selected by default |
| | 248 | * extra - a string to be attached to the end of the attributes |
| | 249 | * |
| | 250 | * Returns: |
| | 251 | * An HTML form select tag, with options. |
| | 252 | */ |
| | 253 | public static function dropdown($data = '', $options = array(), $selected = '', $extra = '') |
| | 254 | { |
| | 255 | if ( ! is_array($data)) |
| | 256 | { |
| | 257 | $data = array('name' => $data); |
| | 258 | } |
| | 259 | |
| | 260 | $input = '<select '.self::attributes($data).$extra.'>'."\n"; |
| | 261 | foreach ($options as $key => $val) |
| | 262 | { |
| | 263 | $sel = ($selected == $key) ? ' selected="selected"' : ''; |
| | 264 | |
| | 265 | $input .= '<option value="'.$key.'"'.$sel.'>'.$val.'</option>'."\n"; |
| | 266 | } |
| | 267 | $input .= '</select>'; |
| | 268 | |
| | 269 | return $input; |
| | 270 | } |
| | 271 | |
| | 272 | /* |
| | 273 | * Method: checkbox |
| | 274 | * Creates an HTML form checkbox input tag. |
| | 275 | * |
| | 276 | * Parameters: |
| | 277 | * data - input name or an array of HTML attributes |
| | 278 | * value - input value, when using a name |
| | 279 | * checked - make the checkbox checked by default |
| | 280 | * extra - a string to be attached to the end of the attributes |
| | 281 | * |
| | 282 | * Returns: |
| | 283 | * An HTML form checkbox tag. |
| | 284 | */ |
| | 285 | public static function checkbox($data = '', $value = '', $checked = FALSE, $extra = '') |
| | 286 | { |
| | 287 | if ( ! is_array($data)) |
| | 288 | { |
| | 289 | $data = array('name' => $data); |
| | 290 | } |
| | 291 | |
| | 292 | $data['type'] = 'checkbox'; |
| | 293 | |
| | 294 | if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE)) |
| | 295 | { |
| | 296 | $data['checked'] = 'checked'; |
| | 297 | } |
| | 298 | else |
| | 299 | { |
| | 300 | unset($data['checked']); |
| | 301 | } |
| | 302 | |
| | 303 | return self::input($data, $value, $extra); |
| | 304 | } |
| | 305 | |
| | 306 | /* |
| | 307 | * Method: radio |
| | 308 | * Creates an HTML form radio input tag. |
| | 309 | * |
| | 310 | * Parameters: |
| | 311 | * data - input name or an array of HTML attributes |
| | 312 | * value - input value, when using a name |
| | 313 | * checked - make the radio selected by default |
| | 314 | * extra - a string to be attached to the end of the attributes |
| | 315 | * |
| | 316 | * Returns: |
| | 317 | * An HTML form radio tag. |
| | 318 | */ |
| | 319 | public static function radio($data = '', $value = '', $checked = FALSE, $extra = '') |
| | 320 | { |
| | 321 | if ( ! is_array($data)) |
| | 322 | { |
| | 323 | $data = array('name' => $data); |
| | 324 | } |
| | 325 | |
| | 326 | $data['type'] = 'radio'; |
| | 327 | |
| | 328 | if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE)) |
| | 329 | { |
| | 330 | $data['checked'] = 'checked'; |
| | 331 | } |
| | 332 | else |
| | 333 | { |
| | 334 | unset($data['checked']); |
| | 335 | } |
| | 336 | |
| | 337 | return self::input($data, $value, $extra); |
| | 338 | } |
| | 339 | |
| | 340 | /* |
| | 341 | * Method: submit |
| | 342 | * Creates an HTML form submit input tag. |
| | 343 | * |
| | 344 | * Parameters: |
| | 345 | * data - input name or an array of HTML attributes |
| | 346 | * value - input value, when using a name |
| | 347 | * extra - a string to be attached to the end of the attributes |
| | 348 | * |
| | 349 | * Returns: |
| | 350 | * An HTML form submit tag. |
| | 351 | */ |
| | 352 | public static function submit($data = '', $value = '', $extra = '') |
| | 353 | { |
| | 354 | if ( ! is_array($data)) |
| | 355 | { |
| | 356 | $data = array('name' => $data); |
| | 357 | } |
| | 358 | |
| | 359 | $data['type'] = 'submit'; |
| | 360 | |
| | 361 | return self::input($data, $value, $extra); |
| | 362 | } |
| | 363 | |
| | 364 | /* |
| | 365 | * Method: button |
| | 366 | * Creates an HTML form button input tag. |
| | 367 | * |
| | 368 | * Parameters: |
| | 369 | * data - input name or an array of HTML attributes |
| | 370 | * value - input value, when using a name |
| | 371 | * extra - a string to be attached to the end of the attributes |
| | 372 | * |
| | 373 | * Returns: |
| | 374 | * An HTML form button tag. |
| | 375 | */ |
| | 376 | public static function button($data = '', $value = '', $extra = '') |
| | 377 | { |
| | 378 | if ( ! is_array($data)) |
| | 379 | { |
| | 380 | $data = array('name' => $data); |
| | 381 | } |
| | 382 | |
| | 383 | $data += array |
| | 384 | ( |
| | 385 | 'type' => 'button' |
| | 386 | ); |
| | 387 | |
| | 388 | if (isset($data['value'])) |
| | 389 | { |
| | 390 | $value = $data['value']; |
| | 391 | unset($data['value']); |
| | 392 | } |
| | 393 | |
| | 394 | return '<button'.self::attributes($data).$extra.'>'.html::specialchars($value).'</button>'; |
| | 395 | } |
| | 396 | |
| | 397 | /* |
| | 398 | * Method: close |
| | 399 | * Closes an open form tag. |
| | 400 | * |
| | 401 | * Parameters: |
| | 402 | * extra - string to be attached after the closing tag |
| | 403 | * |
| | 404 | * Returns: |
| | 405 | * A closing HTML form tag. |
| | 406 | */ |
| | 407 | public static function close($extra = '') |
| | 408 | { |
| | 409 | return '</form>'."\n".$extra; |
| | 410 | } |
| | 411 | |
| | 412 | /* |
| | 413 | * Method: label |
| | 414 | * Creates an HTML form label tag. |
| | 415 | * |
| | 416 | * Parameters: |
| | 417 | * data - label "for" name or an array of HTML attributes |
| | 418 | * text - label text or HTML |
| | 419 | * extra - a string to be attached to the end of the attributes |
| | 420 | * |
| | 421 | * Returns: |
| | 422 | * An HTML form submit tag. |
| | 423 | */ |
| | 424 | public static function label($data = '', $text = '', $extra = '') |
| | 425 | { |
| | 426 | if ( ! is_array($data)) |
| | 427 | { |
| | 428 | $data = array |
| | 429 | ( |
| | 430 | 'for' => $data |
| | 431 | ); |
| | 432 | } |
| | 433 | |
| | 434 | return '<label'.self::attributes($data).$extra.'>'.$text.'</label>'; |
| | 435 | } |
| | 436 | |
| | 437 | /* |
| | 438 | * Method: attributes |
| | 439 | * Sorts a key/value array of HTML attributes, putting form attributes first, |
| | 440 | * and returns an attribute string. |
| | 441 | * |
| | 442 | * Parameters: |
| | 443 | * attr - HTML attributes array |
| | 444 | * |
| | 445 | * Returns: |
| | 446 | * An HTML attribute string. |
| | 447 | */ |