location_csv_export.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Exports;
  3. class LocationCsvExport {
  4. /** @var \Location[] */
  5. private $locations = [];
  6. public function __construct(array $locations) {
  7. $this->locations = $locations;
  8. }
  9. public function export(string $filename): void {
  10. header("Content-Type: text/csv; charset=utf-8");
  11. header("Content-Disposition: attachment; filename=$filename");
  12. $handle = fopen("php://output", "wb");
  13. fputcsv($handle, [
  14. "ID", "Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
  15. ]);
  16. foreach($this->locations as $location) {
  17. fputcsv($handle, [
  18. $location->id,
  19. $location->internal_id,
  20. $location->city,
  21. $location->legal_entity_name,
  22. $location->region,
  23. $location->area_manager,
  24. $location->area_manager_email,
  25. "",
  26. "",
  27. $location->name,
  28. $location->address_1,
  29. $location->postal_code,
  30. $location->email,
  31. $location->latitude,
  32. $location->longitude,
  33. ]);
  34. }
  35. fclose($handle);
  36. }
  37. }