From 01062c70a1cd36f40a46a63b637b5bdc6f32a2a0 Mon Sep 17 00:00:00 2001 From: Hamish Friedlander Date: Thu, 21 Jun 2012 15:34:03 +1200 Subject: [PATCH] ENHANCEMENT: Add back CountryDropdownField We removed this because it depended on GeoIP, which was weird, and had its own dependancies. But Zend_Locale is included in core, and can give us all the data we need for CountryDropdownField --- forms/CountryDropdownField.php | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 forms/CountryDropdownField.php diff --git a/forms/CountryDropdownField.php b/forms/CountryDropdownField.php new file mode 100644 index 000000000..43fa12a97 --- /dev/null +++ b/forms/CountryDropdownField.php @@ -0,0 +1,71 @@ +Locale) return $member->Locale; + return i18n::get_locale(); + } + + function __construct($name, $title = null, $source = null, $value = "", $form=null) { + if(!is_array($source)) { + // Get a list of countries from Zend + $source = Zend_Locale::getTranslationList('territory', $this->locale(), 2); + + // We want them ordered by display name, not country code + + // PHP 5.3 has an extension that sorts UTF-8 strings correctly + if (class_exists('Collator') && ($collator = Collator::create($this->locale()))) { + $collator->asort($source); + } + // Otherwise just put up with them being weirdly ordered for now + else { + asort($source); + } + + // We don't want "unknown country" as an option + unset($source['ZZ']); + } + + parent::__construct($name, ($title===null) ? $name : $title, $source, $value, $form); + } + + function Field($properties = array()) { + $source = $this->getSource(); + + if (!$this->value || !isset($source[$this->value])) { + if ($this->config()->get('default_to_locale') && $this->locale()) { + $locale = new Zend_Locale(); + $locale->setLocale($this->locale()); + $this->value = $locale->getRegion(); + } + } + + if (!$this->value || !isset($source[$this->value])) { + $this->value = $this->config()->get('default_country'); + } + + return parent::Field(); + } +}