Development

/branches/1.1/lib/helper/DateFormHelper.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/helper/DateFormHelper.php

Revision 14121, 40.0 kB (checked in by FabianLange, 6 years ago)

[1.1] fixed select_timezone_tag not possible to display city. fixes #5359

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