update_geolocation_coordinates.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. exit;
  4. require __DIR__.'/../../main/inc/global.inc.php';
  5. $extraField = new ExtraField('user');
  6. $extraFieldValue = new ExtraFieldValue('user');
  7. $infoStage = $extraField->get_handler_field_info_by_field_variable('terms_villedustage');
  8. $infoVille = $extraField->get_handler_field_info_by_field_variable('terms_ville');
  9. $tableUser = Database::get_main_table(TABLE_MAIN_USER);
  10. $tableValues = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  11. // Ville
  12. echo '<h3>Ville:</h3>';
  13. $sql = "SELECT u.id, ev.id ville_id, ev.value ville
  14. FROM $tableUser u
  15. INNER JOIN extra_field_values ev
  16. ON ev.item_id = u.id
  17. WHERE
  18. ev.field_id = ".$infoVille['id']." AND
  19. u.active = 1 AND
  20. (ev.value <> '') AND
  21. (ev.value NOT LIKE '%::%')
  22. ";
  23. //2643 or u.id = 2692 or u.id = 2656
  24. $result = Database::query($sql);
  25. $data = Database::store_result($result, 'ASSOC');
  26. foreach ($data as $result) {
  27. if (!empty($result['ville'])) {
  28. $newAddress = Database::escape_string(getCoordinates($result['ville']));
  29. if ($newAddress) {
  30. $sql = "UPDATE $tableValues SET value = '".$newAddress."' WHERE id = ".$result['ville_id'];
  31. Database::query($sql);
  32. var_dump($result['ville']."-".$sql);
  33. } else {
  34. var_dump("nothing found for ville: ".$result['ville']);
  35. }
  36. }
  37. }
  38. // stage
  39. echo '<h3>Stage:</h3>';
  40. $sql = "SELECT u.id, ev2.id stage_id, ev2.value stage
  41. FROM $tableUser u
  42. INNER JOIN extra_field_values ev2
  43. ON ev2.item_id = u.id
  44. WHERE
  45. ev2.field_id = ".$infoStage['id']." AND
  46. u.active = 1 AND
  47. (ev2.value <> '') AND
  48. (ev2.value NOT LIKE '%::%')
  49. ";
  50. //2643 or u.id = 2692 or u.id = 2656
  51. $result = Database::query($sql);
  52. $data = Database::store_result($result, 'ASSOC');
  53. foreach ($data as $result) {
  54. if (!empty($result['stage'])) {
  55. $newAddress = Database::escape_string(getCoordinates($result['stage']));
  56. if ($newAddress) {
  57. $sql = "UPDATE $tableValues SET value = '".$newAddress."' WHERE id = ".$result['stage_id'];
  58. Database::query($sql);
  59. var_dump($result['stage']."-".$sql);
  60. } else {
  61. var_dump("nothing found for ".$result['stage']);
  62. }
  63. }
  64. }
  65. function getCoordinates($address)
  66. {
  67. static $list;
  68. if (empty($address)) {
  69. return false;
  70. }
  71. // ignore one letter fields
  72. if (strlen($address) == 1) {
  73. return false;
  74. }
  75. if (isset($list[$address])) {
  76. return $list[$address];
  77. }
  78. $key = api_get_configuration_value('google_api_key');
  79. $prepAddr = str_replace(' ', '+', $address);
  80. $geocode = file_get_contents(
  81. 'https://maps.google.com/maps/api/geocode/json?key='.$key.'&address='.$prepAddr.'&sensor=false'
  82. );
  83. $output = json_decode($geocode);
  84. $error = json_last_error();
  85. if ($error == JSON_ERROR_NONE && isset($output->results[0])) {
  86. $latitude = $output->results[0]->geometry->location->lat;
  87. $longitude = $output->results[0]->geometry->location->lng;
  88. if ($latitude != '' && $longitude != '') {
  89. $result = "$address::$latitude,$longitude";
  90. $list[$address] = $result;
  91. return $result;
  92. }
  93. }
  94. return false;
  95. }