Просмотр исходного кода

Update import venues script to support full address columns

Alan Lam 6 лет назад
Родитель
Сommit
3333cdebd7
1 измененных файлов с 33 добавлено и 6 удалено
  1. 33 6
      tasks/import_venues.php

+ 33 - 6
tasks/import_venues.php

@@ -2,15 +2,20 @@
 
 require "../initialise.php";
 
-$path = trim(readline("Type relative path to file, CWD is " . getcwd() . ": "));
+if($argc < 3) {
+	die(sprintf("Usage: php %s [PATH] [CUSTOMER ID] [REGION CODE]\n", basename(__FILE__)));
+}
+
+$script = array_shift($argv);
+$path = array_shift($argv);
+$customer_id = array_shift($argv);
+$region_code = array_shift($argv);
+
 if(!file_exists($path)) die($path . " not found.");
 
-$customer_id = trim(readline("Type customer ID: "));
 $customer = Customer::get($customer_id);
 if(!$customer) die("Customer not found.");
 
-$region_code = trim(readline("Type region code (leave blank to skip geocoding): "));
-
 $confirm = trim(readline($customer->name . ": Confirm Y/N: "));
 if($confirm != "Y") die("Aborted");
 
@@ -20,6 +25,8 @@ date_default_timezone_set("UTC");
 
 for($i = 0; $data = fgetcsv($handle); $i++) {
 
+	$row_number = $i + 1;
+
 	$data = array_map("trim", $data);
 
 	if($i == 0) {
@@ -29,6 +36,7 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 
 				case "Name": $name_column = $index; break;
 				case "Legal Entity Name": $legal_entity_name_column = $index; break;
+				case "Address": $address_column = $index; break;
 				case "Address 1": $address_1_column = $index; break;
 				case "Address 2": $address_2_column = $index; break;
 				case "City": $city_column = $index; break;
@@ -74,6 +82,7 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 	$opening_hours = isset($opening_hours_column) ? $data[$opening_hours_column] : null;
 	$closing_hours = isset($closing_hours_column) ? $data[$closing_hours_column] : null;
 	$name = isset($name_column) ? $data[$name_column] : null;
+	$address = isset($address_column) ? $data[$address_column] : null;
 	$address_1 = isset($address_1_column) ? $data[$address_1_column] : null;
 	$postal_code = isset($postal_code_column) ? $data[$postal_code_column] : null;
 
@@ -90,8 +99,26 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 	$region_name = isset($region_name_column) ? $data[$region_name_column] : null;
 	$operations_manager_name = isset($operations_manager_name_column) ? $data[$operations_manager_name_column] : null;
 
+	if($address) {
+		// Try to determine address parts from address
+		$parts = explode(",", $address);
+		$parts = array_map("trim", $parts);
+
+		$postal_code = array_pop($parts);
+		$address_1 = array_shift($parts);
+
+		$count = count($parts);
+
+		if($count === 2) list($address_2, $city) = $parts;
+		else if($count === 1) list($city) = $parts;
+
+	}
+
 	if(empty($internal_id)) continue;
-	if(empty($city)) continue;
+	if(empty($city) && empty($postal_code)) {
+		echo sprintf("Skipping row %d as there is no city or postal code\n", $row_number);
+		continue;
+	}
 
 	echo "Importing $internal_id - $city ... ";
 
@@ -134,7 +161,7 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 
 	if(!empty($name)) {
 		$location->name = $name;
-		if(stripos($name, $city) === false) {
+		if($city && stripos($name, $city) === false) {
 			$location->name .= " ($city)";
 		}
 	}