location_kml_export.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Exports;
  3. use Customer;
  4. use Location;
  5. class LocationKmlExport {
  6. /** @var Location[] */
  7. private $locations = [];
  8. /** @var Customer */
  9. private $customer;
  10. public function __construct(array $locations) {
  11. $this->locations = $locations;
  12. }
  13. public function export(string $filename): void {
  14. header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
  15. header("Content-Disposition: attachment; filename=$filename");
  16. $xml = simplexml_load_string(file_get_contents(ROOT_DIR . "/files/empty.kml"));
  17. $folder_node = $xml->addChild("Folder");
  18. $folder_node->addChild("name", $this->customer->name);
  19. $folder_node->addChild("open", 1);
  20. $folder_node->addChild("description", $this->customer->name . " Locations");
  21. foreach($this->locations as $location) {
  22. $placemark_node = $folder_node->addChild("Placemark");
  23. $placemark_node->addChild("name", htmlspecialchars($location->name));
  24. $placemark_node->addChild("description", htmlspecialchars($location->getDisplayAddress()));
  25. $point_node = $placemark_node->addChild("Point");
  26. $point_node->addChild("coordinates", $location->longitude . "," . $location->latitude);
  27. }
  28. echo $xml->asXML();
  29. }
  30. /**
  31. * @param Customer $customer
  32. */
  33. public function setCustomer(Customer $customer): void {
  34. $this->customer = $customer;
  35. }
  36. }