/lib/geo/geolib.php

https://gitlab.com/ElvisAns/tiki · PHP · 206 lines · 136 code · 27 blank · 43 comment · 24 complexity · cb8a9b1ef71c50348820a3857d6f9b24 MD5 · raw file

  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /**
  8. *
  9. */
  10. class GeoLib
  11. {
  12. /**
  13. * @param $type
  14. * @param $itemId
  15. * @return array
  16. */
  17. public function get_coordinates($type, $itemId)
  18. {
  19. $attributelib = TikiLib::lib('attribute');
  20. $attributes = $attributelib->get_attributes($type, $itemId);
  21. if (isset($attributes['tiki.geo.lat'], $attributes['tiki.geo.lon'])) {
  22. $coords = [
  23. 'lat' => $attributes['tiki.geo.lat'],
  24. 'lon' => $attributes['tiki.geo.lon'],
  25. ];
  26. if ($coords['lat'] == 0 && $coords['lon'] == 0) {
  27. return;
  28. }
  29. if (! empty($attributes['tiki.geo.google.zoom'])) {
  30. $coords['zoom'] = $attributes['tiki.geo.google.zoom'];
  31. }
  32. return $coords;
  33. }
  34. }
  35. /**
  36. * @param $type
  37. * @param $itemId
  38. * @return string
  39. */
  40. public function get_coordinates_string($type, $itemId)
  41. {
  42. if ($coords = $this->get_coordinates($type, $itemId)) {
  43. return $this->build_location_string($coords);
  44. }
  45. }
  46. /**
  47. * @param array $coords
  48. * @return string
  49. */
  50. public function build_location_string($coords)
  51. {
  52. $string = '';
  53. if (isset($coords['lat']) && isset($coords['lon'])) {
  54. $string = "{$coords['lon']},{$coords['lat']}";
  55. if (isset($coords['zoom'])) {
  56. $string .= ",{$coords['zoom']}";
  57. }
  58. }
  59. return $string;
  60. }
  61. /**
  62. * @param $type
  63. * @param $itemId
  64. * @param $coordinates
  65. */
  66. public function set_coordinates($type, $itemId, $coordinates)
  67. {
  68. if (is_string($coordinates)) {
  69. $coordinates = $this->parse_coordinates($coordinates);
  70. }
  71. if (isset($coordinates['lat'], $coordinates['lon'])) {
  72. $attributelib = TikiLib::lib('attribute');
  73. $attributelib->set_attribute($type, $itemId, 'tiki.geo.lat', $coordinates['lat']);
  74. $attributelib->set_attribute($type, $itemId, 'tiki.geo.lon', $coordinates['lon']);
  75. if (isset($coordinates['zoom'])) {
  76. $attributelib->set_attribute($type, $itemId, 'tiki.geo.google.zoom', $coordinates['zoom']);
  77. }
  78. }
  79. }
  80. /**
  81. * @param $string
  82. * @return array
  83. */
  84. public function parse_coordinates($string)
  85. {
  86. if (preg_match("/^(-?\d*(\.\d+)?),(-?\d*(\.\d+)?)(,(\d+))?$/", $string, $parts)) {
  87. $coords = [
  88. 'lat' => $parts[3],
  89. 'lon' => $parts[1],
  90. ];
  91. if (isset($parts[6])) {
  92. $coords['zoom'] = $parts[6];
  93. }
  94. return $coords;
  95. }
  96. }
  97. /**
  98. * @param $where
  99. * @return array|bool
  100. */
  101. public function geocode($where)
  102. {
  103. global $prefs;
  104. $url = 'https://maps.googleapis.com/maps/api/geocode/json?' . http_build_query(
  105. [
  106. 'address' => $where,
  107. 'sensor' => 'false',
  108. 'key' => $prefs['gmap_key'],
  109. ],
  110. '',
  111. '&'
  112. );
  113. $response = TikiLib::lib('tiki')->httprequest($url);
  114. $data = json_decode($response);
  115. if ($data->status !== 'OK') {
  116. return [
  117. 'error' => $data->error_message,
  118. 'status' => $data->status,
  119. ];
  120. }
  121. $first = reset($data->results);
  122. return [
  123. 'status' => 'OK',
  124. 'accuracy' => 500,
  125. 'label' => $first->formatted_address,
  126. 'lat' => $first->geometry->location->lat,
  127. 'lon' => $first->geometry->location->lng,
  128. 'address_components' => $first->address_components,
  129. ];
  130. }
  131. /**
  132. * @param $geo
  133. * @return array|bool
  134. */
  135. public function geofudge($geo)
  136. {
  137. if (! $geo) {
  138. return false;
  139. }
  140. if (empty($geo["lon"]) || empty($geo["lat"])) {
  141. return ["lon" => 0, "lat" => 0];
  142. }
  143. $geo["lon"] = $geo["lon"] + mt_rand(0, 10000) / 8000;
  144. $geo["lat"] = $geo["lat"] + mt_rand(0, 10000) / 10000;
  145. return $geo;
  146. }
  147. /**
  148. * @param $itemId
  149. * @param $geo
  150. */
  151. public function setTrackerGeo($itemId, $geo)
  152. {
  153. global $prefs;
  154. $trklib = TikiLib::lib('trk');
  155. $item = $trklib->get_tracker_item($itemId);
  156. $fields = $trklib->list_tracker_fields($item['trackerId']);
  157. foreach ($fields["data"] as $f) {
  158. if ($f["type"] == 'G' && $f["options_array"][0] == 'y') {
  159. $fieldId = $f["fieldId"];
  160. $options_array = $f["options_array"];
  161. $pointx = $geo['lon'];
  162. $pointy = $geo['lat'];
  163. $pointz = $prefs["gmap_defaultz"];
  164. break;
  165. }
  166. }
  167. if (isset($fieldId)) {
  168. $ins_fields["data"][$fieldId] = ['fieldId' => $fieldId, 'options_array' => $options_array, 'value' => "$pointx,$pointy,$pointz", 'type' => 'G'];
  169. $res = $trklib->replace_item($item['trackerId'], $itemId, $ins_fields);
  170. }
  171. }
  172. public function get_default_center()
  173. {
  174. global $prefs;
  175. $coords = $this->parse_coordinates($prefs['gmap_defaultx'] . ',' . $prefs['gmap_defaulty'] . ',' . $prefs['gmap_defaultz']);
  176. $center = ' data-geo-center="' . smarty_modifier_escape($this->build_location_string($coords)) . '" ';
  177. return $center;
  178. }
  179. }