PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/form.php

https://github.com/Toushi/flow
PHP | 526 lines | 287 code | 65 blank | 174 comment | 31 complexity | dbfeeb1e8ac5034f20c3573939a50c51 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Form helper class.
  4. *
  5. * $Id: form.php 2914 2008-06-25 22:45:11Z Shadowhand $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class form_Core {
  13. /**
  14. * Generates an opening HTML form tag.
  15. *
  16. * @param string form action attribute
  17. * @param array extra attributes
  18. * @param array hidden fields to be created immediately after the form tag
  19. * @return string
  20. */
  21. public static function open($action = NULL, $attr = array(), $hidden = NULL)
  22. {
  23. // Make sure that the method is always set
  24. empty($attr['method']) and $attr['method'] = 'post';
  25. if ($attr['method'] !== 'post' AND $attr['method'] !== 'get')
  26. {
  27. // If the method is invalid, use post
  28. $attr['method'] = 'post';
  29. }
  30. if ($action === NULL)
  31. {
  32. // Use the current URL as the default action
  33. $action = url::site(Router::$complete_uri);
  34. }
  35. elseif (strpos($action, '://') === FALSE)
  36. {
  37. // Make the action URI into a URL
  38. $action = url::site($action);
  39. }
  40. // Set action
  41. $attr['action'] = $action;
  42. // Form opening tag
  43. $form = '<form'.form::attributes($attr).'>'."\n";
  44. // Add hidden fields immediate after opening tag
  45. empty($hidden) or $form .= form::hidden($hidden);
  46. return $form;
  47. }
  48. /**
  49. * Generates an opening HTML form tag that can be used for uploading files.
  50. *
  51. * @param string form action attribute
  52. * @param array extra attributes
  53. * @param array hidden fields to be created immediately after the form tag
  54. * @return string
  55. */
  56. public static function open_multipart($action = NULL, $attr = array(), $hidden = array())
  57. {
  58. // Set multi-part form type
  59. $attr['enctype'] = 'multipart/form-data';
  60. return form::open($action, $attr, $hidden);
  61. }
  62. /**
  63. * Generates a fieldset opening tag.
  64. *
  65. * @param array html attributes
  66. * @param string a string to be attached to the end of the attributes
  67. * @return string
  68. */
  69. public static function open_fieldset($data = NULL, $extra = '')
  70. {
  71. return '<fieldset'.html::attributes((array) $data).' '.$extra.'>'."\n";
  72. }
  73. /**
  74. * Generates a fieldset closing tag.
  75. *
  76. * @return string
  77. */
  78. public static function close_fieldset()
  79. {
  80. return '</fieldset>'."\n";
  81. }
  82. /**
  83. * Generates a legend tag for use with a fieldset.
  84. *
  85. * @param string legend text
  86. * @param array HTML attributes
  87. * @param string a string to be attached to the end of the attributes
  88. * @return string
  89. */
  90. public static function legend($text = '', $data = NULL, $extra = '')
  91. {
  92. return '<legend'.form::attributes((array) $data).' '.$extra.'>'.$text.'</legend>'."\n";
  93. }
  94. /**
  95. * Generates hidden form fields.
  96. * You can pass a simple key/value string or an associative array with multiple values.
  97. *
  98. * @param string|array input name (string) or key/value pairs (array)
  99. * @param string input value, if using an input name
  100. * @return string
  101. */
  102. public static function hidden($data, $value = '')
  103. {
  104. if ( ! is_array($data))
  105. {
  106. $data = array
  107. (
  108. $data => $value
  109. );
  110. }
  111. $input = '';
  112. foreach ($data as $name => $value)
  113. {
  114. $attr = array
  115. (
  116. 'type' => 'hidden',
  117. 'name' => $name,
  118. 'value' => $value
  119. );
  120. $input .= form::input($attr)."\n";
  121. }
  122. return $input;
  123. }
  124. /**
  125. * Creates an HTML form input tag. Defaults to a text type.
  126. *
  127. * @param string|array input name or an array of HTML attributes
  128. * @param string input value, when using a name
  129. * @param string a string to be attached to the end of the attributes
  130. * @return string
  131. */
  132. public static function input($data, $value = '', $extra = '')
  133. {
  134. if ( ! is_array($data))
  135. {
  136. $data = array('name' => $data);
  137. }
  138. // Type and value are required attributes
  139. $data += array
  140. (
  141. 'type' => 'text',
  142. 'value' => $value
  143. );
  144. // For safe form data
  145. $data['value'] = html::specialchars($data['value']);
  146. return '<input'.form::attributes($data).' '.$extra.' />';
  147. }
  148. /**
  149. * Creates a HTML form password input tag.
  150. *
  151. * @param string|array input name or an array of HTML attributes
  152. * @param string input value, when using a name
  153. * @param string a string to be attached to the end of the attributes
  154. * @return string
  155. */
  156. public static function password($data, $value = '', $extra = '')
  157. {
  158. if ( ! is_array($data))
  159. {
  160. $data = array('name' => $data);
  161. }
  162. $data['type'] = 'password';
  163. return form::input($data, $value, $extra);
  164. }
  165. /**
  166. * Creates an HTML form upload input tag.
  167. *
  168. * @param string|array input name or an array of HTML attributes
  169. * @param string input value, when using a name
  170. * @param string a string to be attached to the end of the attributes
  171. * @return string
  172. */
  173. public static function upload($data, $value = '', $extra = '')
  174. {
  175. if ( ! is_array($data))
  176. {
  177. $data = array('name' => $data);
  178. }
  179. $data['type'] = 'file';
  180. return form::input($data, $value, $extra);
  181. }
  182. /**
  183. * Creates an HTML form textarea tag.
  184. *
  185. * @param string|array input name or an array of HTML attributes
  186. * @param string input value, when using a name
  187. * @param string a string to be attached to the end of the attributes
  188. * @return string
  189. */
  190. public static function textarea($data, $value = '', $extra = '')
  191. {
  192. if ( ! is_array($data))
  193. {
  194. $data = array('name' => $data);
  195. }
  196. // Use the value from $data if possible, or use $value
  197. $value = isset($data['value']) ? $data['value'] : $value;
  198. // Value is not part of the attributes
  199. unset($data['value']);
  200. return '<textarea'.form::attributes($data, 'textarea').' '.$extra.'>'.html::specialchars($value).'</textarea>';
  201. }
  202. /**
  203. * Creates an HTML form select tag, or "dropdown menu".
  204. *
  205. * @param string|array input name or an array of HTML attributes
  206. * @param array select options, when using a name
  207. * @param string option key that should be selected by default
  208. * @param string a string to be attached to the end of the attributes
  209. * @return string
  210. */
  211. public static function dropdown($data, $options = NULL, $selected = NULL, $extra = '')
  212. {
  213. if ( ! is_array($data))
  214. {
  215. $data = array('name' => $data);
  216. }
  217. else
  218. {
  219. if (isset($data['options']))
  220. {
  221. // Use data options
  222. $options = $data['options'];
  223. }
  224. if (isset($data['selected']))
  225. {
  226. // Use data selected
  227. $selected = $data['selected'];
  228. }
  229. }
  230. // Selected value should always be a string
  231. $selected = (string) $selected;
  232. $input = '<select'.form::attributes($data, 'select').' '.$extra.'>'."\n";
  233. foreach ((array) $options as $key => $val)
  234. {
  235. // Key should always be a string
  236. $key = (string) $key;
  237. if (is_array($val))
  238. {
  239. $input .= '<optgroup label="'.$key.'">'."\n";
  240. foreach ($val as $inner_key => $inner_val)
  241. {
  242. // Inner key should always be a string
  243. $inner_key = (string) $inner_key;
  244. $sel = ($selected === $inner_key) ? ' selected="selected"' : '';
  245. $input .= '<option value="'.$inner_key.'"'.$sel.'>'.$inner_val.'</option>'."\n";
  246. }
  247. $input .= '</optgroup>'."\n";
  248. }
  249. else
  250. {
  251. $sel = ($selected === $key) ? ' selected="selected"' : '';
  252. $input .= '<option value="'.$key.'"'.$sel.'>'.$val.'</option>'."\n";
  253. }
  254. }
  255. $input .= '</select>';
  256. return $input;
  257. }
  258. /**
  259. * Creates an HTML form checkbox input tag.
  260. *
  261. * @param string|array input name or an array of HTML attributes
  262. * @param string input value, when using a name
  263. * @param boolean make the checkbox checked by default
  264. * @param string a string to be attached to the end of the attributes
  265. * @return string
  266. */
  267. public static function checkbox($data, $value = '', $checked = FALSE, $extra = '')
  268. {
  269. if ( ! is_array($data))
  270. {
  271. $data = array('name' => $data);
  272. }
  273. $data['type'] = 'checkbox';
  274. if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
  275. {
  276. $data['checked'] = 'checked';
  277. }
  278. else
  279. {
  280. unset($data['checked']);
  281. }
  282. return form::input($data, $value, $extra);
  283. }
  284. /**
  285. * Creates an HTML form radio input tag.
  286. *
  287. * @param string|array input name or an array of HTML attributes
  288. * @param string input value, when using a name
  289. * @param boolean make the radio selected by default
  290. * @param string a string to be attached to the end of the attributes
  291. * @return string
  292. */
  293. public static function radio($data = '', $value = '', $checked = FALSE, $extra = '')
  294. {
  295. if ( ! is_array($data))
  296. {
  297. $data = array('name' => $data);
  298. }
  299. $data['type'] = 'radio';
  300. if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
  301. {
  302. $data['checked'] = 'checked';
  303. }
  304. else
  305. {
  306. unset($data['checked']);
  307. }
  308. return form::input($data, $value, $extra);
  309. }
  310. /**
  311. * Creates an HTML form submit input tag.
  312. *
  313. * @param string|array input name or an array of HTML attributes
  314. * @param string input value, when using a name
  315. * @param string a string to be attached to the end of the attributes
  316. * @return string
  317. */
  318. public static function submit($data = '', $value = '', $extra = '')
  319. {
  320. if ( ! is_array($data))
  321. {
  322. $data = array('name' => $data);
  323. }
  324. if (empty($data['name']))
  325. {
  326. // Remove the name if it is empty
  327. unset($data['name']);
  328. }
  329. $data['type'] = 'submit';
  330. return form::input($data, $value, $extra);
  331. }
  332. /**
  333. * Creates an HTML form button input tag.
  334. *
  335. * @param string|array input name or an array of HTML attributes
  336. * @param string input value, when using a name
  337. * @param string a string to be attached to the end of the attributes
  338. * @return string
  339. */
  340. public static function button($data = '', $value = '', $extra = '')
  341. {
  342. if ( ! is_array($data))
  343. {
  344. $data = array('name' => $data);
  345. }
  346. if (empty($data['name']))
  347. {
  348. // Remove the name if it is empty
  349. unset($data['name']);
  350. }
  351. if (isset($data['value']) AND empty($value))
  352. {
  353. $value = arr::remove('value', $data);
  354. }
  355. return '<button'.form::attributes($data, 'button').' '.$extra.'>'.$value.'</button>';
  356. }
  357. /**
  358. * Closes an open form tag.
  359. *
  360. * @param string string to be attached after the closing tag
  361. * @return string
  362. */
  363. public static function close($extra = '')
  364. {
  365. return '</form>'."\n".$extra;
  366. }
  367. /**
  368. * Creates an HTML form label tag.
  369. *
  370. * @param string|array label "for" name or an array of HTML attributes
  371. * @param string label text or HTML
  372. * @param string a string to be attached to the end of the attributes
  373. * @return string
  374. */
  375. public static function label($data = '', $text = '', $extra = '')
  376. {
  377. if ( ! is_array($data))
  378. {
  379. if (strpos($data, '[') !== FALSE)
  380. {
  381. $data = preg_replace('/\[.*\]/', '', $data);
  382. }
  383. $data = empty($data) ? array() : array('for' => $data);
  384. }
  385. return '<label'.form::attributes($data).' '.$extra.'>'.$text.'</label>';
  386. }
  387. /**
  388. * Sorts a key/value array of HTML attributes, putting form attributes first,
  389. * and returns an attribute string.
  390. *
  391. * @param array HTML attributes array
  392. * @return string
  393. */
  394. public static function attributes($attr, $type = NULL)
  395. {
  396. if (empty($attr))
  397. return '';
  398. if (isset($attr['name']) AND empty($attr['id']) AND strpos($attr['name'], '[') === FALSE)
  399. {
  400. if ($type === NULL AND ! empty($attr['type']))
  401. {
  402. // Set the type by the attributes
  403. $type = $attr['type'];
  404. }
  405. switch ($type)
  406. {
  407. case 'text':
  408. case 'textarea':
  409. case 'password':
  410. case 'select':
  411. case 'checkbox':
  412. case 'file':
  413. case 'image':
  414. case 'button':
  415. case 'submit':
  416. // Only specific types of inputs use name to id matching
  417. $attr['id'] = $attr['name'];
  418. break;
  419. }
  420. }
  421. $order = array
  422. (
  423. 'action',
  424. 'method',
  425. 'type',
  426. 'id',
  427. 'name',
  428. 'value',
  429. 'src',
  430. 'size',
  431. 'maxlength',
  432. 'rows',
  433. 'cols',
  434. 'accept',
  435. 'tabindex',
  436. 'accesskey',
  437. 'align',
  438. 'alt',
  439. 'title',
  440. 'class',
  441. 'style',
  442. 'selected',
  443. 'checked',
  444. 'readonly',
  445. 'disabled'
  446. );
  447. $sorted = array();
  448. foreach ($order as $key)
  449. {
  450. if (isset($attr[$key]))
  451. {
  452. // Move the attribute to the sorted array
  453. $sorted[$key] = $attr[$key];
  454. // Remove the attribute from unsorted array
  455. unset($attr[$key]);
  456. }
  457. }
  458. // Combine the sorted and unsorted attributes and create an HTML string
  459. return html::attributes(array_merge($sorted, $attr));
  460. }
  461. } // End form