aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-25 20:09:14 +0000
committermarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-25 20:09:14 +0000
commit53d085f513d5e29af160991739e7ad1b515090a7 (patch)
treead7ca627fbf2656653edbc40f05e2c192da31078
parentbf9e9feefa7e1f3af12a534999817eae9bca24ca (diff)
downloadelgg-53d085f513d5e29af160991739e7ad1b515090a7.tar.gz
elgg-53d085f513d5e29af160991739e7ad1b515090a7.tar.bz2
Hook for future geocoding library added.
git-svn-id: https://code.elgg.org/elgg/trunk@2488 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r--engine/lib/entities.php51
-rw-r--r--engine/lib/location.php94
-rw-r--r--engine/schema/mysql.sql12
-rw-r--r--engine/schema/upgrades/2008112501.sql10
-rw-r--r--version.php2
5 files changed, 168 insertions, 1 deletions
diff --git a/engine/lib/entities.php b/engine/lib/entities.php
index 99c8bdca6..b645fc372 100644
--- a/engine/lib/entities.php
+++ b/engine/lib/entities.php
@@ -17,6 +17,9 @@
/// Cache subtype searches
$SUBTYPE_CACHE = NULL;
+ /// Require the locatable interface TODO: Move this into start.php?
+ require_once('location.php');
+
/**
* ElggEntity The elgg entity superclass
* This class holds methods for accessing the main entities table.
@@ -27,6 +30,7 @@
*/
abstract class ElggEntity implements
Notable, // Calendar interface
+ Locatable, // Geocoding interface
Exportable, // Allow export of data
Importable, // Allow import of data
Loggable, // Can events related to this object class be logged
@@ -711,6 +715,53 @@
return $res;
}
+ // LOCATABLE INTERFACE /////////////////////////////////////////////////////////////
+
+ /** Interface to set the location */
+ public function setLocation($location)
+ {
+ $location = sanitise_string($location);
+
+ $this->location = $location;
+
+ return true;
+ }
+
+ /**
+ * Set latitude and longitude tags for a given entity.
+ *
+ * @param float $lat
+ * @param float $long
+ */
+ public function setLatLong($lat, $long)
+ {
+ $lat = sanitise_string($lat);
+ $long = sanitise_string($long);
+
+ $this->set('geo:lat', $lat);
+ $this->set('geo:long', $long);
+
+ return true;
+ }
+
+ /**
+ * Get the contents of the ->geo:lat field.
+ *
+ */
+ public function getLatitude() { return $this->get('geo:lat'); }
+
+ /**
+ * Get the contents of the ->geo:lat field.
+ *
+ */
+ public function getLongitude() { return $this->get('geo:long'); }
+
+ /**
+ * Get the ->location metadata.
+ *
+ */
+ public function getLocation() { return $this->get('location'); }
+
// NOTABLE INTERFACE ///////////////////////////////////////////////////////////////
/**
diff --git a/engine/lib/location.php b/engine/lib/location.php
new file mode 100644
index 000000000..eec665234
--- /dev/null
+++ b/engine/lib/location.php
@@ -0,0 +1,94 @@
+<?php
+
+ /**
+ * Elgg geo-location tagging library.
+ *
+ * @package Elgg
+ * @subpackage Core
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Curverider Ltd
+ * @copyright Curverider Ltd 2008
+ * @link http://elgg.org/
+ */
+
+ /**
+ * Define an interface for geo-tagging entities.
+ *
+ */
+ interface Locatable
+ {
+ /** Set a location text */
+ public function setLocation($location);
+
+ /**
+ * Set latitude and longitude tags for a given entity.
+ *
+ * @param float $lat
+ * @param float $long
+ */
+ public function setLatLong($lat, $long);
+
+ /**
+ * Get the contents of the ->geo:lat field.
+ *
+ */
+ public function getLatitude();
+
+ /**
+ * Get the contents of the ->geo:lat field.
+ *
+ */
+ public function getLongitude();
+
+ /**
+ * Get the ->location metadata.
+ *
+ */
+ public function getLocation();
+ }
+
+ /**
+ * Encode a location into a latitude and longitude, caching the result.
+ *
+ * Works by triggering the 'geocode' 'location' plugin hook, and requires a geocoding module to be installed
+ * activated in order to work.
+ *
+ * @param String $location The location, e.g. "London", or "24 Foobar Street, Gotham City"
+ */
+ function elgg_geocode_location($location)
+ {
+ global $CONFIG;
+
+ $location = sanitise_string($location);
+
+ // Look for cached version
+ $cached_location = get_data_row("SELECT * from {$CONFIG->dbprefix}geocode_cache WHERE location='$location'");
+
+ // Trigger geocode event
+ $return = false;
+ $return = trigger_plugin_hook('geocode', 'location', array('location' => $location, $return));
+
+ // If returned, cache and return value
+ if (($return) && (is_array($return)))
+ {
+ $lat = (int)$return['lat'];
+ $long = (int)$return['long'];
+
+ // Put into cache at the end of the page since we don't really care that much
+ execute_delayed_write_query("INSERT DELAYED INTO {$CONFIG->dbprefix}geocode_cache (lat, long) VALUES ({$lat}, {$long}) ON DUPLICATE KEY UPDATE lat={$lat} long={$long}");
+ }
+
+ return $return;
+ }
+
+ // Some distances in degrees (approximate)
+ define("MILE", 0.01515);
+ define("KILOMETER", 0.00932);
+
+
+ // TODO: get objects within x miles by entities, metadata and relationship
+
+ // TODO: List
+
+
+?> \ No newline at end of file
diff --git a/engine/schema/mysql.sql b/engine/schema/mysql.sql
index bf2833ee4..59f11357b 100644
--- a/engine/schema/mysql.sql
+++ b/engine/schema/mysql.sql
@@ -274,6 +274,18 @@ CREATE TABLE `prefix_hmac_cache` (
KEY `ts` (`ts`)
) ENGINE=MEMORY;
+-- Geocode engine cache
+CREATE TABLE `prefix_geocode_cache` (
+ id int(11) auto_increment,
+ location varchar(128),
+ `lat` varchar(20),
+ `long` varchar(20),
+
+ PRIMARY KEY (`id`),
+ KEY `location` (`location`)
+
+) ENGINE=MEMORY;
+
-- PHP Session storage
CREATE TABLE `prefix_users_sessions` (
`session` varchar(255) NOT NULL,
diff --git a/engine/schema/upgrades/2008112501.sql b/engine/schema/upgrades/2008112501.sql
new file mode 100644
index 000000000..41e41f93e
--- /dev/null
+++ b/engine/schema/upgrades/2008112501.sql
@@ -0,0 +1,10 @@
+CREATE TABLE IF NOT EXISTS `prefix_geocode_cache` (
+ id int(11) auto_increment,
+ location varchar(128),
+ `lat` varchar(20),
+ `long` varchar(20),
+
+ PRIMARY KEY (`id`),
+ KEY `location` (`location`)
+
+) ENGINE=MEMORY; \ No newline at end of file
diff --git a/version.php b/version.php
index d7184d437..93850eb08 100644
--- a/version.php
+++ b/version.php
@@ -13,7 +13,7 @@
* @link http://elgg.org/
*/
- $version = 2008112002; // YYYYMMDD = Elgg Date
+ $version = 2008112501; // YYYYMMDD = Elgg Date
// XX = Interim incrementer
$release = '1.1'; // Human-friendly version name