diff --git a/system/form.php b/system/form.php index d4264636..5783a79b 100644 --- a/system/form.php +++ b/system/form.php @@ -19,23 +19,15 @@ class Form { */ public static function open($action = null, $method = 'POST', $attributes = array()) { - if (is_null($action)) - { - $action = Request::uri(); - } - - $attributes['action'] = HTML::entities(URL::to($action)); + $attributes['action'] = HTML::entities(URL::to((is_null($action)) ? Request::uri() : $action)); // ------------------------------------------------------- // If the request method is PUT or DELETE, we'll default - // the request method to POST. + // the request method to POST since the reqeust method + // is being spoofed by the form. // ------------------------------------------------------- - $attributes['method'] = ($method == 'GET' or $method == 'POST') ? $method : 'POST'; + $attributes['method'] = ($method == 'PUT' or $method == 'DELETE') ? 'POST' : $method; - // ------------------------------------------------------- - // Set the default character set if it hasn't already been - // set in the attributes. - // ------------------------------------------------------- if ( ! array_key_exists('accept-charset', $attributes)) { $attributes['accept-charset'] = Config::get('application.encoding'); @@ -51,7 +43,7 @@ public static function open($action = null, $method = 'POST', $attributes = arra // ------------------------------------------------------- if ($method == 'PUT' or $method == 'DELETE') { - $html .= PHP_EOL.static::hidden('REQUEST_METHOD', $method); + $html .= PHP_EOL.static::input('hidden', 'REQUEST_METHOD', $method); } return $html.PHP_EOL; @@ -65,20 +57,11 @@ public static function open($action = null, $method = 'POST', $attributes = arra * @param array $attributes * @return string */ - public static function open_multipart($action = null, $method = 'POST', $attributes = array()) + public static function open_for_files($action = null, $method = 'POST', $attributes = array()) { $attributes['enctype'] = 'multipart/form-data'; - return static::open($action, $method, $attributes); - } - /** - * Close a HTML form. - * - * @return string - */ - public static function close() - { - return ''.PHP_EOL; + return static::open($action, $method, $attributes); } /** @@ -88,7 +71,7 @@ public static function close() */ public static function token() { - return static::hidden('csrf_token', static::raw_token()); + return static::input('hidden', 'csrf_token', static::raw_token()); } /** @@ -121,20 +104,21 @@ public static function raw_token() public static function label($name, $value, $attributes = array()) { static::$labels[] = $name; + return ''.PHP_EOL; } /** - * Create a HTML text input element. + * Create a HTML input element. * * @param string $name - * @param string $value + * @param mixed $value * @param array $attributes * @return string */ - public static function text($name, $value = null, $attributes = array()) + public static function input($type, $name, $value = null, $attributes = array()) { - return static::input('text', $name, $value, $attributes); + return ' $type, 'name' => $name, 'value' => $value, 'id' => static::id($name, $attributes)))).'>'.PHP_EOL; } /** @@ -143,25 +127,12 @@ public static function text($name, $value = null, $attributes = array()) * @param string $name * @param array $attributes * @return string - */ + */ public static function password($name, $attributes = array()) { return static::input('password', $name, null, $attributes); } - /** - * Create a HTML hidden input element. - * - * @param string $name - * @param string $value - * @param array $attributes - * @return string - */ - public static function hidden($name, $value = null, $attributes = array()) - { - return static::input('hidden', $name, $value, $attributes); - } - /** * Create a HTML email input element. * @@ -169,23 +140,23 @@ public static function hidden($name, $value = null, $attributes = array()) * @param string $value * @param array $attributes * @return string - */ + */ public static function email($name, $value = null, $attributes = array()) { return static::input('email', $name, $value, $attributes); } /** - * Create a HTML URL input element. + * Create a HTML telephone input element. * * @param string $name * @param string $value * @param array $attributes * @return string - */ - public static function url($name, $value = null, $attributes = array()) + */ + public static function telephone($name, $value = null, $attributes = array()) { - return static::input('url', $name, $value, $attributes); + return static::input('tel', $name, $value, $attributes); } /** @@ -195,12 +166,25 @@ public static function url($name, $value = null, $attributes = array()) * @param string $value * @param array $attributes * @return string - */ + */ public static function search($name, $value = null, $attributes = array()) { return static::input('search', $name, $value, $attributes); } + /** + * Create a HTML URL input element. + * + * @param string $name + * @param string $value + * @param array $attributes + * @return string + */ + public static function url($name, $value = null, $attributes = array()) + { + return static::input('url', $name, $value, $attributes); + } + /** * Create a HTML color input element. * @@ -208,12 +192,25 @@ public static function search($name, $value = null, $attributes = array()) * @param string $value * @param array $attributes * @return string - */ + */ public static function color($name, $value = null, $attributes = array()) { return static::input('color', $name, $value, $attributes); } + /** + * Create a HTML date input element. + * + * @param string $name + * @param string $value + * @param array $attributes + * @return string + */ + public static function date($name, $value = null, $attributes = array()) + { + return static::input('date', $name, $value, $attributes); + } + /** * Create a HTML number input element. * @@ -221,7 +218,7 @@ public static function color($name, $value = null, $attributes = array()) * @param string $value * @param array $attributes * @return string - */ + */ public static function number($name, $value = null, $attributes = array()) { return static::input('number', $name, $value, $attributes); @@ -234,77 +231,12 @@ public static function number($name, $value = null, $attributes = array()) * @param string $value * @param array $attributes * @return string - */ + */ public static function range($name, $value = null, $attributes = array()) { return static::input('range', $name, $value, $attributes); } - /** - * Create a HTML telephone input element. - * - * @param string $name - * @param string $value - * @param array $attributes - * @return string - */ - public static function tel($name, $value = null, $attributes = array()) - { - return static::input('tel', $name, $value, $attributes); - } - - /** - * Create a HTML date input element. - * - * @param string $name - * @param string $value - * @param array $attributes - * @return string - */ - public static function date($name, $value = null, $attributes = array()) - { - return static::input('date', $name, $value, $attributes); - } - - /** - * Create a HTML time input element. - * - * @param string $name - * @param string $value - * @param array $attributes - * @return string - */ - public static function time($name, $value = null, $attributes = array()) - { - return static::input('time', $name, $value, $attributes); - } - - /** - * Create a HTML datetime input element. - * - * @param string $name - * @param string $value - * @param array $attributes - * @return string - */ - public static function datetime($name, $value = null, $attributes = array()) - { - return static::input('datetime', $name, $value, $attributes); - } - - /** - * Create a HTML local datetime input element. - * - * @param string $name - * @param string $value - * @param array $attributes - * @return string - */ - public static function datetime_local($name, $value = null, $attributes = array()) - { - return static::input('datetime-local', $name, $value, $attributes); - } - /** * Create a HTML file input element. * @@ -318,52 +250,51 @@ public static function file($name, $attributes = array()) } /** - * Create a HTML submit input element. - * - * @param string $value - * @param array $attributes - * @return string - */ - public static function submit($value, $attributes = array()) - { - return static::input('submit', null, $value, $attributes); - } - - /** - * Create a HTML reset input element. - * - * @param string $value - * @param array $attributes - * @return string - */ - public static function reset($value, $attributes = array()) - { - return static::input('reset', null, $value, $attributes); - } - - /** - * Create a HTML image input element. - * - * @param string $value - * @param array $attributes - * @return string - */ - public static function image($value, $attributes = array()) - { - return static::input('image', null, $value, $attributes); - } - - /** - * Create a HTML button element. + * Create a HTML textarea element. * * @param string $name * @param string $value * @param array $attributes * @return string */ - public static function button($value, $attributes = array()) + public static function textarea($name, $value = '', $attributes = array()) { - return ''.HTML::entities($value).''.PHP_EOL; + $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name)); + + if ( ! isset($attributes['rows'])) + { + $attributes['rows'] = 10; + } + + if ( ! isset($attributes['cols'])) + { + $attributes['cols'] = 50; + } + + return ''.HTML::entities($value).''.PHP_EOL; + } + + /** + * Create a HTML select element. + * + * @param string $name + * @param array $options + * @param string $selected + * @param array $attributes + * @return string + */ + public static function select($name, $options = array(), $selected = null, $attributes = array()) + { + $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name)); + + $html = array(); + + foreach ($options as $value => $display) + { + $html[] = ' HTML::entities($value), 'selected' => ($value == $selected) ? 'selected' : null)).'>'.HTML::entities($display).''; + } + + return ''.implode('', $html).''.PHP_EOL; } /** @@ -406,92 +337,60 @@ public static function radio($name, $value = null, $checked = false, $attributes */ private static function checkable($type, $name, $value, $checked, $attributes) { - if ($checked === true) - { - $attributes['checked'] = 'checked'; - } - - $attributes['id'] = static::id($name, $attributes); + $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'checked' => ($checked) ? 'checked' : null)); return static::input($type, $name, $value, $attributes); } /** - * Create a HTML textarea element. + * Create a HTML submit input element. + * + * @param string $value + * @param array $attributes + * @return string + */ + public static function submit($value, $attributes = array()) + { + return static::input('submit', null, $value, $attributes); + } + + /** + * Create a HTML reset input element. + * + * @param string $value + * @param array $attributes + * @return string + */ + public static function reset($value, $attributes = array()) + { + return static::input('reset', null, $value, $attributes); + } + + /** + * Create a HTML image input element. + * + * @param string $url + * @param array $attributes + * @return string + */ + public static function image($url, $name = null, $attributes = array()) + { + $attributes['src'] = URL::to_asset($url); + + return static::input('image', $name, null, $attributes); + } + + /** + * Create a HTML button element. * * @param string $name * @param string $value * @param array $attributes * @return string */ - public static function textarea($name, $value = '', $attributes = array()) + public static function button($value, $attributes = array()) { - $attributes['name'] = $name; - $attributes['id'] = static::id($name, $attributes); - - // ------------------------------------------------------- - // Set the default number of rows. - // ------------------------------------------------------- - if ( ! isset($attributes['rows'])) - { - $attributes['rows'] = 10; - } - - // ------------------------------------------------------- - // Set the default number of columns. - // ------------------------------------------------------- - if ( ! isset($attributes['cols'])) - { - $attributes['cols'] = 50; - } - - return ''.HTML::entities($value).''.PHP_EOL; - } - - /** - * Create a HTML select element. - * - * @param string $name - * @param array $options - * @param string $selected - * @param array $attributes - * @return string - */ - public static function select($name, $options = array(), $selected = null, $attributes = array()) - { - $attributes['name'] = $name; - $attributes['id'] = static::id($name, $attributes); - - $html_options = array(); - - // ------------------------------------------------------- - // Build the HTML options for the drop-down. - // ------------------------------------------------------- - foreach ($options as $value => $display) - { - $option_attributes = array('value' => HTML::entities($value), 'selected' => ($value == $selected) ? 'selected' : null); - $html_options[] = ''.HTML::entities($display).''; - } - - return ''.implode('', $html_options).''.PHP_EOL; - } - - /** - * Create a HTML input element. - * - * @param string $name - * @param mixed $value - * @param array $attributes - * @return string - */ - public static function input($type, $name, $value = null, $attributes = array()) - { - $attributes['type'] = $type; - $attributes['name'] = $name; - $attributes['value'] = $value; - $attributes['id'] = static::id($name, $attributes); - - return ''.PHP_EOL; + return ''.HTML::entities($value).''.PHP_EOL; } /** @@ -505,7 +404,7 @@ private static function id($name, $attributes) { // ------------------------------------------------------- // If an ID attribute was already explicitly specified - // for the element, just use that. + // for the element in its attributes, use that ID. // ------------------------------------------------------- if (array_key_exists('id', $attributes)) {