2007-07-19 10:40:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A simple extension to dropdown field, pre-configured to list countries.
|
|
|
|
* It will default to the country of the current visiotr.
|
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 {
|
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();
|
|
|
|
if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) {
|
2008-10-15 03:26:24 +00:00
|
|
|
$this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::$default_country_code;
|
|
|
|
}
|
2007-07-19 10:40:28 +00:00
|
|
|
return parent::Field();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-01 23:49:53 +00:00
|
|
|
?>
|