sfWidgetFormDateTime.class.php 3.9 KB

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