1 | <?php |
||
2 | |||
3 | namespace Backend\Modules\Location\Engine; |
||
4 | |||
5 | use Backend\Core\Language\Language as BL; |
||
6 | use Backend\Core\Engine\Model as BackendModel; |
||
7 | use Common\ModuleExtraType; |
||
8 | use Symfony\Component\Intl\Intl as Intl; |
||
9 | |||
10 | /** |
||
11 | * In this file we store all generic functions that we will be using in the location module |
||
12 | */ |
||
13 | class Model |
||
14 | { |
||
15 | const QUERY_DATAGRID_BROWSE = |
||
16 | 'SELECT id, title, CONCAT(street, " ", number, ", ", zip, " ", city, ", ", country) AS address |
||
17 | FROM location |
||
18 | WHERE language = ?'; |
||
19 | |||
20 | /** |
||
21 | * Delete an item |
||
22 | * |
||
23 | * @param int $id The id of the record to delete. |
||
24 | */ |
||
25 | public static function delete(int $id): void |
||
26 | { |
||
27 | // get database |
||
28 | $database = BackendModel::getContainer()->get('database'); |
||
29 | |||
30 | // get item |
||
31 | $item = self::get($id); |
||
32 | |||
33 | BackendModel::deleteExtraById($item['extra_id']); |
||
34 | |||
35 | // delete location and its settings |
||
36 | $database->delete('location', 'id = ? AND language = ?', [$id, BL::getWorkingLanguage()]); |
||
37 | $database->delete('location_settings', 'map_id = ?', [$id]); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Check if an item exists |
||
42 | * |
||
43 | * @param int $id The id of the record to look for. |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | public static function exists(int $id): bool |
||
48 | { |
||
49 | return (bool) BackendModel::getContainer()->get('database')->getVar( |
||
50 | 'SELECT 1 |
||
51 | FROM location AS i |
||
52 | WHERE i.id = ? AND i.language = ? |
||
53 | LIMIT 1', |
||
54 | [$id, BL::getWorkingLanguage()] |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Fetch a record from the database |
||
60 | * |
||
61 | * @param int $id The id of the record to fetch. |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | public static function get(int $id): array |
||
66 | { |
||
67 | return (array) BackendModel::getContainer()->get('database')->getRecord( |
||
68 | 'SELECT i.* |
||
69 | FROM location AS i |
||
70 | WHERE i.id = ? AND i.language = ?', |
||
71 | [$id, BL::getWorkingLanguage()] |
||
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Fetch a record from the database |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public static function getAll(): array |
||
81 | { |
||
82 | return (array) BackendModel::getContainer()->get('database')->getRecords( |
||
83 | 'SELECT i.* |
||
84 | FROM location AS i |
||
85 | WHERE i.language = ? AND i.show_overview = ?', |
||
86 | [BL::getWorkingLanguage(), true] |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get coordinates latitude/longitude |
||
92 | * |
||
93 | * @param string $street |
||
94 | * @param string $streetNumber |
||
95 | * @param string $city |
||
96 | * @param string $zip |
||
97 | * @param string $country |
||
98 | * |
||
99 | * @return array Contains 'latitude' and 'longitude' as variables |
||
100 | */ |
||
101 | public static function getCoordinates( |
||
102 | string $street = null, |
||
103 | string $streetNumber = null, |
||
104 | string $city = null, |
||
105 | string $zip = null, |
||
106 | string $country = null |
||
107 | ): array { |
||
108 | // init item |
||
109 | $item = []; |
||
110 | |||
111 | // building item |
||
112 | if (!empty($street)) { |
||
113 | $item[] = $street; |
||
114 | } |
||
115 | |||
116 | if (!empty($streetNumber)) { |
||
117 | $item[] = $streetNumber; |
||
118 | } |
||
119 | |||
120 | if (!empty($city)) { |
||
121 | $item[] = $city; |
||
122 | } |
||
123 | |||
124 | if (!empty($zip)) { |
||
125 | $item[] = $zip; |
||
126 | } |
||
127 | |||
128 | if (!empty($country)) { |
||
129 | $item[] = Intl::getRegionBundle()->getCountryName($country, BL::getInterfaceLanguage()); |
||
130 | } |
||
131 | |||
132 | // define address |
||
133 | $address = implode(' ', $item); |
||
134 | |||
135 | // fetch the geo coordinates |
||
136 | $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address); |
||
137 | $geocodes = json_decode(file_get_contents($url), true); |
||
138 | |||
139 | // return coordinates latitude/longitude |
||
140 | return [ |
||
141 | 'latitude' => array_key_exists( |
||
142 | 0, |
||
143 | $geocodes['results'] |
||
144 | ) ? $geocodes['results'][0]['geometry']['location']['lat'] : null, |
||
145 | 'longitude' => array_key_exists( |
||
146 | 0, |
||
147 | $geocodes['results'] |
||
148 | ) ? $geocodes['results'][0]['geometry']['location']['lng'] : null, |
||
149 | ]; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Retrieve a map setting |
||
154 | * |
||
155 | * @param int $mapId |
||
156 | * @param string $name |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public static function getMapSetting(int $mapId, string $name) |
||
161 | { |
||
162 | $serializedData = (string) BackendModel::getContainer()->get('database')->getVar( |
||
163 | 'SELECT s.value |
||
164 | FROM location_settings AS s |
||
165 | WHERE s.map_id = ? AND s.name = ?', |
||
166 | [$mapId, $name] |
||
167 | ); |
||
168 | |||
169 | if ($serializedData !== null) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
170 | return unserialize($serializedData); |
||
171 | } |
||
172 | |||
173 | return false; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Fetch all the settings for a specific map |
||
178 | * |
||
179 | * @param int $mapId |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | public static function getMapSettings(int $mapId): array |
||
184 | { |
||
185 | $mapSettings = (array) BackendModel::getContainer()->get('database')->getPairs( |
||
186 | 'SELECT s.name, s.value |
||
187 | FROM location_settings AS s |
||
188 | WHERE s.map_id = ?', |
||
189 | [$mapId] |
||
190 | ); |
||
191 | |||
192 | foreach ($mapSettings as $key => $value) { |
||
193 | $mapSettings[$key] = unserialize($value); |
||
194 | } |
||
195 | |||
196 | return $mapSettings; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Insert an item |
||
201 | * |
||
202 | * @param array $item The data of the record to insert. |
||
203 | * |
||
204 | * @return int |
||
205 | */ |
||
206 | public static function insert(array $item): int |
||
207 | { |
||
208 | $database = BackendModel::getContainer()->get('database'); |
||
209 | |||
210 | // insert extra |
||
211 | $item['extra_id'] = BackendModel::insertExtra( |
||
212 | ModuleExtraType::widget(), |
||
213 | 'Location', |
||
214 | 'Location' |
||
215 | ); |
||
216 | |||
217 | // insert new location |
||
218 | $item['created_on'] = $item['edited_on'] = BackendModel::getUTCDate(); |
||
219 | $item['id'] = $database->insert('location', $item); |
||
220 | |||
221 | // update extra (item id is now known) |
||
222 | BackendModel::updateExtra( |
||
223 | $item['extra_id'], |
||
224 | 'data', |
||
225 | [ |
||
226 | 'id' => $item['id'], |
||
227 | 'extra_label' => \SpoonFilter::ucfirst(BL::lbl('Location', 'Core')) . ': ' . $item['title'], |
||
228 | 'language' => $item['language'], |
||
229 | 'edit_url' => BackendModel::createUrlForAction('Edit') . '&id=' . $item['id'], |
||
230 | ] |
||
231 | ); |
||
232 | |||
233 | return $item['id']; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Save the map settings |
||
238 | * |
||
239 | * @param int $mapId |
||
240 | * @param string $key |
||
241 | * @param mixed $value |
||
242 | */ |
||
243 | public static function setMapSetting(int $mapId, string $key, $value): void |
||
244 | { |
||
245 | $value = serialize($value); |
||
246 | |||
247 | BackendModel::getContainer()->get('database')->execute( |
||
248 | 'INSERT INTO location_settings(map_id, name, value) |
||
249 | VALUES(?, ?, ?) |
||
250 | ON DUPLICATE KEY UPDATE value = ?', |
||
251 | [$mapId, $key, $value, $value] |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Update an item |
||
257 | * |
||
258 | * @param array $item The data of the record to update. |
||
259 | * |
||
260 | * @return int |
||
261 | */ |
||
262 | public static function update(array $item): int |
||
263 | { |
||
264 | // redefine edited on date |
||
265 | $item['edited_on'] = BackendModel::getUTCDate(); |
||
266 | |||
267 | // we have an extra_id |
||
268 | if (isset($item['extra_id'])) { |
||
269 | // update extra |
||
270 | BackendModel::updateExtra( |
||
271 | $item['extra_id'], |
||
272 | 'data', |
||
273 | [ |
||
274 | 'id' => $item['id'], |
||
275 | 'extra_label' => \SpoonFilter::ucfirst(BL::lbl('Location', 'Core')) . ': ' . $item['title'], |
||
276 | 'language' => $item['language'], |
||
277 | 'edit_url' => BackendModel::createUrlForAction('Edit') . '&id=' . $item['id'], |
||
278 | ] |
||
279 | ); |
||
280 | } |
||
281 | |||
282 | // update item |
||
283 | return BackendModel::get('database')->update( |
||
284 | 'location', |
||
285 | $item, |
||
286 | 'id = ? AND language = ?', |
||
287 | [$item['id'], $item['language']] |
||
288 | ); |
||
289 | } |
||
290 | } |
||
291 |