Permalink
Browse files

Initial commit of geoip module

  • Loading branch information...
0 parents commit 7db53b54a1ead1e2c7e87abe749deee0f4d67f5e @halkyon halkyon committed Mar 17, 2011
Showing with 496 additions and 0 deletions.
  1. +7 −0 README.md
  2. +1 −0 _config.php
  3. +35 −0 code/CountryDropdownField.php
  4. +441 −0 code/Geoip.php
  5. +12 −0 tests/GeoipTest.php
@@ -0,0 +1,7 @@
+GeoIP Module
+============
+
+Maintainer Contact
+------------------
+Sean Harvey (Nickname: sharvey, halkyon)
+<sean (at) silverstripe (dot) com>
@@ -0,0 +1 @@
+<?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();
+ }
+}
Oops, something went wrong.

0 comments on commit 7db53b5

Please sign in to comment.