1
0

location_csv_export.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Exports;
  3. use Exception;
  4. use Location;
  5. use Test;
  6. class LocationCsvExport {
  7. /** @var Location[] */
  8. private array $locations;
  9. public function __construct(array $locations) {
  10. $this->locations = $locations;
  11. }
  12. /**
  13. * @param string $filename
  14. * @return void
  15. * @throws Exception
  16. */
  17. public function export(string $filename): void {
  18. header("Content-Type: text/csv; charset=utf-8");
  19. header("Content-Disposition: attachment; filename=$filename");
  20. $handle = fopen("php://output", "wb");
  21. fputcsv($handle, [
  22. "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", "Last Test Date", "Last Gaming Test Result",
  23. ]);
  24. foreach($this->locations as $location) {
  25. $entries = $location->getUpdateLogEntries([], ["limit" => 1]);
  26. $entry = $entries[0] ?? null;
  27. $completed_tests = $location->getCompletedTests();
  28. /** @var Test $completed_test */
  29. $completed_test = array_shift($completed_tests);
  30. $last_test_date = "";
  31. $last_test_result = "";
  32. if($completed_test) {
  33. $last_test_date = $completed_test->getDisplayDate();
  34. if($completed_test->isVoid()) {
  35. $last_test_result = "Void";
  36. }
  37. else {
  38. $last_test_result = $completed_test->isPass() ? "Pass" : "Fail";
  39. }
  40. }
  41. fputcsv($handle, [
  42. $location->id,
  43. $location->internal_id,
  44. $location->city,
  45. $location->legal_entity_name,
  46. $location->brand,
  47. $location->region,
  48. $location->area_manager,
  49. $location->area_manager_email,
  50. "",
  51. "",
  52. $location->name,
  53. $location->address_1,
  54. $location->postal_code,
  55. $location->email,
  56. $location->latitude,
  57. $location->longitude,
  58. $location->archived,
  59. $location->closed,
  60. $location->dts_created,
  61. $entry ? $entry->dts : "",
  62. $entry ? $entry->getUser()->getDisplayName() : "",
  63. $last_test_date,
  64. $last_test_result
  65. ]);
  66. }
  67. fclose($handle);
  68. }
  69. }