/lib/vendor/symfony-1.4.14/lib/widget/sfWidgetFormTime.class.php

https://github.com/yuya-takeyama/symfony-hackathon-20110924 · PHP · 107 lines · 47 code · 9 blank · 51 comment · 3 complexity · 966991acd5d013bd426d06895ee6459a MD5 · raw file

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