1. sfWidgetFormDateTime.class.php
  2. /** * sfWidgetFormDateTime represents a datetime widget. * * @package symfony * @subpackage widget * @author Fabien Potencier * @version SVN: $Id: sfWidgetFormDateTime.class.php 20301 2009-07-19 10:57:32Z fabien $ */
  3. class sfWidgetFormDateTime extends sfWidgetForm
  4. {
  5. /**
  6. * Configures the current widget.
  7. *
  8. * The attributes are passed to both the date and the time widget.
  9. *
  10. * If you want to pass HTML attributes to one of the two widget, pass an
  11. * attributes option to the date or time option (see below).
  12. *
  13. * Available options:
  14. *
  15. * * date: Options for the date widget (see sfWidgetFormDate)
  16. * * time: Options for the time widget (see sfWidgetFormTime)
  17. * * with_time: Whether to include time (true by default)
  18. * * format: The format string for the date and the time widget (default to %date% %time%)
  19. *
  20. * @param array $options An array of options
  21. * @param array $attributes An array of default HTML attributes
  22. *
  23. * @see sfWidgetForm
  24. */
  25. protected function configure($options = array(), $attributes = array())
  26. {
  27. $this->addOption('date', array());
  28. $this->addOption('time', array());
  29. $this->addOption('with_time', true);
  30. $this->addOption('format', '%date% %time%');
  31. }
  32. /**
  33. * @param string $name The element name
  34. * @param string $value The date and time displayed in this widget
  35. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  36. * @param array $errors An array of errors for the field
  37. *
  38. * @return string An HTML tag string
  39. *
  40. * @see sfWidgetForm
  41. */
  42. function render($name, $value = null, $attributes = array(), $errors = array())
  43. {
  44. $date = $this->getDateWidget($attributes)->render($name, $value);
  45. if (!$this->getOption('with_time'))
  46. {
  47. return $date;
  48. }
  49. return strtr($this->getOption('format'), array(
  50. '%date%' => $date,
  51. '%time%' => $this->getTimeWidget($attributes)->render($name, $value),
  52. ));
  53. }
  54. /**
  55. * Returns the date widget.
  56. *
  57. * @param array $attributes An array of attributes
  58. *
  59. * @return sfWidgetForm A Widget representing the date
  60. */
  61. protected function getDateWidget($attributes = array())
  62. {
  63. return new sfWidgetFormDate($this->getOptionsFor('date'), $this->getAttributesFor('date', $attributes));
  64. }
  65. /**
  66. * Returns the time widget.
  67. *
  68. * @param array $attributes An array of attributes
  69. *
  70. * @return sfWidgetForm A Widget representing the time
  71. */
  72. protected function getTimeWidget($attributes = array())
  73. {
  74. return new sfWidgetFormTime($this->getOptionsFor('time'), $this->getAttributesFor('time', $attributes));
  75. }
  76. /**
  77. * Returns an array of options for the given type.
  78. *
  79. * @param string $type The type (date or time)
  80. *
  81. * @return array An array of options
  82. *
  83. * @throws InvalidArgumentException when option date|time type is not array
  84. */
  85. protected function getOptionsFor($type)
  86. {
  87. $options = $this->getOption($type);
  88. if (!is_array($options))
  89. {
  90. throw new InvalidArgumentException(sprintf('You must pass an array for the %s option.', $type));
  91. }
  92. return $options;
  93. }
  94. /**
  95. * Returns an array of HTML attributes for the given type.
  96. *
  97. * @param string $type The type (date or time)
  98. * @param array $attributes An array of attributes
  99. *
  100. * @return array An array of HTML attributes
  101. */
  102. protected function getAttributesFor($type, $attributes)
  103. {
  104. $defaults = isset($this->attributes[$type]) ? $this->attributes[$type] : array();
  105. return isset($attributes[$type]) ? array_merge($defaults, $attributes[$type]) : $defaults;
  106. }
  107. }

Debug toolbar