import_venues.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. if(empty($city)) continue;
  17. echo "Importing $internal_id - $city ... ";
  18. $location = Location::queryOne([
  19. "customer_id" => $customer_id,
  20. "internal_id" => $internal_id
  21. ]);
  22. if(!$location) {
  23. $location = new Location;
  24. $location->customer_id = $customer_id;
  25. $location->internal_id = $internal_id;
  26. }
  27. $location->city = $city;
  28. $location->legal_entity_name = $legal_entity_name;
  29. $location->region = $region;
  30. $location->area_manager = $area_manager;
  31. $hours_parts = [];
  32. if($opening_hours) $hours_parts[] = "Opening Hours: $opening_hours";
  33. if($closing_hours) $hours_parts[] = "Closing Hours: $closing_hours";
  34. $location->opening_hours = implode("\n", $hours_parts);
  35. $location->name = $name . " ($city)";
  36. $location->address_1 = str_replace(", $city", "", $address);
  37. $location->postal_code = $postal_code;
  38. if(!$location->latitude) {
  39. for($attempt = 0; $attempt < 2; $attempt++) {
  40. switch($attempt) {
  41. case 0: $query = $location->getDisplayAddress(); break;
  42. case 1: $query = $postal_code; break;
  43. }
  44. try {
  45. $results = Geolocation::geocode($query);
  46. if($results) {
  47. $result = $results[0];
  48. $location->latitude = $result->geometry->location->lat;
  49. $location->longitude = $result->geometry->location->lng;
  50. }
  51. }
  52. catch(Exception $e) {
  53. echo $e->getMessage() . ": $query ... ";
  54. }
  55. }
  56. }
  57. $success = $location->commit();
  58. if(!$success) throw new Exception("Commit failed.");
  59. echo "OK\n";
  60. }
  61. ?>