1: <?php namespace Laravel;
2:
3: class Form {
4:
5: /**
6: * All of the label names that have been created.
7: *
8: * @var array
9: */
10: public static $labels = array();
11:
12: /**
13: * The registered custom macros.
14: *
15: * @var array
16: */
17: public static $macros = array();
18:
19: /**
20: * Registers a custom macro.
21: *
22: * @param string $name
23: * @param Closure $macro
24: * @return void
25: */
26: public static function macro($name, $macro)
27: {
28: static::$macros[$name] = $macro;
29: }
30:
31: /**
32: * Open a HTML form.
33: *
34: * <code>
35: * // Open a "POST" form to the current request URI
36: * echo Form::open();
37: *
38: * // Open a "POST" form to a given URI
39: * echo Form::open('user/profile');
40: *
41: * // Open a "PUT" form to a given URI
42: * echo Form::open('user/profile', 'put');
43: *
44: * // Open a form that has HTML attributes
45: * echo Form::open('user/profile', 'post', array('class' => 'profile'));
46: * </code>
47: *
48: * @param string $action
49: * @param string $method
50: * @param array $attributes
51: * @param bool $https
52: * @return string
53: */
54: public static function open($action = null, $method = 'POST', $attributes = array(), $https = null)
55: {
56: $method = strtoupper($method);
57:
58: $attributes['method'] = static::method($method);
59:
60: $attributes['action'] = static::action($action, $https);
61:
62: // If a character encoding has not been specified in the attributes, we will
63: // use the default encoding as specified in the application configuration
64: // file for the "accept-charset" attribute.
65: if ( ! array_key_exists('accept-charset', $attributes))
66: {
67: $attributes['accept-charset'] = Config::get('application.encoding');
68: }
69:
70: $append = '';
71:
72: // Since PUT and DELETE methods are not actually supported by HTML forms,
73: // we'll create a hidden input element that contains the request method
74: // and set the actual request method variable to POST.
75: if ($method == 'PUT' or $method == 'DELETE')
76: {
77: $append = static::hidden(Request::spoofer, $method);
78: }
79:
80: return '<form'.HTML::attributes($attributes).'>'.$append;
81: }
82:
83: /**
84: * Determine the appropriate request method to use for a form.
85: *
86: * @param string $method
87: * @return string
88: */
89: protected static function method($method)
90: {
91: return ($method !== 'GET') ? 'POST' : $method;
92: }
93:
94: /**
95: * Determine the appropriate action parameter to use for a form.
96: *
97: * If no action is specified, the current request URI will be used.
98: *
99: * @param string $action
100: * @param bool $https
101: * @return string
102: */
103: protected static function action($action, $https)
104: {
105: $uri = (is_null($action)) ? URI::current() : $action;
106:
107: return HTML::entities(URL::to($uri, $https));
108: }
109:
110: /**
111: * Open a HTML form with a HTTPS action URI.
112: *
113: * @param string $action
114: * @param string $method
115: * @param array $attributes
116: * @return string
117: */
118: public static function open_secure($action = null, $method = 'POST', $attributes = array())
119: {
120: return static::open($action, $method, $attributes, true);
121: }
122:
123: /**
124: * Open a HTML form that accepts file uploads.
125: *
126: * @param string $action
127: * @param string $method
128: * @param array $attributes
129: * @param bool $https
130: * @return string
131: */
132: public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = null)
133: {
134: $attributes['enctype'] = 'multipart/form-data';
135:
136: return static::open($action, $method, $attributes, $https);
137: }
138:
139: /**
140: * Open a HTML form that accepts file uploads with a HTTPS action URI.
141: *
142: * @param string $action
143: * @param string $method
144: * @param array $attributes
145: * @return string
146: */
147: public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
148: {
149: return static::open_for_files($action, $method, $attributes, true);
150: }
151:
152: /**
153: * Close a HTML form.
154: *
155: * @return string
156: */
157: public static function close()
158: {
159: return '</form>';
160: }
161:
162: /**
163: * Generate a hidden field containing the current CSRF token.
164: *
165: * @return string
166: */
167: public static function token()
168: {
169: return static::input('hidden', Session::csrf_token, Session::token());
170: }
171:
172: /**
173: * Create a HTML label element.
174: *
175: * <code>
176: * // Create a label for the "email" input element
177: * echo Form::label('email', 'E-Mail Address');
178: * </code>
179: *
180: * @param string $name
181: * @param string $value
182: * @param array $attributes
183: * @return string
184: */
185: public static function label($name, $value, $attributes = array(), $escape_html = true)
186: {
187: static::$labels[] = $name;
188:
189: $attributes = HTML::attributes($attributes);
190:
191: if ($escape_html) {
192: $value = HTML::entities($value);
193: }
194:
195: return '<label for="'.$name.'"'.$attributes.'>'.$value.'</label>';
196: }
197:
198: /**
199: * Create a HTML input element.
200: *
201: * <code>
202: * // Create a "text" input element named "email"
203: * echo Form::input('text', 'email');
204: *
205: * // Create an input element with a specified default value
206: * echo Form::input('text', 'email', 'example@gmail.com');
207: * </code>
208: *
209: * @param string $type
210: * @param string $name
211: * @param mixed $value
212: * @param array $attributes
213: * @return string
214: */
215: public static function input($type, $name, $value = null, $attributes = array())
216: {
217: $name = (isset($attributes['name'])) ? $attributes['name'] : $name;
218:
219: $id = static::id($name, $attributes);
220:
221: $attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));
222:
223: return '<input'.HTML::attributes($attributes).'>';
224: }
225:
226: /**
227: * Create a HTML text input element.
228: *
229: * @param string $name
230: * @param string $value
231: * @param array $attributes
232: * @return string
233: */
234: public static function text($name, $value = null, $attributes = array())
235: {
236: return static::input('text', $name, $value, $attributes);
237: }
238:
239: /**
240: * Create a HTML password input element.
241: *
242: * @param string $name
243: * @param array $attributes
244: * @return string
245: */
246: public static function password($name, $attributes = array())
247: {
248: return static::input('password', $name, null, $attributes);
249: }
250:
251: /**
252: * Create a HTML hidden input element.
253: *
254: * @param string $name
255: * @param string $value
256: * @param array $attributes
257: * @return string
258: */
259: public static function ($name, $value = null, $attributes = array())
260: {
261: return static::input('hidden', $name, $value, $attributes);
262: }
263:
264: /**
265: * Create a HTML search input element.
266: *
267: * @param string $name
268: * @param string $value
269: * @param array $attributes
270: * @return string
271: */
272: public static function search($name, $value = null, $attributes = array())
273: {
274: return static::input('search', $name, $value, $attributes);
275: }
276:
277: /**
278: * Create a HTML email input element.
279: *
280: * @param string $name
281: * @param string $value
282: * @param array $attributes
283: * @return string
284: */
285: public static function email($name, $value = null, $attributes = array())
286: {
287: return static::input('email', $name, $value, $attributes);
288: }
289:
290: /**
291: * Create a HTML telephone input element.
292: *
293: * @param string $name
294: * @param string $value
295: * @param array $attributes
296: * @return string
297: */
298: public static function telephone($name, $value = null, $attributes = array())
299: {
300: return static::input('tel', $name, $value, $attributes);
301: }
302:
303: /**
304: * Create a HTML URL input element.
305: *
306: * @param string $name
307: * @param string $value
308: * @param array $attributes
309: * @return string
310: */
311: public static function url($name, $value = null, $attributes = array())
312: {
313: return static::input('url', $name, $value, $attributes);
314: }
315:
316: /**
317: * Create a HTML number input element.
318: *
319: * @param string $name
320: * @param string $value
321: * @param array $attributes
322: * @return string
323: */
324: public static function number($name, $value = null, $attributes = array())
325: {
326: return static::input('number', $name, $value, $attributes);
327: }
328:
329: /**
330: * Create a HTML date input element.
331: *
332: * @param string $name
333: * @param string $value
334: * @param array $attributes
335: * @return string
336: */
337: public static function date($name, $value = null, $attributes = array())
338: {
339: return static::input('date', $name, $value, $attributes);
340: }
341:
342: /**
343: * Create a HTML file input element.
344: *
345: * @param string $name
346: * @param array $attributes
347: * @return string
348: */
349: public static function file($name, $attributes = array())
350: {
351: return static::input('file', $name, null, $attributes);
352: }
353:
354: /**
355: * Create a HTML textarea element.
356: *
357: * @param string $name
358: * @param string $value
359: * @param array $attributes
360: * @return string
361: */
362: public static function textarea($name, $value = '', $attributes = array())
363: {
364: $attributes['name'] = $name;
365:
366: $attributes['id'] = static::id($name, $attributes);
367:
368: if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
369:
370: if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
371:
372: return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>';
373: }
374:
375: /**
376: * Create a HTML select element.
377: *
378: * <code>
379: * // Create a HTML select element filled with options
380: * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
381: *
382: * // Create a select element with a default selected value
383: * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
384: * </code>
385: *
386: * @param string $name
387: * @param array $options
388: * @param string $selected
389: * @param array $attributes
390: * @return string
391: */
392: public static function select($name, $options = array(), $selected = null, $attributes = array())
393: {
394: $attributes['id'] = static::id($name, $attributes);
395:
396: $attributes['name'] = $name;
397:
398: $html = array();
399:
400: foreach ($options as $value => $display)
401: {
402: if (is_array($display))
403: {
404: $html[] = static::optgroup($display, $value, $selected);
405: }
406: else
407: {
408: $html[] = static::option($value, $display, $selected);
409: }
410: }
411:
412: return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>';
413: }
414:
415: /**
416: * Create a HTML select element optgroup.
417: *
418: * @param array $options
419: * @param string $label
420: * @param string $selected
421: * @return string
422: */
423: protected static function optgroup($options, $label, $selected)
424: {
425: $html = array();
426:
427: foreach ($options as $value => $display)
428: {
429: $html[] = static::option($value, $display, $selected);
430: }
431:
432: return '<optgroup label="'.HTML::entities($label).'">'.implode('', $html).'</optgroup>';
433: }
434:
435: /**
436: * Create a HTML select element option.
437: *
438: * @param string $value
439: * @param string $display
440: * @param string $selected
441: * @return string
442: */
443: protected static function option($value, $display, $selected)
444: {
445: if (is_array($selected))
446: {
447: $selected = (in_array($value, $selected)) ? 'selected' : null;
448: }
449: else
450: {
451: $selected = ((string) $value == (string) $selected) ? 'selected' : null;
452: }
453:
454: $attributes = array('value' => HTML::entities($value), 'selected' => $selected);
455:
456: return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
457: }
458:
459: /**
460: * Create a HTML checkbox input element.
461: *
462: * <code>
463: * // Create a checkbox element
464: * echo Form::checkbox('terms', 'yes');
465: *
466: * // Create a checkbox that is selected by default
467: * echo Form::checkbox('terms', 'yes', true);
468: * </code>
469: *
470: * @param string $name
471: * @param string $value
472: * @param bool $checked
473: * @param array $attributes
474: * @return string
475: */
476: public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
477: {
478: return static::checkable('checkbox', $name, $value, $checked, $attributes);
479: }
480:
481: /**
482: * Create a HTML radio button input element.
483: *
484: * <code>
485: * // Create a radio button element
486: * echo Form::radio('drinks', 'Milk');
487: *
488: * // Create a radio button that is selected by default
489: * echo Form::radio('drinks', 'Milk', true);
490: * </code>
491: *
492: * @param string $name
493: * @param string $value
494: * @param bool $checked
495: * @param array $attributes
496: * @return string
497: */
498: public static function radio($name, $value = null, $checked = false, $attributes = array())
499: {
500: if (is_null($value)) $value = $name;
501:
502: return static::checkable('radio', $name, $value, $checked, $attributes);
503: }
504:
505: /**
506: * Create a checkable input element.
507: *
508: * @param string $type
509: * @param string $name
510: * @param string $value
511: * @param bool $checked
512: * @param array $attributes
513: * @return string
514: */
515: protected static function checkable($type, $name, $value, $checked, $attributes)
516: {
517: if ($checked) $attributes['checked'] = 'checked';
518:
519: $attributes['id'] = static::id($name, $attributes);
520:
521: return static::input($type, $name, $value, $attributes);
522: }
523:
524: /**
525: * Create a HTML submit input element.
526: *
527: * @param string $value
528: * @param array $attributes
529: * @return string
530: */
531: public static function submit($value = null, $attributes = array())
532: {
533: return static::input('submit', null, $value, $attributes);
534: }
535:
536: /**
537: * Create a HTML reset input element.
538: *
539: * @param string $value
540: * @param array $attributes
541: * @return string
542: */
543: public static function reset($value = null, $attributes = array())
544: {
545: return static::input('reset', null, $value, $attributes);
546: }
547:
548: /**
549: * Create a HTML image input element.
550: *
551: * <code>
552: * // Create an image input element
553: * echo Form::image('img/submit.png');
554: * </code>
555: *
556: * @param string $url
557: * @param string $name
558: * @param array $attributes
559: * @return string
560: */
561: public static function image($url, $name = null, $attributes = array())
562: {
563: $attributes['src'] = URL::to_asset($url);
564:
565: return static::input('image', $name, null, $attributes);
566: }
567:
568: /**
569: * Create a HTML button element.
570: *
571: * @param string $value
572: * @param array $attributes
573: * @return string
574: */
575: public static function button($value = null, $attributes = array())
576: {
577: return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>';
578: }
579:
580: /**
581: * Determine the ID attribute for a form element.
582: *
583: * @param string $name
584: * @param array $attributes
585: * @return mixed
586: */
587: protected static function id($name, $attributes)
588: {
589: // If an ID has been explicitly specified in the attributes, we will
590: // use that ID. Otherwise, we will look for an ID in the array of
591: // label names so labels and their elements have the same ID.
592: if (array_key_exists('id', $attributes))
593: {
594: return $attributes['id'];
595: }
596:
597: if (in_array($name, static::$labels))
598: {
599: return $name;
600: }
601: }
602:
603: /**
604: * Dynamically handle calls to custom macros.
605: *
606: * @param string $method
607: * @param array $parameters
608: * @return mixed
609: */
610: public static function __callStatic($method, $parameters)
611: {
612: if (isset(static::$macros[$method]))
613: {
614: return call_user_func_array(static::$macros[$method], $parameters);
615: }
616:
617: throw new \Exception("Method [$method] does not exist.");
618: }
619:
620: }
621: