PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.7.1
VikBooking Hotel Booking Engine & PMS v1.7.1
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / helpers / geocoding.php

geocoding.php in VikBooking Hotel Booking Engine & PMS 1.7.1, at admin/helpers/geocoding.php

731 lines 24.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage com_vikbooking
5 * @author Alessio Gaggii - e4j - Extensionsforjoomla.com
6 * @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 * @link https://vikwp.com
9 */
10
11 defined('ABSPATH') or die('No script kiddies please!');
12
13 /**
14 * Helper class for the geocoding functions.
15 *
16 * @since 1.4.0
17 */
18 class VikBookingHelperGeocoding
19 {
20 /**
21 * The singleton instance of the class.
22 *
23 * @var VikBookingHelperGeocoding
24 */
25 protected static $instance = null;
26
27 /**
28 * The Google Maps API Key.
29 *
30 * @var string
31 */
32 protected $gmaps_apikey = null;
33
34 /**
35 * The current room geo-params.
36 *
37 * @var mixed
38 */
39 protected $room_geoparams = null;
40
41 /**
42 * The current room params.
43 *
44 * @var mixed
45 */
46 protected $room_params = null;
47
48 /**
49 * Class constructor is protected.
50 *
51 * @see getInstance()
52 */
53 protected function __construct()
54 {
55 // load the Google Maps API Key from the configuration settings
56 $this->setGoogleMapsKey(VikBooking::getGoogleMapsKey());
57 }
58
59 /**
60 * Returns the global object, either
61 * a new instance or the existing instance
62 * if the class was already instantiated.
63 *
64 * @return self A new instance of the class.
65 */
66 public static function getInstance()
67 {
68 if (is_null(static::$instance)) {
69 static::$instance = new static();
70 }
71
72 return static::$instance;
73 }
74
75 /**
76 * Sets the Google Maps API Key.
77 *
78 * @param string the Google Maps API Key to set
79 *
80 * @return self
81 */
82 public function setGoogleMapsKey($apikey = null)
83 {
84 $this->gmaps_apikey = $apikey;
85
86 return $this;
87 }
88
89 /**
90 * Gets the current Google Maps API Key.
91 *
92 * @return mixed string or false if empty.
93 */
94 public function getGoogleMapsKey()
95 {
96 if (empty($this->gmaps_apikey)) {
97 return false;
98 }
99
100 return $this->gmaps_apikey;
101 }
102
103 /**
104 * Checks whether the Google Maps can be used.
105 *
106 * @return bool true or false
107 */
108 public function isSupported()
109 {
110 return (!empty($this->gmaps_apikey));
111 }
112
113 /**
114 * Loads the necessary assets to the document for Google Maps.
115 *
116 * @param mixed string or array values values to append to the URL.
117 *
118 * @return self
119 */
120 public function loadAssets($params = null)
121 {
122 if (empty($this->gmaps_apikey)) {
123 return $this;
124 }
125
126 // values like &callback=initMap could be passed as argument
127 $qstring = '';
128
129 if (!empty($params)) {
130 if (is_string($params)) {
131 $qstring = (substr($params, 0, 1) != '&' ? '&' : '') . $params;
132 }
133 if (is_array($params)) {
134 $qstring = '&' . http_build_query($params);
135 }
136 }
137
138 // load Google Maps API JS
139 JFactory::getDocument()->addScript('https://maps.googleapis.com/maps/api/js?key=' . $this->gmaps_apikey . $qstring);
140
141 return $this;
142 }
143
144 /**
145 * Gets the room parameters (one or all) related to the geocoding.
146 * Class variables are using to cache parameters into the buffer.
147 *
148 * @param mixed $rparams string to be decoded or array/object params.
149 * @param string $key the optional param key to get.
150 * @param mixed $def the default room param value to return.
151 *
152 * @return mixed object with all params or empty object, requested param otherwise.
153 */
154 public function getRoomGeoParams($rparams, $key = null, $def = null)
155 {
156 $geoparams = new stdClass;
157
158 if (empty($rparams)) {
159 return !empty($key) ? $def : $geoparams;
160 }
161
162 if ($this->room_geoparams === null || $this->room_params != $rparams || $key === null) {
163 if (is_string($rparams)) {
164 $rparams_obj = json_decode($rparams);
165 if (!is_object($rparams_obj) || !isset($rparams_obj->geo)) {
166 return !empty($key) ? $def : $geoparams;
167 }
168 $geoparams = $rparams_obj->geo;
169 } elseif (is_array($rparams)) {
170 // rooms parameters already decoded, convert it to object
171 $geoparams = json_decode(json_encode($rparams));
172 } elseif (is_object($rparams)) {
173 // rooms parameters already decoded
174 $geoparams = $rparams;
175 }
176
177 if (is_object($geoparams) && isset($geoparams->geo)) {
178 $geoparams = $geoparams->geo;
179 }
180
181 // update global vars
182 $this->room_params = $rparams;
183 $this->room_geoparams = $geoparams;
184 }
185
186 if (($key == 'units_pos' || $key === null) && is_object($this->room_geoparams) && isset($this->room_geoparams->units_pos)) {
187 $units_pos = $this->room_geoparams->units_pos;
188 // make sure to adjust some properties of type float (number) so that they are not strings
189 foreach ($units_pos as $k => $unit) {
190 if (isset($unit->lat)) {
191 $units_pos->{$k}->lat = (float)$unit->lat;
192 }
193 if (isset($unit->lng)) {
194 $units_pos->{$k}->lng = (float)$unit->lng;
195 }
196 if (isset($unit->icon)) {
197 foreach ($unit->icon as $icon_prop => $icon_val) {
198 if ($icon_prop == 'fillOpacity') {
199 $units_pos->{$k}->icon->{$icon_prop} = (float)$icon_val;
200 }
201 if ($icon_prop == 'scale') {
202 $units_pos->{$k}->icon->{$icon_prop} = (float)$icon_val;
203 }
204 if ($icon_prop == 'strokeWeight') {
205 $units_pos->{$k}->icon->{$icon_prop} = (float)$icon_val;
206 }
207 if ($icon_prop == 'anchor' && isset($icon_val->x) && isset($icon_val->y)) {
208 // anchor point object is composed of coordinates with numbers
209 $units_pos->{$k}->icon->{$icon_prop}->x = (float)$icon_val->x;
210 $units_pos->{$k}->icon->{$icon_prop}->y = (float)$icon_val->y;
211 }
212 if (($icon_prop == 'size' || $icon_prop == 'scaledSize') && isset($icon_val->width) && isset($icon_val->height)) {
213 // size and scaledSize object is composed of size width and height numbers
214 $units_pos->{$k}->icon->{$icon_prop}->width = (int)$icon_val->width;
215 $units_pos->{$k}->icon->{$icon_prop}->height = (int)$icon_val->height;
216 }
217 }
218 }
219 }
220 $this->room_geoparams->units_pos = $units_pos;
221 }
222
223 if (!empty($key)) {
224 return is_object($this->room_geoparams) && isset($this->room_geoparams->{$key}) ? $this->room_geoparams->{$key} : $def;
225 }
226
227 return $geoparams;
228 }
229
230 /**
231 * Gets the list of default marker symbols (SVG icons) for Google Maps.
232 *
233 * @return array list of default marker symbols.
234 */
235 public function getDefaultMarkerSymbols()
236 {
237 $symbols = array();
238 $group = 'default';
239 $color = '#000000';
240 $opacity = 1;
241
242 $symbol = new stdClass;
243 $symbol->id = 'hotel';
244 $symbol->name = 'Hotel';
245 $symbol->group = $group;
246 $symbol->fill = $color;
247 $symbol->opacity = $opacity;
248 $symbol->width = 48.4;
249 $symbol->height = 43;
250 $symbol->path = 'M47,5.4c0.7,0,1.3-0.6,1.3-1.3V1.3C48.4,0.6,47.8,0,47,0H1.3C0.6,0,0,0.6,0,1.3V4c0,0.7,0.6,1.3,1.3,1.3h1.3v32.2H1.3 C0.6,37.6,0,38.2,0,39v2.7C0,42.4,0.6,43,1.3,43h20.2v-6.7c0-0.7,0.6-1.3,1.3-1.3h2.7c0.7,0,1.3,0.6,1.3,1.3V43H47 c0.7,0,1.3-0.6,1.3-1.3V39c0-0.7-0.6-1.3-1.3-1.3h-1.3V5.4H47z M21.5,9.1c0-0.5,0.5-1.1,1.1-1.1h3.2c0.5,0,1.1,0.5,1.1,1.1v3.2 c0,0.5-0.5,1.1-1.1,1.1h-3.2c-0.5,0-1.1-0.5-1.1-1.1L21.5,9.1L21.5,9.1z M21.5,17.2c0-0.5,0.5-1.1,1.1-1.1h3.2 c0.5,0,1.1,0.5,1.1,1.1v3.2c0,0.5-0.5,1.1-1.1,1.1h-3.2c-0.5,0-1.1-0.5-1.1-1.1L21.5,17.2L21.5,17.2z M10.8,9.1 c0-0.5,0.5-1.1,1.1-1.1h3.2c0.5,0,1.1,0.5,1.1,1.1v3.2c0,0.5-0.5,1.1-1.1,1.1h-3.2c-0.5,0-1.1-0.5-1.1-1.1L10.8,9.1L10.8,9.1z M15.1,21.5h-3.2c-0.5,0-1.1-0.5-1.1-1.1v-3.2c0-0.5,0.5-1.1,1.1-1.1H15c0.5,0,1.1,0.5,1.1,1.1v3.2C16.1,21,15.6,21.5,15.1,21.5 L15.1,21.5z M16.1,32.2c0-4.5,3.6-8.1,8.1-8.1s8.1,3.6,8.1,8.1H16.1z M37.6,20.4c0,0.5-0.5,1.1-1.1,1.1h-3.2c-0.5,0-1.1-0.5-1.1-1.1 v-3.2c0-0.5,0.5-1.1,1.1-1.1h3.2c0.5,0,1.1,0.5,1.1,1.1V20.4L37.6,20.4z M37.6,12.4c0,0.5-0.5,1.1-1.1,1.1h-3.2 c-0.5,0-1.1-0.5-1.1-1.1V9.1c0-0.5,0.5-1.1,1.1-1.1h3.2c0.5,0,1.1,0.5,1.1,1.1V12.4z';
251 array_push($symbols, $symbol);
252
253 $symbol = new stdClass;
254 $symbol->id = 'home';
255 $symbol->name = 'Home';
256 $symbol->group = $group;
257 $symbol->fill = $color;
258 $symbol->opacity = $opacity;
259 $symbol->width = 56.8;
260 $symbol->height = 43;
261 $symbol->path = 'M27.7,11.2L10,25.8v15.7c0,0.8,0.7,1.5,1.5,1.5l10.8,0c0.8,0,1.5-0.7,1.5-1.5v-9.2c0-0.8,0.7-1.5,1.5-1.5h6.1 c0.8,0,1.5,0.7,1.5,1.5v9.2c0,0.8,0.7,1.5,1.5,1.5c0,0,0,0,0,0l10.8,0c0.8,0,1.5-0.7,1.5-1.5V25.7L29.1,11.2 C28.7,10.8,28.1,10.8,27.7,11.2L27.7,11.2z M55.6,21.1l-8-6.6V1.2c0-0.6-0.5-1.2-1.2-1.2h-5.4c-0.6,0-1.2,0.5-1.2,1.2v7l-8.6-7.1 c-1.7-1.4-4.2-1.4-5.9,0l-24.3,20c-0.5,0.4-0.6,1.1-0.2,1.6c0,0,0,0,0,0l2.4,3c0.4,0.5,1.1,0.6,1.6,0.2c0,0,0,0,0,0L27.7,7.2 c0.4-0.3,1-0.3,1.5,0l22.6,18.6c0.5,0.4,1.2,0.3,1.6-0.2c0,0,0,0,0,0l2.4-3C56.2,22.2,56.1,21.5,55.6,21.1 C55.6,21.1,55.6,21.1,55.6,21.1L55.6,21.1z';
262 array_push($symbols, $symbol);
263
264 $symbol = new stdClass;
265 $symbol->id = 'city';
266 $symbol->name = 'City';
267 $symbol->group = $group;
268 $symbol->fill = $color;
269 $symbol->opacity = $opacity;
270 $symbol->width = 53.8;
271 $symbol->height = 43;
272 $symbol->path = 'M51.7,16.1H40.3V2c0-1.1-0.9-2-2-2H26.2c-1.1,0-2,0.9-2,2v6h-5.4V1.3c0-0.7-0.6-1.3-1.3-1.3h-1.3c-0.7,0-1.3,0.6-1.3,1.3 v6.7H9.4V1.3C9.4,0.6,8.8,0,8.1,0H6.7C6,0,5.4,0.6,5.4,1.3v6.7H2c-1.1,0-2,0.9-2,2v30.2C0,41.8,1.2,43,2.7,43h48.4 c1.5,0,2.7-1.2,2.7-2.7V18.1C53.8,17,52.8,16.1,51.7,16.1z M10.8,33.9c0,0.6-0.5,1-1,1H6.4c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1 h3.4c0.6,0,1,0.5,1,1V33.9z M10.8,25.9c0,0.6-0.5,1-1,1H6.4c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V25.9z M10.8,17.8c0,0.6-0.5,1-1,1H6.4c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V17.8z M21.5,33.9c0,0.6-0.5,1-1,1h-3.4 c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V33.9z M21.5,25.9c0,0.6-0.5,1-1,1h-3.4c-0.6,0-1-0.5-1-1v-3.4 c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V25.9z M21.5,17.8c0,0.6-0.5,1-1,1h-3.4c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1h3.4 c0.6,0,1,0.5,1,1V17.8z M34.9,25.9c0,0.6-0.5,1-1,1h-3.4c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V25.9z M34.9,17.8c0,0.6-0.5,1-1,1h-3.4c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V17.8z M34.9,9.7c0,0.6-0.5,1-1,1h-3.4 c-0.6,0-1-0.5-1-1V6.4c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V9.7z M48.4,33.9c0,0.6-0.5,1-1,1H44c-0.6,0-1-0.5-1-1v-3.4 c0-0.6,0.5-1,1-1h3.4c0.6,0,1,0.5,1,1V33.9z M48.4,25.9c0,0.6-0.5,1-1,1H44c-0.6,0-1-0.5-1-1v-3.4c0-0.6,0.5-1,1-1h3.4 c0.6,0,1,0.5,1,1V25.9z';
273 array_push($symbols, $symbol);
274
275 $symbol = new stdClass;
276 $symbol->id = 'campground';
277 $symbol->name = 'Campground';
278 $symbol->group = $group;
279 $symbol->fill = $color;
280 $symbol->opacity = $opacity;
281 $symbol->width = 53.7;
282 $symbol->height = 43;
283 $symbol->path = 'M52.4,37.6h-2.1L30.2,9.9l4.5-6.2c0.4-0.6,0.3-1.4-0.3-1.9l-2.2-1.6c-0.6-0.4-1.4-0.3-1.9,0.3l-3.5,4.8l-3.5-4.8 C23,0,22.1-0.2,21.5,0.3l-2.2,1.6c-0.6,0.4-0.7,1.3-0.3,1.9l4.5,6.2L3.4,37.6H1.3C0.6,37.6,0,38.2,0,39v2.7C0,42.4,0.6,43,1.3,43 h51.1c0.7,0,1.3-0.6,1.3-1.3V39C53.7,38.2,53.1,37.6,52.4,37.6z M26.9,24.2l9.8,13.4H17.1L26.9,24.2z';
284 array_push($symbols, $symbol);
285
286 $symbol = new stdClass;
287 $symbol->id = 'trailer';
288 $symbol->name = 'Trailer';
289 $symbol->group = $group;
290 $symbol->fill = $color;
291 $symbol->opacity = $opacity;
292 $symbol->width = 66.2;
293 $symbol->height = 43;
294 $symbol->path = 'M64.5,26.5h-8.3V1.7c0-0.9-0.7-1.7-1.7-1.7H1.7C0.7,0,0,0.7,0,1.7v29.8c0,0.9,0.7,1.7,1.7,1.7h5.1c0.8-5.6,5.6-9.9,11.4-9.9 s10.6,4.3,11.4,9.9h34.9c0.9,0,1.7-0.7,1.7-1.7v-3.3C66.2,27.2,65.4,26.5,64.5,26.5z M9.9,18.6c-1.2,0.6-2.3,1.3-3.3,2.1V7.4 C6.6,7,7,6.6,7.4,6.6h1.7c0.5,0,0.8,0.4,0.8,0.8V18.6z M19.8,16.7c-0.5-0.1-1.1-0.1-1.7-0.1c-0.6,0-1.1,0.1-1.7,0.1V7.4 c0-0.5,0.4-0.8,0.8-0.8H19c0.5,0,0.8,0.4,0.8,0.8V16.7z M29.8,20.7c-1-0.8-2.1-1.5-3.3-2.1V7.4c0-0.5,0.4-0.8,0.8-0.8h1.7 c0.5,0,0.8,0.4,0.8,0.8V20.7z M39.7,26.5h-3.3v-19c0-0.5,0.4-0.8,0.8-0.8h1.7c0.5,0,0.8,0.4,0.8,0.8V26.5z M49.6,26.5h-3.3v-19 c0-0.5,0.4-0.8,0.8-0.8h1.7c0.5,0,0.8,0.4,0.8,0.8V26.5z M18.2,26.5c-4.6,0-8.3,3.7-8.3,8.3s3.7,8.3,8.3,8.3s8.3-3.7,8.3-8.3 S22.8,26.5,18.2,26.5z M18.2,38c-1.8,0-3.3-1.5-3.3-3.3s1.5-3.3,3.3-3.3s3.3,1.5,3.3,3.3S20,38,18.2,38z';
295 array_push($symbols, $symbol);
296
297 return $symbols;
298 }
299
300 /**
301 * Gets the list of marker symbols for Google Maps.
302 *
303 * @param string $group optionally filter the symbols by group
304 *
305 * @return array list of marker symbols.
306 */
307 public function getMarkerSymbols($group = null)
308 {
309 $dbo = JFactory::getDbo();
310 $symbols = array();
311
312 $q = "SELECT `param`, `setting` FROM `#__vikbooking_config` WHERE `param` LIKE 'marker_symbols_%' ORDER BY `param` ASC;";
313 $dbo->setQuery($q);
314 $dbo->execute();
315 if ($dbo->getNumRows()) {
316 $all_symbols = $dbo->loadAssocList();
317 foreach ($all_symbols as $symbols_data) {
318 $list = json_decode($symbols_data['setting']);
319 if (!is_array($list) || !count($list)) {
320 continue;
321 }
322 $symbols = array_merge($symbols, $list);
323 }
324 } else {
325 $default_symbols = $this->getDefaultMarkerSymbols();
326 $q = "INSERT INTO `#__vikbooking_config` (`param`,`setting`) VALUES ('marker_symbols_default', " . $dbo->quote(json_encode($default_symbols)) . ");";
327 $dbo->setQuery($q);
328 $dbo->execute();
329
330 return $default_symbols;
331 }
332
333 $symbols = count($symbols) ? $symbols : $this->getDefaultMarkerSymbols();
334
335 if (!empty($group)) {
336 // filter symbol objects by group
337 $group = strtolower($group);
338 foreach ($symbols as $k => $symbol) {
339 if (!isset($symbol->group)) {
340 // filtering is impossible
341 continue;
342 }
343 if ($symbol->group != $group) {
344 unset($symbols[$k]);
345 }
346 }
347 // reset keys
348 $symbols = array_values($symbols);
349 }
350
351 return $symbols;
352 }
353
354 /**
355 * Gets one marker symbol for Google Maps.
356 *
357 * @param string $id the symbol identifier
358 * @param string $group optionally filter the symbols by group
359 *
360 * @return mixed marker symbol object or null.
361 */
362 public function getMarkerSymbol($id, $group = null)
363 {
364 if (empty($id)) {
365 return null;
366 }
367
368 $symbols = $this->getMarkerSymbols($group);
369 foreach ($symbols as $symbol) {
370 if (isset($symbol->id) && $symbol->id == $id) {
371 // symbol found
372 return $symbol;
373 }
374 }
375
376 // symbol not found
377 return null;
378 }
379
380 /**
381 * AJAX method called by the main controller when adding
382 * or updating a new or existing SVG symbol icon.
383 *
384 * @return mixed
385 *
386 * @throws Exception
387 */
388 public function storeSvgSymbol()
389 {
390 $symbol = VikRequest::getVar('symbol', array(), 'request', 'array');
391 if (!is_array($symbol) || empty($symbol['id'])) {
392 throw new Exception("Symbol ID not found", 404);
393 }
394
395 if (empty($symbol['name'])) {
396 throw new Exception("Symbol name is empty", 500);
397 }
398
399 if (empty($symbol['path'])) {
400 throw new Exception("Symbol path is empty", 500);
401 }
402
403 // sanitize values
404 $symbol['group'] = !empty($symbol['group']) ? strtolower($symbol['group']) : 'default';
405 if (isset($symbol['opacity'])) {
406 // this must be a float, not a string
407 $symbol['opacity'] = floatval($symbol['opacity']);
408 }
409
410 $current_symbol = $this->getMarkerSymbol($symbol['id']);
411 $addnew = false;
412 if (is_object($current_symbol)) {
413 // force symbol group because ID exists
414 $symbol['group'] = $current_symbol->group;
415 // update existing marker symbol
416 foreach ($current_symbol as $prop => $val) {
417 if (isset($symbol[$prop])) {
418 // overwrite existing property taken from the new icon
419 $current_symbol->{$prop} = $symbol[$prop];
420 }
421 }
422 } else {
423 // create new marker symbol
424 $addnew = true;
425 $current_symbol = (object)$symbol;
426 }
427
428 $dbo = JFactory::getDbo();
429
430 $group_symbols = array();
431 $update = false;
432 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote('marker_symbols_' . $symbol['group']) . ";";
433 $dbo->setQuery($q);
434 $dbo->execute();
435 if ($dbo->getNumRows()) {
436 $update = true;
437 $group_symbols = json_decode($dbo->loadResult());
438 $group_symbols = !is_array($group_symbols) ? array() : $group_symbols;
439 }
440
441 if ($addnew) {
442 // push new symbol
443 array_push($group_symbols, $current_symbol);
444 } else {
445 // find current symbol
446 $found = false;
447 foreach ($group_symbols as $k => $s) {
448 if ($s->id == $symbol['id']) {
449 // replace existing symbol
450 $found = true;
451 $group_symbols[$k] = $current_symbol;
452 break;
453 }
454 }
455 if (!$found) {
456 // push new symbol as it was not found
457 array_push($group_symbols, $current_symbol);
458 }
459 }
460
461 // update values in db
462 if ($update) {
463 $q = "UPDATE `#__vikbooking_config` SET `setting`=" . $dbo->quote(json_encode($group_symbols)) . " WHERE `param`=" . $dbo->quote('marker_symbols_' . $symbol['group']) . ";";
464 } else {
465 $q = "INSERT INTO `#__vikbooking_config` (`param`,`setting`) VALUES (" . $dbo->quote('marker_symbols_' . $symbol['group']) . ", " . $dbo->quote(json_encode($group_symbols)) . ");";
466 }
467 $dbo->setQuery($q);
468 $dbo->execute();
469
470 // return the new symbol object just stored
471 return $current_symbol;
472 }
473
474 /**
475 * AJAX method called by the main controller when needing
476 * to delete an existing SVG symbol icon.
477 *
478 * @return mixed
479 *
480 * @throws Exception
481 */
482 public function deleteSvgSymbol()
483 {
484 $symbol_id = VikRequest::getString('symbol_id', '', 'request');
485 if (empty($symbol_id)) {
486 throw new Exception("Symbol ID is empty", 404);
487 }
488
489 $current_symbol = $this->getMarkerSymbol($symbol_id);
490 if (!is_object($current_symbol)) {
491 throw new Exception("Symbol ID not found", 404);
492 }
493
494 $dbo = JFactory::getDbo();
495 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote('marker_symbols_' . $current_symbol->group) . ";";
496 $dbo->setQuery($q);
497 $dbo->execute();
498 if (!$dbo->getNumRows()) {
499 throw new Exception("Symbol Group not found", 404);
500 }
501 $records = json_decode($dbo->loadResult());
502 if (!is_array($records) || !count($records)) {
503 throw new Exception("Symbol Group has got no symbols", 404);
504 }
505
506 $found = false;
507 foreach ($records as $k => $v) {
508 if ($v->id == $symbol_id) {
509 $found = true;
510 unset($records[$k]);
511 break;
512 }
513 }
514
515 if (!$found) {
516 throw new Exception("Symbol ID not found in all symbols of this group", 404);
517 }
518
519 // reset keys
520 $records = array_values($records);
521
522 // update db
523 $q = "UPDATE `#__vikbooking_config` SET `setting`=" . $dbo->quote(json_encode($records)) . " WHERE `param`=" . $dbo->quote('marker_symbols_' . $current_symbol->group) . ";";
524 $dbo->setQuery($q);
525 $dbo->execute();
526
527 return $symbol_id;
528 }
529
530 /**
531 * AJAX method called by the main controller when starting to work
532 * on the geo coding information for one specific room.
533 *
534 * @return int a simple result integer code identifier.
535 *
536 * @throws Exception
537 */
538 public function initRoomGeoTransient()
539 {
540 $dbo = JFactory::getDbo();
541 $room_id = VikRequest::getInt('room_id', 0, 'request');
542
543 $param = "geocoding_transient_room_" . $room_id;
544 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param) . ";";
545 $dbo->setQuery($q);
546 $dbo->execute();
547 if ($dbo->getNumRows()) {
548 // make the temporary record empty
549 $q = "UPDATE `#__vikbooking_config` SET `setting`='' WHERE `param`=" . $dbo->quote($param) . ";";
550 $dbo->setQuery($q);
551 $dbo->execute();
552
553 return 2;
554 }
555
556 return 1;
557 }
558
559 /**
560 * AJAX method called by the main controller when updating
561 * the geo coding information for one specific room.
562 *
563 * @return object the geo params stored.
564 *
565 * @throws Exception
566 */
567 public function updateRoomGeoTransient()
568 {
569 $dbo = JFactory::getDbo();
570
571 $room_id = VikRequest::getInt('room_id', 0, 'request');
572 $geo_enabled = VikRequest::getInt('geo_enabled', 0, 'request');
573 $geo_address = VikRequest::getString('geo_address', '', 'request');
574 $geo_latitude = VikRequest::getFloat('geo_latitude', 0, 'request');
575 $geo_longitude = VikRequest::getFloat('geo_longitude', 0, 'request');
576 $geo_zoom = VikRequest::getInt('geo_zoom', 1, 'request');
577 $geo_mtype = VikRequest::getString('geo_mtype', '', 'request');
578 $geo_height = VikRequest::getInt('geo_height', 100, 'request');
579 $geo_markers_multi = VikRequest::getInt('geo_markers_multi', 0, 'request');
580 $geo_marker_lat = VikRequest::getFloat('geo_marker_lat', 0, 'request');
581 $geo_marker_lng = VikRequest::getFloat('geo_marker_lng', 0, 'request');
582 $geo_marker_hide = VikRequest::getInt('geo_marker_hide', 0, 'request');
583 $geo_units_pos = VikRequest::getVar('geo_units_pos', array(), 'request', 'array');
584 $geo_goverlay = VikRequest::getInt('geo_goverlay', 0, 'request');
585 $geo_overlay_img = VikRequest::getString('geo_overlay_img', '', 'request');
586 $geo_overlay_south = VikRequest::getFloat('geo_overlay_south', 0, 'request');
587 $geo_overlay_west = VikRequest::getFloat('geo_overlay_west', 0, 'request');
588 $geo_overlay_north = VikRequest::getFloat('geo_overlay_north', 0, 'request');
589 $geo_overlay_east = VikRequest::getFloat('geo_overlay_east', 0, 'request');
590
591 // prepare geo params object
592 $geo_params = new stdClass;
593 /**
594 * Property "enabled" must ALWAYS be the first one
595 *
596 * @see getImportableConfigRooms()
597 */
598 $geo_params->enabled = $geo_enabled;
599 //
600 $geo_params->address = $geo_address;
601 $geo_params->latitude = $geo_latitude;
602 $geo_params->longitude = $geo_longitude;
603 $geo_params->zoom = $geo_zoom;
604 $geo_params->mtype = $geo_mtype;
605 $geo_params->height = $geo_height;
606 $geo_params->markers_multi = $geo_markers_multi;
607 $geo_params->marker_lat = $geo_marker_lat;
608 $geo_params->marker_lng = $geo_marker_lng;
609 $geo_params->marker_hide = $geo_marker_hide;
610 $geo_params->units_pos = $geo_units_pos;
611 $geo_params->goverlay = $geo_goverlay;
612 $geo_params->overlay_img = $geo_overlay_img;
613 $geo_params->overlay_south = $geo_overlay_south;
614 $geo_params->overlay_west = $geo_overlay_west;
615 $geo_params->overlay_north = $geo_overlay_north;
616 $geo_params->overlay_east = $geo_overlay_east;
617
618 // check transient record
619 $param = "geocoding_transient_room_" . $room_id;
620 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param) . ";";
621 $dbo->setQuery($q);
622 $dbo->execute();
623 if ($dbo->getNumRows()) {
624 $q = "UPDATE `#__vikbooking_config` SET `setting`=" . $dbo->quote(json_encode($geo_params)) . " WHERE `param`=" . $dbo->quote($param) . ";";
625 } else {
626 $q = "INSERT INTO `#__vikbooking_config` (`param`,`setting`) VALUES (" . $dbo->quote($param) . ", " . $dbo->quote(json_encode($geo_params)) . ");";
627 }
628 // update transient record with new geo params
629 $dbo->setQuery($q);
630 $dbo->execute();
631
632 return $geo_params;
633 }
634
635 /**
636 * Retrieves the last transient for the given room ID (or new room). This
637 * way the room geo params are ready to be stored by the main controller.
638 *
639 * @param int $room_id the room ID (0 for new).
640 *
641 * @return mixed geo params object or false.
642 */
643 public function getRoomGeoTransient($room_id)
644 {
645 $dbo = JFactory::getDbo();
646 $param = "geocoding_transient_room_" . (int)$room_id;
647
648 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param) . ";";
649 $dbo->setQuery($q);
650 $dbo->execute();
651 if (!$dbo->getNumRows()) {
652 $geo_enabled = VikRequest::getInt('geo_enabled', 0, 'request');
653 if ($geo_enabled) {
654 VikError::raiseWarning('', 'no records found ' . $param);
655 }
656 return false;
657 }
658 $geo_params = json_decode($dbo->loadResult());
659
660 return is_object($geo_params) ? $geo_params : false;
661 }
662
663 /**
664 * Returns a list of room-types with a complete geocoding information that could be imported.
665 * This facilitates the setup of the geographic information for a new room-type.
666 *
667 * @param int $exclude_id the current room to exclude, 0 for new.
668 *
669 * @return array list of room-types with an importable geo configuration.
670 */
671 public function getImportableConfigRooms($exclude_id = 0)
672 {
673 $importable = array();
674
675 $dbo = JFactory::getDbo();
676 $clauses = array();
677
678 if ($exclude_id > 0) {
679 array_push($clauses, '`id` != ' . $exclude_id);
680 }
681
682 // fetch all rooms with the geo information enabled
683 array_push($clauses, '`params` LIKE ' . $dbo->quote('%"geo":{"enabled":1%'));
684
685 $q = "SELECT `id`, `name` FROM `#__vikbooking_rooms` WHERE " . implode(' AND ', $clauses) . " ORDER BY `name` ASC;";
686 $dbo->setQuery($q);
687 $dbo->execute();
688 if ($dbo->getNumRows()) {
689 $importable = $dbo->loadAssocList();
690 }
691
692 return $importable;
693 }
694
695 /**
696 * AJAX method called by the main controller when requesting
697 * to import the geo coding information for one specific room.
698 *
699 * @return object the geo params of the room requested.
700 *
701 * @throws Exception
702 */
703 public function importRoomGeoConfig()
704 {
705 $dbo = JFactory::getDbo();
706
707 $room_id = VikRequest::getInt('room_id', 0, 'request');
708
709 $q = "SELECT `id`, `name`, `params` FROM `#__vikbooking_rooms` WHERE id={$room_id};";
710 $dbo->setQuery($q);
711 $dbo->execute();
712 if (!$dbo->getNumRows()) {
713 throw new Exception("Room not found for importing geo config", 404);
714 }
715 $room = $dbo->loadAssoc();
716 $rparams = json_decode($room['params']);
717 if (!is_object($rparams)) {
718 throw new Exception("Unable to decode room params", 500);
719 }
720
721 // get all geo params
722 $geo_params = $this->getRoomGeoParams($rparams);
723
724 if (!is_object($geo_params)) {
725 throw new Exception("Unable to get room geo params", 500);
726 }
727
728 return $geo_params;
729 }
730 }
731