1
0

import_venues.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. require "../initialise.php";
  3. $path = readline("Type relative path to file, CWD is " . getcwd() . ": ");
  4. if(!file_exists($path)) die($path . " not found.");
  5. $customer_id = readline("Type customer ID: ");
  6. $customer = Customer::get($customer_id);
  7. if(!$customer) die("Customer not found.");
  8. $confirm = readline($customer->name . ": Confirm Y/N: ");
  9. if($confirm != "Y") die("Aborted");
  10. $handle = fopen($path, "r");
  11. date_default_timezone_set("UTC");
  12. for($i = 0; $line = fgetcsv($handle); $i++) {
  13. if($i == 0) continue;
  14. $line = array_map("trim", $line);
  15. list($internal_id, $city, $legal_entity_name, $region, $area_manager, $opening_hours, $closing_hours, $name, $address, $postal_code) = $line;
  16. $email = null;
  17. $latitude = null;
  18. $longitude = null;
  19. if(!empty($line[10])) $email = $line[10];
  20. if(!empty($line[11])) $latitude = $line[11];
  21. if(!empty($line[12])) $longitude = $line[12];
  22. if(empty($city)) continue;
  23. echo "Importing $internal_id - $city ... ";
  24. $location = Location::queryOne([
  25. "customer_id" => $customer_id,
  26. "internal_id" => $internal_id
  27. ]);
  28. if(!$location) {
  29. $location = new Location;
  30. $location->customer_id = $customer_id;
  31. $location->internal_id = $internal_id;
  32. }
  33. if(!empty($email)) $location->email = $email;
  34. if(!empty($latitude)) $location->latitude = $latitude;
  35. if(!empty($longitude)) $location->longitude = $longitude;
  36. $location->city = $city;
  37. $location->legal_entity_name = $legal_entity_name;
  38. $location->region = $region;
  39. $location->area_manager = $area_manager;
  40. $hours_parts = [];
  41. if($opening_hours) $hours_parts[] = "Opening Hours: $opening_hours";
  42. if($closing_hours) $hours_parts[] = "Closing Hours: $closing_hours";
  43. $location->opening_hours = implode("\n", $hours_parts);
  44. $location->name = $name . " ($city)";
  45. $location->address_1 = str_replace(", $city", "", $address);
  46. $location->postal_code = $postal_code;
  47. if(!$location->latitude) {
  48. for($attempt = 0; $attempt < 2; $attempt++) {
  49. switch($attempt) {
  50. case 0: $query = $location->getDisplayAddress(); break;
  51. case 1: $query = $postal_code; break;
  52. }
  53. try {
  54. $results = Geolocation::geocode($query);
  55. if($results) {
  56. $result = $results[0];
  57. $location->latitude = $result->geometry->location->lat;
  58. $location->longitude = $result->geometry->location->lng;
  59. }
  60. }
  61. catch(Exception $e) {
  62. echo $e->getMessage() . ": $query ... ";
  63. }
  64. }
  65. }
  66. $success = $location->commit();
  67. if(!$success) throw new Exception("Commit failed.");
  68. echo "OK\n";
  69. }
  70. ?>