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

Debug toolbar