2007-07-19 10:40:28 +00:00
|
|
|
<?php
|
2011-01-09 05:55:17 +00:00
|
|
|
|
2007-07-19 10:40:28 +00:00
|
|
|
/**
|
2011-01-09 05:55:17 +00:00
|
|
|
* A simple extension to {@link DropdownField}, pre-configured to list countries.
|
|
|
|
* It will default to the country of the current visitor or use the default
|
|
|
|
* country code provided using {@link Geoip::set_default_country_code()}.
|
|
|
|
*
|
2008-01-09 04:18:36 +00:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
2007-07-19 10:40:28 +00:00
|
|
|
*/
|
|
|
|
class CountryDropdownField extends DropdownField {
|
2011-01-09 05:55:17 +00:00
|
|
|
|
2008-09-18 21:33:23 +00:00
|
|
|
protected $defaultToVisitorCountry = true;
|
|
|
|
|
2009-01-05 06:19:48 +00:00
|
|
|
function __construct($name, $title = null, $source = null, $value = "", $form=null) {
|
2008-11-10 03:51:35 +00:00
|
|
|
if(!is_array($source)) $source = Geoip::getCountryDropDown();
|
|
|
|
if(!$value) $value = Geoip::visitor_country();
|
|
|
|
|
2009-01-05 06:19:48 +00:00
|
|
|
parent::__construct($name, ($title===null) ? $name : $title, $source, $value, $form);
|
2007-07-19 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
2008-09-18 21:33:23 +00:00
|
|
|
function defaultToVisitorCountry($val) {
|
|
|
|
$this->defaultToVisitorCountry = $val;
|
|
|
|
}
|
|
|
|
|
2007-07-19 10:40:28 +00:00
|
|
|
function Field() {
|
2008-10-15 12:39:09 +00:00
|
|
|
$source = $this->getSource();
|
2011-01-09 05:55:17 +00:00
|
|
|
|
2008-10-15 12:39:09 +00:00
|
|
|
if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) {
|
2011-01-09 05:55:17 +00:00
|
|
|
$this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::get_default_country_code();
|
2008-10-15 03:26:24 +00:00
|
|
|
}
|
2011-01-09 05:55:17 +00:00
|
|
|
|
2007-07-19 10:40:28 +00:00
|
|
|
return parent::Field();
|
|
|
|
}
|
2011-01-09 05:55:17 +00:00
|
|
|
}
|