2012-06-21 05:34:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple extension to dropdown field, pre-configured to list countries.
|
|
|
|
* It will default to the country of the current visitor.
|
|
|
|
*
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
|
|
|
class CountryDropdownField extends DropdownField {
|
|
|
|
|
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Should we default the dropdown to the region determined from the user's locale?
|
2015-02-13 05:35:39 +01:00
|
|
|
*
|
|
|
|
* @config
|
2012-09-26 23:34:00 +02:00
|
|
|
* @var bool
|
2012-06-21 05:34:03 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_to_locale = true;
|
2012-06-21 05:34:03 +02:00
|
|
|
|
|
|
|
/**
|
2015-02-13 05:35:39 +01:00
|
|
|
* The region code to default to if default_to_locale is set to false, or we can't
|
|
|
|
* determine a region from a locale.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
2012-06-21 05:34:03 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_country = 'NZ';
|
2012-06-21 05:34:03 +02:00
|
|
|
|
2012-06-26 17:56:07 +02:00
|
|
|
protected $extraClasses = array('dropdown');
|
|
|
|
|
2012-06-21 05:34:03 +02:00
|
|
|
/**
|
|
|
|
* Get the locale of the Member, or if we're not logged in or don't have a locale, use the default one
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function locale() {
|
2015-02-13 05:35:39 +01:00
|
|
|
if (($member = Member::currentUser()) && $member->Locale) {
|
|
|
|
return $member->Locale;
|
|
|
|
}
|
2012-06-21 05:34:03 +02:00
|
|
|
return i18n::get_locale();
|
|
|
|
}
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
public function setSource($source) {
|
|
|
|
if($source) {
|
|
|
|
return parent::setSource($source);
|
|
|
|
}
|
2012-06-21 05:34:03 +02:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// map empty source to country list
|
|
|
|
// Get a list of countries from Zend
|
|
|
|
$source = Zend_Locale::getTranslationList('territory', $this->locale(), 2);
|
2012-06-21 05:34:03 +02:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// We want them ordered by display name, not country code
|
2012-06-21 05:34:03 +02:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// PHP 5.3 has an extension that sorts UTF-8 strings correctly
|
|
|
|
if (class_exists('Collator') && ($collator = Collator::create($this->locale()))) {
|
|
|
|
$collator->asort($source);
|
|
|
|
} else {
|
|
|
|
// Otherwise just put up with them being weirdly ordered for now
|
|
|
|
asort($source);
|
2012-06-21 05:34:03 +02:00
|
|
|
}
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// We don't want "unknown country" as an option
|
|
|
|
unset($source['ZZ']);
|
|
|
|
|
|
|
|
return parent::setSource($source);
|
2012-06-21 05:34:03 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-06-21 05:34:03 +02:00
|
|
|
$source = $this->getSource();
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Default value to best availabel locale
|
|
|
|
$value = $this->Value();
|
|
|
|
if ($this->config()->default_to_locale
|
|
|
|
&& (!$value || !isset($source[$value]))
|
|
|
|
&& $this->locale()
|
|
|
|
) {
|
|
|
|
$locale = new Zend_Locale();
|
|
|
|
$locale->setLocale($this->locale());
|
|
|
|
$value = $locale->getRegion();
|
|
|
|
$this->setValue($value);
|
2012-06-21 05:34:03 +02:00
|
|
|
}
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Default to default country otherwise
|
|
|
|
if (!$value || !isset($source[$value])) {
|
|
|
|
$this->setValue($this->config()->default_country);
|
2012-06-21 05:34:03 +02:00
|
|
|
}
|
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
return parent::Field($properties);
|
2012-06-21 05:34:03 +02:00
|
|
|
}
|
|
|
|
}
|