FormHelper.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004 David Heinemeier Hansson
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * FormHelper.
  12. *
  13. * @package symfony
  14. * @subpackage helper
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author David Heinemeier Hansson
  17. * @version SVN: $Id: FormHelper.php 14152 2008-12-17 22:39:33Z FabianLange $
  18. */
  19. /**
  20. * Returns a formatted set of <option> tags based on optional <i>$options</i> array variable.
  21. *
  22. * The options_for_select helper is usually called in conjunction with the select_tag helper, as it is relatively
  23. * useless on its own. By passing an array of <i>$options</i>, the helper will automatically generate <option> tags
  24. * using the array key as the value and the array value as the display title. Additionally the options_for_select tag is
  25. * smart enough to detect nested arrays as <optgroup> tags. If the helper detects that the array value is an array itself,
  26. * it creates an <optgroup> tag with the name of the group being the key and the contents of the <optgroup> being the array.
  27. *
  28. * <b>Options:</b>
  29. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value
  30. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value
  31. *
  32. * <b>Examples:</b>
  33. * <code>
  34. * echo select_tag('person', options_for_select(array(1 => 'Larry', 2 => 'Moe', 3 => 'Curly')));
  35. * </code>
  36. *
  37. * <code>
  38. * $card_list = array('VISA' => 'Visa', 'MAST' => 'MasterCard', 'AMEX' => 'American Express', 'DISC' => 'Discover');
  39. * echo select_tag('cc_type', options_for_select($card_list, 'AMEX', array('include_custom' => '-- Select Credit Card Type --')));
  40. * </code>
  41. *
  42. * <code>
  43. * $optgroup_array = array(1 => 'Joe', 2 => 'Sue', 'Group A' => array(3 => 'Mary', 4 => 'Tom'), 'Group B' => array(5 => 'Bill', 6 =>'Andy'));
  44. * echo select_tag('employee', options_for_select($optgroup_array, null, array('include_blank' => true)), array('class' => 'mystyle'));
  45. * </code>
  46. *
  47. * @param array $options dataset to create <option> tags and <optgroup> tags from
  48. * @param string $selected selected option value
  49. * @param array $html_options additional HTML compliant <option> tag parameters
  50. *
  51. * @return string populated with <option> tags derived from the <i>$options</i> array variable
  52. * @see select_tag
  53. */
  54. function options_for_select($options = array(), $selected = '', $html_options = array())
  55. {
  56. $html_options = _parse_attributes($html_options);
  57. if (is_array($selected))
  58. {
  59. $selected = array_map('strval', array_values($selected));
  60. }
  61. $html = '';
  62. if ($value = _get_option($html_options, 'include_custom'))
  63. {
  64. $html .= content_tag('option', $value, array('value' => ''))."\n";
  65. }
  66. else if (_get_option($html_options, 'include_blank'))
  67. {
  68. $html .= content_tag('option', '', array('value' => ''))."\n";
  69. }
  70. foreach ($options as $key => $value)
  71. {
  72. if (is_array($value) || $value instanceof sfOutputEscaperArrayDecorator)
  73. {
  74. $html .= content_tag('optgroup', options_for_select($value, $selected, $html_options), array('label' => $key))."\n";
  75. }
  76. else
  77. {
  78. $option_options = array('value' => $key);
  79. if (
  80. (is_array($selected) && in_array(strval($key), $selected, true))
  81. ||
  82. (strval($key) == strval($selected))
  83. )
  84. {
  85. $option_options['selected'] = 'selected';
  86. }
  87. $html .= content_tag('option', $value, $option_options)."\n";
  88. }
  89. }
  90. return $html;
  91. }
  92. /**
  93. * Returns an HTML <form> tag that points to a valid action, route or URL as defined by <i>$url_for_options</i>.
  94. *
  95. * By default, the form tag is generated in POST format, but can easily be configured along with any additional
  96. * HTML parameters via the optional <i>$options</i> parameter. If you are using file uploads, be sure to set the
  97. * <i>multipart</i> option to true.
  98. *
  99. * <b>Options:</b>
  100. * - multipart - When set to true, enctype is set to "multipart/form-data".
  101. *
  102. * <b>Examples:</b>
  103. * <code><?php echo form_tag('@myroute'); ?></code>
  104. * <code><?php echo form_tag('/module/action', array('name' => 'myformname', 'multipart' => true)); ?></code>
  105. *
  106. * @param string $url_for_options valid action, route or URL
  107. * @param array $options optional HTML parameters for the <form> tag
  108. *
  109. * @return string opening HTML <form> tag with options
  110. */
  111. function form_tag($url_for_options = '', $options = array())
  112. {
  113. $options = _parse_attributes($options);
  114. $html_options = $options;
  115. if (!isset($html_options['method']))
  116. {
  117. $html_options['method'] = 'post';
  118. }
  119. if (_get_option($html_options, 'multipart'))
  120. {
  121. $html_options['enctype'] = 'multipart/form-data';
  122. }
  123. $html_options['action'] = url_for($url_for_options);
  124. return tag('form', $html_options, true);
  125. }
  126. /**
  127. * Returns a <select> tag, optionally comprised of <option> tags.
  128. *
  129. * The select tag does not generate <option> tags by default.
  130. * To do so, you must populate the <i>$option_tags</i> parameter with a string of valid HTML compliant <option> tags.
  131. * Fortunately, Symfony provides a handy helper function to convert an array of data into option tags (see options_for_select).
  132. * If you need to create a "multiple" select tag (ability to select multiple options), set the <i>multiple</i> option to true.
  133. * Doing so will automatically convert the name field to an array type variable (i.e. name="name" becomes name="name[]").
  134. *
  135. * <b>Options:</b>
  136. * - multiple - If set to true, the select tag will allow multiple options to be selected at once.
  137. *
  138. * <b>Examples:</b>
  139. * <code>
  140. * $person_list = array(1 => 'Larry', 2 => 'Moe', 3 => 'Curly');
  141. * echo select_tag('person', options_for_select($person_list, $sf_params->get('person')), array('class' => 'full'));
  142. * </code>
  143. *
  144. * <code>
  145. * echo select_tag('department', options_for_select($department_list), array('multiple' => true));
  146. * </code>
  147. *
  148. * <code>
  149. * echo select_tag('url', options_for_select($url_list), array('onChange' => 'Javascript:this.form.submit();'));
  150. * </code>
  151. *
  152. * @param string $name field name
  153. * @param mixed $option_tags contains a string of valid <option></option> tags, or an array of options that will be passed to options_for_select
  154. * @param array $options additional HTML compliant <select> tag parameters
  155. *
  156. * @return string <select> tag optionally comprised of <option> tags.
  157. * @see options_for_select, content_tag
  158. */
  159. function select_tag($name, $option_tags = null, $options = array())
  160. {
  161. $options = _convert_options($options);
  162. $id = $name;
  163. if (isset($options['multiple']) && $options['multiple'] && substr($name, -2) !== '[]')
  164. {
  165. $name .= '[]';
  166. }
  167. if (is_array($option_tags))
  168. {
  169. $option_tags = options_for_select($option_tags);
  170. }
  171. return content_tag('select', $option_tags, array_merge(array('name' => $name, 'id' => get_id_from_name($id)), $options));
  172. }
  173. /**
  174. * Returns a <select> tag populated with all the countries in the world.
  175. *
  176. * The select_country_tag builds off the traditional select_tag function, and is conveniently populated with
  177. * all the countries in the world (sorted alphabetically). Each option in the list has a two-character country
  178. * code for its value and the country's name as its display title. The country data is retrieved via the sfCultureInfo
  179. * class, which stores a wide variety of i18n and i10n settings for various countries and cultures throughout the world.
  180. * Here's an example of an <option> tag generated by the select_country_tag:
  181. *
  182. * <samp>
  183. * <option value="US">United States</option>
  184. * </samp>
  185. *
  186. * <b>Examples:</b>
  187. * <code>
  188. * echo select_country_tag('country', 'FR');
  189. * </code>
  190. * <code>
  191. * echo select_country_tag('country', 'de', array('countries' => array('US','FR')));
  192. * </code>
  193. *
  194. * @param string $name field name
  195. * @param string $selected selected field value (two-character country code)
  196. * @param array $options additional HTML compliant <select> tag parameters
  197. *
  198. * @return string <select> tag populated with all the countries in the world.
  199. * @see select_tag, options_for_select, sfCultureInfo
  200. */
  201. function select_country_tag($name, $selected = null, $options = array())
  202. {
  203. $c = sfCultureInfo::getInstance(sfContext::getInstance()->getUser()->getCulture());
  204. $countries = $c->getCountries();
  205. if ($country_option = _get_option($options, 'countries'))
  206. {
  207. foreach ($countries as $key => $value)
  208. {
  209. if (!in_array($key, $country_option))
  210. {
  211. unset($countries[$key]);
  212. }
  213. }
  214. }
  215. asort($countries);
  216. $option_tags = options_for_select($countries, $selected, $options);
  217. unset($options['include_blank'], $options['include_custom']);
  218. return select_tag($name, $option_tags, $options);
  219. }
  220. /**
  221. * Returns a <select> tag populated with all the languages in the world (or almost).
  222. *
  223. * The select_language_tag builds off the traditional select_tag function, and is conveniently populated with
  224. * all the languages in the world (sorted alphabetically). Each option in the list has a two or three character
  225. * language/culture code for its value and the language's name as its display title. The country data is
  226. * retrieved via the sfCultureInfo class, which stores a wide variety of i18n and i10n settings for various
  227. * countries and cultures throughout the world. Here's an example of an <option> tag generated by the select_language_tag:
  228. *
  229. * <samp>
  230. * <option value="en">English</option>
  231. * </samp>
  232. *
  233. * <b>Examples:</b>
  234. * <code>
  235. * echo select_language_tag('language', 'de');
  236. * </code>
  237. * <code>
  238. * echo select_language_tag('language', 'de', array('languages' => array('en','fr','fi')));
  239. * </code>
  240. *
  241. * @param string $name field name
  242. * @param string $selected selected field value (two or threecharacter language/culture code)
  243. * @param array $options additional HTML compliant <select> tag parameters
  244. *
  245. * @return string <select> tag populated with all the languages in the world.
  246. * @see select_tag, options_for_select, sfCultureInfo
  247. */
  248. function select_language_tag($name, $selected = null, $options = array())
  249. {
  250. $c = sfCultureInfo::getInstance(sfContext::getInstance()->getUser()->getCulture());
  251. $languages = $c->getLanguages();
  252. if ($language_option = _get_option($options, 'languages'))
  253. {
  254. foreach ($languages as $key => $value)
  255. {
  256. if (!in_array($key, $language_option))
  257. {
  258. unset($languages[$key]);
  259. }
  260. }
  261. }
  262. asort($languages);
  263. $option_tags = options_for_select($languages, $selected, $options);
  264. unset($options['include_blank'], $options['include_custom']);
  265. return select_tag($name, $option_tags, $options);
  266. }
  267. /**
  268. * Returns a <select> tag populated with all the currencies in the world (or almost).
  269. *
  270. * The select_currency_tag builds off the traditional select_tag function, and is conveniently populated with
  271. * all the currencies in the world (sorted alphabetically). Each option in the list has a three character
  272. * currency code for its value and the currency's name as its display title. The currency data is
  273. * retrieved via the sfCultureInfo class, which stores a wide variety of i18n and i10n settings for various
  274. * countries and cultures throughout the world. Here's an example of an <option> tag generated by the select_currency_tag:
  275. *
  276. * <samp>
  277. * <option value="EUR">Euro</option>
  278. * </samp>
  279. *
  280. * <b>Examples:</b>
  281. * <code>
  282. * echo select_currency_tag('currency', 'EUR');
  283. * </code>
  284. * <code>
  285. * echo select_currency_tag('currency', 'EUR', array('currencies' => array('EUR', 'USD'), 'display' => 'symbol'));
  286. * </code>
  287. *
  288. * @param string $name field name
  289. * @param string $selected selected field value (threecharacter currency code)
  290. * @param array $options additional HTML compliant <select> tag parameters
  291. *
  292. * @return string <select> tag populated with all the currencies in the world.
  293. * @see select_tag, options_for_select, sfCultureInfo
  294. */
  295. function select_currency_tag($name, $selected = null, $options = array())
  296. {
  297. $c = sfCultureInfo::getInstance(sfContext::getInstance()->getUser()->getCulture());
  298. $currencies = $c->getCurrencies();
  299. $currency_option = _get_option($options, 'currencies');
  300. $display_option = _get_option($options, 'display');
  301. foreach ($currencies as $key => $value)
  302. {
  303. if ($currency_option && !in_array($key, $currency_option))
  304. {
  305. unset($currencies[$key]);
  306. }
  307. else
  308. {
  309. switch ($display_option)
  310. {
  311. case 'symbol' : $currencies[$key] = $value[0]; break;
  312. case 'code' : $currencies[$key] = $key; break;
  313. default : $currencies[$key] = ucfirst($value[1]); break;
  314. }
  315. }
  316. }
  317. asort($currencies);
  318. $option_tags = options_for_select($currencies, $selected, $options);
  319. unset($options['include_blank'], $options['include_custom']);
  320. return select_tag($name, $option_tags, $options);
  321. }
  322. /**
  323. * Returns an XHTML compliant <input> tag with type="text".
  324. *
  325. * The input_tag helper generates your basic XHTML <input> tag and can utilize any standard <input> tag parameters
  326. * passed in the optional <i>$options</i> parameter.
  327. *
  328. * <b>Examples:</b>
  329. * <code>
  330. * echo input_tag('name');
  331. * </code>
  332. *
  333. * <code>
  334. * echo input_tag('amount', $sf_params->get('amount'), array('size' => 8, 'maxlength' => 8));
  335. * </code>
  336. *
  337. * @param string $name field name
  338. * @param string $value selected field value
  339. * @param array $options additional HTML compliant <input> tag parameters
  340. *
  341. * @return string XHTML compliant <input> tag with type="text"
  342. */
  343. function input_tag($name, $value = null, $options = array())
  344. {
  345. return tag('input', array_merge(array('type' => 'text', 'name' => $name, 'id' => get_id_from_name($name, $value), 'value' => $value), _convert_options($options)));
  346. }
  347. /**
  348. * Returns an XHTML compliant <input> tag with type="hidden".
  349. *
  350. * Similar to the input_tag helper, the input_hidden_tag helper generates an XHTML <input> tag and can utilize
  351. * any standard <input> tag parameters passed in the optional <i>$options</i> parameter. The only difference is
  352. * that it creates the tag with type="hidden", meaning that is not visible on the page.
  353. *
  354. * <b>Examples:</b>
  355. * <code>
  356. * echo input_hidden_tag('id', $id);
  357. * </code>
  358. *
  359. * @param string $name field name
  360. * @param string $value populated field value
  361. * @param array $options additional HTML compliant <input> tag parameters
  362. *
  363. * @return string XHTML compliant <input> tag with type="hidden"
  364. */
  365. function input_hidden_tag($name, $value = null, $options = array())
  366. {
  367. $options = _parse_attributes($options);
  368. $options['type'] = 'hidden';
  369. return input_tag($name, $value, $options);
  370. }
  371. /**
  372. * Returns an XHTML compliant <input> tag with type="file".
  373. *
  374. * Similar to the input_tag helper, the input_hidden_tag helper generates your basic XHTML <input> tag and can utilize
  375. * any standard <input> tag parameters passed in the optional <i>$options</i> parameter. The only difference is that it
  376. * creates the tag with type="file", meaning that next to the field will be a "browse" (or similar) button.
  377. * This gives the user the ability to choose a file from there computer to upload to the web server. Remember, if you
  378. * plan to upload files to your website, be sure to set the <i>multipart</i> option form_tag helper function to true
  379. * or your files will not be properly uploaded to the web server.
  380. *
  381. * <b>Examples:</b>
  382. * <code>
  383. * echo input_file_tag('filename', array('size' => 30));
  384. * </code>
  385. *
  386. * @param string $name field name
  387. * @param array $options additional HTML compliant <input> tag parameters
  388. *
  389. * @return string XHTML compliant <input> tag with type="file"
  390. * @see input_tag, form_tag
  391. */
  392. function input_file_tag($name, $options = array())
  393. {
  394. $options = _parse_attributes($options);
  395. $options['type'] = 'file';
  396. return input_tag($name, null, $options);
  397. }
  398. /**
  399. * Returns an XHTML compliant <input> tag with type="password".
  400. *
  401. * Similar to the input_tag helper, the input_hidden_tag helper generates your basic XHTML <input> tag and can utilize
  402. * any standard <input> tag parameters passed in the optional <i>$options</i> parameter. The only difference is that it
  403. * creates the tag with type="password", meaning that the text entered into this field will not be visible to the end user.
  404. * In most cases it is replaced by * * * * * * * *. Even though this text is not readable, it is recommended that you do not
  405. * populate the optional <i>$value</i> option with a plain-text password or any other sensitive information, as this is a
  406. * potential security risk.
  407. *
  408. * <b>Examples:</b>
  409. * <code>
  410. * echo input_password_tag('password');
  411. * echo input_password_tag('password_confirm');
  412. * </code>
  413. *
  414. * @param string $name field name
  415. * @param string $value populated field value
  416. * @param array $options additional HTML compliant <input> tag parameters
  417. *
  418. * @return string XHTML compliant <input> tag with type="password"
  419. * @see input_tag
  420. */
  421. function input_password_tag($name = 'password', $value = null, $options = array())
  422. {
  423. $options = _parse_attributes($options);
  424. $options['type'] = 'password';
  425. return input_tag($name, $value, $options);
  426. }
  427. /**
  428. * Returns a <textarea> tag, optionally wrapped with an inline rich-text JavaScript editor.
  429. *
  430. * The texarea_tag helper generates a standard HTML <textarea> tag and can be manipulated with
  431. * any number of standard HTML parameters via the <i>$options</i> array variable. However, the
  432. * textarea tag also has the unique capability of being transformed into a WYSIWYG rich-text editor
  433. * such as TinyMCE (http://tinymce.moxiecode.com) very easily with the use of some specific options:
  434. *
  435. * <b>Options:</b>
  436. * - rich: A rich text editor class (for example sfRichTextEditorTinyMCE for TinyMCE).
  437. *
  438. * <b>Examples:</b>
  439. * <code>
  440. * echo textarea_tag('notes');
  441. * </code>
  442. *
  443. * <code>
  444. * echo textarea_tag('description', 'This is a description', array('rows' => 10, 'cols' => 50));
  445. * </code>
  446. *
  447. * @param string $name field name
  448. * @param string $content populated field value
  449. * @param array $options additional HTML compliant <textarea> tag parameters
  450. *
  451. * @return string <textarea> tag optionally wrapped with a rich-text WYSIWYG editor
  452. */
  453. function textarea_tag($name, $content = null, $options = array())
  454. {
  455. $options = _parse_attributes($options);
  456. if ($size = _get_option($options, 'size'))
  457. {
  458. list($options['cols'], $options['rows']) = split('x', $size, 2);
  459. }
  460. // rich control?
  461. if ($rich = _get_option($options, 'rich', false))
  462. {
  463. if (true === $rich)
  464. {
  465. $rich = sfConfig::get('sf_rich_text_editor_class', 'TinyMCE');
  466. }
  467. // switch for backward compatibility
  468. switch ($rich)
  469. {
  470. case 'tinymce':
  471. $rich = 'TinyMCE';
  472. break;
  473. case 'fck':
  474. $rich = 'FCK';
  475. break;
  476. }
  477. $editorClass = 'sfRichTextEditor'.$rich;
  478. if (!class_exists($editorClass))
  479. {
  480. throw new sfConfigurationException(sprintf('The rich text editor "%s" does not exist.', $editorClass));
  481. }
  482. $sfEditor = new $editorClass();
  483. if (!in_array('sfRichTextEditor', class_parents($sfEditor)))
  484. {
  485. throw new sfConfigurationException(sprintf('The editor "%s" must extend sfRichTextEditor.', $editorClass));
  486. }
  487. $sfEditor->initialize($name, $content, $options);
  488. return $sfEditor->toHTML();
  489. }
  490. return content_tag('textarea', escape_once((is_object($content)) ? $content->__toString() : $content), array_merge(array('name' => $name, 'id' => get_id_from_name(_get_option($options, 'id', $name), null)), _convert_options($options)));
  491. }
  492. /**
  493. * Returns an XHTML compliant <input> tag with type="checkbox".
  494. *
  495. * When creating multiple checkboxes with the same name, be sure to use an array for the
  496. * <i>$name</i> parameter (i.e. 'name[]'). The checkbox_tag is smart enough to create unique ID's
  497. * based on the <i>$value</i> parameter like so:
  498. *
  499. * <samp>
  500. * <input type="checkbox" name="status[]" id="status_3" value="3" />
  501. * <input type="checkbox" name="status[]" id="status_4" value="4" />
  502. * </samp>
  503. *
  504. * <b>Examples:</b>
  505. * <code>
  506. * echo checkbox_tag('newsletter', 1, $sf_params->get('newsletter'));
  507. * </code>
  508. *
  509. * <code>
  510. * echo checkbox_tag('option_a', 'yes', true, array('class' => 'style_a'));
  511. * </code>
  512. *
  513. * <code>
  514. * // one request variable with an array of checkbox values
  515. * echo checkbox_tag('choice[]', 1);
  516. * echo checkbox_tag('choice[]', 2);
  517. * echo checkbox_tag('choice[]', 3);
  518. * echo checkbox_tag('choice[]', 4);
  519. * </code>
  520. *
  521. * <code>
  522. * // assuming you have Prototype.js enabled, you could do this
  523. * echo checkbox_tag('show_tos', 1, false, array('onclick' => "Element.toggle('tos'); return false;"));
  524. * </code>
  525. *
  526. * @param string $name field name
  527. * @param string $value checkbox value (if checked)
  528. * @param bool $checked is the checkbox checked? (1 or 0)
  529. * @param array $options additional HTML compliant <input> tag parameters
  530. *
  531. * @return string XHTML compliant <input> tag with type="checkbox"
  532. */
  533. function checkbox_tag($name, $value = '1', $checked = false, $options = array())
  534. {
  535. $html_options = array_merge(array('type' => 'checkbox', 'name' => $name, 'id' => get_id_from_name($name, $value), 'value' => $value), _convert_options($options));
  536. if ($checked)
  537. {
  538. $html_options['checked'] = 'checked';
  539. }
  540. return tag('input', $html_options);
  541. }
  542. /**
  543. * Returns an XHTML compliant <input> tag with type="radio".
  544. *
  545. * <b>Examples:</b>
  546. * <code>
  547. * echo ' Yes '.radiobutton_tag('newsletter', 1);
  548. * echo ' No '.radiobutton_tag('newsletter', 0);
  549. * </code>
  550. *
  551. * @param string $name field name
  552. * @param string $value radio button value (if selected)
  553. * @param bool $checked is the radio button selected? (1 or 0)
  554. * @param array $options additional HTML compliant <input> tag parameters
  555. *
  556. * @return string XHTML compliant <input> tag with type="radio"
  557. */
  558. function radiobutton_tag($name, $value, $checked = false, $options = array())
  559. {
  560. $html_options = array_merge(array('type' => 'radio', 'name' => $name, 'id' => get_id_from_name($name.'[]', $value), 'value' => $value), _convert_options($options));
  561. if ($checked)
  562. {
  563. $html_options['checked'] = 'checked';
  564. }
  565. return tag('input', $html_options);
  566. }
  567. /**
  568. * Returns two XHTML compliant <input> tags to be used as a free-text date fields for a date range.
  569. *
  570. * Built on the input_date_tag, the input_date_range_tag combines two input tags that allow the user
  571. * to specify a from and to date.
  572. * You can easily implement a JavaScript calendar by enabling the 'rich' option in the
  573. * <i>$options</i> parameter. This includes a button next to the field that when clicked,
  574. * will open an inline JavaScript calendar. When a date is selected, it will automatically
  575. * populate the <input> tag with the proper date, formatted to the user's culture setting.
  576. *
  577. * <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names.
  578. * For example, a <i>$name</i> of "date" becomes date[from] and date[to]
  579. *
  580. * <b>Options:</b>
  581. * - rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date
  582. * - before - string to be displayed before the input_date_range_tag
  583. * - middle - string to be displayed between the from and to tags
  584. * - after - string to be displayed after the input_date_range_tag
  585. *
  586. * <b>Examples:</b>
  587. * <code>
  588. * $date = array('from' => '2006-05-15', 'to' => '2006-06-15');
  589. * echo input_date_range_tag('date', $date, array('rich' => true));
  590. * </code>
  591. *
  592. * <code>
  593. * echo input_date_range_tag('date', null, array('middle' => ' through ', 'rich' => true));
  594. * </code>
  595. *
  596. * @param string $name field name
  597. * @param array $value dates: $value['from'] and $value['to']
  598. * @param array $options additional HTML compliant <input> tag parameters
  599. *
  600. * @return string XHTML compliant <input> tag with optional JS calendar integration
  601. * @see input_date_tag
  602. */
  603. function input_date_range_tag($name, $value, $options = array())
  604. {
  605. $options = _parse_attributes($options);
  606. $before = _get_option($options, 'before', '');
  607. $middle = _get_option($options, 'middle', '');
  608. $after = _get_option($options, 'after', '');
  609. return $before.
  610. input_date_tag($name.'[from]', isset($value['from']) ? $value['from'] : null, $options).
  611. $middle.
  612. input_date_tag($name.'[to]', isset($value['to']) ? $value['to'] : null, $options).
  613. $after;
  614. }
  615. /**
  616. * Returns an XHTML compliant <input> tag to be used as a free-text date field.
  617. *
  618. * You can easily implement a JavaScript calendar by enabling the 'rich' option in the
  619. * <i>$options</i> parameter. This includes a button next to the field that when clicked,
  620. * will open an inline JavaScript calendar. When a date is selected, it will automatically
  621. * populate the <input> tag with the proper date, formatted to the user's culture setting.
  622. * Symfony also conveniently offers the input_date_range_tag, that allows you to specify a to
  623. * and from date.
  624. *
  625. * <b>Options:</b>
  626. * - rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date
  627. *
  628. * <b>Examples:</b>
  629. * <code>
  630. * echo input_date_tag('date', null, array('rich' => true));
  631. * </code>
  632. *
  633. * @param string $name field name
  634. * @param string $value date
  635. * @param array $options additional HTML compliant <input> tag parameters
  636. *
  637. * @return string XHTML compliant <input> tag with optional JS calendar integration
  638. * @see input_date_range_tag
  639. */
  640. function input_date_tag($name, $value = null, $options = array())
  641. {
  642. $options = _parse_attributes($options);
  643. $context = sfContext::getInstance();
  644. $culture = _get_option($options, 'culture', $context->getUser()->getCulture());
  645. $withTime = _get_option($options, 'withtime', false);
  646. // rich control?
  647. if (!_get_option($options, 'rich', false))
  648. {
  649. use_helper('DateForm');
  650. // set culture for month tag
  651. $options['culture'] = $culture;
  652. if ($withTime)
  653. {
  654. return select_datetime_tag($name, $value, $options, isset($options['html']) ? $options['html'] : array());
  655. }
  656. else
  657. {
  658. return select_date_tag($name, $value, $options, isset($options['html']) ? $options['html'] : array());
  659. }
  660. }
  661. $pattern = _get_option($options, 'format', $withTime ? 'g' : 'd');
  662. $dateFormat = new sfDateFormat($culture);
  663. $pattern = $dateFormat->getInputPattern($pattern);
  664. // parse date
  665. if ($value === null || $value === '')
  666. {
  667. $value = '';
  668. }
  669. else
  670. {
  671. $value = $dateFormat->format($value, $pattern);
  672. }
  673. // register our javascripts and stylesheets
  674. $langFile = sfConfig::get('sf_calendar_web_dir').'/lang/calendar-'.$culture;
  675. if((!is_readable(sfConfig::get('sf_web_dir').'/'.$langFile.'.js')) &&
  676. (!is_readable(sfConfig::get('sf_symfony_lib_dir').'/../data/web/'.$langFile.'.js')) &&
  677. (!is_readable(sfConfig::get('sf_symfony_lib_dir').'/../data/symfony/web/'.$langFile.'.js')))
  678. {
  679. $langFile = sfConfig::get('sf_calendar_web_dir').'/lang/calendar-'.substr($culture,0,2);
  680. if((!is_readable(sfConfig::get('sf_web_dir').'/'.$langFile.'.js')) &&
  681. (!is_readable(sfConfig::get('sf_symfony_lib_dir').'/../data/web/'.$langFile.'.js')) &&
  682. (!is_readable(sfConfig::get('sf_symfony_lib_dir').'/../data/symfony/web/'.$langFile.'.js')))
  683. {
  684. $langFile = sfConfig::get('sf_calendar_web_dir').'/lang/calendar-en';
  685. }
  686. }
  687. $jss = array(
  688. sfConfig::get('sf_calendar_web_dir').'/calendar',
  689. $langFile,
  690. sfConfig::get('sf_calendar_web_dir').'/calendar-setup',
  691. );
  692. foreach ($jss as $js)
  693. {
  694. $context->getResponse()->addJavascript($js);
  695. }
  696. // css
  697. if ($calendar_style = _get_option($options, 'css', 'skins/aqua/theme'))
  698. {
  699. $context->getResponse()->addStylesheet(sfConfig::get('sf_calendar_web_dir').'/'.$calendar_style);
  700. }
  701. // date format
  702. $date_format = $dateFormat->getPattern($pattern);
  703. // calendar date format
  704. $calendar_date_format = $date_format;
  705. $calendar_date_format = strtr($date_format, array('yyyy' => 'Y', 'yy'=>'y', 'MM' => 'm', 'M'=>'m', 'dd'=>'d', 'd'=>'e', 'HH'=>'H', 'H'=>'k', 'hh'=>'I', 'h'=>'l', 'mm'=>'M', 'ss'=>'S', 'a'=>'p'));
  706. $calendar_date_format = preg_replace('/([mdyhklspe])+/i', '%\\1', $calendar_date_format);
  707. $id_inputField = isset($options['id']) ? $options['id'] : get_id_from_name($name);
  708. $id_calendarButton = 'trigger_'.$id_inputField;
  709. $js = '
  710. document.getElementById("'.$id_calendarButton.'").disabled = false;
  711. Calendar.setup({
  712. inputField : "'.$id_inputField.'",
  713. ifFormat : "'.$calendar_date_format.'",
  714. daFormat : "'.$calendar_date_format.'",
  715. button : "'.$id_calendarButton.'"';
  716. if ($withTime)
  717. {
  718. $js .= ",\n showsTime : true";
  719. }
  720. // calendar options
  721. if ($calendar_options = _get_option($options, 'calendar_options'))
  722. {
  723. $js .= ",\n".$calendar_options;
  724. }
  725. $js .= '
  726. });
  727. ';
  728. // calendar button
  729. $calendar_button = '...';
  730. $calendar_button_type = 'txt';
  731. if ($calendar_button_img = _get_option($options, 'calendar_button_img'))
  732. {
  733. $calendar_button = $calendar_button_img;
  734. $calendar_button_type = 'img';
  735. }
  736. else if ($calendar_button_txt = _get_option($options, 'calendar_button_txt'))
  737. {
  738. $calendar_button = $calendar_button_txt;
  739. $calendar_button_type = 'txt';
  740. }
  741. // construct html
  742. if (!isset($options['size']))
  743. {
  744. // educated guess about the size
  745. $options['size'] = strlen($date_format)+2;
  746. }
  747. $html = input_tag($name, $value, $options);
  748. if ($calendar_button_type == 'img')
  749. {
  750. $html .= image_tag($calendar_button, array('id' => $id_calendarButton, 'style' => 'cursor: pointer; vertical-align: middle'));
  751. }
  752. else
  753. {
  754. $html .= content_tag('button', $calendar_button, array('type' => 'button', 'disabled' => 'disabled', 'onclick' => 'return false', 'id' => $id_calendarButton));
  755. }
  756. if (_get_option($options, 'with_format'))
  757. {
  758. $html .= '('.$date_format.')';
  759. }
  760. // add javascript
  761. $html .= content_tag('script', $js, array('type' => 'text/javascript'));
  762. return $html;
  763. }
  764. /**
  765. * Returns an XHTML compliant <input> tag with type="submit".
  766. *
  767. * By default, this helper creates a submit tag with a name of <em>commit</em> to avoid
  768. * conflicts with other parts of the framework. It is recommended that you do not use the name
  769. * "submit" for submit tags unless absolutely necessary. Also, the default <i>$value</i> parameter
  770. * (title of the button) is set to "Save changes", which can be easily overwritten by passing a
  771. * <i>$value</i> parameter.
  772. *
  773. * <b>Examples:</b>
  774. * <code>
  775. * echo submit_tag();
  776. * </code>
  777. *
  778. * <code>
  779. * echo submit_tag('Update Record');
  780. * </code>
  781. *
  782. * @param string $name field value (title of submit button)
  783. * @param array $options additional HTML compliant <input> tag parameters
  784. *
  785. * @return string XHTML compliant <input> tag with type="submit"
  786. */
  787. function submit_tag($value = 'Save changes', $options = array())
  788. {
  789. return tag('input', array_merge(array('type' => 'submit', 'name' => 'commit', 'value' => $value), _convert_options_to_javascript(_convert_options($options))));
  790. }
  791. /**
  792. * Returns an XHTML compliant <input> tag with type="reset".
  793. *
  794. * By default, this helper creates a submit tag with a name of <em>reset</em>. Also, the default
  795. * <i>$value</i> parameter (title of the button) is set to "Reset" which can be easily overwritten
  796. * by passing a <i>$value</i> parameter.
  797. *
  798. * <b>Examples:</b>
  799. * <code>
  800. * echo reset_tag();
  801. * </code>
  802. *
  803. * <code>
  804. * echo reset_tag('Start Over');
  805. * </code>
  806. *
  807. * @param string $name field value (title of reset button)
  808. * @param array $options additional HTML compliant <input> tag parameters
  809. *
  810. * @return string XHTML compliant <input> tag with type="reset"
  811. */
  812. function reset_tag($value = 'Reset', $options = array())
  813. {
  814. return tag('input', array_merge(array('type' => 'reset', 'name' => 'reset', 'value' => $value), _convert_options($options)));
  815. }
  816. /**
  817. * Returns an XHTML compliant <input> tag with type="image".
  818. *
  819. * The submit_image_tag is very similar to the submit_tag, the only difference being that it uses an image
  820. * for the submit button instead of the browser-generated default button. The image is defined by the
  821. * <i>$source</i> parameter and must be a valid image, either local or remote (URL). By default, this
  822. * helper creates a submit tag with a name of <em>commit</em> to avoid conflicts with other parts of the
  823. * framework. It is recommended that you do not use the name "submit" for submit tags unless absolutely necessary.
  824. *
  825. * <b>Examples:</b>
  826. * <code>
  827. * // Assuming your image is in the /web/images/ directory
  828. * echo submit_image_tag('my_submit_button.gif');
  829. * </code>
  830. *
  831. * <code>
  832. * echo submit_image_tag('http://mydomain.com/my_submit_button.gif');
  833. * </code>
  834. *
  835. * @param string $source path to image file
  836. * @param array $options additional HTML compliant <input> tag parameters
  837. *
  838. * @return string XHTML compliant <input> tag with type="image"
  839. */
  840. function submit_image_tag($source, $options = array())
  841. {
  842. if (!isset($options['alt']))
  843. {
  844. $path_pos = strrpos($source, '/');
  845. $dot_pos = strrpos($source, '.');
  846. $begin = $path_pos ? $path_pos + 1 : 0;
  847. $nb_str = ($dot_pos ? $dot_pos : strlen($source)) - $begin;
  848. $options['alt'] = ucfirst(substr($source, $begin, $nb_str));
  849. }
  850. return tag('input', array_merge(array('type' => 'image', 'name' => 'commit', 'src' => image_path($source)), _convert_options_to_javascript(_convert_options($options))));
  851. }
  852. /**
  853. * Returns a <label> tag with <i>$label</i> for the specified <i>$id</i> parameter.
  854. *
  855. * @param string $id id
  856. * @param string $label label or title
  857. * @param array $options additional HTML compliant <label> tag parameters
  858. *
  859. * @return string <label> tag with <i>$label</i> for the specified <i>$id</i> parameter.
  860. */
  861. function label_for($id, $label, $options = array())
  862. {
  863. $options = _parse_attributes($options);
  864. if (is_object($label) && method_exists($label, '__toString'))
  865. {
  866. $label = $label->__toString();
  867. }
  868. return content_tag('label', $label, array_merge(array('for' => get_id_from_name($id, null)), $options));
  869. }
  870. function _convert_include_custom_for_select($options, &$select_options)
  871. {
  872. if (_get_option($options, 'include_blank'))
  873. {
  874. $select_options[''] = '';
  875. }
  876. else if ($include_custom = _get_option($options, 'include_custom'))
  877. {
  878. $select_options[''] = $include_custom;
  879. }
  880. }