PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/widget/sfWidgetFormTime.class.php

https://github.com/coolpink/CpSymfony
PHP | 105 lines | 47 code | 9 blank | 49 comment | 3 complexity | 378eec7a8fefd94643686a8fcca30d94 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 29674 2010-05-30 12:35:21Z Kris.Wallsmith $
  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. * @param string $name The element name
  51. * @param string $value The time displayed in this widget
  52. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  53. * @param array $errors An array of errors for the field
  54. *
  55. * @return string An HTML tag string
  56. *
  57. * @see sfWidgetForm
  58. */
  59. public function render($name, $value = null, $attributes = array(), $errors = array())
  60. {
  61. // convert value to an array
  62. $default = array('hour' => null, 'minute' => null, 'second' => null);
  63. if (is_array($value))
  64. {
  65. $value = array_merge($default, $value);
  66. }
  67. else
  68. {
  69. $value = ctype_digit($value) ? (integer) $value : strtotime($value);
  70. if (false === $value)
  71. {
  72. $value = $default;
  73. }
  74. else
  75. {
  76. // int cast required to get rid of leading zeros
  77. $value = array('hour' => (int) date('H', $value), 'minute' => (int) date('i', $value), 'second' => (int) date('s', $value));
  78. }
  79. }
  80. $time = array();
  81. $emptyValues = $this->getOption('empty_values');
  82. // hours
  83. $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));
  84. $time['%hour%'] = $widget->render($name.'[hour]', $value['hour']);
  85. // minutes
  86. $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));
  87. $time['%minute%'] = $widget->render($name.'[minute]', $value['minute']);
  88. if ($this->getOption('with_seconds'))
  89. {
  90. // seconds
  91. $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));
  92. $time['%second%'] = $widget->render($name.'[second]', $value['second']);
  93. }
  94. return strtr($this->getOption('with_seconds') ? $this->getOption('format') : $this->getOption('format_without_seconds'), $time);
  95. }
  96. }