Jelajahi Sumber

Factorise location export code

Alan Lam 4 tahun lalu
induk
melakukan
2a5351fbec

+ 10 - 58
controllers/locations.php

@@ -1,5 +1,8 @@
 <?php /** @noinspection PhpUnhandledExceptionInspection */
 
+use Exports\LocationCsvExport;
+use Exports\LocationKmlExport;
+
 Core::addScript("public/js/app-locations.js");
 
 Core::addBreadcrumb("/account", "Account");
@@ -93,67 +96,16 @@ $action = http::value("action");
 if(!empty($action)) {
 	switch($action) {
 		case "export":
+			if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource.");
 
-			if(!Core::$user->canAdmin()) throw new Exception("You do not have permission to access this resource.");
-
-			header("Content-Type: text/csv; charset=utf-8");
-			header("Content-Disposition: attachment; filename=$customer_slug-export.csv");
-
-			$handle = fopen("php://output", "w");
-
-			fputcsv($handle, [
-				"ID", "Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
-			]);
-
-			foreach($locations as $location) {
-				fputcsv($handle, [
-					$location->id,
-					$location->internal_id,
-					$location->city,
-					$location->legal_entity_name,
-					$location->region,
-					$location->area_manager,
-					$location->area_manager_email,
-					"",
-					"",
-					$location->name,
-					$location->address_1,
-					$location->postal_code,
-					$location->email,
-					$location->latitude,
-					$location->longitude,
-				]);
-			}
-
-			fclose($handle);
-
-			exit;
+			$export = new LocationCsvExport($locations);
+			$export->export("$customer_slug-export.csv");
+			exit();
 
 		case "export-kml":
-
-			header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
-			header("Content-Disposition: attachment; filename=$customer_slug-export.kml");
-
-			$xml = simplexml_load_file(ROOT_DIR . "/files/empty.kml");
-
-			$folder_node = $xml->addChild("Folder");
-			$folder_node->addChild("name", $customer->name);
-			$folder_node->addChild("open", 1);
-			$folder_node->addChild("description", $customer->name . " Locations");
-
-			foreach($locations as $location) {
-				$placemark_node = $folder_node->addChild("Placemark");
-
-				$placemark_node->addChild("name", htmlspecialchars($location->name));
-				$placemark_node->addChild("description", htmlspecialchars($location->getDisplayAddress()));
-
-				$point_node = $placemark_node->addChild("Point");
-				$point_node->addChild("coordinates", $location->longitude . "," . $location->latitude);
-
-			}
-
-			echo $xml->asXML();
-
+			$export = new LocationKmlExport($locations);
+			$export->setCustomer($customer);
+			$export->export("$customer_slug-export.kml");
 			exit;
 	}
 }

+ 47 - 0
engine/exports/location_csv_export.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace Exports;
+
+class LocationCsvExport {
+
+	/** @var \Location[] */
+	private $locations = [];
+
+	public function __construct(array $locations) {
+		$this->locations = $locations;
+	}
+
+	public function export(string $filename): void {
+		header("Content-Type: text/csv; charset=utf-8");
+		header("Content-Disposition: attachment; filename=$filename");
+
+		$handle = fopen("php://output", "wb");
+
+		fputcsv($handle, [
+			"ID", "Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
+		]);
+
+		foreach($this->locations as $location) {
+			fputcsv($handle, [
+				$location->id,
+				$location->internal_id,
+				$location->city,
+				$location->legal_entity_name,
+				$location->region,
+				$location->area_manager,
+				$location->area_manager_email,
+				"",
+				"",
+				$location->name,
+				$location->address_1,
+				$location->postal_code,
+				$location->email,
+				$location->latitude,
+				$location->longitude,
+			]);
+		}
+
+		fclose($handle);
+	}
+
+}

+ 51 - 0
engine/exports/location_kml_export.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace Exports;
+
+use Customer;
+use Location;
+
+class LocationKmlExport {
+
+	/** @var Location[] */
+	private $locations = [];
+
+	/** @var Customer */
+	private $customer;
+
+	public function __construct(array $locations) {
+		$this->locations = $locations;
+	}
+
+	public function export(string $filename): void {
+		header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
+		header("Content-Disposition: attachment; filename=$filename");
+
+		$xml = simplexml_load_string(file_get_contents(ROOT_DIR . "/files/empty.kml"));
+
+		$folder_node = $xml->addChild("Folder");
+		$folder_node->addChild("name", $this->customer->name);
+		$folder_node->addChild("open", 1);
+		$folder_node->addChild("description", $this->customer->name . " Locations");
+
+		foreach($this->locations as $location) {
+			$placemark_node = $folder_node->addChild("Placemark");
+
+			$placemark_node->addChild("name", htmlspecialchars($location->name));
+			$placemark_node->addChild("description", htmlspecialchars($location->getDisplayAddress()));
+
+			$point_node = $placemark_node->addChild("Point");
+			$point_node->addChild("coordinates", $location->longitude . "," . $location->latitude);
+		}
+
+		echo $xml->asXML();
+	}
+
+	/**
+	 * @param Customer $customer
+	 */
+	public function setCustomer(Customer $customer): void {
+		$this->customer = $customer;
+	}
+
+}