form.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php namespace System;
  2. class Form {
  3. /**
  4. * Stores labels names.
  5. *
  6. * @var array
  7. */
  8. private static $labels = array();
  9. /**
  10. * Open a HTML form.
  11. *
  12. * @param string $action
  13. * @param string $method
  14. * @param array $attributes
  15. * @return string
  16. */
  17. public static function open($action = null, $method = 'POST', $attributes = array())
  18. {
  19. if (is_null($action))
  20. {
  21. $action = Request::uri();
  22. }
  23. $action = URL::to($action);
  24. $attributes['action'] = HTML::entities($action);
  25. $attributes['method'] = ($method == 'GET' or $method == 'POST') ? $method : 'POST';
  26. if ( ! array_key_exists('accept-charset', $attributes))
  27. {
  28. $attributes['accept-charset'] = Config::get('application.encoding');
  29. }
  30. $html = '<form'.HTML::attributes($attributes).'>';
  31. // -------------------------------------------------------
  32. // If the method is PUT or DELETE, we'll need to spoof it
  33. // using a hidden input field.
  34. //
  35. // For more information, see the Input library.
  36. // -------------------------------------------------------
  37. if ($method == 'PUT' or $method == 'DELETE')
  38. {
  39. $html .= PHP_EOL.static::hidden('REQUEST_METHOD', $method);
  40. }
  41. return $html.PHP_EOL;
  42. }
  43. /**
  44. * Open a HTML form that accepts file uploads.
  45. *
  46. * @param string $action
  47. * @param string $method
  48. * @param array $attributes
  49. * @return string
  50. */
  51. public static function open_multipart($action = null, $method = 'POST', $attributes = array())
  52. {
  53. $attributes['enctype'] = 'multipart/form-data';
  54. return static::open($action, $method, $attributes);
  55. }
  56. /**
  57. * Close a HTML form.
  58. *
  59. * @return string
  60. */
  61. public static function close()
  62. {
  63. return '</form>'.PHP_EOL;
  64. }
  65. /**
  66. * Generate a hidden field containing the current CSRF token.
  67. *
  68. * @return string
  69. */
  70. public static function token()
  71. {
  72. return static::hidden('csrf_token', static::raw_token());
  73. }
  74. /**
  75. * Retrieve the current CSRF token.
  76. *
  77. * @return string
  78. */
  79. public static function raw_token()
  80. {
  81. // -------------------------------------------------------
  82. // CSRF tokens are stored in the session, so we need to
  83. // make sure a driver has been specified.
  84. // -------------------------------------------------------
  85. if (Config::get('session.driver') == '')
  86. {
  87. throw new \Exception('Sessions must be enabled to retrieve a CSRF token.');
  88. }
  89. return Session::get('csrf_token');
  90. }
  91. /**
  92. * Create a HTML label element.
  93. *
  94. * @param string $name
  95. * @param string $value
  96. * @param array $attributes
  97. * @return string
  98. */
  99. public static function label($name, $value, $attributes = array())
  100. {
  101. static::$labels[] = $name;
  102. return '<label for="'.$name.'"'.HTML::attributes($attributes).'>'.HTML::entities($value).'</label>'.PHP_EOL;
  103. }
  104. /**
  105. * Create a HTML text input element.
  106. *
  107. * @param string $name
  108. * @param string $value
  109. * @param array $attributes
  110. * @return string
  111. */
  112. public static function text($name, $value = null, $attributes = array())
  113. {
  114. return static::input('text', $name, $value, $attributes);
  115. }
  116. /**
  117. * Create a HTML password input element.
  118. *
  119. * @param string $name
  120. * @param array $attributes
  121. * @return string
  122. */
  123. public static function password($name, $attributes = array())
  124. {
  125. return static::input('password', $name, null, $attributes);
  126. }
  127. /**
  128. * Create a HTML hidden input element.
  129. *
  130. * @param string $name
  131. * @param string $value
  132. * @param array $attributes
  133. * @return string
  134. */
  135. public static function hidden($name, $value = null, $attributes = array())
  136. {
  137. return static::input('hidden', $name, $value, $attributes);
  138. }
  139. /**
  140. * Create a HTML email input element.
  141. *
  142. * @param string $name
  143. * @param string $value
  144. * @param array $attributes
  145. * @return string
  146. */
  147. public static function email($name, $value = null, $attributes = array())
  148. {
  149. return static::input('email', $name, $value, $attributes);
  150. }
  151. /**
  152. * Create a HTML URL input element.
  153. *
  154. * @param string $name
  155. * @param string $value
  156. * @param array $attributes
  157. * @return string
  158. */
  159. public static function url($name, $value = null, $attributes = array())
  160. {
  161. return static::input('url', $name, $value, $attributes);
  162. }
  163. /**
  164. * Create a HTML search input element.
  165. *
  166. * @param string $name
  167. * @param string $value
  168. * @param array $attributes
  169. * @return string
  170. */
  171. public static function search($name, $value = null, $attributes = array())
  172. {
  173. return static::input('search', $name, $value, $attributes);
  174. }
  175. /**
  176. * Create a HTML color input element.
  177. *
  178. * @param string $name
  179. * @param string $value
  180. * @param array $attributes
  181. * @return string
  182. */
  183. public static function color($name, $value = null, $attributes = array())
  184. {
  185. return static::input('color', $name, $value, $attributes);
  186. }
  187. /**
  188. * Create a HTML number input element.
  189. *
  190. * @param string $name
  191. * @param string $value
  192. * @param array $attributes
  193. * @return string
  194. */
  195. public static function number($name, $value = null, $attributes = array())
  196. {
  197. return static::input('number', $name, $value, $attributes);
  198. }
  199. /**
  200. * Create a HTML range input element.
  201. *
  202. * @param string $name
  203. * @param string $value
  204. * @param array $attributes
  205. * @return string
  206. */
  207. public static function range($name, $value = null, $attributes = array())
  208. {
  209. return static::input('range', $name, $value, $attributes);
  210. }
  211. /**
  212. * Create a HTML telephone input element.
  213. *
  214. * @param string $name
  215. * @param string $value
  216. * @param array $attributes
  217. * @return string
  218. */
  219. public static function tel($name, $value = null, $attributes = array())
  220. {
  221. return static::input('tel', $name, $value, $attributes);
  222. }
  223. /**
  224. * Create a HTML date input element.
  225. *
  226. * @param string $name
  227. * @param string $value
  228. * @param array $attributes
  229. * @return string
  230. */
  231. public static function date($name, $value = null, $attributes = array())
  232. {
  233. return static::input('date', $name, $value, $attributes);
  234. }
  235. /**
  236. * Create a HTML file input element.
  237. *
  238. * @param string $name
  239. * @param array $attributes
  240. * @return string
  241. */
  242. public static function file($name, $attributes = array())
  243. {
  244. return static::input('file', $name, null, $attributes);
  245. }
  246. /**
  247. * Create a HTML submit input element.
  248. *
  249. * @param string $value
  250. * @param array $attributes
  251. * @return string
  252. */
  253. public static function submit($value, $attributes = array())
  254. {
  255. return static::input('submit', null, $value, $attributes);
  256. }
  257. /**
  258. * Create a HTML reset input element.
  259. *
  260. * @param string $value
  261. * @param array $attributes
  262. * @return string
  263. */
  264. public static function reset($value, $attributes = array())
  265. {
  266. return static::input('reset', null, $value, $attributes);
  267. }
  268. /**
  269. * Create a HTML button element.
  270. *
  271. * @param string $name
  272. * @param string $value
  273. * @param array $attributes
  274. * @return string
  275. */
  276. public static function button($value, $attributes = array())
  277. {
  278. return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
  279. }
  280. /**
  281. * Create a HTML checkbox input element.
  282. *
  283. * @param string $name
  284. * @param string $value
  285. * @param bool $checked
  286. * @param array $attributes
  287. * @return string
  288. */
  289. public static function checkbox($name, $value = null, $checked = false, $attributes = array())
  290. {
  291. return static::checkable('checkbox', $name, $value, $checked, $attributes);
  292. }
  293. /**
  294. * Create a HTML radio button input element.
  295. *
  296. * @param string $name
  297. * @param string $value
  298. * @param bool $checked
  299. * @param array $attributes
  300. * @return string
  301. */
  302. public static function radio($name, $value = null, $checked = false, $attributes = array())
  303. {
  304. return static::checkable('radio', $name, $value, $checked, $attributes);
  305. }
  306. /**
  307. * Create a checkable input element.
  308. *
  309. * @param string $type
  310. * @param string $name
  311. * @param string $value
  312. * @param bool $checked
  313. * @param array $attributes
  314. * @return string
  315. */
  316. private static function checkable($type, $name, $value, $checked, $attributes)
  317. {
  318. if ($checked === true)
  319. {
  320. $attributes['checked'] = 'checked';
  321. }
  322. $attributes['id'] = static::id($name, $attributes);
  323. return static::input($type, $name, $value, $attributes);
  324. }
  325. /**
  326. * Create a HTML textarea element.
  327. *
  328. * @param string $name
  329. * @param string $value
  330. * @param array $attributes
  331. * @return string
  332. */
  333. public static function textarea($name, $value = '', $attributes = array())
  334. {
  335. $attributes['name'] = $name;
  336. $attributes['id'] = static::id($name, $attributes);
  337. // -------------------------------------------------------
  338. // Set the default number of rows.
  339. // -------------------------------------------------------
  340. if ( ! isset($attributes['rows']))
  341. {
  342. $attributes['rows'] = 10;
  343. }
  344. // -------------------------------------------------------
  345. // Set the default number of columns.
  346. // -------------------------------------------------------
  347. if ( ! isset($attributes['cols']))
  348. {
  349. $attributes['cols'] = 50;
  350. }
  351. return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
  352. }
  353. /**
  354. * Create a HTML select element.
  355. *
  356. * @param string $name
  357. * @param array $options
  358. * @param string $selected
  359. * @param array $attributes
  360. * @return string
  361. */
  362. public static function select($name, $options = array(), $selected = null, $attributes = array())
  363. {
  364. $attributes['name'] = $name;
  365. $attributes['id'] = static::id($name, $attributes);
  366. $html_options = array();
  367. foreach ($options as $value => $display)
  368. {
  369. $option_attributes = array();
  370. $option_attributes['value'] = HTML::entities($value);
  371. $option_attributes['selected'] = ($value == $selected) ? 'selected' : null;
  372. $html_options[] = '<option'.HTML::attributes($option_attributes).'>'.HTML::entities($display).'</option>';
  373. }
  374. return '<select'.HTML::attributes($attributes).'>'.implode('', $html_options).'</select>'.PHP_EOL;
  375. }
  376. /**
  377. * Create a HTML input element.
  378. *
  379. * @param string $name
  380. * @param mixed $value
  381. * @param array $attributes
  382. * @return string
  383. */
  384. public static function input($type, $name, $value = null, $attributes = array())
  385. {
  386. $attributes['type'] = $type;
  387. $attributes['name'] = $name;
  388. $attributes['value'] = $value;
  389. $attributes['id'] = static::id($name, $attributes);
  390. return '<input'.HTML::attributes($attributes).'>'.PHP_EOL;
  391. }
  392. /**
  393. * Determine the ID attribute for a form element.
  394. *
  395. * @param string $name
  396. * @param array $attributes
  397. * @return mixed
  398. */
  399. private static function id($name, $attributes)
  400. {
  401. // -------------------------------------------------------
  402. // If an ID attribute was already explicitly specified
  403. // for the element, just use that.
  404. // -------------------------------------------------------
  405. if (array_key_exists('id', $attributes))
  406. {
  407. return $attributes['id'];
  408. }
  409. // -------------------------------------------------------
  410. // If a label element was created with a value matching
  411. // the name of the form element, use the name as the ID.
  412. // -------------------------------------------------------
  413. if (in_array($name, static::$labels))
  414. {
  415. return $name;
  416. }
  417. }
  418. }