import_venues.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $address_2 = null;
  20. $county = null;
  21. $phone = null;
  22. if(!empty($line[10])) $email = $line[10];
  23. if(!empty($line[11])) $latitude = $line[11];
  24. if(!empty($line[12])) $longitude = $line[12];
  25. if(!empty($line[13])) $address_2 = $line[13];
  26. if(!empty($line[14])) $county = $line[14];
  27. if(!empty($line[15])) $phone = $line[15];
  28. if(empty($city)) continue;
  29. echo "Importing $internal_id - $city ... ";
  30. $location = Location::queryOne([
  31. "customer_id" => $customer_id,
  32. "internal_id" => $internal_id
  33. ]);
  34. if(!$location) {
  35. $location = new Location;
  36. $location->customer_id = $customer_id;
  37. $location->internal_id = $internal_id;
  38. }
  39. if(!empty($email)) $location->email = $email;
  40. if(!empty($latitude)) $location->latitude = $latitude;
  41. if(!empty($longitude)) $location->longitude = $longitude;
  42. if(!empty($address_2)) $location->address_2 = $address_2;
  43. if(!empty($county)) $location->county = $county;
  44. if(!empty($phone)) $location->phone = $phone;
  45. $location->city = $city;
  46. $location->legal_entity_name = $legal_entity_name;
  47. $location->region = $region;
  48. $location->area_manager = $area_manager;
  49. $hours_parts = [];
  50. if($opening_hours) $hours_parts[] = "Opening Hours: $opening_hours";
  51. if($closing_hours) $hours_parts[] = "Closing Hours: $closing_hours";
  52. $location->opening_hours = implode("\n", $hours_parts);
  53. $location->name = $name . " ($city)";
  54. $location->address_1 = str_replace(", $city", "", $address);
  55. $location->postal_code = $postal_code;
  56. if(!$location->latitude) {
  57. for($attempt = 0; $attempt < 2; $attempt++) {
  58. switch($attempt) {
  59. case 0: $query = $location->getDisplayAddress(); break;
  60. case 1: $query = $postal_code; break;
  61. }
  62. try {
  63. $results = Geolocation::geocode($query);
  64. if($results) {
  65. $result = $results[0];
  66. $location->latitude = $result->geometry->location->lat;
  67. $location->longitude = $result->geometry->location->lng;
  68. }
  69. }
  70. catch(Exception $e) {
  71. echo $e->getMessage() . ": $query ... ";
  72. }
  73. }
  74. }
  75. $success = $location->commit();
  76. if(!$success) throw new Exception("Commit failed.");
  77. echo "OK\n";
  78. }
  79. ?>