ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilGeoLocationDBRepository.php
Go to the documentation of this file.
1<?php
2
4
5 public const TABLE_NAME = 'geo_location';
6
7 public function __construct(\ilDBInterface $a_db)
8 {
9 $this->db = $a_db;
10 }
11
12 public function createGeoLocation(
13 string $a_title,
14 float $a_latitude,
15 float $a_longitude,
16 \DateTimeImmutable $a_expiration_timestamp
18 {
19 // Get next free id for object
20 $id = $this->db->nextId($this->db->quoteIdentifier(self::TABLE_NAME));
21
22 // Insert in database
23 $this->db->insert($this->db->quoteIdentifier(self::TABLE_NAME), array(
24 'id' => array('integer', $id),
25 'title' => array('text', $a_title),
26 'latitude' => array('float', $a_latitude),
27 'longitude' => array('float', $a_longitude),
28 'expiration_timestamp' => array('timestamp', $a_expiration_timestamp->getTimestamp())
29 ));
30
31 // Return the new created object or just the id
32 return new ilGeoLocation(
33 $id,
34 $a_title,
35 $a_latitude,
36 $a_longitude,
37 $a_expiration_timestamp
38 );
39 }
40
41 public function getGeoLocationById(int $a_id) : ilGeoLocation
42 {
43 // Set up SQL-Statement
44 $query = 'SELECT title, latitude, longitude, expiration_timestamp' .
45 ' FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
46 ' WHERE id = ' . $this->db->quote($a_id, 'integer');
47
48 // Execute query
49 $result = $this->db->query($query);
50
51 // Fetch row for returning
52 if($row = $this->db->fetchAssoc($result))
53 {
54 // Create object out of fetched data and return it
55 return new ilGeoLocation(
56 $a_id,
57 $row['title'],
58 (float)$row['latitude'],
59 (float)$row['longitude'],
60 new DateTimeImmutable($row['expiration_timestamp'])
61 );
62 }
63
64 throw new \InvalidArgumentException("Unknown id for geolocation: $a_id");
65 }
66
67 public function getGeoLocationsByCoordinates(float $a_latitude, float $a_longitude) : array
68 {
69 // Set up SQL-Statement
70 $query = 'SELECT title, expiration_timestamp' .
71 ' FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
72 ' WHERE latitude = ' . $this->db->quote($a_latitude, 'float') .
73 ' AND longitude = ' . $this->db->quote($a_longitude, 'float');
74
75 // Execute query
76 $result = $this->db->query($query);
77
78 // Fill array with all matching objects
79 $locations = array();
80 while($row = $this->db->fetchAssoc($result))
81 {
82 // Create object and add it to list
83 $locations[] = new ilGeoLocation(
84 (int)$row['id'],
85 $row['title'],
86 $a_latitude,
87 $a_longitude,
88 new DateTimeImmutable($row['expiration_timestamp'])
89 );
90 }
91
92 // Return list of objects (might be empty if no object was found)
93 return $locations;
94 }
95
96 public function ifGeoLocationExistsById(int $a_id) : bool
97 {
98 // Set up SQL-Statement
99 $query = 'SELECT EXISTS(SELECT 1 FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
100 ' WHERE id = ' . $this->db->quote($a_id, 'integer') . ") AS count";
101
102 // Execute statement
103 $result = $this->db->query($query);
104
105 // Return true if object was found
106 return $result['count'] == 1;
107 }
108
109 public function ifAnyGeoLocationExistsByCoordinates(float $a_latitude, float $a_longitude) : bool
110 {
111 // Set up SQL-Statement
112 $query = 'SELECT EXISTS(SELECT 1 FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
113 ' WHERE latitude = ' . $this->db->quote($a_latitude, 'float') .
114 ' AND longitude = ' . $this->db->quote($a_longitude, 'float') . ") AS count";
115 // Execute statement
116 $result = $this->db->query($query);
117
118 // Return if any object was found
119 return $result['count'] == 1;
120 }
121
122 public function updateGeoLocation(ilGeoLocation $a_obj)
123 {
124 // Update of one entire geo location object
125 $this->db->update($this->db->quoteIdentifier(self::TABLE_NAME),
126 // Update columns (in this case all except for id):
127 array('title' => array($a_obj->getTitle(), 'text')),
128 array('latitude' => array($a_obj->getLatitude(), 'float')),
129 array('longitude' => array($a_obj->getLongitude(), 'float')),
130 array('expiration_timestamp' => array($a_obj->getExpirationAsTimestamp(), 'timestamp')),
131 // Where (in this case only the object with the given id):
132 array('id' => array($a_obj->getId(), 'int'))
133 );
134 }
135
136 public function updateGeoLocationTimestampByCoordinates(float $a_searched_latitude, float $a_searched_longitude, \DateTimeImmutable $a_update_timestamp)
137 {
138 // Update for single attribute of a set of geo location objects
139 $this->db->update($this->db->quoteIdentifier(self::TABLE_NAME),
140 // Update columns (in this case only the timestamp):
141 array('expiration_timestamp' => array('timestamp', $a_update_timestamp->getTimestamp())),
142 // Where (in this case every object on the given location):
143 array('latitude' => array($a_searched_latitude, 'float'),
144 'longitude' => array($a_searched_longitude, 'float'))
145 );
146 }
147
148 public function deleteGeoLocation(int $a_id)
149 {
150 // Set up delete query
151 $query = 'DELETE FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
152 ' WHERE id = ' . $this->db->quote($a_id, 'integer');
153
154 // Execute delete query
155 $this->db->manipulate($query);
156 }
157
158 public function deleteGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
159 {
160 // Set up delete query
161 $query = 'DELETE FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
162 ' WHERE latitude < ' . $this->db->quote($a_latitude, 'float') .
163 ' AND longitude = ' . $this->db->quote($a_longitude, 'float');
164
165 // Execute delete query
166 $this->db->manipulate($query);
167 }
168
170 {
171 // Set up delete query
172 $query = 'DELETE FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
173 ' WHERE expiration_timestamp < ' . $this->db->quote(time(), 'timestamp');
174
175 // Execute delete query
176 $this->db->manipulate($query);
177 }
178}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
deleteGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
Example for a condition based deletion of multiple geo locations.
deleteGeoLocation(int $a_id)
Example for deleting single geo location identified by its id.
ifGeoLocationExistsById(int $a_id)
Example for checking if geo location with a certain id exists.
updateGeoLocationTimestampByCoordinates(float $a_searched_latitude, float $a_searched_longitude, \DateTimeImmutable $a_update_timestamp)
Example for updating multiple objects at once.
getGeoLocationById(int $a_id)
Get a single geo location, identified by its id.
deleteExpiredGeoLocations()
Example for a condition based deletion of multiple geo locations.
getGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
Example for reading an array of geo locations which have a given attribute.
ifAnyGeoLocationExistsByCoordinates(float $a_latitude, float $a_longitude)
Example for checking if a geo location (one or more) with a given attribute exists.
updateGeoLocation(ilGeoLocation $a_obj)
Example for updating all attributes of a given geo location.
createGeoLocation(string $a_title, float $a_latitude, float $a_longitude, \DateTimeImmutable $a_expiration_timestamp)
Create a new geo location entry.
Interface ilDBInterface.
This code is just an example for the Repository Pattern! It is a basic interface to the 'ilGeoLocatio...