Jelajahi Sumber

Add bar name support to import venues script; increase name column length

Alan Lam 3 tahun lalu
induk
melakukan
5c022d1832

+ 11 - 0
db/migrations/20230615114329_increase_location_name_field_length.php

@@ -0,0 +1,11 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class IncreaseLocationNameFieldLength extends AbstractMigration {
+	public function change() {
+		$this->table("location")
+			->changeColumn("name", "string", ["limit" => 200, "null" => false])
+			->update();
+	}
+}

+ 37 - 11
tasks/import_venues.php

@@ -14,17 +14,18 @@ use Carbon\CarbonImmutable;
 
 
 require "../initialise.php";
 require "../initialise.php";
 
 
-if($argc < 4) {
-	echo sprintf("Usage: php %s [PATH] [CUSTOMER ID] [REGION CODE]\n", basename(__FILE__));
+if($argc < 3) {
+	echo sprintf("Usage: php %s [PATH] [CUSTOMER ID]\n", basename(__FILE__));
 	exit(0);
 	exit(0);
 }
 }
 
 
 $script = array_shift($argv);
 $script = array_shift($argv);
 $path = array_shift($argv);
 $path = array_shift($argv);
 $customer_id = array_shift($argv);
 $customer_id = array_shift($argv);
-$region_code = array_shift($argv);
 
 
+$dry_run = false;
 $archive_missing = false;
 $archive_missing = false;
+$region_code = "GB";
 
 
 while($argv) {
 while($argv) {
 	$argument = array_shift($argv);
 	$argument = array_shift($argv);
@@ -34,6 +35,14 @@ while($argv) {
 			$archive_missing = true;
 			$archive_missing = true;
 			break;
 			break;
 
 
+		case "--region-code":
+			$region_code = array_shift($argv);
+			break;
+
+		case "--dry-run":
+			$dry_run = true;
+			break;
+
 		default:
 		default:
 			throw new RuntimeException("Invalid argument: " . $argument);
 			throw new RuntimeException("Invalid argument: " . $argument);
 	}
 	}
@@ -55,7 +64,13 @@ foreach($customer->getLocations() as $location) {
 	$missing_locations[$location->id] = $location;
 	$missing_locations[$location->id] = $location;
 }
 }
 
 
-$confirm = trim(readline($customer->name . ": Confirm Y/N: "));
+echo $customer->name . "\n";
+echo sprintf("Region Code: %s\n", $region_code);
+if($dry_run) {
+	echo "Dry run\n";
+}
+
+$confirm = trim(readline("Confirm Y/N: "));
 if($confirm !== "Y") {
 if($confirm !== "Y") {
 	echo "Aborted.\n";
 	echo "Aborted.\n";
 	exit(0);
 	exit(0);
@@ -81,6 +96,8 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 				case "Name": $name_column = $index; break;
 				case "Name": $name_column = $index; break;
 				case "Legal Entity Name": $legal_entity_name_column = $index; break;
 				case "Legal Entity Name": $legal_entity_name_column = $index; break;
 
 
+				case "Bar Name": $bar_name_column = $index; break;
+
 				case "Address": $address_column = $index; break;
 				case "Address": $address_column = $index; break;
 				case "Address 1": $address_1_column = $index; break;
 				case "Address 1": $address_1_column = $index; break;
 				case "Address 2": $address_2_column = $index; break;
 				case "Address 2": $address_2_column = $index; break;
@@ -130,6 +147,7 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 	$internal_id = isset($internal_id_column) ? $data[$internal_id_column] : null;
 	$internal_id = isset($internal_id_column) ? $data[$internal_id_column] : null;
 	$city = isset($city_column) ? $data[$city_column] : null;
 	$city = isset($city_column) ? $data[$city_column] : null;
 	$legal_entity_name = isset($legal_entity_name_column) ? $data[$legal_entity_name_column] : null;
 	$legal_entity_name = isset($legal_entity_name_column) ? $data[$legal_entity_name_column] : null;
+	$bar_name = isset($bar_name_column) ? $data[$bar_name_column] : null;
 	$region = isset($region_column) ? $data[$region_column] : null;
 	$region = isset($region_column) ? $data[$region_column] : null;
 	$area_manager = isset($area_manager_column) ? $data[$area_manager_column] : null;
 	$area_manager = isset($area_manager_column) ? $data[$area_manager_column] : null;
 	$opening_hours = isset($opening_hours_column) ? $data[$opening_hours_column] : null;
 	$opening_hours = isset($opening_hours_column) ? $data[$opening_hours_column] : null;
@@ -176,7 +194,7 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 		continue;
 		continue;
 	}
 	}
 
 
-	echo "Importing $internal_id - $city ... ";
+	echo "Importing $internal_id - $address_1 ($city) ($bar_name) ... ";
 
 
 	$location = Location::queryOne([
 	$location = Location::queryOne([
 		"customer_id" => $customer_id,
 		"customer_id" => $customer_id,
@@ -214,19 +232,23 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 		$location->opening_hours = implode("\n", $hours_parts);
 		$location->opening_hours = implode("\n", $hours_parts);
 	}
 	}
 
 
-
 	if(!empty($name)) {
 	if(!empty($name)) {
 		$location->name = $name;
 		$location->name = $name;
 		if($city && stripos($name, $city) === false) {
 		if($city && stripos($name, $city) === false) {
 			$location->name .= " ($city)";
 			$location->name .= " ($city)";
 		}
 		}
+		if($bar_name) {
+			$location->name .= " ($bar_name)";
+		}
 	}
 	}
 
 
 	if(!empty($address_1) && !empty($city)) {
 	if(!empty($address_1) && !empty($city)) {
 		$location->address_1 = str_replace(", $city", "", $address_1);
 		$location->address_1 = str_replace(", $city", "", $address_1);
 	}
 	}
 
 
-	if(!empty($postal_code)) $location->postal_code = $postal_code;
+	if(!empty($postal_code)) {
+		$location->postal_code = $postal_code;
+	}
 
 
 	if($archived !== null) {
 	if($archived !== null) {
 		$location->archived = $archived ? 1 : 0;
 		$location->archived = $archived ? 1 : 0;
@@ -235,10 +257,8 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 		$location->closed = $closed ? 1 : 0;
 		$location->closed = $closed ? 1 : 0;
 	}
 	}
 
 
-	if($region_code && !$location->latitude) {
-
+	if(!$dry_run && !$location->latitude) {
 		for($attempt = 0; $attempt < 2; $attempt++) {
 		for($attempt = 0; $attempt < 2; $attempt++) {
-
 			switch($attempt) {
 			switch($attempt) {
 				default:
 				default:
 				case 0: $query = $location->getDisplayAddress(); break;
 				case 0: $query = $location->getDisplayAddress(); break;
@@ -260,7 +280,13 @@ for($i = 0; $data = fgetcsv($handle); $i++) {
 		}
 		}
 	}
 	}
 
 
-	$success = $location->commit();
+	if($dry_run) {
+		$success = true;
+	}
+	else {
+		$success = $location->commit();
+	}
+
 	if(!$success) {
 	if(!$success) {
 		throw new RuntimeException("Commit failed.");
 		throw new RuntimeException("Commit failed.");
 	}
 	}

+ 1 - 1
views/location_form.php

@@ -56,7 +56,7 @@ if($is_edit) {
 						<div class="row mb-1">
 						<div class="row mb-1">
 							<label class="col-sm-4 col-form-label" for="name">Name</label>
 							<label class="col-sm-4 col-form-label" for="name">Name</label>
 							<div class="col-sm">
 							<div class="col-sm">
-								<input class="form-control" type="text" name="name" id="name" placeholder="Enter name" value="<?=htmlspecialchars($location->name)?>" required />
+								<input class="form-control" type="text" name="name" id="name" placeholder="Enter name" value="<?=htmlspecialchars($location->name)?>" maxlength="200" required />
 							</div>
 							</div>
 						</div>
 						</div>