[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/helper/ -> DateFormHelper.php (source)

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


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7