BUGFIX: fixed accessing private variable from Geoip. ENHANCEMENT: added Geoip::get_default_country_code(). Fixes #6315

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@115364 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Will Rossiter 2011-01-09 05:55:17 +00:00
parent d57c347452
commit 2ed7c9c1b0
3 changed files with 23 additions and 8 deletions

View File

@ -1,11 +1,15 @@
<?php <?php
/** /**
* A simple extension to dropdown field, pre-configured to list countries. * A simple extension to {@link DropdownField}, pre-configured to list countries.
* It will default to the country of the current visiotr. * It will default to the country of the current visitor or use the default
* country code provided using {@link Geoip::set_default_country_code()}.
*
* @package forms * @package forms
* @subpackage fields-relational * @subpackage fields-relational
*/ */
class CountryDropdownField extends DropdownField { class CountryDropdownField extends DropdownField {
protected $defaultToVisitorCountry = true; protected $defaultToVisitorCountry = true;
function __construct($name, $title = null, $source = null, $value = "", $form=null) { function __construct($name, $title = null, $source = null, $value = "", $form=null) {
@ -21,11 +25,11 @@ class CountryDropdownField extends DropdownField {
function Field() { function Field() {
$source = $this->getSource(); $source = $this->getSource();
if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) { if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) {
$this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::$default_country_code; $this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::get_default_country_code();
} }
return parent::Field(); return parent::Field();
} }
} }
?>

View File

@ -289,7 +289,7 @@ class Geoip {
} }
/** /**
* Set the default country * Set the default country code
* *
* @param string $country_code * @param string $country_code
*/ */
@ -297,6 +297,15 @@ class Geoip {
self::$default_country_code = $country_code; self::$default_country_code = $country_code;
} }
/**
* Returns the default country code
*
* @return string
*/
public static function get_default_country_code() {
return self::$default_country_code;
}
/** /**
* Find the country for an IP address. * Find the country for an IP address.
* *
@ -365,7 +374,7 @@ class Geoip {
// if geoip fails, lets default to default country code (if any) // if geoip fails, lets default to default country code (if any)
if(!isset($code) || !$code) { if(!isset($code) || !$code) {
$code = self::$default_country_code; $code = self::get_default_country_code();
} }
return ($code) ? $code : false; return ($code) ? $code : false;
@ -408,6 +417,7 @@ class Geoip {
/** /**
* Returns the country name from the appropriate code. * Returns the country name from the appropriate code.
*
* @return null|string String if country found, null if none found * @return null|string String if country found, null if none found
*/ */
static function countryCode2name($code) { static function countryCode2name($code) {

View File

@ -7,5 +7,6 @@ class GeoipTest extends SapphireTest {
Geoip::set_enabled(false); Geoip::set_enabled(false);
$this->assertEquals('DE', Geoip::visitor_country()); $this->assertEquals('DE', Geoip::visitor_country());
$this->assertEquals('DE', Geoip::get_default_country_code());
} }
} }