Skip to content

Commit

Permalink
Initial commit of geoip module
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Harvey committed Mar 17, 2011
0 parents commit 7db53b5
Show file tree
Hide file tree
Showing 5 changed files with 496 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
@@ -0,0 +1,7 @@
GeoIP Module
============

Maintainer Contact
------------------
Sean Harvey (Nickname: sharvey, halkyon)
<sean (at) silverstripe (dot) com>
1 change: 1 addition & 0 deletions _config.php
@@ -0,0 +1 @@
<?php
35 changes: 35 additions & 0 deletions code/CountryDropdownField.php
@@ -0,0 +1,35 @@
<?php

/**
* 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) {
if(!is_array($source)) $source = Geoip::getCountryDropDown();
if(!$value) $value = Geoip::visitor_country();

parent::__construct($name, ($title===null) ? $name : $title, $source, $value, $form);
}

function defaultToVisitorCountry($val) {
$this->defaultToVisitorCountry = $val;
}

function Field() {
$source = $this->getSource();

if($this->defaultToVisitorCountry && !$this->value || !isset($source[$this->value])) {
$this->value = ($vc = Geoip::visitor_country()) ? $vc : Geoip::get_default_country_code();
}

return parent::Field();
}
}

0 comments on commit 7db53b5

Please sign in to comment.