map_overview_ajax.php 3.8 KB

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