sfWidgetFormI18nTime.class.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * sfWidgetFormI18nTime represents a time widget.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormI18nTime.class.php 9046 2008-05-19 08:13:51Z FabianLange $
  16. */
  17. class sfWidgetFormI18nTime extends sfWidgetFormTime
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * Available options:
  23. *
  24. * * culture: The culture to use for internationalized strings (required)
  25. *
  26. * @param array $options An array of options
  27. * @param array $attributes An array of default HTML attributes
  28. *
  29. * @see sfWidgetFormTime
  30. */
  31. protected function configure($options = array(), $attributes = array())
  32. {
  33. parent::configure($options, $attributes);
  34. $this->addRequiredOption('culture');
  35. $culture = isset($options['culture']) ? $options['culture'] : 'en';
  36. // format
  37. $this->setOption('format', $this->getTimeFormat($culture, true));
  38. // format_without_seconds
  39. $this->setOption('format_without_seconds', $this->getTimeFormat($culture, false));
  40. }
  41. protected function getTimeFormat($culture, $withSeconds)
  42. {
  43. $timeFormat = $withSeconds ? sfDateTimeFormatInfo::getInstance($culture)->getMediumTimePattern() : sfDateTimeFormatInfo::getInstance($culture)->getShortTimePattern();
  44. if (false === ($hourPos = stripos($timeFormat, 'h')) || false === ($minutePos = stripos($timeFormat, 'm')))
  45. {
  46. return $this->getOption('format');
  47. }
  48. $trans = array(
  49. substr($timeFormat, $hourPos, strripos($timeFormat, 'h') - $hourPos + 1) => '%hour%',
  50. substr($timeFormat, $minutePos, strripos($timeFormat, 'm') - $minutePos + 1) => '%minute%',
  51. );
  52. if ($withSeconds)
  53. {
  54. if (false === $secondPos = stripos($timeFormat, 's'))
  55. {
  56. return $this->getOption('format');
  57. }
  58. $trans[substr($timeFormat, $secondPos, strripos($timeFormat, 's') - $secondPos + 1)] = '%second%';
  59. }
  60. return strtr($timeFormat, $trans);
  61. }
  62. }