2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2011-01-09 06:55:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2011-01-09 06:55:17 +01: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 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CountryDropdownField extends DropdownField {
|
2011-01-09 06:55:17 +01:00
|
|
|
|
2008-09-18 23:33:23 +02:00
|
|
|
protected $defaultToVisitorCountry = true;
|
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
function __construct($name, $title = null, $source = null, $value = "", $form=null) {
|
2008-11-10 04:51:35 +01:00
|
|
|
if(!is_array($source)) $source = Geoip::getCountryDropDown();
|
|
|
|
if(!$value) $value = Geoip::visitor_country();
|
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
parent::__construct($name, ($title===null) ? $name : $title, $source, $value, $form);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-09-18 23:33:23 +02:00
|
|
|
function defaultToVisitorCountry($val) {
|
|
|
|
$this->defaultToVisitorCountry = $val;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function Field() {
|
2008-10-15 14:39:09 +02:00
|
|
|
$source = $this->getSource();
|
2011-01-09 06:55:17 +01:00
|
|
|
|
2008-10-15 14:39:09 +02:00
|
|
|
if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) {
|
2011-01-09 06:55:17 +01:00
|
|
|
$this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::get_default_country_code();
|
2008-10-15 05:26:24 +02:00
|
|
|
}
|
2011-01-09 06:55:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return parent::Field();
|
|
|
|
}
|
2011-01-09 06:55:17 +01:00
|
|
|
}
|