form.php 9.6 KB
Newer Older
1 2 3 4 5
<?php namespace System;

class Form {

	/**
Pedro Borges committed
6
	 * Stores labels names.
7 8 9 10 11 12
	 *
	 * @var array
	 */
	private static $labels = array();

	/**
13 14 15 16 17 18 19 20 21
	 * Open a HTML form.
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
	 * @return string
	 */	
	public static function open($action = null, $method = 'POST', $attributes = array())
	{
Taylor Otwell committed
22
		$attributes['action'] = HTML::entities(URL::to((is_null($action)) ? Request::uri() : $action));
23

24 25
		// If the request method is PUT or DELETE, we'll default the request method to POST
		// since the request method is being spoofed by the form.
Taylor Otwell committed
26
		$attributes['method'] = ($method == 'PUT' or $method == 'DELETE') ? 'POST' : $method;
27 28 29

		if ( ! array_key_exists('accept-charset', $attributes))
		{
30
			$attributes['accept-charset'] = Config::get('application.encoding');			
31 32 33 34
		}

		$html = '<form'.HTML::attributes($attributes).'>';

35 36
		// If the request method is PUT or DELETE, create a hidden input element with the
		// request method in it since HTML forms do not support these two methods.
37 38
		if ($method == 'PUT' or $method == 'DELETE')
		{
Taylor Otwell committed
39
			$html .= PHP_EOL.static::input('hidden', 'REQUEST_METHOD', $method);
40 41 42 43 44 45
		}

		return $html.PHP_EOL;
	}

	/**
46 47 48 49 50 51 52
	 * Open a HTML form that accepts file uploads.
	 *
	 * @param  string  $action
	 * @param  string  $method
	 * @param  array   $attributes
	 * @return string
	 */	
Taylor Otwell committed
53
	public static function open_for_files($action = null, $method = 'POST', $attributes = array())
54 55 56
	{
		$attributes['enctype'] = 'multipart/form-data';

Taylor Otwell committed
57
		return static::open($action, $method, $attributes);
58 59 60
	}

	/**
Taylor Otwell committed
61 62 63 64 65 66 67 68 69 70
	 * Close a HTML form.
	 *
	 * @return string
	 */
	public static function close()
	{
		return '</form>';
	}

	/**
71 72 73 74 75 76
	 * Generate a hidden field containing the current CSRF token.
	 *
	 * @return string
	 */
	public static function token()
	{
Taylor Otwell committed
77
		return static::input('hidden', 'csrf_token', static::raw_token());
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	}

	/**
	 * Retrieve the current CSRF token.
	 *
	 * @return string
	 */
	public static function raw_token()
	{
		if (Config::get('session.driver') == '')
		{
			throw new \Exception('Sessions must be enabled to retrieve a CSRF token.');			
		}

		return Session::get('csrf_token');
	}

	/**
96 97 98 99 100 101 102 103 104
	 * Create a HTML label element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */		
	public static function label($name, $value, $attributes = array())
	{
105
		static::$labels[] = $name;
Taylor Otwell committed
106

107 108 109 110
		return '<label for="'.$name.'"'.HTML::attributes($attributes).'>'.HTML::entities($value).'</label>'.PHP_EOL;
	}

	/**
Taylor Otwell committed
111
	 * Create a HTML input element.
112 113
	 *
	 * @param  string  $name
Taylor Otwell committed
114
	 * @param  mixed   $value
115 116 117
	 * @param  array   $attributes
	 * @return string
	 */		
Taylor Otwell committed
118
	public static function input($type, $name, $value = null, $attributes = array())
119
	{
Taylor Otwell committed
120
		return '<input'.HTML::attributes(array_merge($attributes, array('type' => $type, 'name' => $name, 'value' => $value, 'id' => static::id($name, $attributes)))).'>'.PHP_EOL;
121 122 123
	}

	/**
124 125 126 127 128 129 130 131 132 133 134 135 136
	 * 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())
	{
		return static::input('text', $name, $value, $attributes);
	}

	/**
137 138 139 140 141
	 * Create a HTML password input element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
142
	 */		
143 144 145 146 147 148
	public static function password($name, $attributes = array())
	{
		return static::input('password', $name, null, $attributes);
	}

	/**
149 150 151 152 153 154 155 156 157 158 159 160 161
	 * 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);
	}

	/**
162 163 164 165 166 167
	 * Create a HTML search input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
168
	 */		
169 170 171 172 173 174
	public static function search($name, $value = null, $attributes = array())
	{
		return static::input('search', $name, $value, $attributes);
	}

	/**
175
	 * Create a HTML email input element.
176 177 178 179 180
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
181
	 */		
182
	public static function email($name, $value = null, $attributes = array())
183
	{
184
		return static::input('email', $name, $value, $attributes);
185 186 187
	}

	/**
188
	 * Create a HTML telephone input element.
189 190 191 192 193
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
194
	 */		
195
	public static function telephone($name, $value = null, $attributes = array())
196
	{
197
		return static::input('tel', $name, $value, $attributes);
198 199 200
	}

	/**
201
	 * Create a HTML URL input element.
202 203 204 205 206
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
207
	 */		
208
	public static function url($name, $value = null, $attributes = array())
209
	{
210
		return static::input('url', $name, $value, $attributes);
211 212 213
	}

	/**
Taylor Otwell committed
214
	 * Create a HTML number input element.
215 216 217 218 219
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
220 221
	 */		
	public static function number($name, $value = null, $attributes = array())
222
	{
Taylor Otwell committed
223
		return static::input('number', $name, $value, $attributes);
224 225 226
	}

	/**
227 228 229 230 231 232 233 234 235 236 237 238
	 * Create a HTML file input element.
	 *
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return string
	 */			
	public static function file($name, $attributes = array())
	{
		return static::input('file', $name, null, $attributes);
	}

	/**
Taylor Otwell committed
239
	 * Create a HTML textarea element.
240
	 *
Taylor Otwell committed
241
	 * @param  string  $name
242
	 * @param  string  $value
243 244 245
	 * @param  array   $attributes
	 * @return string
	 */
Taylor Otwell committed
246
	public static function textarea($name, $value = '', $attributes = array())
247
	{
Taylor Otwell committed
248
		$attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));
249

Taylor Otwell committed
250 251 252 253
		if ( ! isset($attributes['rows']))
		{
			$attributes['rows'] = 10;
		}
254

Taylor Otwell committed
255 256 257 258 259 260
		if ( ! isset($attributes['cols']))
		{
			$attributes['cols'] = 50;
		}

		return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
261 262 263
	}

	/**
Taylor Otwell committed
264
	 * Create a HTML select element.
265 266
	 *
	 * @param  string  $name
Taylor Otwell committed
267 268
	 * @param  array   $options
	 * @param  string  $selected
269 270
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
271 272
	 */	
	public static function select($name, $options = array(), $selected = null, $attributes = array())
273
	{
Taylor Otwell committed
274 275 276 277 278 279 280 281 282 283
		$attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));

		$html = array();

		foreach ($options as $value => $display)
		{
			$html[] = '<option'.HTML::attributes(array('value' => HTML::entities($value), 'selected' => ($value == $selected) ? 'selected' : null)).'>'.HTML::entities($display).'</option>';
		}

		return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	}

	/**
	 * Create a HTML checkbox input element.
	 *
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
	public static function checkbox($name, $value = null, $checked = false, $attributes = array())
	{
		return static::checkable('checkbox', $name, $value, $checked, $attributes);
	}

	/**
	 * Create a HTML radio button input element.
	 *
	 * @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())
	{
		return static::checkable('radio', $name, $value, $checked, $attributes);
	}

	/**
	 * Create a checkable input element.
	 *
	 * @param  string  $type
	 * @param  string  $name
	 * @param  string  $value
	 * @param  bool    $checked
	 * @param  array   $attributes
	 * @return string
	 */
	private static function checkable($type, $name, $value, $checked, $attributes)
	{
Taylor Otwell committed
326
		$attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'checked' => ($checked) ? 'checked' : null));
327 328 329 330 331

		return static::input($type, $name, $value, $attributes);
	}

	/**
Taylor Otwell committed
332
	 * Create a HTML submit input element.
333 334 335 336 337
	 *
	 * @param  string  $value
	 * @param  array   $attributes
	 * @return string
	 */
Taylor Otwell committed
338
	public static function submit($value, $attributes = array())
339
	{
Taylor Otwell committed
340
		return static::input('submit', null, $value, $attributes);
341 342 343
	}

	/**
Taylor Otwell committed
344
	 * Create a HTML reset input element.
345
	 *
Taylor Otwell committed
346
	 * @param  string  $value
347 348
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
349 350
	 */
	public static function reset($value, $attributes = array())
351
	{
Taylor Otwell committed
352 353
		return static::input('reset', null, $value, $attributes);
	}
354

Taylor Otwell committed
355 356 357 358 359 360 361 362 363 364
	/**
	 * 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);
365

Taylor Otwell committed
366
		return static::input('image', $name, null, $attributes);
367 368 369
	}

	/**
Taylor Otwell committed
370
	 * Create a HTML button element.
371 372
	 *
	 * @param  string  $name
Taylor Otwell committed
373
	 * @param  string  $value
374 375
	 * @param  array   $attributes
	 * @return string
Taylor Otwell committed
376 377
	 */
	public static function button($value, $attributes = array())
378
	{
Taylor Otwell committed
379
		return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
380 381
	}

382 383 384
	/**
	 * Determine the ID attribute for a form element.
	 *
385 386 387
	 * An explicitly specified ID in the attributes takes first precedence, then
	 * the label names will be checked for a label matching the element name.
	 *
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
	 * @param  string  $name
	 * @param  array   $attributes
	 * @return mixed
	 */
	private static function id($name, $attributes)
	{
		if (array_key_exists('id', $attributes))
		{
			return $attributes['id'];
		}

		if (in_array($name, static::$labels))
		{
			return $name;
		}
	}

405
}