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

Debug toolbar