| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- require "../initialise.php";
- $path = readline("Type relative path to file, CWD is " . getcwd() . ": ");
- if(!file_exists($path)) die($path . " not found.");
- $customer_id = readline("Type customer ID: ");
- $customer = Customer::get($customer_id);
- if(!$customer) die("Customer not found.");
- $confirm = readline($customer->name . ": Confirm Y/N: ");
- if($confirm != "Y") die("Aborted");
- $handle = fopen($path, "r");
- date_default_timezone_set("UTC");
- for($i = 0; $line = fgetcsv($handle); $i++) {
- if($i == 0) continue;
- $line = array_map("trim", $line);
- list($internal_id, $city, $legal_entity_name, $region, $area_manager, $opening_hours, $closing_hours, $name, $address, $postal_code) = $line;
- if(empty($city)) continue;
- echo "Importing $internal_id - $city ... ";
- $location = Location::queryOne([
- "customer_id" => $customer_id,
- "internal_id" => $internal_id
- ]);
- if(!$location) {
- $location = new Location;
- $location->customer_id = $customer_id;
- $location->internal_id = $internal_id;
- }
- $location->city = $city;
- $location->legal_entity_name = $legal_entity_name;
- $location->region = $region;
- $location->area_manager = $area_manager;
- $hours_parts = [];
- if($opening_hours) $hours_parts[] = "Opening Hours: $opening_hours";
- if($closing_hours) $hours_parts[] = "Closing Hours: $closing_hours";
- $location->opening_hours = implode("\n", $hours_parts);
- $location->name = $name . " ($city)";
- $location->address_1 = str_replace(", $city", "", $address);
- $location->postal_code = $postal_code;
- if(!$location->latitude) {
- for($attempt = 0; $attempt < 2; $attempt++) {
- switch($attempt) {
- case 0: $query = $location->getDisplayAddress(); break;
- case 1: $query = $postal_code; break;
- }
- try {
- $results = Geolocation::geocode($query);
- if($results) {
- $result = $results[0];
- $location->latitude = $result->geometry->location->lat;
- $location->longitude = $result->geometry->location->lng;
- }
- }
- catch(Exception $e) {
- echo $e->getMessage() . ": $query ... ";
- }
- }
- }
- $success = $location->commit();
- if(!$success) throw new Exception("Commit failed.");
- echo "OK\n";
- }
- ?>
|