1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
5 * $Id: form.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
15 * Generates an opening HTML form tag.
17 * @param string form action attribute
18 * @param array extra attributes
19 * @param array hidden fields to be created immediately after the form tag
22 public static function open($action = NULL, $attr = array(), $hidden = NULL)
24 // Make sure that the method is always set
25 empty($attr['method']) and $attr['method'] = 'post';
27 if ($attr['method'] !== 'post' AND $attr['method'] !== 'get')
29 // If the method is invalid, use post
30 $attr['method'] = 'post';
35 // Use the current URL as the default action
36 $action = url
::site(Router
::$complete_uri);
38 elseif (strpos($action, '://') === FALSE)
40 // Make the action URI into a URL
41 $action = url
::site($action);
45 $attr['action'] = $action;
48 $form = '<form'.form
::attributes($attr).'>'."\n";
50 // Add hidden fields immediate after opening tag
51 empty($hidden) or $form .= form
::hidden($hidden);
57 * Generates an opening HTML form tag that can be used for uploading files.
59 * @param string form action attribute
60 * @param array extra attributes
61 * @param array hidden fields to be created immediately after the form tag
64 public static function open_multipart($action = NULL, $attr = array(), $hidden = array())
66 // Set multi-part form type
67 $attr['enctype'] = 'multipart/form-data';
69 return form
::open($action, $attr, $hidden);
73 * Generates a fieldset opening tag.
75 * @param array html attributes
76 * @param string a string to be attached to the end of the attributes
79 public static function open_fieldset($data = NULL, $extra = '')
81 return '<fieldset'.html
::attributes((array) $data).' '.$extra.'>'."\n";
85 * Generates a fieldset closing tag.
89 public static function close_fieldset()
91 return '</fieldset>'."\n";
95 * Generates a legend tag for use with a fieldset.
97 * @param string legend text
98 * @param array HTML attributes
99 * @param string a string to be attached to the end of the attributes
102 public static function legend($text = '', $data = NULL, $extra = '')
104 return '<legend'.form
::attributes((array) $data).' '.$extra.'>'.$text.'</legend>'."\n";
108 * Generates hidden form fields.
109 * You can pass a simple key/value string or an associative array with multiple values.
111 * @param string|array input name (string) or key/value pairs (array)
112 * @param string input value, if using an input name
115 public static function hidden($data, $value = '')
117 if ( ! is_array($data))
126 foreach ($data as $name => $value)
135 $input .= form
::input($attr)."\n";
142 * Creates an HTML form input tag. Defaults to a text type.
144 * @param string|array input name or an array of HTML attributes
145 * @param string input value, when using a name
146 * @param string a string to be attached to the end of the attributes
147 * @param boolean encode existing entities
150 public static function input($data, $value = '', $extra = '', $double_encode = TRUE )
152 if ( ! is_array($data))
154 $data = array('name' => $data);
157 // Type and value are required attributes
164 return '<input'.form
::attributes($data).' '.$extra.' />';
168 * Creates a HTML form password input tag.
170 * @param string|array input name or an array of HTML attributes
171 * @param string input value, when using a name
172 * @param string a string to be attached to the end of the attributes
175 public static function password($data, $value = '', $extra = '')
177 if ( ! is_array($data))
179 $data = array('name' => $data);
182 $data['type'] = 'password';
184 return form
::input($data, $value, $extra);
188 * Creates an HTML form upload input tag.
190 * @param string|array input name or an array of HTML attributes
191 * @param string input value, when using a name
192 * @param string a string to be attached to the end of the attributes
195 public static function upload($data, $value = '', $extra = '')
197 if ( ! is_array($data))
199 $data = array('name' => $data);
202 $data['type'] = 'file';
204 return form
::input($data, $value, $extra);
208 * Creates an HTML form textarea tag.
210 * @param string|array input name or an array of HTML attributes
211 * @param string input value, when using a name
212 * @param string a string to be attached to the end of the attributes
213 * @param boolean encode existing entities
216 public static function textarea($data, $value = '', $extra = '', $double_encode = TRUE )
218 if ( ! is_array($data))
220 $data = array('name' => $data);
223 // Use the value from $data if possible, or use $value
224 $value = isset($data['value']) ?
$data['value'] : $value;
226 // Value is not part of the attributes
227 unset($data['value']);
229 return '<textarea'.form
::attributes($data, 'textarea').' '.$extra.'>'.html
::specialchars($value, $double_encode).'</textarea>';
233 * Creates an HTML form select tag, or "dropdown menu".
235 * @param string|array input name or an array of HTML attributes
236 * @param array select options, when using a name
237 * @param string option key that should be selected by default
238 * @param string a string to be attached to the end of the attributes
241 public static function dropdown($data, $options = NULL, $selected = NULL, $extra = '')
244 if ( ! is_array($data))
246 $data = array('name' => $data);
250 if (isset($data['options']))
253 $options = $data['options'];
254 unset($data['options']);
257 if (isset($data['selected']))
260 $selected = $data['selected'];
261 unset($data['selected']);
265 $input = '<select'.form
::attributes($data, 'select').' '.$extra.'>'."\n";
266 foreach ((array) $options as $key => $val)
268 // Key should always be a string
269 $key = (string) $key;
271 // Selected must always be a string
272 $selected = (string) $selected;
276 $input .= '<optgroup label="'.$key.'">'."\n";
277 foreach ($val as $inner_key => $inner_val)
279 // Inner key should always be a string
280 $inner_key = (string) $inner_key;
282 if (is_array($selected))
284 $sel = in_array($inner_key, $selected, TRUE);
288 $sel = ($selected === $inner_key);
291 $sel = ($sel === TRUE) ?
' selected="selected"' : '';
292 $input .= '<option value="'.$inner_key.'"'.$sel.'>'.$inner_val.'</option>'."\n";
294 $input .= '</optgroup>'."\n";
298 $sel = ($selected === $key) ?
' selected="selected"' : '';
299 $input .= '<option value="'.$key.'"'.$sel.'>'.$val.'</option>'."\n";
302 $input .= '</select>';
308 * Creates an HTML form checkbox input tag.
310 * @param string|array input name or an array of HTML attributes
311 * @param string input value, when using a name
312 * @param boolean make the checkbox checked by default
313 * @param string a string to be attached to the end of the attributes
316 public static function checkbox($data, $value = '', $checked = FALSE, $extra = '')
318 if ( ! is_array($data))
320 $data = array('name' => $data);
323 $data['type'] = 'checkbox';
325 if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
327 $data['checked'] = 'checked';
331 unset($data['checked']);
334 return form
::input($data, $value, $extra);
338 * Creates an HTML form radio input tag.
340 * @param string|array input name or an array of HTML attributes
341 * @param string input value, when using a name
342 * @param boolean make the radio selected by default
343 * @param string a string to be attached to the end of the attributes
346 public static function radio($data = '', $value = '', $checked = FALSE, $extra = '')
348 if ( ! is_array($data))
350 $data = array('name' => $data);
353 $data['type'] = 'radio';
355 if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
357 $data['checked'] = 'checked';
361 unset($data['checked']);
364 return form
::input($data, $value, $extra);
368 * Creates an HTML form submit input tag.
370 * @param string|array input name or an array of HTML attributes
371 * @param string input value, when using a name
372 * @param string a string to be attached to the end of the attributes
375 public static function submit($data = '', $value = '', $extra = '')
377 if ( ! is_array($data))
379 $data = array('name' => $data);
382 if (empty($data['name']))
384 // Remove the name if it is empty
385 unset($data['name']);
388 $data['type'] = 'submit';
390 return form
::input($data, $value, $extra);
394 * Creates an HTML form button input tag.
396 * @param string|array input name or an array of HTML attributes
397 * @param string input value, when using a name
398 * @param string a string to be attached to the end of the attributes
401 public static function button($data = '', $value = '', $extra = '')
403 if ( ! is_array($data))
405 $data = array('name' => $data);
408 if (empty($data['name']))
410 // Remove the name if it is empty
411 unset($data['name']);
414 if (isset($data['value']) AND empty($value))
416 $value = arr
::remove('value', $data);
419 return '<button'.form
::attributes($data, 'button').' '.$extra.'>'.$value.'</button>';
423 * Closes an open form tag.
425 * @param string string to be attached after the closing tag
428 public static function close($extra = '')
430 return '</form>'."\n".$extra;
434 * Creates an HTML form label tag.
436 * @param string|array label "for" name or an array of HTML attributes
437 * @param string label text or HTML
438 * @param string a string to be attached to the end of the attributes
441 public static function label($data = '', $text = NULL, $extra = '')
443 if ( ! is_array($data))
445 if (is_string($data))
447 // Specify the input this label is for
448 $data = array('for' => $data);
452 // No input specified
457 if ($text === NULL AND isset($data['for']))
459 // Make the text the human-readable input name
460 $text = ucwords(inflector
::humanize($data['for']));
463 return '<label'.form
::attributes($data).' '.$extra.'>'.$text.'</label>';
467 * Sorts, possibly shuffles, but most likely keeps the order of a
468 * key/value array of HTML attributes, and returns an attribute string.
470 * Beacause this is Kohana, this also does some magical stuff... In this
471 * case, it sets id attribute to the name attribute, if id doesn't exist.
473 * @param array HTML attributes array
476 public static function attributes($attr, $type = NULL)
481 if (isset($attr['name']) AND empty($attr['id']) AND strpos($attr['name'], '[') === FALSE)
483 if ($type === NULL AND ! empty($attr['type']))
485 // Set the type by the attributes
486 $type = $attr['type'];
500 // Only specific types of inputs use name to id matching
501 $attr['id'] = $attr['name'];
506 // Combine the sorted and unsorted attributes and create an HTML string
507 return html
::attributes($attr);