| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
|
|---|
| 45 |
$this->setOption('format', $this->getDateFormat($culture)); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 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 |
} |
|---|
| 82 |
|
|---|