sfWidgetFormI18nDate.class.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * sfWidgetFormI18nDate represents a date widget.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormI18nDate.class.php 9046 2008-05-19 08:13:51Z FabianLange $
  16. */
  17. class sfWidgetFormI18nDate extends sfWidgetFormDate
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * Available options:
  23. *
  24. * * culture: The culture to use for internationalized strings (required)
  25. * * month_format: The month format (name - default, short_name, number)
  26. *
  27. * @param array $options An array of options
  28. * @param array $attributes An array of default HTML attributes
  29. *
  30. * @see sfWidgetFormDate
  31. */
  32. protected function configure($options = array(), $attributes = array())
  33. {
  34. parent::configure($options, $attributes);
  35. $this->addRequiredOption('culture');
  36. $this->addOption('month_format');
  37. $culture = isset($options['culture']) ? $options['culture'] : 'en';
  38. $monthFormat = isset($options['month_format']) ? $options['month_format'] : 'name';
  39. // format
  40. $this->setOption('format', $this->getDateFormat($culture));
  41. // months
  42. $this->setOption('months', $this->getMonthFormat($culture, $monthFormat));
  43. }
  44. protected function getMonthFormat($culture, $monthFormat)
  45. {
  46. switch ($monthFormat)
  47. {
  48. case 'name':
  49. return array_combine(range(1, 12), sfDateTimeFormatInfo::getInstance($culture)->getMonthNames());
  50. case 'short_name':
  51. return array_combine(range(1, 12), sfDateTimeFormatInfo::getInstance($culture)->getAbbreviatedMonthNames());
  52. case 'number':
  53. return $this->getOption('months');
  54. default:
  55. throw new InvalidArgumentException(sprintf('The month format "%s" is invalid.', $monthFormat));
  56. }
  57. }
  58. protected function getDateFormat($culture)
  59. {
  60. $dateFormat = sfDateTimeFormatInfo::getInstance($culture)->getShortDatePattern();
  61. if (false === ($dayPos = stripos($dateFormat, 'd')) || false === ($monthPos = stripos($dateFormat, 'm')) || false === ($yearPos = stripos($dateFormat, 'y')))
  62. {
  63. return $this->getOption('format');
  64. }
  65. return strtr($dateFormat, array(
  66. substr($dateFormat, $dayPos, strripos($dateFormat, 'd') - $dayPos + 1) => '%day%',
  67. substr($dateFormat, $monthPos, strripos($dateFormat, 'm') - $monthPos + 1) => '%month%',
  68. substr($dateFormat, $yearPos, strripos($dateFormat, 'y') - $yearPos + 1) => '%year%',
  69. ));
  70. }
  71. }