PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/obsolete/web_client/system/helpers/form.php

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