PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/form_helper.php

https://github.com/mikexstudios/memorize_chinese
PHP | 420 lines | 196 code | 57 blank | 167 comment | 18 complexity | dd272408f2d4e9a664349dcc4e46c63c MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Form Helpers
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author Rick Ellis
  23. * @link http://www.codeigniter.com/user_guide/helpers/form_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Form Declaration
  28. *
  29. * Creates the opening portion of the form.
  30. *
  31. * @access public
  32. * @param string the URI segments of the form destination
  33. * @param array a key/value pair of attributes
  34. * @param array a key/value pair hidden data
  35. * @return string
  36. */
  37. function form_open($action = '', $attributes = array(), $hidden = array())
  38. {
  39. $CI =& get_instance();
  40. $form = '<form action="'.$CI->config->site_url($action).'"';
  41. if ( ! isset($attributes['method']))
  42. {
  43. $form .= ' method="post"';
  44. }
  45. if (is_array($attributes) AND count($attributes) > 0)
  46. {
  47. foreach ($attributes as $key => $val)
  48. {
  49. $form .= ' '.$key.'="'.$val.'"';
  50. }
  51. }
  52. $form .= '>';
  53. if (is_array($hidden) AND count($hidden > 0))
  54. {
  55. $form .= form_hidden($hidden);
  56. }
  57. return $form;
  58. }
  59. // ------------------------------------------------------------------------
  60. /**
  61. * Form Declaration - Multipart type
  62. *
  63. * Creates the opening portion of the form, but with "multipart/form-data".
  64. *
  65. * @access public
  66. * @param string the URI segments of the form destination
  67. * @param array a key/value pair of attributes
  68. * @param array a key/value pair hidden data
  69. * @return string
  70. */
  71. function form_open_multipart($action, $attributes = array(), $hidden = array())
  72. {
  73. $attributes['enctype'] = 'multipart/form-data';
  74. return form_open($action, $attributes, $hidden);
  75. }
  76. // ------------------------------------------------------------------------
  77. /**
  78. * Hidden Input Field
  79. *
  80. * Generates hidden fields. You can pass a simple key/value string or an associative
  81. * array with multiple values.
  82. *
  83. * @access public
  84. * @param mixed
  85. * @param string
  86. * @return string
  87. */
  88. function form_hidden($name, $value = '')
  89. {
  90. if ( ! is_array($name))
  91. {
  92. return '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
  93. }
  94. $form = '';
  95. foreach ($name as $name => $value)
  96. {
  97. $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value).'" />';
  98. }
  99. return $form;
  100. }
  101. // ------------------------------------------------------------------------
  102. /**
  103. * Text Input Field
  104. *
  105. * @access public
  106. * @param mixed
  107. * @param string
  108. * @param string
  109. * @return string
  110. */
  111. function form_input($data = '', $value = '', $extra = '')
  112. {
  113. $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50');
  114. return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
  115. }
  116. // ------------------------------------------------------------------------
  117. /**
  118. * Password Field
  119. *
  120. * Identical to the input function but adds the "password" type
  121. *
  122. * @access public
  123. * @param mixed
  124. * @param string
  125. * @param string
  126. * @return string
  127. */
  128. function form_password($data = '', $value = '', $extra = '')
  129. {
  130. if ( ! is_array($data))
  131. {
  132. $data = array('name' => $data);
  133. }
  134. $data['type'] = 'password';
  135. return form_input($data, $value, $extra);
  136. }
  137. // ------------------------------------------------------------------------
  138. /**
  139. * Upload Field
  140. *
  141. * Identical to the input function but adds the "file" type
  142. *
  143. * @access public
  144. * @param mixed
  145. * @param string
  146. * @param string
  147. * @return string
  148. */
  149. function form_upload($data = '', $value = '', $extra = '')
  150. {
  151. if ( ! is_array($data))
  152. {
  153. $data = array('name' => $data);
  154. }
  155. $data['type'] = 'file';
  156. return form_input($data, $value, $extra);
  157. }
  158. // ------------------------------------------------------------------------
  159. /**
  160. * Textarea field
  161. *
  162. * @access public
  163. * @param mixed
  164. * @param string
  165. * @param string
  166. * @return string
  167. */
  168. function form_textarea($data = '', $value = '', $extra = '')
  169. {
  170. $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
  171. if ( ! is_array($data) OR ! isset($data['value']))
  172. {
  173. $val = $value;
  174. }
  175. else
  176. {
  177. $val = $data['value'];
  178. unset($data['value']); // textareas don't use the value attribute
  179. }
  180. return "<textarea ".parse_form_attributes($data, $defaults).$extra.">".$val."</textarea>\n";
  181. }
  182. // ------------------------------------------------------------------------
  183. /**
  184. * Drop-down Menu
  185. *
  186. * @access public
  187. * @param string
  188. * @param array
  189. * @param string
  190. * @param string
  191. * @return string
  192. */
  193. function form_dropdown($name = '', $options = array(), $selected = '', $extra = '')
  194. {
  195. if ($extra != '') $extra = ' '.$extra;
  196. $form = '<select name="'.$name.'"'.$extra.">\n";
  197. foreach ($options as $key => $val)
  198. {
  199. $key = (string) $key;
  200. $val = (string) $val;
  201. $sel = ($selected != $key) ? '' : ' selected="selected"';
  202. $form .= '<option value="'.$key.'"'.$sel.'>'.$val."</option>\n";
  203. }
  204. $form .= '</select>';
  205. return $form;
  206. }
  207. // ------------------------------------------------------------------------
  208. /**
  209. * Checkbox Field
  210. *
  211. * @access public
  212. * @param mixed
  213. * @param string
  214. * @param bool
  215. * @param string
  216. * @return string
  217. */
  218. function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
  219. {
  220. $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
  221. if (is_array($data) AND array_key_exists('checked', $data))
  222. {
  223. $checked = $data['checked'];
  224. if ($checked == FALSE)
  225. {
  226. unset($data['checked']);
  227. }
  228. else
  229. {
  230. $data['checked'] = 'checked';
  231. }
  232. }
  233. if ($checked == TRUE)
  234. $defaults['checked'] = 'checked';
  235. else
  236. unset($defaults['checked']);
  237. return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
  238. }
  239. // ------------------------------------------------------------------------
  240. /**
  241. * Radio Button
  242. *
  243. * @access public
  244. * @param mixed
  245. * @param string
  246. * @param bool
  247. * @param string
  248. * @return string
  249. */
  250. function form_radio($data = '', $value = '', $checked = TRUE, $extra = '')
  251. {
  252. if ( ! is_array($data))
  253. {
  254. $data = array('name' => $data);
  255. }
  256. $data['type'] = 'radio';
  257. return form_checkbox($data, $value, $checked, $extra);
  258. }
  259. // ------------------------------------------------------------------------
  260. /**
  261. * Submit Button
  262. *
  263. * @access public
  264. * @param mixed
  265. * @param string
  266. * @param string
  267. * @return string
  268. */
  269. function form_submit($data = '', $value = '', $extra = '')
  270. {
  271. $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
  272. return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
  273. }
  274. // ------------------------------------------------------------------------
  275. /**
  276. * Form Close Tag
  277. *
  278. * @access public
  279. * @param string
  280. * @return string
  281. */
  282. function form_close($extra = '')
  283. {
  284. return "</form>\n".$extra;
  285. }
  286. // ------------------------------------------------------------------------
  287. /**
  288. * Form Prep
  289. *
  290. * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
  291. *
  292. * @access public
  293. * @param string
  294. * @return string
  295. */
  296. function form_prep($str = '')
  297. {
  298. if ($str === '')
  299. {
  300. return '';
  301. }
  302. $temp = '__TEMP_AMPERSANDS__';
  303. // Replace entities to temporary markers so that
  304. // htmlspecialchars won't mess them up
  305. $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
  306. $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
  307. $str = htmlspecialchars($str);
  308. // In case htmlspecialchars misses these.
  309. $str = str_replace(array("'", '"'), array("&#39;", "&quot;"), $str);
  310. // Decode the temp markers back to entities
  311. $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
  312. $str = preg_replace("/$temp(\w+);/","&\\1;",$str);
  313. return $str;
  314. }
  315. // ------------------------------------------------------------------------
  316. /**
  317. * Parse the form attributes
  318. *
  319. * Helper function used by some of the form helpers
  320. *
  321. * @access private
  322. * @param array
  323. * @param array
  324. * @return string
  325. */
  326. function parse_form_attributes($attributes, $default)
  327. {
  328. if (is_array($attributes))
  329. {
  330. foreach ($default as $key => $val)
  331. {
  332. if (isset($attributes[$key]))
  333. {
  334. $default[$key] = $attributes[$key];
  335. unset($attributes[$key]);
  336. }
  337. }
  338. if (count($attributes) > 0)
  339. {
  340. $default = array_merge($default, $attributes);
  341. }
  342. }
  343. $att = '';
  344. foreach ($default as $key => $val)
  345. {
  346. if ($key == 'value')
  347. {
  348. $val = form_prep($val);
  349. }
  350. $att .= $key . '="' . $val . '" ';
  351. }
  352. return $att;
  353. }
  354. ?>