DateFormHelper.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. <?php
  2. use_helper('Form');
  3. /*
  4. * This file is part of the symfony package.
  5. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  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. * DateFormHelper.
  12. *
  13. * @package symfony
  14. * @subpackage helper
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: DateFormHelper.php 14121 2008-12-17 10:29:09Z FabianLange $
  17. */
  18. /**
  19. * Returns a <select> tag populated with all the days of the month (1 - 31).
  20. *
  21. * By default, the <i>$value</i> parameter is set to today's day. To override this, simply pass an integer
  22. * (1 - 31) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
  23. * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
  24. * parameter. For convenience, symfony also offers the select_date_tag helper function which combines the
  25. * select_year_tag, select_month_tag, and select_day_tag functions into a single helper.
  26. *
  27. * <b>Options:</b>
  28. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  29. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  30. *
  31. * <b>Examples:</b>
  32. * <code>
  33. * echo select_day_tag('day', 14);
  34. * </code>
  35. *
  36. * @param string $name field name
  37. * @param int $value selected value (1 - 31)
  38. * @param array $options special options for the select tag
  39. * @param array $html_options additional HTML compliant <select> tag parameters
  40. *
  41. * @return string <select> tag populated with all the days of the month (1 - 31).
  42. * @see select_date_tag, select datetime_tag
  43. */
  44. function select_day_tag($name, $value = null, $options = array(), $html_options = array())
  45. {
  46. if ($value === null)
  47. {
  48. $value = date('j');
  49. }
  50. $options = _parse_attributes($options);
  51. $select_options = array();
  52. _convert_include_custom_for_select($options, $select_options);
  53. for ($x = 1; $x < 32; $x++)
  54. {
  55. $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
  56. }
  57. return select_tag($name, options_for_select($select_options, $value), $html_options);
  58. }
  59. /**
  60. * Returns a <select> tag populated with all the months of the year (1 - 12).
  61. *
  62. * By default, the <i>$value</i> parameter is set to today's month. To override this, simply pass an integer
  63. * (1 - 12) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
  64. * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
  65. * parameter. Also, the each month's display title is set to return its respective full month name, which can be easily
  66. * overridden by passing the 'use_short_names' or 'use_month_numbers' options to the <i>$options</i> parameter.
  67. * For convenience, Symfony also offers the select_date_tag helper function which combines the
  68. * select_year_tag, select_month_tag, and select_day_tag functions into a single helper.
  69. *
  70. * <b>Options:</b>
  71. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value
  72. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value
  73. * - use_month_numbers - If set to true, will show the month's numerical value (1 - 12) instead of the months full name.
  74. * - use_short_month - If set to true, will show the month's short name (i.e. Jan, Feb, Mar) instead of its full name.
  75. *
  76. * <b>Examples:</b>
  77. * <code>
  78. * echo select_month_tag('month', 5, array('use_short_month' => true));
  79. * </code>
  80. *
  81. * <code>
  82. * echo select_month_tag('month', null, array('use_month_numbers' => true, 'include_blank' => true));
  83. * </code>
  84. *
  85. * @param string $name field name
  86. * @param int $value selected value (1 - 12)
  87. * @param array $options special options for the select tag
  88. * @param array $html_options additional HTML compliant <select> tag parameters
  89. *
  90. * @return string <select> tag populated with all the months of the year (1 - 12).
  91. * @see select_date_tag, select datetime_tag
  92. */
  93. function select_month_tag($name, $value = null, $options = array(), $html_options = array())
  94. {
  95. if ($value === null)
  96. {
  97. $value = date('n');
  98. }
  99. $options = _parse_attributes($options);
  100. $select_options = array();
  101. _convert_include_custom_for_select($options, $select_options);
  102. if (_get_option($options, 'use_month_numbers'))
  103. {
  104. for ($k = 1; $k < 13; $k++)
  105. {
  106. $select_options[$k] = str_pad($k, 2, '0', STR_PAD_LEFT);
  107. }
  108. }
  109. else
  110. {
  111. $culture = _get_option($options, 'culture', sfContext::getInstance()->getUser()->getCulture());
  112. $I18n_arr = _get_I18n_date_locales($culture);
  113. if (_get_option($options, 'use_short_month'))
  114. {
  115. $month_names = $I18n_arr['dateFormatInfo']->getAbbreviatedMonthNames();
  116. }
  117. else
  118. {
  119. $month_names = $I18n_arr['dateFormatInfo']->getMonthNames();
  120. }
  121. $add_month_numbers = _get_option($options, 'add_month_numbers');
  122. foreach ($month_names as $k => $v)
  123. {
  124. $select_options[$k + 1] = $add_month_numbers ? ($k + 1).' - '.$v : $v;
  125. }
  126. }
  127. return select_tag($name, options_for_select($select_options, $value), $html_options);
  128. }
  129. /**
  130. * Returns a <select> tag populated with a range of years.
  131. *
  132. * By default, the <i>$value</i> parameter is set to today's year. To override this, simply pass a four-digit integer (YYYY)
  133. * to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
  134. * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
  135. * parameter. Also, the default selectable range of years is set to show five years back and five years forward from today's year.
  136. * For instance, if today's year is 2006, the default 'year_start' option will be set to 2001 and the 'year_end' option will be set
  137. * to 2011. These start and end dates can easily be overwritten by setting the 'year_start' and 'year_end' options in the <i>$options</i>
  138. * parameter. For convenience, Symfony also offers the select_date_tag helper function which combines the
  139. * select_year_tag, select_month_tag, and select_day_tag functions into a single helper.
  140. *
  141. * <b>Options:</b>
  142. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value
  143. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value
  144. * - year_start - If set, the range of years will begin at this four-digit date (i.e. 1979)
  145. * - year_end - If set, the range of years will end at this four-digit date (i.e. 2025)
  146. *
  147. * <b>Examples:</b>
  148. * <code>
  149. * echo select_year_tag('year');
  150. * </code>
  151. *
  152. * <code>
  153. * $year_start = date('Y', strtotime('-10 years'));
  154. * $year_end = date('Y', strtotime('+10 years'));
  155. * echo select_year_tag('year', null, array('year_start' => $year_start, 'year_end' => $year_end));
  156. * </code>
  157. *
  158. * @param string $name field name
  159. * @param int $value selected value within the range of years.
  160. * @param array $options special options for the select tag
  161. * @param array $html_options additional HTML compliant <select> tag parameters
  162. *
  163. * @return string <select> tag populated with a range of years.
  164. * @see select_date_tag, select datetime_tag
  165. */
  166. function select_year_tag($name, $value = null, $options = array(), $html_options = array())
  167. {
  168. if ($value === null)
  169. {
  170. $value = date('Y');
  171. }
  172. $options = _parse_attributes($options);
  173. $select_options = array();
  174. _convert_include_custom_for_select($options, $select_options);
  175. if (strlen($value) > 0 && is_numeric($value))
  176. {
  177. $year_origin = $value;
  178. }
  179. else
  180. {
  181. $year_origin = date('Y');
  182. }
  183. $year_start = _get_option($options, 'year_start', $year_origin - 5);
  184. $year_end = _get_option($options, 'year_end', $year_origin + 5);
  185. $ascending = ($year_start < $year_end);
  186. $until_year = ($ascending) ? $year_end + 1 : $year_end - 1;
  187. for ($x = $year_start; $x != $until_year; ($ascending) ? $x++ : $x--)
  188. {
  189. $select_options[$x] = $x;
  190. }
  191. return select_tag($name, options_for_select($select_options, $value), $html_options);
  192. }
  193. /**
  194. * Returns three <select> tags populated with a range of months, days, and years.
  195. *
  196. * By default, the <i>$value</i> parameter is set to today's month, day and year. To override this, simply pass a valid date
  197. * or a correctly formatted date array (see example) to the <i>$value</i> parameter. You can also set the <i>$value</i>
  198. * parameter to null which will disable the <i>$value</i>, however this will only be useful if you pass 'include_blank' or
  199. * 'include_custom' to the <i>$options</i> parameter. Also, the default selectable range of years is set to show five years
  200. * back and five years forward from today's year. For instance, if today's year is 2006, the default 'year_start' option will
  201. * be set to 2001 and the 'year_end' option will be set to 2011. These start and end dates can easily be overwritten by
  202. * setting the 'year_start' and 'year_end' options in the <i>$options</i> parameter.
  203. *
  204. * <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names. For example, a <i>$name</i> of "date" becomes:
  205. * <samp>
  206. * <select name="date[month]">...</select>
  207. * <select name="date[day]">...</select>
  208. * <select name="date[year]">...</select>
  209. * </samp>
  210. *
  211. * <b>Options:</b>
  212. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  213. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  214. * - discard_month - If set to true, will only return select tags for day and year.
  215. * - discard_day - If set to true, will only return select tags for month and year.
  216. * - discard_year - If set to true, will only return select tags for month and day.
  217. * - use_month_numbers - If set to true, will show the month's numerical value (1 - 12) instead of the months full name.
  218. * - use_short_month - If set to true, will show the month's short name (i.e. Jan, Feb, Mar) instead of its full name.
  219. * - year_start - If set, the range of years will begin at this four-digit date (i.e. 1979)
  220. * - year_end - If set, the range of years will end at this four-digit date (i.e. 2025)
  221. * - date_seperator - Includes a string of defined text between each generated select tag
  222. *
  223. * <b>Examples:</b>
  224. * <code>
  225. * echo select_date_tag('date');
  226. * </code>
  227. *
  228. * <code>
  229. * echo select_date_tag('date', '2006-10-30');
  230. * </code>
  231. *
  232. * <code>
  233. * $date = array('year' => '1979', 'month' => 10, 'day' => 30);
  234. * echo select_date_tag('date', $date, array('year_start' => $date['year'] - 10, 'year_end' => $date['year'] + 10));
  235. * </code>
  236. *
  237. * @param string $name field name (automatically becomes an array of parts: name[year], name[month], year[day])
  238. * @param mixed $value accepts a valid date string or properly formatted date array
  239. * @param array $options special options for the select tags
  240. * @param array $html_options additional HTML compliant <select> tag parameters
  241. *
  242. * @return string three <select> tags populated with a months, days and years
  243. * @see select datetime_tag, select_month_tag, select_date_tag, select_year_tag
  244. */
  245. function select_date_tag($name, $value = null, $options = array(), $html_options = array())
  246. {
  247. $options = _parse_attributes($options);
  248. $culture = _get_option($options, 'culture', sfContext::getInstance()->getUser()->getCulture());
  249. // set it back for month tag
  250. $options['culture'] = $culture;
  251. $I18n_arr = _get_I18n_date_locales($culture);
  252. $date_seperator = _get_option($options, 'date_seperator', $I18n_arr['date_seperator']);
  253. $discard_month = _get_option($options, 'discard_month');
  254. $discard_day = _get_option($options, 'discard_day');
  255. $discard_year = _get_option($options, 'discard_year');
  256. // discarding month automatically discards day
  257. if ($discard_month)
  258. {
  259. $discard_day = true;
  260. }
  261. $order = _get_option($options, 'order');
  262. $tags = array();
  263. if (is_array($order) && count($order) == 3)
  264. {
  265. foreach ($order as $v)
  266. {
  267. $tags[] = $v[0];
  268. }
  269. }
  270. else
  271. {
  272. $tags = $I18n_arr['date_order'];
  273. }
  274. if ($include_custom = _get_option($options, 'include_custom'))
  275. {
  276. $include_custom_month = is_array($include_custom)
  277. ? (isset($include_custom['month']) ? array('include_custom' => $include_custom['month']) : array())
  278. : array('include_custom' => $include_custom);
  279. $include_custom_day = is_array($include_custom)
  280. ? (isset($include_custom['day']) ? array('include_custom' => $include_custom['day']) : array())
  281. : array('include_custom' => $include_custom);
  282. $include_custom_year = is_array($include_custom)
  283. ? (isset($include_custom['year']) ? array('include_custom' => $include_custom['year']) : array())
  284. : array('include_custom' => $include_custom);
  285. }
  286. else
  287. {
  288. $include_custom_month = array();
  289. $include_custom_day = array();
  290. $include_custom_year = array();
  291. }
  292. $month_name = $name.'[month]';
  293. $m = !$discard_month ? select_month_tag($month_name, _parse_value_for_date($value, 'month', 'm'), $options + $include_custom_month, $html_options) : '';
  294. $day_name = $name.'[day]';
  295. $d = !$discard_day ? select_day_tag($day_name, _parse_value_for_date($value, 'day', 'd'), $options + $include_custom_day, $html_options) : '';
  296. $year_name = $name.'[year]';
  297. $y = !$discard_year ? select_year_tag($year_name, _parse_value_for_date($value, 'year', 'Y'), $options + $include_custom_year, $html_options) : '';
  298. // we have $tags = array ('m','d','y')
  299. foreach ($tags as $k => $v)
  300. {
  301. // $tags['m|d|y'] = $m|$d|$y
  302. if (strlen($$v))
  303. {
  304. $tags[$k] = $$v;
  305. }
  306. else
  307. {
  308. unset($tags[$k]);
  309. }
  310. }
  311. return implode($date_seperator, $tags);
  312. }
  313. /**
  314. * Returns a <select> tag populated with 60 seconds (0 - 59).
  315. *
  316. * By default, the <i>$value</i> parameter is set to the current second (right now). To override this, simply pass an integer
  317. * (0 - 59) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
  318. * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
  319. * parameter. In many cases, you have no need for all 60 seconds in a minute. the 'second_step' option in the
  320. * <i>$options</i> parameter gives you the ability to define intervals to display. So for instance you could define 15 as your
  321. * 'minute_step' interval and the select tag would return the values 0, 15, 30, and 45. For convenience, Symfony also offers the
  322. * select_time_tag select_datetime_tag helper functions which combine other date and time helpers to easily build date and time select boxes.
  323. *
  324. * <b>Options:</b>
  325. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  326. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  327. * - second_step - If set, the seconds will be incremented in blocks of X, where X = 'second_step'
  328. *
  329. * <b>Examples:</b>
  330. * <code>
  331. * echo select_second_tag('second');
  332. * </code>
  333. *
  334. * <code>
  335. * echo select_second_tag('second', 15, array('second_step' => 15));
  336. * </code>
  337. *
  338. * @param string $name field name
  339. * @param int $value selected value (0 - 59)
  340. * @param array $options special options for the select tag
  341. * @param array $html_options additional HTML compliant <select> tag parameters
  342. *
  343. * @return string <select> tag populated with 60 seconds (0 - 59).
  344. * @see select_time_tag, select datetime_tag
  345. */
  346. function select_second_tag($name, $value = null, $options = array(), $html_options = array())
  347. {
  348. if ($value === null)
  349. {
  350. $value = date('s');
  351. }
  352. $options = _parse_attributes($options);
  353. $select_options = array();
  354. _convert_include_custom_for_select($options, $select_options);
  355. $second_step = _get_option($options, 'second_step', 1);
  356. for ($x = 0; $x < 60; $x += $second_step)
  357. {
  358. $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
  359. }
  360. return select_tag($name, options_for_select($select_options, $value), $html_options);
  361. }
  362. /**
  363. * Returns a <select> tag populated with 60 minutes (0 - 59).
  364. *
  365. * By default, the <i>$value</i> parameter is set to the current minute. To override this, simply pass an integer
  366. * (0 - 59) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
  367. * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
  368. * parameter. In many cases, you have no need for all 60 minutes in an hour. the 'minute_step' option in the
  369. * <i>$options</i> parameter gives you the ability to define intervals to display. So for instance you could define 15 as your
  370. * 'minute_step' interval and the select tag would return the values 0, 15, 30, and 45. For convenience, Symfony also offers the
  371. * select_time_tag select_datetime_tag helper functions which combine other date and time helpers to easily build date and time select boxes.
  372. *
  373. * <b>Options:</b>
  374. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  375. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  376. * - minute_step - If set, the minutes will be incremented in blocks of X, where X = 'minute_step'
  377. *
  378. * <b>Examples:</b>
  379. * <code>
  380. * echo select_minute_tag('minute');
  381. * </code>
  382. *
  383. * <code>
  384. * echo select_minute_tag('minute', 15, array('minute_step' => 15));
  385. * </code>
  386. *
  387. * @param string $name field name
  388. * @param int $value selected value (0 - 59)
  389. * @param array $options special options for the select tag
  390. * @param array $html_options additional HTML compliant <select> tag parameters
  391. *
  392. * @return string <select> tag populated with 60 minutes (0 - 59).
  393. * @see select_time_tag, select datetime_tag
  394. */
  395. function select_minute_tag($name, $value = null, $options = array(), $html_options = array())
  396. {
  397. if ($value === null)
  398. {
  399. $value = date('i');
  400. }
  401. $options = _parse_attributes($options);
  402. $select_options = array();
  403. _convert_include_custom_for_select($options, $select_options);
  404. $minute_step = _get_option($options, 'minute_step', 1);
  405. for ($x = 0; $x < 60; $x += $minute_step)
  406. {
  407. $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
  408. }
  409. return select_tag($name, options_for_select($select_options, $value), $html_options);
  410. }
  411. /**
  412. * Returns a <select> tag populated with 24 hours (0 - 23), or optionally 12 hours (1 - 12).
  413. *
  414. * By default, the <i>$value</i> parameter is set to the current hour. To override this, simply pass an integer
  415. * (0 - 23 or 1 - 12 if '12hour_time' = true) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
  416. * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
  417. * parameter. For convenience, Symfony also offers the select_time_tag select_datetime_tag helper functions
  418. * which combine other date and time helpers to easily build date and time select boxes.
  419. *
  420. * <b>Options:</b>
  421. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  422. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  423. * - 12hour_time - If set to true, will return integers 1 through 12 instead of the default 0 through 23 as well as an AM/PM select box.
  424. *
  425. * <b>Examples:</b>
  426. * <code>
  427. * echo select_hour_tag('hour');
  428. * </code>
  429. *
  430. * <code>
  431. * echo select_hour_tag('hour', 6, array('12hour_time' => true));
  432. * </code>
  433. *
  434. * @param string $name field name
  435. * @param int $value selected value (0 - 23 or 1 - 12 if '12hour_time' = true)
  436. * @param array $options special options for the select tag
  437. * @param array $html_options additional HTML compliant <select> tag parameters
  438. *
  439. * @return string <select> tag populated with 24 hours (0 - 23), or optionally 12 hours (1 - 12).
  440. * @see select_time_tag, select datetime_tag
  441. */
  442. function select_hour_tag($name, $value = null, $options = array(), $html_options = array())
  443. {
  444. $options = _parse_attributes($options);
  445. $select_options = array();
  446. _convert_include_custom_for_select($options, $select_options);
  447. $_12hour_time = _get_option($options, '12hour_time');
  448. if ($value === null)
  449. {
  450. $value = date($_12hour_time ? 'h' : 'H');
  451. }
  452. $start_hour = $_12hour_time ? 1 : 0;
  453. $end_hour = $_12hour_time ? 12 : 23;
  454. for ($x = $start_hour; $x <= $end_hour; $x++)
  455. {
  456. $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
  457. }
  458. return select_tag($name, options_for_select($select_options, $value), $html_options);
  459. }
  460. /**
  461. * Returns a <select> tag populated with AM and PM options for use with 12-Hour time.
  462. *
  463. * By default, the <i>$value</i> parameter is set to the correct AM/PM setting based on the current time.
  464. * To override this, simply pass either AM or PM to the <i>$value</i> parameter. You can also set the
  465. * <i>$value</i> parameter to null which will disable the <i>$value</i>, however this will only be
  466. * useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i> parameter. For
  467. * convenience, Symfony also offers the select_time_tag select_datetime_tag helper functions
  468. * which combine other date and time helpers to easily build date and time select boxes.
  469. *
  470. * <b>Options:</b>
  471. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  472. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  473. *
  474. * <b>Examples:</b>
  475. * <code>
  476. * echo select_ampm_tag('ampm');
  477. * </code>
  478. *
  479. * <code>
  480. * echo select_ampm_tag('ampm', 'PM', array('include_blank' => true));
  481. * </code>
  482. *
  483. * @param string $name field name
  484. * @param string $value selected value (AM or PM)
  485. * @param array $options special options for the select tag
  486. * @param array $html_options additional HTML compliant <select> tag parameters
  487. * @return string <select> tag populated with AM and PM options for use with 12-Hour time.
  488. * @see select_time_tag, select datetime_tag
  489. */
  490. function select_ampm_tag($name, $value = null, $options = array(), $html_options = array())
  491. {
  492. if ($value === null)
  493. {
  494. $value = date('A');
  495. }
  496. $options = _parse_attributes($options);
  497. $select_options = array();
  498. _convert_include_custom_for_select($options, $select_options);
  499. $select_options['AM'] = 'AM';
  500. $select_options['PM'] = 'PM';
  501. return select_tag($name, options_for_select($select_options, $value), $html_options);
  502. }
  503. /**
  504. * Returns three <select> tags populated with hours, minutes, and optionally seconds.
  505. *
  506. * By default, the <i>$value</i> parameter is set to the current hour and minute. To override this, simply pass a valid time
  507. * or a correctly formatted time array (see example) to the <i>$value</i> parameter. You can also set the <i>$value</i>
  508. * parameter to null which will disable the <i>$value</i>, however this will only be useful if you pass 'include_blank' or
  509. * 'include_custom' to the <i>$options</i> parameter. To include seconds to the result, use set the 'include_second' option in the
  510. * <i>$options</i> parameter to true. <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names.
  511. * For example, a <i>$name</i> of "time" becomes:
  512. * <samp>
  513. * <select name="time[hour]">...</select>
  514. * <select name="time[minute]">...</select>
  515. * <select name="time[second]">...</select>
  516. * </samp>
  517. *
  518. * <b>Options:</b>
  519. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  520. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  521. * - include_second - If set to true, includes the "seconds" select tag as part of the result.
  522. * - second_step - If set, the seconds will be incremented in blocks of X, where X = 'second_step'
  523. * - minute_step - If set, the minutes will be incremented in blocks of X, where X = 'minute_step'
  524. * - 12hour_time - If set to true, will return integers 1 through 12 instead of the default 0 through 23 as well as an AM/PM select box.
  525. * - time_seperator - Includes a string of defined text between each generated select tag
  526. * - ampm_seperator - Includes a string of defined text between the minute/second select box and the AM/PM select box
  527. *
  528. * <b>Examples:</b>
  529. * <code>
  530. * echo select_time_tag('time');
  531. * </code>
  532. *
  533. * <code>
  534. * echo select_time_tag('date', '09:31');
  535. * </code>
  536. *
  537. * <code>
  538. * $time = array('hour' => '15', 'minute' => 46, 'second' => 01);
  539. * echo select_time_tag('time', $time, array('include_second' => true, '12hour_time' => true));
  540. * </code>
  541. *
  542. * @param string $name field name (automatically becomes an array of parts: name[hour], name[minute], year[second])
  543. * @param mixed $value accepts a valid time string or properly formatted time array
  544. * @param array $options special options for the select tag
  545. * @param array $html_options additional HTML compliant <select> tag parameters
  546. * @return string three <select> tags populated with a hours, minutes and optionally seconds.
  547. * @see select datetime_tag, select_hour_tag, select_minute_tag, select_second_tag
  548. */
  549. function select_time_tag($name, $value = null, $options = array(), $html_options = array())
  550. {
  551. $options = _parse_attributes($options);
  552. $time_seperator = _get_option($options, 'time_seperator', ':');
  553. $ampm_seperator = _get_option($options, 'ampm_seperator', '');
  554. $include_second = _get_option($options, 'include_second');
  555. $_12hour_time = _get_option($options, '12hour_time');
  556. $options['12hour_time'] = $_12hour_time; // set it back. hour tag needs it.
  557. if ($include_custom = _get_option($options, 'include_custom'))
  558. {
  559. $include_custom_hour = (is_array($include_custom))
  560. ? ((isset($include_custom['hour'])) ? array('include_custom'=>$include_custom['hour']) : array())
  561. : array('include_custom'=>$include_custom);
  562. $include_custom_minute = (is_array($include_custom))
  563. ? ((isset($include_custom['minute'])) ? array('include_custom'=>$include_custom['minute']) : array())
  564. : array('include_custom'=>$include_custom);
  565. $include_custom_second = (is_array($include_custom))
  566. ? ((isset($include_custom['second'])) ? array('include_custom'=>$include_custom['second']) : array())
  567. : array('include_custom'=>$include_custom);
  568. $include_custom_ampm = (is_array($include_custom))
  569. ? ((isset($include_custom['ampm'])) ? array('include_custom'=>$include_custom['ampm']) : array())
  570. : array('include_custom'=>$include_custom);
  571. }
  572. else
  573. {
  574. $include_custom_hour = array();
  575. $include_custom_minute = array();
  576. $include_custom_second = array();
  577. $include_custom_ampm = array();
  578. }
  579. $tags = array();
  580. $hour_name = $name.'[hour]';
  581. $tags[] = select_hour_tag($hour_name, _parse_value_for_date($value, 'hour', $_12hour_time ? 'h' : 'H'), $options + $include_custom_hour, $html_options);
  582. $minute_name = $name.'[minute]';
  583. $tags[] = select_minute_tag($minute_name, _parse_value_for_date($value, 'minute', 'i'), $options + $include_custom_minute, $html_options);
  584. if ($include_second)
  585. {
  586. $second_name = $name.'[second]';
  587. $tags[] = select_second_tag($second_name, _parse_value_for_date($value, 'second', 's'), $options + $include_custom_second, $html_options);
  588. }
  589. $time = implode($time_seperator, $tags);
  590. if ($_12hour_time)
  591. {
  592. $ampm_name = $name.'[ampm]';
  593. $time .= $ampm_seperator.select_ampm_tag($ampm_name, _parse_value_for_date($value, 'ampm', 'A'), $options + $include_custom_ampm, $html_options);
  594. }
  595. return $time;
  596. }
  597. /**
  598. * Returns a variable number of <select> tags populated with date and time related select boxes.
  599. *
  600. * The select_datetime_tag is the culmination of both the select_date_tag and the select_time_tag.
  601. * By default, the <i>$value</i> parameter is set to the current date and time. To override this, simply pass a valid
  602. * date, time, datetime string or correctly formatted array (see example) to the <i>$value</i> parameter.
  603. * You can also set the <i>$value</i> parameter to null which will disable the <i>$value</i>, however this
  604. * will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i> parameter.
  605. * To include seconds to the result, use set the 'include_second' option in the <i>$options</i> parameter to true.
  606. * <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names.
  607. * For example, a <i>$name</i> of "datetime" becomes:
  608. * <samp>
  609. * <select name="datetime[month]">...</select>
  610. * <select name="datetime[day]">...</select>
  611. * <select name="datetime[year]">...</select>
  612. * <select name="datetime[hour]">...</select>
  613. * <select name="datetime[minute]">...</select>
  614. * <select name="datetime[second]">...</select>
  615. * </samp>
  616. *
  617. * <b>Options:</b>
  618. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  619. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  620. * - include_second - If set to true, includes the "seconds" select tag as part of the result.
  621. * - discard_month - If set to true, will only return select tags for day and year.
  622. * - discard_day - If set to true, will only return select tags for month and year.
  623. * - discard_year - If set to true, will only return select tags for month and day.
  624. * - use_month_numbers - If set to true, will show the month's numerical value (1 - 12) instead of the months full name.
  625. * - use_short_month - If set to true, will show the month's short name (i.e. Jan, Feb, Mar) instead of its full name.
  626. * - year_start - If set, the range of years will begin at this four-digit date (i.e. 1979)
  627. * - year_end - If set, the range of years will end at this four-digit date (i.e. 2025)
  628. * - second_step - If set, the seconds will be incremented in blocks of X, where X = 'second_step'
  629. * - minute_step - If set, the minutes will be incremented in blocks of X, where X = 'minute_step'
  630. * - 12hour_time - If set to true, will return integers 1 through 12 instead of the default 0 through 23.
  631. * - date_seperator - Includes a string of defined text between each generated select tag
  632. * - time_seperator - Includes a string of defined text between each generated select tag
  633. * - ampm_seperator - Includes a string of defined text between the minute/second select box and the AM/PM select box
  634. *
  635. * <b>Examples:</b>
  636. * <code>
  637. * echo select_datetime_tag('datetime');
  638. * </code>
  639. *
  640. * <code>
  641. * echo select_datetime_tag('datetime', '1979-10-30');
  642. * </code>
  643. *
  644. * <code>
  645. * $datetime = array('year' => '1979', 'month' => 10, 'day' => 30, 'hour' => '15', 'minute' => 46);
  646. * echo select_datetime_tag('time', $datetime, array('use_short_month' => true, '12hour_time' => true));
  647. * </code>
  648. *
  649. * @param string $name field name (automatically becomes an array of date and time parts)
  650. * @param mixed $value accepts a valid time string or properly formatted time array
  651. * @param array $options special options for the select tagss
  652. * @param array $html_options additional HTML compliant <select> tag parameters
  653. *
  654. * @return string a variable number of <select> tags populated with date and time related select boxes
  655. * @see select date_tag, select_time_tag
  656. */
  657. function select_datetime_tag($name, $value = null, $options = array(), $html_options = array())
  658. {
  659. $options = _parse_attributes($options);
  660. $datetime_seperator = _get_option($options, 'datetime_seperator', '');
  661. $date = select_date_tag($name, $value, $options, $html_options);
  662. $time = select_time_tag($name, $value, $options, $html_options);
  663. return $date.$datetime_seperator.$time;
  664. }
  665. /**
  666. * Returns a <select> tag, populated with a range of numbers
  667. *
  668. * By default, the select_number_tag generates a list of numbers from 1 - 10, with an incremental value of 1. These values
  669. * can be easily changed by passing one or several <i>$options</i>. Numbers can be either positive or negative, integers or decimals,
  670. * and can be incremented by any number, decimal or integer. If you require the range of numbers to be listed in descending order, pass
  671. * the 'reverse' option to easily display the list of numbers in the opposite direction.
  672. *
  673. * <b>Options:</b>
  674. * - include_blank - Includes a blank <option> tag at the beginning of the string with an empty value.
  675. * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
  676. * - multiple - If set to true, the select tag will allow multiple numbers to be selected at once.
  677. * - start - The first number in the list. If not specified, the default value is 1.
  678. * - end - The last number in the list. If not specified, the default value is 10.
  679. * - increment - The number by which to increase each number in the list by until the number is greater than or equal to the 'end' option.
  680. * If not specified, the default value is 1.
  681. * - reverse - Reverses the order of numbers so they are display in descending order
  682. *
  683. * <b>Examples:</b>
  684. * <code>
  685. * echo select_number_tag('rating', '', array('reverse' => true));
  686. * </code>
  687. *
  688. * <code>
  689. * echo echo select_number_tag('tax_rate', '0.07', array('start' => '0.05', 'end' => '0.09', 'increment' => '0.01'));
  690. * </code>
  691. *
  692. * <code>
  693. * echo select_number_tag('limit', 5, array('start' => 5, 'end' => 120, 'increment' => 15));
  694. * </code>
  695. *
  696. * @param string $name field name
  697. * @param string $value the selected option
  698. * @param array $options special options for the select tagss
  699. * @param array $html_options additional HTML compliant <select> tag parameters
  700. *
  701. * @return string <select> tag populated with a range of numbers.
  702. * @see options_for_select, content_tag
  703. */
  704. function select_number_tag($name, $value, $options = array(), $html_options = array())
  705. {
  706. $increment = _get_option($options, 'increment', 1);
  707. $range = array();
  708. $max = _get_option($options, 'end', 10) + $increment;
  709. for ($x = _get_option($options, 'start', 1); $x < $max; $x += $increment)
  710. {
  711. $range[(string) $x] = $x;
  712. }
  713. if (_get_option($options, 'reverse'))
  714. {
  715. $range = array_reverse($range, true);
  716. }
  717. return select_tag($name, options_for_select($range, $value, $options), $html_options);
  718. }
  719. /**
  720. * Returns a <select> tag populated with all the timezones in the world.
  721. *
  722. * The select_timezone_tag builds off the traditional select_tag function, and is conveniently populated with
  723. * all the timezones in the world (sorted alphabetically). Each option in the list has a unique timezone identifier
  724. * for its value and the timezone's locale as its display title. The timezone data is retrieved via the sfCultureInfo
  725. * class, which stores a wide variety of i18n and i10n settings for various countries and cultures throughout the world.
  726. * Here's an example of an <option> tag generated by the select_timezone_tag:
  727. *
  728. * <b>Options:</b>
  729. * - display -
  730. * identifer - Display the PHP timezone identifier (e.g. America/Denver)
  731. * timezone - Display the full timezone name (e.g. Mountain Standard Time)
  732. * timezone_abbr - Display the timezone abbreviation (e.g. MST)
  733. * timzone_dst - Display the full timezone name with daylight savings time (e.g. Mountain Daylight Time)
  734. * timezone_dst_abbr - Display the timezone abbreviation with daylight savings time (e.g. MDT)
  735. * city - Display the city/region that relates to the timezone (e.g. Denver)
  736. *
  737. * <samp>
  738. * <option value="America/Denver">America/Denver</option>
  739. * </samp>
  740. *
  741. * <b>Examples:</b>
  742. * <code>
  743. * echo select_timezone_tag('timezone', 'America/Denver');
  744. * </code>
  745. *
  746. * @param string $name field name
  747. * @param string $selected selected field value (timezone identifier)
  748. * @param array $options additional HTML compliant <select> tag parameters
  749. *
  750. * @return string <select> tag populated with all the timezones in the world.
  751. * @see select_tag, options_for_select, sfCultureInfo
  752. */
  753. function select_timezone_tag($name, $selected = null, $options = array())
  754. {
  755. if (!isset($options['display'])) $options['display'] = 'identifier';
  756. $c = sfCultureInfo::getInstance(sfContext::getInstance()->getUser()->getCulture());
  757. $timezone_groups = $c->getTimeZones();
  758. $display_key = 0;
  759. switch ($options['display'])
  760. {
  761. case "identifier":
  762. $display_key = 0;
  763. break;
  764. case "timezone":
  765. $display_key = 1;
  766. break;
  767. case "timezone_abbr":
  768. $display_key = 2;
  769. break;
  770. case "timezone_dst":
  771. $display_key = 3;
  772. break;
  773. case "timezone_dst_abbr":
  774. $display_key = 4;
  775. break;
  776. case "city":
  777. $display_key = 5;
  778. break;
  779. default:
  780. $display_key = 0;
  781. break;
  782. }
  783. unset($options['display']);
  784. $timezones = array();
  785. foreach ($timezone_groups as $tz_group_key => $tz_group)
  786. {
  787. $array_key = null;
  788. foreach ($tz_group as $tz_key => $tz)
  789. {
  790. if ($tz_key == 0) $array_key = $tz;
  791. if ($tz_key == $display_key AND !empty($tz)) $timezones[$array_key] = $tz;
  792. }
  793. }
  794. if ($timezone_option = _get_option($options, 'timezones'))
  795. {
  796. $diff = array_diff_key($timezones, array_flip((array) $timezone_option));
  797. foreach ($diff as $key => $v)
  798. {
  799. unset($timezones[$key]);
  800. }
  801. }
  802. // Remove duplicate values
  803. $timezones = array_unique($timezones);
  804. asort($timezones);
  805. $option_tags = options_for_select($timezones, $selected);
  806. return select_tag($name, $option_tags, $options);
  807. }
  808. /**
  809. * Converts date values (<i>$value</i>) into its correct date format (<i>$format_char</i>)
  810. *
  811. * This function is primarily used in select_date_tag, select_time_tag and select_datetime_tag.
  812. *
  813. * <b>Note:</b> If <i>$value</i> is empty, it will be populated with the current date and time.
  814. *
  815. * @param string $value date or date part
  816. * @param string $key custom key for array values
  817. * @param string $format_char date format
  818. *
  819. * @return string properly formatted date part value.
  820. * @see select_date_tag, select_time_tag, select_datetime_tag
  821. */
  822. function _parse_value_for_date($value, $key, $format_char)
  823. {
  824. if (is_array($value))
  825. {
  826. return (isset($value[$key])) ? $value[$key] : '';
  827. }
  828. else if (is_numeric($value))
  829. {
  830. return date($format_char, $value);
  831. }
  832. else if ($value == '' || ($key == 'ampm' && ($value == 'AM' || $value == 'PM')))
  833. {
  834. return $value;
  835. }
  836. else if (empty($value))
  837. {
  838. $value = date('Y-m-d H:i:s');
  839. }
  840. // english text presentation
  841. return date($format_char, strtotime($value));
  842. }
  843. /**
  844. * Retrieves the proper date format based on the specified <i>$culture</i> setting
  845. *
  846. * <b>Note:</b> If no <i>$culture</i> is defined, the user's culture setting will be used in its place.
  847. *
  848. * @param string $culture two or three character culture setting variable
  849. *
  850. * @return string formatted date/time format based on the specified date/time setting
  851. * @see sfUser
  852. */
  853. function _get_I18n_date_locales($culture = null)
  854. {
  855. if (!$culture)
  856. {
  857. $culture = sfContext::getInstance()->getUser()->getCulture();
  858. }
  859. $retval = array('culture'=>$culture);
  860. $dateFormatInfo = sfDateTimeFormatInfo::getInstance($culture);
  861. $date_format = strtolower($dateFormatInfo->getShortDatePattern());
  862. $retval['dateFormatInfo'] = $dateFormatInfo;
  863. $match_pattern = "/([dmy]+)(.*?)([dmy]+)(.*?)([dmy]+)/";
  864. if (!preg_match($match_pattern, $date_format, $match_arr))
  865. {
  866. // if matching fails use en shortdate
  867. preg_match($match_pattern, 'm/d/yy', $match_arr);
  868. }
  869. $retval['date_seperator'] = $match_arr[2];
  870. // unset all but [dmy]+
  871. unset($match_arr[0], $match_arr[2], $match_arr[4]);
  872. $retval['date_order'] = array();
  873. foreach ($match_arr as $v)
  874. {
  875. // 'm/d/yy' => $retval[date_order] = array ('m', 'd', 'y');
  876. $retval['date_order'][] = $v[0];
  877. }
  878. return $retval;
  879. }