1: <?php
2: /**
3: * General SQL implementation for storing/searching geo location data for
4: * events.
5: *
6: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.
10: *
11: * @author Michael J. Rubinsky <mrubinsk@horde.org>
12: * @category Horde
13: * @package Kronolith
14: */
15: class Kronolith_Geo_Sql extends Kronolith_Geo_Base
16: {
17: /**
18: * @var Horde_Db_Adapter
19: */
20: protected $_db;
21:
22: /**
23: * Set the location of the specified event _id
24: *
25: * @see Kronolith_Geo_Base#setLocation()
26: * @throws Kronolith_Exception
27: */
28: public function setLocation($event_id, $point)
29: {
30: /* First make sure it doesn't already exist */
31: $sql = 'SELECT COUNT(*) FROM kronolith_events_geo WHERE event_id = ?';
32: try {
33: $count = $this->_db->selectValue($sql, array($event_id));
34: } catch (Horde_Db_Exception $e) {
35: throw new Kronolith_Exception($e);
36: }
37:
38: /* Do we actually have data? If not, see if we are deleting an
39: * existing entry. */
40: if ((empty($point['lat']) || empty($point['lon'])) && $count) {
41: // Delete the record.
42: $this->deleteLocation($event_id);
43: return;
44: } elseif (empty($point['lat']) || empty($point['lon'])) {
45: return;
46: }
47:
48: if (empty($point['zoom'])) {
49: $point['zoom'] = 0;
50: }
51:
52: /* INSERT or UPDATE */
53: $params = array($point['lat'], $point['lon'], $point['zoom'], $event_id);
54: if ($count) {
55: $sql = 'UPDATE kronolith_events_geo SET event_lat = ?, event_lon = ?, event_zoom = ? WHERE event_id = ?';
56: } else {
57: $sql = 'INSERT into kronolith_events_geo (event_lat, event_lon, event_zoom, event_id) VALUES(?, ?, ?, ?)';
58: }
59: try {
60: $this->_db->execute($sql, $params);
61: } catch (Horde_Db_Exception $e) {
62: throw new Horde_Exception($e);
63: }
64: }
65:
66: /**
67: * Get the location of the provided event_id.
68: *
69: * @see Kronolith_Geo_Base#getLocation()
70: * @throws Kronolith_Exception
71: */
72: public function getLocation($event_id)
73: {
74: $sql = 'SELECT event_lat as lat, event_lon as lon, event_zoom as zoom FROM kronolith_events_geo WHERE event_id = ?';
75: try {
76: return $this->_db->selectOne($sql, array($event_id));
77: } catch (Horde_Db_Exception $e) {
78: throw new Kronolith_Exception($e);
79: }
80: }
81:
82: /**
83: * Deletes an entry from storage
84: *
85: * @see Kronolith_Geo_Base#removeLocation()
86: *
87: * @param string $event_id
88: *
89: * @throws Kronolith_Exception
90: */
91: public function deleteLocation($event_id)
92: {
93: $sql = 'DELETE FROM kronolith_events_geo WHERE event_id = ?';
94: try {
95: $this->_db->delete($sql, array($event_id));
96: } catch (Horde_Db_Exception $e) {
97: throw new Horde_Exception($e);
98: }
99: }
100:
101: /**
102: * Search for events "close to" a given point.
103: *
104: * TODO: If all we really use the geodata for is distance, it really doesn't
105: * make sense to use the GIS extensions since the distance calculations
106: * are done with Euclidian geometry ONLY ... and therefore will give
107: * incorrect results when done on a geocentric coordinate system.
108: * They might be useful if we eventually want to do searches on
109: * MBRs
110: *
111: * @see kronolith/lib/Driver/Kronolith_Driver_Geo#search($criteria)
112: * @throws Kronolith_Exception
113: */
114: public function search($criteria)
115: {
116: throw new Horde_Exception(_("Searching requires a GIS enabled database."));
117: }
118: }
119: