1. sfWidgetFormTime.class.php
  2. /** * sfWidgetFormTime represents a time widget. * * @package symfony * @subpackage widget * @author Fabien Potencier * @version SVN: $Id: sfWidgetFormTime.class.php 15283 2009-02-05 15:19:29Z fabien $ */
  3. class sfWidgetFormTime extends sfWidgetForm
  4. {
  5. /**
  6. * Constructor.
  7. *
  8. * Available options:
  9. *
  10. * * format: The time format string (%hour%:%minute%:%second%)
  11. * * format_without_seconds: The time format string without seconds (%hour%:%minute%)
  12. * * with_seconds: Whether to include a select for seconds (false by default)
  13. * * hours: An array of hours for the hour select tag (optional)
  14. * * minutes: An array of minutes for the minute select tag (optional)
  15. * * seconds: An array of seconds for the second select tag (optional)
  16. * * can_be_empty: Whether the widget accept an empty value (true by default)
  17. * * empty_values: An array of values to use for the empty value (empty string for hours, minutes, and seconds by default)
  18. *
  19. * @param array $options An array of options
  20. * @param array $attributes An array of default HTML attributes
  21. *
  22. * @see sfWidgetForm
  23. */
  24. protected function configure($options = array(), $attributes = array())
  25. {
  26. $this->addOption('format', '%hour%:%minute%:%second%');
  27. $this->addOption('format_without_seconds', '%hour%:%minute%');
  28. $this->addOption('with_seconds', false);
  29. $this->addOption('hours', parent::generateTwoCharsRange(0, 23));
  30. $this->addOption('minutes', parent::generateTwoCharsRange(0, 59));
  31. $this->addOption('seconds', parent::generateTwoCharsRange(0, 59));
  32. $this->addOption('can_be_empty', true);
  33. $this->addOption('empty_values', array('hour' => '', 'minute' => '', 'second' => ''));
  34. }
  35. /**
  36. * @param string $name The element name
  37. * @param string $value The time displayed in this widget
  38. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  39. * @param array $errors An array of errors for the field
  40. *
  41. * @return string An HTML tag string
  42. *
  43. * @see sfWidgetForm
  44. */
  45. public function render($name, $value = null, $attributes = array(), $errors = array())
  46. {
  47. // convert value to an array
  48. $default = array('hour' => null, 'minute' => null, 'second' => null);
  49. if (is_array($value))
  50. {
  51. $value = array_merge($default, $value);
  52. }
  53. else
  54. {
  55. $value = ctype_digit($value) ? (integer) $value : strtotime($value);
  56. if (false === $value)
  57. {
  58. $value = $default;
  59. }
  60. else
  61. {
  62. // int cast required to get rid of leading zeros
  63. $value = array('hour' => (int) date('H', $value), 'minute' => (int) date('i', $value), 'second' => (int) date('s', $value));
  64. }
  65. }
  66. $time = array();
  67. $emptyValues = $this->getOption('empty_values');
  68. // hours
  69. $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['hour']) + $this->getOption('hours') : $this->getOption('hours')), array_merge($this->attributes, $attributes));
  70. $time['%hour%'] = $widget->render($name.'[hour]', $value['hour']);
  71. // minutes
  72. $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['minute']) + $this->getOption('minutes') : $this->getOption('minutes')), array_merge($this->attributes, $attributes));
  73. $time['%minute%'] = $widget->render($name.'[minute]', $value['minute']);
  74. if ($this->getOption('with_seconds'))
  75. {
  76. // seconds
  77. $widget = new sfWidgetFormSelect(array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['second']) + $this->getOption('seconds') : $this->getOption('seconds')), array_merge($this->attributes, $attributes));
  78. $time['%second%'] = $widget->render($name.'[second]', $value['second']);
  79. }
  80. return strtr($this->getOption('with_seconds') ? $this->getOption('format') : $this->getOption('format_without_seconds'), $time);
  81. }
  82. }

Debug toolbar