ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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(\ilDBInterfacee $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 * FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
45 ' WHERE id = ' . $this->db->quote($a_id, 'integer');
46
47 // Execute query
48 $result = $this->db->query($query);
49
50 // Fetch row for returning
51 if($row = $this->db->fetchAssoc($result))
52 {
53 // Create object out of fetched data and return it
54 return new ilGeoLocation(
55 (int)$row['id'],
56 $row['title'],
57 (float)$row['latitude'],
58 (float)$row['longitude'],
59 new DateTimeImmutable($row['expiration_timestamp'])
60 );
61 }
62
63 throw new \InvalidArgumentException("Unknown id for geolocation: $a_id");
64 }
65
66 public function getGeoLocationsByCoordinates(float $a_latitude, float $a_longitude) : array
67 {
68 // Set up SQL-Statement
69 $query = 'Select * FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
70 ' WHERE latitude = ' . $this->db->quote($a_latitude, 'float') .
71 ' AND longitude = ' . $this->db->quote($a_longitude, 'float');
72
73 // Execute query
74 $result = $this->db->query($query);
75
76 // Fill array with all matching objects
77 $locations = array();
78 while($row = $this->db->fetchAssoc($result))
79 {
80 // Create object and add it to list
81 $locations[] = new ilGeoLocation(
82 (int)$row['id'],
83 $row['title'],
84 (float)$row['latitude'],
85 (float)$row['longitude'],
86 new DateTimeImmutable($row['expiration_timestamp'])
87 );
88 }
89
90 // Return list of objects (might be empty if no object was found)
91 return $locations;
92 }
93
94 public function ifGeoLocationExistsById(int $a_id) : bool
95 {
96 // Set up SQL-Statement
97 $query = 'Select count(*) AS count FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
98 ' WHERE id = ' . $this->db->quote($a_id, 'integer');
99
100 // Execute statement
101 $result = $this->db->query($query);
102
103 // Return if object was found
104 return $result['count'] > 0;
105 }
106
107 public function ifAnyGeoLocationExistsByCoordinates(float $a_latitude, float $a_longitude) : bool
108 {
109 // Set up SQL-Statement
110 $query = 'Select count(*) AS count FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
111 ' WHERE latitude = ' . $this->db->quote($a_latitude, 'float') .
112 ' AND longitude = ' . $this->db->quote($a_longitude, 'float');
113
114 // Execute statement
115 $result = $this->db->query($query);
116
117 // Return if any object was found
118 return $result['count'] > 0;
119 }
120
121 public function updateGeoLocation(ilGeoLocation $a_obj)
122 {
123 // Update of one entire geo location object
124 $this->db->update($this->db->quoteIdentifier(self::TABLE_NAME),
125 // Update columns (in this case all except for id):
126 array('title' => array($a_obj->getTitle(), 'text')),
127 array('latitude' => array($a_obj->getLatitude(), 'float')),
128 array('longitude' => array($a_obj->getLongitude(), 'float')),
129 array('expiration_timestamp' => array($a_obj->getExpirationAsTimestamp(), 'timestamp')),
130 // Where (in this case only the object with the given id):
131 array('id' => array($a_obj->getId(), 'int'))
132 );
133 }
134
135 public function updateGeoLocationTimestampByCoordinates(float $a_searched_latitude, float $a_searched_longitude, \DateTimeImmutable $a_update_timestamp)
136 {
137 // Update for single attribute of a set of geo location objects
138 $this->db->update($this->db->quoteIdentifier(self::TABLE_NAME),
139 // Update columns (in this case only the timestamp):
140 array('expiration_timestamp' => array('timestamp', $a_update_timestamp->getTimestamp())),
141 // Where (in this case every object on the given location):
142 array('latitude' => array($a_searched_latitude, 'float'),
143 'longitude' => array($a_searched_longitude, 'float'))
144 );
145 }
146
147 public function deleteGeoLocation(int $a_id)
148 {
149 // Set up delete query
150 $query = 'DELETE FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
151 ' WHERE id = ' . $this->db->quote($a_id, 'integer');
152
153 // Execute delete query
154 $this->db->manipulate($query);
155 }
156
157 public function deleteGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
158 {
159 // Set up delete query
160 $query = 'DELETE FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
161 ' WHERE latitude < ' . $this->db->quote($a_latitude, 'float') .
162 ' AND longitude = ' . $this->db->quote($a_longitude, 'float');
163
164 // Execute delete query
165 $this->db->manipulate($query);
166 }
167
169 {
170 // Set up delete query
171 $query = 'DELETE FROM ' . $this->db->quoteIdentifier(self::TABLE_NAME) .
172 ' WHERE expiration_timestamp < ' . $this->db->quote(time(), 'timestamp');
173
174 // Execute delete query
175 $this->db->manipulate($query);
176 }
177}
$result
An exception for terminatinating execution or to throw for unit testing.
deleteGeoLocationsByCoordinates(float $a_latitude, float $a_longitude)
Example for a condition based deletion of multiple geo locations.
deleteGeoLocation(int $a_id)
Exaple 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.
__construct(\ilDBInterfacee $a_db)
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.
This code is just an example for the Repository Pattern! It is a basic interface to the 'ilGeoLocatio...
$query