Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * sfWidgetFormI18nDate represents a date widget.
13
 *
14
 * @package    symfony
15
 * @subpackage widget
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfWidgetFormI18nDate.class.php 9046 2008-05-19 08:13:51Z FabianLange $
18
 */
19
class sfWidgetFormI18nDate extends sfWidgetFormDate
20
{
21
  /**
22
   * Constructor.
23
   *
24
   * Available options:
25
   *
26
   *  * culture:      The culture to use for internationalized strings (required)
27
   *  * month_format: The month format (name - default, short_name, number)
28
   *
29
   * @param array $options     An array of options
30
   * @param array $attributes  An array of default HTML attributes
31
   *
32
   * @see sfWidgetFormDate
33
   */
34
  protected function configure($options = array(), $attributes = array())
35
  {
36
    parent::configure($options, $attributes);
37
 
38
    $this->addRequiredOption('culture');
39
    $this->addOption('month_format');
40
 
41
    $culture = isset($options['culture']) ? $options['culture'] : 'en';
42
    $monthFormat = isset($options['month_format']) ? $options['month_format'] : 'name';
43
 
44
    // format
45
    $this->setOption('format', $this->getDateFormat($culture));
46
 
47
    // months
48
    $this->setOption('months', $this->getMonthFormat($culture, $monthFormat));
49
  }
50
 
51
  protected function getMonthFormat($culture, $monthFormat)
52
  {
53
    switch ($monthFormat)
54
    {
55
      case 'name':
56
        return array_combine(range(1, 12), sfDateTimeFormatInfo::getInstance($culture)->getMonthNames());
57
      case 'short_name':
58
        return array_combine(range(1, 12), sfDateTimeFormatInfo::getInstance($culture)->getAbbreviatedMonthNames());
59
      case 'number':
60
        return $this->getOption('months');
61
      default:
62
        throw new InvalidArgumentException(sprintf('The month format "%s" is invalid.', $monthFormat));
63
    }
64
  }
65
 
66
  protected function getDateFormat($culture)
67
  {
68
    $dateFormat = sfDateTimeFormatInfo::getInstance($culture)->getShortDatePattern();
69
 
70
    if (false === ($dayPos = stripos($dateFormat, 'd')) || false === ($monthPos = stripos($dateFormat, 'm')) || false === ($yearPos = stripos($dateFormat, 'y')))
71
    {
72
      return $this->getOption('format');
73
    }
74
 
75
    return strtr($dateFormat, array(
76
      substr($dateFormat, $dayPos,   strripos($dateFormat, 'd') - $dayPos + 1)   => '%day%',
77
      substr($dateFormat, $monthPos, strripos($dateFormat, 'm') - $monthPos + 1) => '%month%',
78
      substr($dateFormat, $yearPos,  strripos($dateFormat, 'y') - $yearPos + 1)  => '%year%',
79
    ));
80
  }
81
}