PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/system/form.php

https://github.com/vudduu/SaltenaSystem
PHP | 423 lines | 147 code | 50 blank | 226 comment | 13 complexity | af4afbe367b6d9a59e091782298e1bc1 MD5 | raw file
  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. * @param bool $https
  16. * @return string
  17. */
  18. public static function open($action = null, $method = 'POST', $attributes = array(), $https = false)
  19. {
  20. $attributes['action'] = HTML::entities(URL::to(((is_null($action)) ? Request::uri() : $action), $https));
  21. // PUT and DELETE methods are spoofed using a hidden field containing the request method.
  22. // Since, HTML does not support PUT and DELETE on forms, we will use POST.
  23. $attributes['method'] = ($method == 'PUT' or $method == 'DELETE') ? 'POST' : $method;
  24. if ( ! array_key_exists('accept-charset', $attributes))
  25. {
  26. $attributes['accept-charset'] = Config::get('application.encoding');
  27. }
  28. $html = '<form'.HTML::attributes($attributes).'>';
  29. if ($method == 'PUT' or $method == 'DELETE')
  30. {
  31. $html .= PHP_EOL.static::input('hidden', 'REQUEST_METHOD', $method);
  32. }
  33. return $html.PHP_EOL;
  34. }
  35. /**
  36. * Open a HTML form with a HTTPS action.
  37. *
  38. * @param string $action
  39. * @param string $method
  40. * @param array $attributes
  41. * @return string
  42. */
  43. public static function open_secure($action = null, $method = 'POST', $attributes = array())
  44. {
  45. return static::open($action, $method, $attributes, true);
  46. }
  47. /**
  48. * Open a HTML form that accepts file uploads.
  49. *
  50. * @param string $action
  51. * @param string $method
  52. * @param array $attributes
  53. * @param bool $https
  54. * @return string
  55. */
  56. public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = false)
  57. {
  58. $attributes['enctype'] = 'multipart/form-data';
  59. return static::open($action, $method, $attributes, $https);
  60. }
  61. /**
  62. * Open a HTML form that accepts file uploads with a HTTPS action.
  63. *
  64. * @param string $action
  65. * @param string $method
  66. * @param array $attributes
  67. * @return string
  68. */
  69. public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
  70. {
  71. return static::open_for_files($action, $method, $attributes, true);
  72. }
  73. /**
  74. * Close a HTML form.
  75. *
  76. * @return string
  77. */
  78. public static function close()
  79. {
  80. return '</form>';
  81. }
  82. /**
  83. * Generate a hidden field containing the current CSRF token.
  84. *
  85. * @return string
  86. */
  87. public static function token()
  88. {
  89. return static::input('hidden', 'csrf_token', static::raw_token());
  90. }
  91. /**
  92. * Retrieve the current CSRF token.
  93. *
  94. * @return string
  95. */
  96. public static function raw_token()
  97. {
  98. if (Config::get('session.driver') == '')
  99. {
  100. throw new \Exception('Sessions must be enabled to retrieve a CSRF token.');
  101. }
  102. return Session::get('csrf_token');
  103. }
  104. /**
  105. * Create a HTML label element.
  106. *
  107. * @param string $name
  108. * @param string $value
  109. * @param array $attributes
  110. * @return string
  111. */
  112. public static function label($name, $value, $attributes = array())
  113. {
  114. static::$labels[] = $name;
  115. return '<label for="'.$name.'"'.HTML::attributes($attributes).'>'.HTML::entities($value).'</label>'.PHP_EOL;
  116. }
  117. /**
  118. * Create a HTML input element.
  119. *
  120. * @param string $name
  121. * @param mixed $value
  122. * @param array $attributes
  123. * @return string
  124. */
  125. public static function input($type, $name, $value = null, $attributes = array())
  126. {
  127. $id = static::id($name, $attributes);
  128. return '<input'.HTML::attributes(array_merge($attributes, compact('type', 'name', 'value', 'id'))).'>'.PHP_EOL;
  129. }
  130. /**
  131. * Create a HTML text input element.
  132. *
  133. * @param string $name
  134. * @param string $value
  135. * @param array $attributes
  136. * @return string
  137. */
  138. public static function text($name, $value = null, $attributes = array())
  139. {
  140. return static::input('text', $name, $value, $attributes);
  141. }
  142. /**
  143. * Create a HTML password input element.
  144. *
  145. * @param string $name
  146. * @param array $attributes
  147. * @return string
  148. */
  149. public static function password($name, $attributes = array())
  150. {
  151. return static::input('password', $name, null, $attributes);
  152. }
  153. /**
  154. * Create a HTML hidden input element.
  155. *
  156. * @param string $name
  157. * @param string $value
  158. * @param array $attributes
  159. * @return string
  160. */
  161. public static function hidden($name, $value = null, $attributes = array())
  162. {
  163. return static::input('hidden', $name, $value, $attributes);
  164. }
  165. /**
  166. * Create a HTML search input element.
  167. *
  168. * @param string $name
  169. * @param string $value
  170. * @param array $attributes
  171. * @return string
  172. */
  173. public static function search($name, $value = null, $attributes = array())
  174. {
  175. return static::input('search', $name, $value, $attributes);
  176. }
  177. /**
  178. * Create a HTML email input element.
  179. *
  180. * @param string $name
  181. * @param string $value
  182. * @param array $attributes
  183. * @return string
  184. */
  185. public static function email($name, $value = null, $attributes = array())
  186. {
  187. return static::input('email', $name, $value, $attributes);
  188. }
  189. /**
  190. * Create a HTML telephone input element.
  191. *
  192. * @param string $name
  193. * @param string $value
  194. * @param array $attributes
  195. * @return string
  196. */
  197. public static function telephone($name, $value = null, $attributes = array())
  198. {
  199. return static::input('tel', $name, $value, $attributes);
  200. }
  201. /**
  202. * Create a HTML URL input element.
  203. *
  204. * @param string $name
  205. * @param string $value
  206. * @param array $attributes
  207. * @return string
  208. */
  209. public static function url($name, $value = null, $attributes = array())
  210. {
  211. return static::input('url', $name, $value, $attributes);
  212. }
  213. /**
  214. * Create a HTML number input element.
  215. *
  216. * @param string $name
  217. * @param string $value
  218. * @param array $attributes
  219. * @return string
  220. */
  221. public static function number($name, $value = null, $attributes = array())
  222. {
  223. return static::input('number', $name, $value, $attributes);
  224. }
  225. /**
  226. * Create a HTML file input element.
  227. *
  228. * @param string $name
  229. * @param array $attributes
  230. * @return string
  231. */
  232. public static function file($name, $attributes = array())
  233. {
  234. return static::input('file', $name, null, $attributes);
  235. }
  236. /**
  237. * Create a HTML textarea element.
  238. *
  239. * @param string $name
  240. * @param string $value
  241. * @param array $attributes
  242. * @return string
  243. */
  244. public static function textarea($name, $value = '', $attributes = array())
  245. {
  246. $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));
  247. if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
  248. if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
  249. return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
  250. }
  251. /**
  252. * Create a HTML select element.
  253. *
  254. * @param string $name
  255. * @param array $options
  256. * @param string $selected
  257. * @param array $attributes
  258. * @return string
  259. */
  260. public static function select($name, $options = array(), $selected = null, $attributes = array())
  261. {
  262. $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));
  263. $html = array();
  264. foreach ($options as $value => $display)
  265. {
  266. $option_attributes = array('value' => HTML::entities($value), 'selected' => ($value == $selected) ? 'selected' : null);
  267. $html[] = '<option'.HTML::attributes($option_attributes).'>'.HTML::entities($display).'</option>';
  268. }
  269. return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
  270. }
  271. /**
  272. * Create a HTML checkbox input element.
  273. *
  274. * @param string $name
  275. * @param string $value
  276. * @param bool $checked
  277. * @param array $attributes
  278. * @return string
  279. */
  280. public static function checkbox($name, $value = null, $checked = false, $attributes = array())
  281. {
  282. return static::checkable('checkbox', $name, $value, $checked, $attributes);
  283. }
  284. /**
  285. * Create a HTML radio button input element.
  286. *
  287. * @param string $name
  288. * @param string $value
  289. * @param bool $checked
  290. * @param array $attributes
  291. * @return string
  292. */
  293. public static function radio($name, $value = null, $checked = false, $attributes = array())
  294. {
  295. return static::checkable('radio', $name, $value, $checked, $attributes);
  296. }
  297. /**
  298. * Create a checkable input element.
  299. *
  300. * @param string $type
  301. * @param string $name
  302. * @param string $value
  303. * @param bool $checked
  304. * @param array $attributes
  305. * @return string
  306. */
  307. private static function checkable($type, $name, $value, $checked, $attributes)
  308. {
  309. $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'checked' => ($checked) ? 'checked' : null));
  310. return static::input($type, $name, $value, $attributes);
  311. }
  312. /**
  313. * Create a HTML submit input element.
  314. *
  315. * @param string $value
  316. * @param array $attributes
  317. * @return string
  318. */
  319. public static function submit($value, $attributes = array())
  320. {
  321. return static::input('submit', null, $value, $attributes);
  322. }
  323. /**
  324. * Create a HTML reset input element.
  325. *
  326. * @param string $value
  327. * @param array $attributes
  328. * @return string
  329. */
  330. public static function reset($value, $attributes = array())
  331. {
  332. return static::input('reset', null, $value, $attributes);
  333. }
  334. /**
  335. * Create a HTML image input element.
  336. *
  337. * @param string $url
  338. * @param array $attributes
  339. * @return string
  340. */
  341. public static function image($url, $name = null, $attributes = array())
  342. {
  343. $attributes['src'] = URL::to_asset($url);
  344. return static::input('image', $name, null, $attributes);
  345. }
  346. /**
  347. * Create a HTML button element.
  348. *
  349. * @param string $name
  350. * @param string $value
  351. * @param array $attributes
  352. * @return string
  353. */
  354. public static function button($value, $attributes = array())
  355. {
  356. return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
  357. }
  358. /**
  359. * Determine the ID attribute for a form element.
  360. *
  361. * An explicitly specified ID in the attributes takes first precedence, then
  362. * the label names will be checked for a label matching the element name.
  363. *
  364. * @param string $name
  365. * @param array $attributes
  366. * @return mixed
  367. */
  368. private static function id($name, $attributes)
  369. {
  370. if (array_key_exists('id', $attributes)) return $attributes['id'];
  371. if (in_array($name, static::$labels)) return $name;
  372. }
  373. }