Files
spa-api/laravel/form.php

592 lines
14 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class Form {
2011-06-14 11:43:18 -03:00
/**
2011-08-23 21:04:40 -05:00
* All of the label names that have been created.
*
2011-06-14 11:43:18 -03:00
* @var array
*/
2012-03-21 10:48:49 -05:00
public static $labels = array();
/**
2012-03-21 10:48:49 -05:00
* The registered custom macros.
*
* @var array
*/
2012-03-21 10:48:49 -05:00
public static $macros = array();
2012-03-21 10:48:49 -05:00
/**
* Registers a custom macro.
*
* @param string $name
* @param Closure $input
* @return void
*/
public static function macro($name, $macro)
{
2012-03-21 10:48:49 -05:00
static::$macros[$name] = $macro;
}
2011-06-14 11:43:18 -03:00
2011-06-08 23:45:08 -05:00
/**
* Open a HTML form.
*
2011-09-21 22:57:32 -05:00
* <code>
* // Open a "POST" form to the current request URI
* echo Form::open();
*
* // Open a "POST" form to a given URI
* echo Form::open('user/profile');
*
* // Open a "PUT" form to a given URI
* echo Form::open('user/profile', 'put');
*
* // Open a form that has HTML attributes
* echo Form::open('user/profile', 'post', array('class' => 'profile'));
* </code>
2011-08-23 21:04:40 -05:00
*
* @param string $action
* @param string $method
* @param array $attributes
* @param bool $https
2011-06-08 23:45:08 -05:00
* @return string
2011-08-22 23:06:53 -05:00
*/
public static function open($action = null, $method = 'POST', $attributes = array(), $https = false)
2011-06-08 23:45:08 -05:00
{
2011-12-01 23:34:31 -06:00
$method = strtoupper($method);
2011-10-15 22:38:43 -05:00
$attributes['method'] = static::method($method);
$attributes['action'] = static::action($action, $https);
2011-06-08 23:45:08 -05:00
2012-01-16 13:59:24 -06:00
// If a character encoding has not been specified in the attributes, we will
// use the default encoding as specified in the application configuration
// file for the "accept-charset" attribute.
2011-06-08 23:45:08 -05:00
if ( ! array_key_exists('accept-charset', $attributes))
{
$attributes['accept-charset'] = Config::get('application.encoding');
2011-06-08 23:45:08 -05:00
}
2011-11-09 21:55:21 -06:00
$append = '';
2012-01-16 13:59:24 -06:00
// Since PUT and DELETE methods are not actually supported by HTML forms,
// we'll create a hidden input element that contains the request method
2012-03-21 10:48:49 -05:00
// and set the actual request method variable to POST.
2011-11-09 21:55:21 -06:00
if ($method == 'PUT' or $method == 'DELETE')
{
$append = static::hidden(Request::spoofer, $method);
}
2011-06-08 23:45:08 -05:00
return '<form'.HTML::attributes($attributes).'>'.$append;
2011-08-23 21:04:40 -05:00
}
/**
* Determine the appropriate request method to use for a form.
*
* @param string $method
* @return string
*/
protected static function method($method)
2011-08-23 21:04:40 -05:00
{
2011-12-01 23:34:31 -06:00
return ($method !== 'GET') ? 'POST' : $method;
2011-08-23 21:04:40 -05:00
}
2011-06-08 23:45:08 -05:00
2011-08-23 21:04:40 -05:00
/**
* Determine the appropriate action parameter to use for a form.
*
* If no action is specified, the current request URI will be used.
*
* @param string $action
* @param bool $https
2011-08-23 21:04:40 -05:00
* @return string
*/
protected static function action($action, $https)
2011-08-23 21:04:40 -05:00
{
2011-11-23 08:23:01 -06:00
$uri = (is_null($action)) ? URI::current() : $action;
return HTML::entities(URL::to($uri, $https));
2011-06-08 23:45:08 -05:00
}
/**
2011-08-23 21:04:40 -05:00
* Open a HTML form with a HTTPS action URI.
*
* @param string $action
* @param string $method
* @param array $attributes
* @return string
*/
public static function open_secure($action = null, $method = 'POST', $attributes = array())
{
return static::open($action, $method, $attributes, true);
}
/**
* Open a HTML form that accepts file uploads.
*
* @param string $action
* @param string $method
* @param array $attributes
* @param bool $https
* @return string
*/
public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = false)
{
$attributes['enctype'] = 'multipart/form-data';
return static::open($action, $method, $attributes, $https);
}
/**
2011-08-23 21:04:40 -05:00
* Open a HTML form that accepts file uploads with a HTTPS action URI.
*
* @param string $action
* @param string $method
* @param array $attributes
* @return string
*/
public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
{
return static::open_for_files($action, $method, $attributes, true);
2011-06-14 11:14:15 -07:00
}
2011-07-16 10:22:57 -05:00
/**
* Close a HTML form.
*
* @return string
*/
public static function close()
2011-07-16 10:22:57 -05:00
{
return '</form>';
}
2011-06-08 23:45:08 -05:00
/**
* Generate a hidden field containing the current CSRF token.
*
* @return string
*/
public static function token()
2011-06-08 23:45:08 -05:00
{
2012-01-16 13:59:24 -06:00
return static::input('hidden', Session::csrf_token, Session::token());
2011-08-31 00:07:45 -05:00
}
/**
* Create a HTML label element.
2011-08-23 21:04:40 -05:00
*
2011-09-21 22:57:32 -05:00
* <code>
* // Create a label for the "email" input element
* echo Form::label('email', 'E-Mail Address');
* </code>
*
2011-06-14 10:20:44 -03:00
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function label($name, $value, $attributes = array())
2011-06-14 10:20:44 -03:00
{
static::$labels[] = $name;
2011-07-05 06:57:42 -07:00
2011-11-09 21:55:21 -06:00
$attributes = HTML::attributes($attributes);
$value = HTML::entities($value);
return '<label for="'.$name.'"'.$attributes.'>'.$value.'</label>';
2011-06-14 10:20:44 -03:00
}
2011-06-08 23:45:08 -05:00
/**
2011-07-05 06:57:42 -07:00
* Create a HTML input element.
2011-06-08 23:45:08 -05:00
*
2011-09-21 22:57:32 -05:00
* <code>
* // Create a "text" input element named "email"
* echo Form::input('text', 'email');
*
* // Create an input element with a specified default value
* echo Form::input('text', 'email', 'example@gmail.com');
* </code>
*
2012-02-04 21:30:52 +00:00
* @param string $type
2011-06-08 23:45:08 -05:00
* @param string $name
2011-07-05 06:57:42 -07:00
* @param mixed $value
2011-06-08 23:45:08 -05:00
* @param array $attributes
* @return string
*/
public static function input($type, $name, $value = null, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-09-08 17:49:16 -05:00
$name = (isset($attributes['name'])) ? $attributes['name'] : $name;
$id = static::id($name, $attributes);
2011-08-08 08:59:51 -05:00
$attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));
return '<input'.HTML::attributes($attributes).'>';
2011-06-08 23:45:08 -05:00
}
2011-07-16 10:09:02 -05:00
/**
* Create a HTML text input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function text($name, $value = null, $attributes = array())
2011-07-16 10:09:02 -05:00
{
return static::input('text', $name, $value, $attributes);
2011-07-16 10:09:02 -05:00
}
2011-06-08 23:45:08 -05:00
/**
* Create a HTML password input element.
*
* @param string $name
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function password($name, $attributes = array())
2011-06-08 23:45:08 -05:00
{
return static::input('password', $name, null, $attributes);
2011-06-08 23:45:08 -05:00
}
2011-07-16 10:09:02 -05:00
/**
* 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())
2011-07-16 10:09:02 -05:00
{
return static::input('hidden', $name, $value, $attributes);
2011-07-16 10:09:02 -05:00
}
/**
* Create a HTML search input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function search($name, $value = null, $attributes = array())
{
return static::input('search', $name, $value, $attributes);
}
2011-07-02 08:43:12 -05:00
/**
2011-07-06 14:19:18 -07:00
* Create a HTML email input element.
2011-07-02 08:43:12 -05:00
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function email($name, $value = null, $attributes = array())
2011-07-02 08:43:12 -05:00
{
return static::input('email', $name, $value, $attributes);
2011-07-02 08:43:12 -05:00
}
/**
2011-07-06 14:19:18 -07:00
* Create a HTML telephone input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
2011-08-26 21:42:04 -05:00
*/
public static function telephone($name, $value = null, $attributes = array())
{
return static::input('tel', $name, $value, $attributes);
}
2011-07-02 08:43:12 -05:00
/**
2011-07-06 14:19:18 -07:00
* Create a HTML URL input element.
2011-07-02 08:43:12 -05:00
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function url($name, $value = null, $attributes = array())
2011-07-02 08:43:12 -05:00
{
return static::input('url', $name, $value, $attributes);
2011-07-02 08:43:12 -05:00
}
2011-07-02 08:51:14 -05:00
/**
2011-07-05 06:57:42 -07:00
* Create a HTML number input element.
2011-07-02 08:51:14 -05:00
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function number($name, $value = null, $attributes = array())
2011-07-02 08:51:14 -05:00
{
return static::input('number', $name, $value, $attributes);
2011-07-02 08:51:14 -05:00
}
2012-02-01 00:16:05 -06:00
/**
* 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);
}
2011-07-02 08:51:14 -05:00
2011-06-08 23:45:08 -05:00
/**
* Create a HTML file input element.
*
* @param string $name
* @param array $attributes
* @return string
*/
public static function file($name, $attributes = array())
2011-06-08 23:45:08 -05:00
{
return static::input('file', $name, null, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
2011-07-05 06:57:42 -07:00
* Create a HTML textarea element.
2011-06-08 23:45:08 -05:00
*
2011-07-05 06:57:42 -07:00
* @param string $name
2011-07-02 08:43:12 -05:00
* @param string $value
2011-06-08 23:45:08 -05:00
* @param array $attributes
* @return string
*/
public static function textarea($name, $value = '', $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-11-09 21:55:21 -06:00
$attributes['name'] = $name;
$attributes['id'] = static::id($name, $attributes);
2011-06-08 23:45:08 -05:00
2011-08-08 08:59:51 -05:00
if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
2011-07-02 08:43:12 -05:00
2011-08-08 08:59:51 -05:00
if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
2011-07-05 06:57:42 -07:00
return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>';
2011-07-02 08:51:14 -05:00
}
2011-06-08 23:45:08 -05:00
/**
2011-07-05 06:57:42 -07:00
* Create a HTML select element.
2011-06-08 23:45:08 -05:00
*
2011-09-21 22:57:32 -05:00
* <code>
* // Create a HTML select element filled with options
* echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
*
* // Create a select element with a default selected value
* echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
* </code>
*
2011-06-08 23:45:08 -05:00
* @param string $name
2011-07-05 06:57:42 -07:00
* @param array $options
* @param string $selected
2011-06-08 23:45:08 -05:00
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function select($name, $options = array(), $selected = null, $attributes = array())
2011-06-08 23:45:08 -05:00
{
$attributes['id'] = static::id($name, $attributes);
$attributes['name'] = $name;
2011-07-05 06:57:42 -07:00
$html = array();
foreach ($options as $value => $display)
{
$html[] = static::option($value, $display, $selected);
2011-07-05 06:57:42 -07:00
}
return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>';
2011-06-08 23:45:08 -05:00
}
/**
* Create a HTML select element option.
*
* @param string $value
* @param string $display
2012-02-04 21:30:52 +00:00
* @param string $selected
* @return string
*/
protected static function option($value, $display, $selected)
{
2012-01-30 10:20:05 -06:00
if (is_array($selected))
{
$selected = (in_array($value, $selected)) ? 'selected' : null;
}
else
{
$selected = ($value == $selected) ? 'selected' : null;
}
$attributes = array('value' => HTML::entities($value), 'selected' => $selected);
return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
}
2011-06-08 23:45:08 -05:00
/**
* Create a HTML checkbox input element.
*
2011-09-21 22:57:32 -05:00
* <code>
* // Create a checkbox element
* echo Form::checkbox('terms', 'yes');
*
* // Create a checkbox that is selected by default
* echo Form::checkbox('terms', 'yes', true);
* </code>
*
2011-06-08 23:45:08 -05:00
* @param string $name
* @param string $value
* @param bool $checked
* @param array $attributes
* @return string
*/
public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
2011-06-08 23:45:08 -05:00
{
return static::checkable('checkbox', $name, $value, $checked, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
* Create a HTML radio button input element.
*
2011-09-21 22:57:32 -05:00
* <code>
* // Create a radio button element
* echo Form::radio('drinks', 'Milk');
*
* // Create a radio button that is selected by default
* echo Form::radio('drinks', 'Milk', true);
* </code>
*
2011-06-08 23:45:08 -05:00
* @param string $name
* @param string $value
* @param bool $checked
* @param array $attributes
* @return string
*/
public static function radio($name, $value = null, $checked = false, $attributes = array())
2011-06-08 23:45:08 -05:00
{
2011-09-08 17:49:16 -05:00
if (is_null($value)) $value = $name;
return static::checkable('radio', $name, $value, $checked, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
* Create a checkable input element.
*
* @param string $type
* @param string $name
* @param string $value
* @param bool $checked
* @param array $attributes
* @return string
*/
protected static function checkable($type, $name, $value, $checked, $attributes)
2011-06-08 23:45:08 -05:00
{
2011-10-15 22:38:43 -05:00
if ($checked) $attributes['checked'] = 'checked';
$attributes['id'] = static::id($name, $attributes);
2011-06-08 23:45:08 -05:00
return static::input($type, $name, $value, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
2011-07-05 06:57:42 -07:00
* Create a HTML submit input element.
2011-06-08 23:45:08 -05:00
*
* @param string $value
* @param array $attributes
* @return string
*/
public static function submit($value, $attributes = array())
2011-06-08 23:45:08 -05:00
{
return static::input('submit', null, $value, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
2011-07-05 06:57:42 -07:00
* Create a HTML reset input element.
2011-06-08 23:45:08 -05:00
*
2011-07-05 06:57:42 -07:00
* @param string $value
2011-06-08 23:45:08 -05:00
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function reset($value, $attributes = array())
2011-06-08 23:45:08 -05:00
{
return static::input('reset', null, $value, $attributes);
2011-07-05 06:57:42 -07:00
}
2011-06-08 23:45:08 -05:00
2011-07-05 06:57:42 -07:00
/**
* Create a HTML image input element.
*
2011-09-21 22:57:32 -05:00
* <code>
* // Create an image input element
* echo Form::image('img/submit.png');
* </code>
*
2011-07-05 06:57:42 -07:00
* @param string $url
2012-02-04 21:30:52 +00:00
* @param string $name
2011-07-05 06:57:42 -07:00
* @param array $attributes
* @return string
*/
public static function image($url, $name = null, $attributes = array())
2011-07-05 06:57:42 -07:00
{
$attributes['src'] = URL::to_asset($url);
2011-06-08 23:45:08 -05:00
return static::input('image', $name, null, $attributes);
2011-06-08 23:45:08 -05:00
}
/**
2011-07-05 06:57:42 -07:00
* Create a HTML button element.
2011-06-08 23:45:08 -05:00
*
2011-07-05 06:57:42 -07:00
* @param string $value
2011-06-08 23:45:08 -05:00
* @param array $attributes
* @return string
2011-07-05 06:57:42 -07:00
*/
public static function button($value, $attributes = array())
2011-06-08 23:45:08 -05:00
{
return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>';
2011-06-08 23:45:08 -05:00
}
2011-06-16 21:09:39 -05:00
/**
* Determine the ID attribute for a form element.
*
* @param string $name
* @param array $attributes
* @return mixed
*/
protected static function id($name, $attributes)
2011-06-16 21:09:39 -05:00
{
2012-01-16 13:59:24 -06:00
// If an ID has been explicitly specified in the attributes, we will
// use that ID. Otherwise, we will look for an ID in the array of
2012-03-21 10:48:49 -05:00
// label names so labels and their elements have the same ID.
2011-10-15 22:38:43 -05:00
if (array_key_exists('id', $attributes))
{
return $attributes['id'];
}
2011-06-16 21:09:39 -05:00
2011-10-15 22:38:43 -05:00
if (in_array($name, static::$labels))
{
return $name;
}
2011-06-16 21:09:39 -05:00
}
2012-03-21 10:48:49 -05:00
/**
* Dynamically handle calls to custom macros.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
2012-03-21 10:49:09 -05:00
if (isset(static::$macros[$method]))
2012-03-21 10:48:49 -05:00
{
2012-03-21 10:49:09 -05:00
return call_user_func_array(static::$macros[$method], $parameters);
2012-03-21 10:48:49 -05:00
}
throw new \Exception("Method [$method] does not exist.");
}
}