From da8ec772c62f609aaee1566543a524c2f91e5e55 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Thu, 3 Dec 2009 00:05:07 -0500 Subject: [PATCH] Start tying in the map ui to the backend storage. This implements saving the geolocation obtained from a geocoding request and adds some other plumbing needed for the rest of the tasks. --- kronolith/js/kronolith.js | 23 +++-- kronolith/lib/Driver/Sql.php | 10 +++ kronolith/lib/Event.php | 17 ++++ kronolith/lib/Geo.php | 49 ++++++++++ kronolith/lib/Geo/Mysql.php | 98 ++++++++++++++++++++ kronolith/lib/Geo/Sql.php | 177 +++++++++++++++++++++++++++++++++++++ kronolith/lib/Kronolith.php | 11 +++ kronolith/templates/index/edit.inc | 2 + 8 files changed, 378 insertions(+), 9 deletions(-) create mode 100644 kronolith/lib/Geo.php create mode 100644 kronolith/lib/Geo/Mysql.php create mode 100644 kronolith/lib/Geo/Sql.php diff --git a/kronolith/js/kronolith.js b/kronolith/js/kronolith.js index aed627c2a..d45e8f124 100644 --- a/kronolith/js/kronolith.js +++ b/kronolith/js/kronolith.js @@ -3560,22 +3560,27 @@ KronolithEventMap = // Do something else with the lonlat? }, - onError: function(r) { }, + onError: function(r) + { + KronolithCore.showNotifications([ { type: 'horde.error', message: Kronolith.text.geocode_error } ]); + }, - onGeocode: function(r) { }, + onGeocode: function(r) + { + r = r.shift(); + ll = new OpenLayers.LonLat(r.lon, r.lat); + this._map.moveMarker(this._marker, { lat: r.lat, lon: r.lon }); + this._map.setCenter(ll); + $('kronolithEventLocationLon').value = r.lon; + $('kronolithEventLocationLat').value = r.lat; + }, geocode: function(a) { if (!a) { return; } var gc = new HordeMap.Geocoder[Kronolith.conf.maps.geocoder](); - gc.geocode(a, function(r) { - r = r.shift(); - ll = new OpenLayers.LonLat(r.lon, r.lat); - this._map.moveMarker(this._marker, { lat: r.lat, lon: r.lon }); - this._map.setCenter(ll); - }.bind(this), - this.onError); + gc.geocode(a, this.onGeocode.bind(this), this.onError); } }; diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index 9ce1266c7..0818a67f3 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -579,6 +579,16 @@ class Kronolith_Driver_Sql extends Kronolith_Driver $tagger = Kronolith::getTagger(); $tagger->replaceTags($event->getUID(), $event->tags, 'event'); + /* Update Geolocation */ + if ($gDriver = Kronolith::getGeoDriver()) { + try { + $gDriver->setLocation($event->getId(), $event->geoLocation); + } catch (Horde_Exception $e) { + Horde::logMessage($e->getMessage(), __FILE__, __LINE__, PEAR_LOG_ERR); + return new PEAR_Error($e->getMessage()); + } + } + /* Notify users about the changed event. */ $result = Kronolith::sendNotification($event, 'edit'); if (is_a($result, 'PEAR_Error')) { diff --git a/kronolith/lib/Event.php b/kronolith/lib/Event.php index add0f8a63..44682816f 100644 --- a/kronolith/lib/Event.php +++ b/kronolith/lib/Event.php @@ -113,6 +113,12 @@ abstract class Kronolith_Event public $tags = array(); /** + * Geolocation + * + */ + public $geoLocation = null; + + /** * Whether this is the event on the first day of a multi-day event. * * @var boolen @@ -257,9 +263,16 @@ abstract class Kronolith_Event } if ($eventObject !== null) { + + /* Get tags */ $this->fromDriver($eventObject); $tagger = Kronolith::getTagger(); $this->tags = $tagger->getTags($this->getUID(), 'event'); + + /* Get geolocation data */ + if ($gDriver = Kronolith::getGeoDriver()) { + $this->geoLocation = $gDriver->getLocation($this->getId()); + } } } @@ -1164,6 +1177,7 @@ abstract class Kronolith_Event $json->et = $this->end->format($time_format); $json->a = $this->alarm; $json->tg = array_values($this->tags); + $json->gl = array_values($this->geoLocation); if ($this->recurs()) { $json->r = $this->recurrence->toJson(); } @@ -2052,6 +2066,9 @@ abstract class Kronolith_Event // Tags. $this->tags = Horde_Util::getFormData('tags', $this->tags); + // Geolocation + $this->geoLocation = array('lat' => Horde_Util::getFormData('lat'), + 'lon' => Horde_Util::getFormData('lon')); $this->initialized = true; } diff --git a/kronolith/lib/Geo.php b/kronolith/lib/Geo.php new file mode 100644 index 000000000..ad10d031f --- /dev/null +++ b/kronolith/lib/Geo.php @@ -0,0 +1,49 @@ + + * + * @package Kronolith + */ +abstract class Kronolith_Geo +{ + protected $_params; + + public function __construct($params = array()) + { + $this->_params = $params; + } + + /** + * Obtain a Kronolith_Geo object. Currently all drivers are SQL based, + * so use the sql config by default. + * + * @param string $driver The type of object to return + * @param unknown_type $params Any driver specific parameters + * + * @return Kronolith_Geo + */ + static public function factory($driver = null, $params = array()) + { + $driver = basename($driver); + $class = 'Kronolith_Geo_' . $driver; + + if (class_exists($class)) { + $driver = new $class(Horde::getDriverConfig('calendar', 'sql')); + } + + $driver->initialize(); + return $driver; + } + + abstract public function setLocation($event_id, $point); + abstract public function getLocation($event_id); + abstract public function removeLocation($event_id); + abstract public function search($criteria); +} \ No newline at end of file diff --git a/kronolith/lib/Geo/Mysql.php b/kronolith/lib/Geo/Mysql.php new file mode 100644 index 000000000..8a8c8f936 --- /dev/null +++ b/kronolith/lib/Geo/Mysql.php @@ -0,0 +1,98 @@ + + * + * @package Kronolith + */ +class Kronolith_Geo_Mysql extends Kronolith_Geo_Sql +{ + // Rouughly 69 miles per distance unit + private $_conversionFactor = 69; + + /** + * Set the location of the specified event _id + * + * @see kronolith/lib/Driver/Kronolith_Driver_Geo#setLocation($event_id, $point) + */ + public function setLocation($event_id, $point) + { + var_dump($event_id); + var_dump($point); + /* First make sure it doesn't already exist */ + $sql = "SELECT COUNT(*) FROM kronolith_events_geo WHERE event_id = ('" . $event_id . "')"; + $count = $this->_db->getOne($sql); + if ($count instanceof PEAR_Error) { + throw new Horde_Exception($count->getMessage()); + } + /* INSERT or UPDATE */ + if ($count) { + $sql = "UPDATE kronolith_events_geo SET coordinates = GeomFromText('POINT(" . $point['lat'] . " " . $point['lon'] . ")') WHERE event_id = '" . $event_id . "'"; + } else { + $sql = "INSERT into kronolith_events_geo (event_id, coordinates) VALUES('" . $event_id . "', GeomFromText('POINT(" . $point['lat'] . " " . $point['lon'] . ")'))"; + } + $result = $this->_write_db->query($sql); + if ($result instanceof PEAR_Error) { + throw new Horde_Exception($result->getMessage()); + } + + return $result; + } + + /** + * Get the location of the provided event_id. + * + * @see kronolith/lib/Driver/Kronolith_Driver_Geo#getLocation($event_id) + */ + public function getLocation($event_id) + { + $sql = "SELECT x(coordinates) as lat, y(coordinates) as lon FROM kronolith_events_geo WHERE event_id = '" . $event_id . "'"; + $result = $this->_db->getRow($sql, null, DB_FETCHMODE_ASSOC); + if ($result instanceof PEAR_Error) { + throw new Horde_Exception($result->getMessage()); + } + return $result; + } + + /** + * Search for events "close to" a given point. + * + * TODO: If all we really use the geodata for is distance, it really doesn't + * make sense to use the GIS extensions since the distance calculations + * are done with Euclidian geometry ONLY ... and therefore will give + * incorrect results when done on a geocentric coordinate system... + * They might be useful if we eventually want to do searches on + * MBRs + * + * @see kronolith/lib/Driver/Kronolith_Driver_Geo#search($criteria) + */ + public function search($criteria) + { + $point = $criteria['point']; + $limit = empty($criteria['limit']) ? 10 : $criteria['limit']; + $radius = empty($criteria['radius']) ? 10 : $criteria['radius']; + + /* Allow overriding the default conversion factor */ + $factor = empty($criteria['factor']) ? $this->_conversionFactor : $criteria['factor']; + + // ... if this works it will be a miracle ;) + $sql = "SELECT event_id, " + . "GLength(LINESTRINGFromWKB(LineString(coordinates, GeomFromText('POINT(" . $point['lat'] . " " . $point['lon'] . ")')))) * " . $factor . " as distance, " + . "x(coordinates) as lat, y(coordinates) as lon FROM kronolith_events_geo HAVING distance < " . $radius . " ORDER BY distance ASC LIMIT " . $limit; + + $results = $this->_db->getAssoc($sql, false, null, DB_FETCHMODE_ASSOC); + if ($results instanceof PEAR_Error) { + throw new Horde_Exception($results->getMessage()); + } + + return $results; + + } +} \ No newline at end of file diff --git a/kronolith/lib/Geo/Sql.php b/kronolith/lib/Geo/Sql.php new file mode 100644 index 000000000..844417ea1 --- /dev/null +++ b/kronolith/lib/Geo/Sql.php @@ -0,0 +1,177 @@ + + * + * @package Kronolith + */ +class Kronolith_Geo_Sql extends Kronolith_Geo +{ + protected $_write_db; + protected $_db; + + /** + * Still needs to return a PEAR_Error since Kronolith_Driver still expects it + * + * @return mixed boolean || PEAR_Error + */ + public function initialize() + { + Horde::assertDriverConfig($this->_params, 'calendar', array('phptype')); + + if (!isset($this->_params['database'])) { + $this->_params['database'] = ''; + } + if (!isset($this->_params['username'])) { + $this->_params['username'] = ''; + } + if (!isset($this->_params['hostspec'])) { + $this->_params['hostspec'] = ''; + } + if (!isset($this->_params['table'])) { + $this->_params['table'] = 'kronolith_events_geo'; + } + + /* Connect to the SQL server using the supplied parameters. */ + $this->_write_db = DB::connect($this->_params, + array('persistent' => !empty($this->_params['persistent']), + 'ssl' => !empty($this->_params['ssl']))); + if (is_a($this->_write_db, 'PEAR_Error')) { + return $this->_write_db; + } + $this->_initConn($this->_write_db); + + /* Check if we need to set up the read DB connection + * seperately. */ + if (!empty($this->_params['splitread'])) { + $params = array_merge($this->_params, $this->_params['read']); + $this->_db = DB::connect($params, + array('persistent' => !empty($params['persistent']), + 'ssl' => !empty($params['ssl']))); + if (is_a($this->_db, 'PEAR_Error')) { + return $this->_db; + } + $this->_initConn($this->_db); + } else { + /* Default to the same DB handle for the writer too. */ + $this->_db = $this->_write_db; + } + + return true; + } + + protected function _initConn(&$db) + { + // Set DB portability options. + switch ($db->phptype) { + case 'mssql': + $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM); + break; + default: + $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); + } + + /* Handle any database specific initialization code to run. */ + switch ($db->dbsyntax) { + case 'oci8': + $query = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"; + + /* Log the query at a DEBUG log level. */ + Horde::logMessage(sprintf('Kronolith_Driver_Sql::_initConn(): user = "%s"; query = "%s"', + Horde_Auth::getAuth(), $query), + __FILE__, __LINE__, PEAR_LOG_DEBUG); + + $db->query($query); + break; + + case 'pgsql': + $query = "SET datestyle TO 'iso'"; + + /* Log the query at a DEBUG log level. */ + Horde::logMessage(sprintf('Kronolith_Driver_Sql::_initConn(): user = "%s"; query = "%s"', + Horde_Auth::getAuth(), $query), + __FILE__, __LINE__, PEAR_LOG_DEBUG); + + $db->query($query); + break; + } + } + + /** + * Set the location of the specified event _id + * + * @see kronolith/lib/Driver/Kronolith_Driver_Geo#setLocation($event_id, $point) + */ + public function setLocation($event_id, $point) + { + /* First make sure it doesn't already exist */ + $sql = "SELECT COUNT(*) FROM kronolith_events_geo WHERE event_id = ('" . $event_id . "')"; + $count = $this->_db->getOne($sql); + if ($count instanceof PEAR_Error) { + throw new Horde_Exception($count->getMessage()); + } + + /* INSERT or UPDATE */ + $params = array($point['lat'], $point['lng'], $event_id); + if ($count) { + $sql = 'UPDATE kronolith_events_geo SET event_lat = ?, event_lng = ? WHERE event_id = ?'; + } else { + $sql = 'INSERT into kronolith_events_geo (event_lat, event_lng, event_id) VALUES(?, ?, ?)'; + } + $result = $this->_write_db->query($sql, $params); + if ($result instanceof PEAR_Error) { + throw new Horde_Exception($result->getMessage()); + } + + return $result; + } + + /** + * Get the location of the provided event_id. + * + * @see kronolith/lib/Driver/Kronolith_Driver_Geo#getLocation($event_id) + */ + public function getLocation($event_id) + { + $sql = 'SELECT event_lat as lat, event_lng as lng FROM kronolith_events_geo WHERE event_id = ?'; + $result = $this->_db->getRow($sql, array($event_id), DB_FETCHMODE_ASSOC); + if ($result instanceof PEAR_Error) { + throw new Horde_Exception($result->getMessage()); + } + + return $result; + } + + /** + * + * @see kronolith/lib/Driver/Kronolith_Driver_Geo#removeLocation($event_id) + */ + public function removeLocation($event_id) + { + $sql = 'DELETE FROM kronolith_events_geo WHERE event_id = ?'; + $this->_write_db->query($sql, array($event_id)); + } + + /** + * Search for events "close to" a given point. + * + * TODO: If all we really use the geodata for is distance, it really doesn't + * make sense to use the GIS extensions since the distance calculations + * are done with Euclidian geometry ONLY ... and therefore will give + * incorrect results when done on a geocentric coordinate system... + * They might be useful if we eventually want to do searches on + * MBRs + * + * @see kronolith/lib/Driver/Kronolith_Driver_Geo#search($criteria) + */ + public function search($criteria) + { + throw new Horde_Exception(_("Searching requires a GIS enabled database.")); + } +} \ No newline at end of file diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index db02ce2b0..0432bb00f 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -303,6 +303,7 @@ class Kronolith 'prefs' => _("Options"), 'no_url' => _("You must specify a URL."), 'wrong_auth' => _("The authentication information you specified wasn't accepted."), + 'geocode_error' => _("Unable to locate requested address"), ); for ($i = 1; $i <= 12; ++$i) { $code['text']['month'][$i - 1] = Horde_Nls::getLangInfo(constant('MON_' . $i)); @@ -2409,6 +2410,16 @@ class Kronolith return self::$_tagger; } + public static function getGeoDriver() + { + /* Get geolocation data */ + if ($GLOBALS['conf']['geo']['driver']) { + return Kronolith_Geo::factory($GLOBALS['conf']['geo']['driver']); + } else { + return false; + } + } + /** * Obtain an internal calendar. Use this where we don't know if we will * have a Horde_Share or a Kronolith_Resource based calendar. diff --git a/kronolith/templates/index/edit.inc b/kronolith/templates/index/edit.inc index 9aeaa0966..bf568e283 100644 --- a/kronolith/templates/index/edit.inc +++ b/kronolith/templates/index/edit.inc @@ -2,6 +2,8 @@
+ +

-- 2.11.0