| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class ExtendedWidgetFormI18nSelectCountry extends sfWidgetFormSelect |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
* Widget configuration |
|---|
| 22 |
* |
|---|
| 23 |
* Available options: |
|---|
| 24 |
* |
|---|
| 25 |
* * culture: The culture to use for internationalized strings (required) |
|---|
| 26 |
* * countries: An array of country codes to use (ISO 3166) |
|---|
| 27 |
* * add_label_option: Adds a custom option at the top of the selector, which the value is empty |
|---|
| 28 |
* |
|---|
| 29 |
* @param array $options An array of options |
|---|
| 30 |
* @param array $attributes An array of default HTML attributes |
|---|
| 31 |
* |
|---|
| 32 |
* @see sfWidgetFormSelect |
|---|
| 33 |
*/ |
|---|
| 34 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 35 |
{ |
|---|
| 36 |
parent::configure($options, $attributes); |
|---|
| 37 |
|
|---|
| 38 |
$this->addRequiredOption('culture'); |
|---|
| 39 |
$this->addOption('countries'); |
|---|
| 40 |
$this->addOption('add_label_option'); |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
$culture = isset($options['culture']) ? $options['culture'] : 'en'; |
|---|
| 44 |
|
|---|
| 45 |
$cultureInfo = new sfCultureInfo($culture); |
|---|
| 46 |
$countries = $cultureInfo->getCountries(); |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
if (isset($options['countries'])) |
|---|
| 50 |
{ |
|---|
| 51 |
if ($problems = array_diff($options['countries'], array_keys($countries))) |
|---|
| 52 |
{ |
|---|
| 53 |
throw new InvalidArgumentException(sprintf('The following countries do not exist: %s.', implode(', ', $problems))); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
$countries = array_intersect_key($countries, array_flip($options['countries'])); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
asort($countries); |
|---|
| 60 |
|
|---|
| 61 |
if (isset($options['add_label_option'])) |
|---|
| 62 |
{ |
|---|
| 63 |
$countries = array('' => $options['add_label_option']) + $countries; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
$this->setOption('choices', $countries); |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|