map_overview_ajax.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var string $action
  4. */
  5. use Helpers\LocationHelper;
  6. $date_format = "/^2\d{3}-[01]\d-[0-3]\d$/";
  7. JSON::header();
  8. $submit = http::value("submit");
  9. if($submit) {
  10. try {
  11. Core::validateCSRFToken($submit);
  12. switch($action) {
  13. // Gets all locations for a customer
  14. case "get-customer-locations": {
  15. // Input
  16. $customer_id = http::value("customer_id", "int");
  17. $min_date = http::value("min_date");
  18. $max_date = http::value("max_date");
  19. // Get customer
  20. $customer = Customer::get($customer_id);
  21. if(!$customer) {
  22. throw new RuntimeException("Customer not found.");
  23. }
  24. // Get locations
  25. $locations = $customer->getLocations(["archived" => 0]);
  26. $output = LocationHelper::getMarkerDataForLocations($locations, $min_date, $max_date);
  27. if($min_date && $max_date) {
  28. if(preg_match($date_format, $min_date) && preg_match($date_format, $max_date)) {
  29. $min_dts = $min_date;
  30. $max_dts = $max_date;
  31. }
  32. else {
  33. throw new RuntimeException("Invalid dates submitted.");
  34. }
  35. }
  36. else {
  37. [$min_dts, $max_dts] = $customer->getYearCycleDates();
  38. }
  39. JSON::success([
  40. "locations" => $output,
  41. "testPeriod" => sprintf("%s to %s", $min_dts, $max_dts)
  42. ]);
  43. break;
  44. }
  45. // Gets all locations for a project
  46. case "get-project-locations": {
  47. $project_id = http::value("project_id", "int");
  48. $project = Project::get($project_id);
  49. if(!$project) throw new RuntimeException("Project not found.");
  50. $customer = $project->getCustomer();
  51. $tests = $project->getTests();
  52. $locations = Location::load(array_column($tests, "location_id"));
  53. $location_tests = [];
  54. foreach($tests as $test) {
  55. if($test->isCancelled()) continue;
  56. $location_tests[$test->location_id] = $test;
  57. }
  58. $output = [];
  59. foreach($locations as $location) {
  60. $test = $location_tests[$location->id] ?? false;
  61. $test_id = null;
  62. $status = 0;
  63. $status_text = "";
  64. $pass = 0;
  65. $pass_class = "";
  66. $pass_text = "";
  67. $pass_icon = "";
  68. $class = "secondary";
  69. if($test) {
  70. $test_id = $test->id;
  71. $class = $test->getStatusClass();
  72. $status = $test->status;
  73. $status_text = $test->getStatusText();
  74. $pass = $test->pass;
  75. $pass_class = $test->getPassClass();
  76. $pass_text = $test->getPassText();
  77. $pass_icon = $test->getPassIcon();
  78. }
  79. $filename = Util::slug($customer->name, "_");
  80. $icon = null;
  81. if(file_exists("public/img/markers/$filename.png")) {
  82. $icon = "$filename.png";
  83. }
  84. $output[] = (object) [
  85. "id" => (int) $location->id,
  86. "internalId" => $location->internal_id,
  87. "testId" => (int) $test_id,
  88. "name" => $location->name,
  89. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  90. "address1" => $location->address_1,
  91. "status" => $status,
  92. "statusText" => $status_text,
  93. "pass" => (int) $pass,
  94. "passClass" => $pass_class,
  95. "passText" => $pass_text,
  96. "passIcon" => $pass_icon,
  97. "class" => $class,
  98. "latitude" => (float) $location->latitude,
  99. "longitude" => (float) $location->longitude,
  100. "icon" => $icon
  101. ];
  102. }
  103. usort($output, static function(object $a, object $b): int {
  104. return $a->status <=> $b->status;
  105. });
  106. JSON::success([
  107. "locations" => $output
  108. ]);
  109. break;
  110. }
  111. default:
  112. throw new RuntimeException("Invalid request.");
  113. }
  114. }
  115. catch(Exception $e) {
  116. JSON::fail($e->getMessage());
  117. }
  118. }
  119. JSON::fail("Invalid request.");