mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
d57c347452
commit
2ed7c9c1b0
@ -1,11 +1,15 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* A simple extension to dropdown field, pre-configured to list countries.
|
||||
* It will default to the country of the current visiotr.
|
||||
* 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()}.
|
||||
*
|
||||
* @package forms
|
||||
* @subpackage fields-relational
|
||||
*/
|
||||
class CountryDropdownField extends DropdownField {
|
||||
|
||||
protected $defaultToVisitorCountry = true;
|
||||
|
||||
function __construct($name, $title = null, $source = null, $value = "", $form=null) {
|
||||
@ -21,11 +25,11 @@ class CountryDropdownField extends DropdownField {
|
||||
|
||||
function Field() {
|
||||
$source = $this->getSource();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -289,7 +289,7 @@ class Geoip {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default country
|
||||
* Set the default country code
|
||||
*
|
||||
* @param string $country_code
|
||||
*/
|
||||
@ -297,6 +297,15 @@ class Geoip {
|
||||
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.
|
||||
*
|
||||
@ -365,7 +374,7 @@ class Geoip {
|
||||
|
||||
// if geoip fails, lets default to default country code (if any)
|
||||
if(!isset($code) || !$code) {
|
||||
$code = self::$default_country_code;
|
||||
$code = self::get_default_country_code();
|
||||
}
|
||||
|
||||
return ($code) ? $code : false;
|
||||
@ -408,6 +417,7 @@ class Geoip {
|
||||
|
||||
/**
|
||||
* Returns the country name from the appropriate code.
|
||||
*
|
||||
* @return null|string String if country found, null if none found
|
||||
*/
|
||||
static function countryCode2name($code) {
|
||||
|
@ -7,5 +7,6 @@ class GeoipTest extends SapphireTest {
|
||||
Geoip::set_enabled(false);
|
||||
|
||||
$this->assertEquals('DE', Geoip::visitor_country());
|
||||
$this->assertEquals('DE', Geoip::get_default_country_code());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user