123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- <?php namespace Laravel;
- class Form {
-
- public static $labels = array();
-
- public static $macros = array();
-
- public static function macro($name, $macro)
- {
- static::$macros[$name] = $macro;
- }
-
- public static function open($action = null, $method = 'POST', $attributes = array(), $https = null)
- {
- $method = strtoupper($method);
- $attributes['method'] = static::method($method);
- $attributes['action'] = static::action($action, $https);
-
-
-
- if ( ! array_key_exists('accept-charset', $attributes))
- {
- $attributes['accept-charset'] = Config::get('application.encoding');
- }
- $append = '';
-
-
-
- if ($method == 'PUT' or $method == 'DELETE')
- {
- $append = static::hidden(Request::spoofer, $method);
- }
- return '<form'.HTML::attributes($attributes).'>'.$append;
- }
-
- protected static function method($method)
- {
- return ($method !== 'GET') ? 'POST' : $method;
- }
-
- protected static function action($action, $https)
- {
- $uri = (is_null($action)) ? URI::current() : $action;
- return HTML::entities(URL::to($uri, $https));
- }
-
- public static function open_secure($action = null, $method = 'POST', $attributes = array())
- {
- return static::open($action, $method, $attributes, true);
- }
-
- public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = null)
- {
- $attributes['enctype'] = 'multipart/form-data';
- return static::open($action, $method, $attributes, $https);
- }
-
- public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
- {
- return static::open_for_files($action, $method, $attributes, true);
- }
-
- public static function close()
- {
- return '</form>';
- }
-
- public static function token()
- {
- return static::input('hidden', Session::csrf_token, Session::token());
- }
-
- public static function label($name, $value, $attributes = array(), $escape_html = true)
- {
- static::$labels[] = $name;
- $attributes = HTML::attributes($attributes);
- if ($escape_html) {
- $value = HTML::entities($value);
- }
- return '<label for="'.$name.'"'.$attributes.'>'.$value.'</label>';
- }
-
- public static function input($type, $name, $value = null, $attributes = array())
- {
- $name = (isset($attributes['name'])) ? $attributes['name'] : $name;
- $id = static::id($name, $attributes);
- $attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));
- return '<input'.HTML::attributes($attributes).'>';
- }
-
- public static function text($name, $value = null, $attributes = array())
- {
- return static::input('text', $name, $value, $attributes);
- }
-
- public static function password($name, $attributes = array())
- {
- return static::input('password', $name, null, $attributes);
- }
-
- public static function hidden($name, $value = null, $attributes = array())
- {
- return static::input('hidden', $name, $value, $attributes);
- }
-
- public static function search($name, $value = null, $attributes = array())
- {
- return static::input('search', $name, $value, $attributes);
- }
-
- public static function email($name, $value = null, $attributes = array())
- {
- return static::input('email', $name, $value, $attributes);
- }
-
- public static function telephone($name, $value = null, $attributes = array())
- {
- return static::input('tel', $name, $value, $attributes);
- }
-
- public static function url($name, $value = null, $attributes = array())
- {
- return static::input('url', $name, $value, $attributes);
- }
-
- public static function number($name, $value = null, $attributes = array())
- {
- return static::input('number', $name, $value, $attributes);
- }
-
- public static function date($name, $value = null, $attributes = array())
- {
- return static::input('date', $name, $value, $attributes);
- }
-
- public static function file($name, $attributes = array())
- {
- return static::input('file', $name, null, $attributes);
- }
-
- public static function textarea($name, $value = '', $attributes = array())
- {
- $attributes['name'] = $name;
- $attributes['id'] = static::id($name, $attributes);
- if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
- if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
- return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>';
- }
-
- public static function select($name, $options = array(), $selected = null, $attributes = array())
- {
- $attributes['id'] = static::id($name, $attributes);
- $attributes['name'] = $name;
- $html = array();
- foreach ($options as $value => $display)
- {
- if (is_array($display))
- {
- $html[] = static::optgroup($display, $value, $selected);
- }
- else
- {
- $html[] = static::option($value, $display, $selected);
- }
- }
- return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>';
- }
-
- protected static function optgroup($options, $label, $selected)
- {
- $html = array();
- foreach ($options as $value => $display)
- {
- $html[] = static::option($value, $display, $selected);
- }
- return '<optgroup label="'.HTML::entities($label).'">'.implode('', $html).'</optgroup>';
- }
-
- protected static function option($value, $display, $selected)
- {
- if (is_array($selected))
- {
- $selected = (in_array($value, $selected)) ? 'selected' : null;
- }
- else
- {
- $selected = ((string) $value == (string) $selected) ? 'selected' : null;
- }
- $attributes = array('value' => HTML::entities($value), 'selected' => $selected);
- return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
- }
-
- public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
- {
- return static::checkable('checkbox', $name, $value, $checked, $attributes);
- }
-
- public static function radio($name, $value = null, $checked = false, $attributes = array())
- {
- if (is_null($value)) $value = $name;
- return static::checkable('radio', $name, $value, $checked, $attributes);
- }
-
- protected static function checkable($type, $name, $value, $checked, $attributes)
- {
- if ($checked) $attributes['checked'] = 'checked';
- $attributes['id'] = static::id($name, $attributes);
- return static::input($type, $name, $value, $attributes);
- }
-
- public static function submit($value = null, $attributes = array())
- {
- return static::input('submit', null, $value, $attributes);
- }
-
- public static function reset($value = null, $attributes = array())
- {
- return static::input('reset', null, $value, $attributes);
- }
-
- public static function image($url, $name = null, $attributes = array())
- {
- $attributes['src'] = URL::to_asset($url);
- return static::input('image', $name, null, $attributes);
- }
-
- public static function button($value = null, $attributes = array())
- {
- return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>';
- }
-
- protected static function id($name, $attributes)
- {
-
-
-
- if (array_key_exists('id', $attributes))
- {
- return $attributes['id'];
- }
- if (in_array($name, static::$labels))
- {
- return $name;
- }
- }
-
- public static function __callStatic($method, $parameters)
- {
- if (isset(static::$macros[$method]))
- {
- return call_user_func_array(static::$macros[$method], $parameters);
- }
- throw new \Exception("Method [$method] does not exist.");
- }
- }
|