location_csv_export.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Exports;
  3. use Exception;
  4. use Location;
  5. class LocationCsvExport {
  6. /** @var Location[] */
  7. private array $locations;
  8. public function __construct(array $locations) {
  9. $this->locations = $locations;
  10. }
  11. /**
  12. * @param string $filename
  13. * @return void
  14. * @throws Exception
  15. */
  16. public function export(string $filename): void {
  17. header("Content-Type: text/csv; charset=utf-8");
  18. header("Content-Disposition: attachment; filename=$filename");
  19. $handle = fopen("php://output", "wb");
  20. fputcsv($handle, [
  21. "ID", "Internal ID", "City", "Legal Entity", "Brand", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude", "Archived", "Closed", "Created", "Updated", "Updated By"
  22. ]);
  23. foreach($this->locations as $location) {
  24. $entries = $location->getUpdateLogEntries([], ["limit" => 1]);
  25. $entry = $entries[0] ?? null;
  26. fputcsv($handle, [
  27. $location->id,
  28. $location->internal_id,
  29. $location->city,
  30. $location->legal_entity_name,
  31. $location->brand,
  32. $location->region,
  33. $location->area_manager,
  34. $location->area_manager_email,
  35. "",
  36. "",
  37. $location->name,
  38. $location->address_1,
  39. $location->postal_code,
  40. $location->email,
  41. $location->latitude,
  42. $location->longitude,
  43. $location->archived,
  44. $location->closed,
  45. $location->dts_created,
  46. $entry ? $entry->dts : "",
  47. $entry ? $entry->getUser()->getDisplayName() : "",
  48. ]);
  49. }
  50. fclose($handle);
  51. }
  52. }