sfWidgetFormDate.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidgetFormDate represents a date widget.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormDate.class.php 16259 2009-03-12 11:42:00Z fabien $
  16. */
  17. class sfWidgetFormDate extends sfWidgetForm
  18. {
  19. /**
  20. * Configures the current widget.
  21. *
  22. * Available options:
  23. *
  24. * * format: The date format string (%month%/%day%/%year% by default)
  25. * * years: An array of years for the year select tag (optional)
  26. * Be careful that the keys must be the years, and the values what will be displayed to the user
  27. * * months: An array of months for the month select tag (optional)
  28. * * days: An array of days for the day select tag (optional)
  29. * * can_be_empty: Whether the widget accept an empty value (true by default)
  30. * * empty_values: An array of values to use for the empty value (empty string for year, month, and day by default)
  31. *
  32. * @param array $options An array of options
  33. * @param array $attributes An array of default HTML attributes
  34. *
  35. * @see sfWidgetForm
  36. */
  37. protected function configure($options = array(), $attributes = array())
  38. {
  39. $this->addOption('format', '%month%/%day%/%year%');
  40. $this->addOption('days', parent::generateTwoCharsRange(1, 31));
  41. $this->addOption('months', parent::generateTwoCharsRange(1, 12));
  42. $years = range(date('Y') - 5, date('Y') + 5);
  43. $this->addOption('years', array_combine($years, $years));
  44. $this->addOption('can_be_empty', true);
  45. $this->addOption('empty_values', array('year' => '', 'month' => '', 'day' => ''));
  46. }
  47. /**
  48. * @param string $name The element name
  49. * @param string $value The date displayed in this widget
  50. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  51. * @param array $errors An array of errors for the field
  52. *
  53. * @return string An HTML tag string
  54. *
  55. * @see sfWidgetForm
  56. */
  57. public function render($name, $value = null, $attributes = array(), $errors = array())
  58. {
  59. // convert value to an array
  60. $default = array('year' => null, 'month' => null, 'day' => null);
  61. if (is_array($value))
  62. {
  63. $value = array_merge($default, $value);
  64. }
  65. else
  66. {
  67. $value = (string) $value == (string) (integer) $value ? (integer) $value : strtotime($value);
  68. if (false === $value)
  69. {
  70. $value = $default;
  71. }
  72. else
  73. {
  74. $value = array('year' => date('Y', $value), 'month' => date('n', $value), 'day' => date('j', $value));
  75. }
  76. }
  77. $date = array();
  78. $emptyValues = $this->getOption('empty_values');
  79. // days
  80. $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['day']) + $this->getOption('days') : $this->getOption('days')), array_merge($this->attributes, $attributes));
  81. $date['%day%'] = $widget->render($name.'[day]', $value['day']);
  82. // months
  83. $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['month']) + $this->getOption('months') : $this->getOption('months')), array_merge($this->attributes, $attributes));
  84. $date['%month%'] = $widget->render($name.'[month]', $value['month']);
  85. // years
  86. $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['year']) + $this->getOption('years') : $this->getOption('years')), array_merge($this->attributes, $attributes));
  87. $date['%year%'] = $widget->render($name.'[year]', $value['year']);
  88. return strtr($this->getOption('format'), $date);
  89. }
  90. }