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; $email = null; $latitude = null; $longitude = null; if(!empty($line[10])) $email = $line[10]; if(!empty($line[11])) $latitude = $line[11]; if(!empty($line[12])) $longitude = $line[12]; 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; } if(!empty($email)) $location->email = $email; if(!empty($latitude)) $location->latitude = $latitude; if(!empty($longitude)) $location->longitude = $longitude; $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"; } ?>