map_overview_ajax.php 3.8 KB

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