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

Add support for exporting locations in KML format

Alan Lam 8 лет назад
Родитель
Сommit
903b15ac31
2 измененных файлов с 31 добавлено и 1 удалено
  1. 30 1
      controllers/locations.php
  2. 1 0
      views/locations.php

+ 30 - 1
controllers/locations.php

@@ -8,6 +8,8 @@ Core::addBreadcrumb("/account/locations", "Locations");
 $customer = Customer::getForAdmin();
 if(!$customer) throw new Exception("You do not have permission to access this page.");
 
+$customer_slug = Util::slug($customer->name);
+
 $sorts = [
 	"name" => "Name",
 	"last-added" => "Last Added",
@@ -56,7 +58,7 @@ if(!empty($action)) {
 			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=export.csv");
+			header("Content-Disposition: attachment; filename=$customer_slug-export.csv");
 
 			$handle = fopen("php://output", "w");
 
@@ -84,6 +86,33 @@ if(!empty($action)) {
 
 			fclose($handle);
 
+			exit;
+
+		case "export-kml":
+
+			header("Content-Type: text/xml; charset=utf-8");
+			header("Content-Disposition: attachment; filename=$customer_slug-export.kml");
+
+			$xml = simplexml_load_file("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();
+
 			exit;
 	}
 }

+ 1 - 0
views/locations.php

@@ -38,6 +38,7 @@ var customerId = <?=$customer->id?>;
 				<div class="dropdown-menu dropdown-menu-right" aria-labelledby="actions-button">
 					<a class="dropdown-item" href="/account/locations/add?customer_id=<?=$customer->id?>/edit">Add</a>
 					<a class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export">Export CSV</a>
+					<a class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export-kml">Export KML</a>
 				</div>
 			</div>