map_overview_ajax.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. JSON::header();
  3. $submit = http::value("submit");
  4. if($submit) {
  5. try {
  6. Core::validateCSRFToken($submit);
  7. switch($action) {
  8. // Gets all locations for a customer
  9. case "get-customer-locations": {
  10. $customer_id = http::value("customer_id", "int");
  11. $customer = Customer::get($customer_id);
  12. if(!$customer) throw new Exception("Customer not found.");
  13. $locations = $customer->getLocations();
  14. $tests = Test::query([
  15. "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
  16. "location_id" => array_column($locations, "id")
  17. ]);
  18. $location_test_array = [];
  19. foreach($tests as $test) {
  20. $exists = isset($location_test_array[$test->location_id]);
  21. if(!$exists) {
  22. $location_test_array[$test->location_id] = $test;
  23. }
  24. else {
  25. $existing = $location_test_array[$test->location_id];
  26. $unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC");
  27. $unix_start_current = is_null($test->dts_start) ? 0 : strtotime($test->dts_start . " UTC");
  28. if($unix_start_current > $unix_start_existing) {
  29. $location_test_array[$test->location_id] = $test;
  30. }
  31. }
  32. }
  33. $output = [];
  34. foreach($locations as $location) {
  35. $test = $location_test_array[$location->id] ?? false;
  36. $customer_slug = str_replace("-", "_", Util::slug($customer->name));
  37. $icon = null;
  38. $icon_file = "{$customer_slug}_none.png";
  39. if($test) {
  40. $unix_due = Util::getUnixTimeFromUTC($test->dts_due);
  41. if(is_null($test->pass)) {
  42. $icon_file = "{$customer_slug}_pending.png";
  43. if(time() > $unix_due) {
  44. $icon_file = "{$customer_slug}_past_due.png";
  45. }
  46. }
  47. else if($test->pass) {
  48. $icon_file = "{$customer_slug}_pass.png";
  49. }
  50. else if(!$test->pass) {
  51. $icon_file = "{$customer_slug}_fail.png";
  52. }
  53. }
  54. if($icon_file && file_exists("public/img/markers/$icon_file")) {
  55. $icon = $icon_file;
  56. }
  57. $data = [
  58. "id" => (int) $location->id,
  59. "internalId" => $location->internal_id,
  60. "name" => $location->name,
  61. "latitude" => (float) $location->latitude,
  62. "longitude" => (float) $location->longitude,
  63. "icon" => $icon
  64. ];
  65. $output[] = $data;
  66. }
  67. JSON::success([
  68. "locations" => $output
  69. ]);
  70. break;
  71. }
  72. // Gets all locations for a project
  73. case "get-project-locations": {
  74. $project_id = http::value("project_id", "int");
  75. $project = Project::get($project_id);
  76. if(!$project) throw new Exception("Project not found.");
  77. $customer = $project->getCustomer();
  78. $tests = $project->getTests();
  79. $locations = Location::load(array_column($tests, "location_id"));
  80. $location_tests = [];
  81. foreach($tests as $test) {
  82. if($test->status == Test::STATUS_CANCELLED) continue;
  83. $location_tests[$test->location_id] = $test;
  84. }
  85. $output = [];
  86. foreach($locations as $location) {
  87. $test = $location_tests[$location->id] ?? false;
  88. $test_id = null;
  89. $status = 0;
  90. $status_text = "";
  91. $pass = 0;
  92. $pass_class = "";
  93. $pass_text = "";
  94. $pass_icon = "";
  95. $class = "secondary";
  96. if($test) {
  97. $test_id = $test->id;
  98. $class = $test->getStatusClass();
  99. $status = $test->status;
  100. $status_text = $test->getStatusText();
  101. $pass = $test->pass;
  102. $pass_class = $test->getPassClass();
  103. $pass_text = $test->getPassText();
  104. $pass_icon = $test->getPassIcon();
  105. }
  106. $filename = str_replace("-", "_", Util::slug($customer->name));
  107. $icon = null;
  108. if(file_exists("public/img/markers/$filename.png")) {
  109. $icon = "$filename.png";
  110. }
  111. $output[] = (object) [
  112. "id" => (int) $location->id,
  113. "internalId" => $location->internal_id,
  114. "testId" => (int) $test_id,
  115. "name" => $location->name,
  116. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  117. "address1" => $location->address_1,
  118. "status" => $status,
  119. "statusText" => $status_text,
  120. "pass" => (int) $pass,
  121. "passClass" => $pass_class,
  122. "passText" => $pass_text,
  123. "passIcon" => $pass_icon,
  124. "class" => $class,
  125. "latitude" => (float) $location->latitude,
  126. "longitude" => (float) $location->longitude,
  127. "icon" => $icon
  128. ];
  129. }
  130. usort($output, function($a, $b) {
  131. return $a->status <=> $b->status;
  132. });
  133. JSON::success([
  134. "locations" => $output
  135. ]);
  136. break;
  137. }
  138. default:
  139. throw new Exception("Invalid request.");
  140. }
  141. }
  142. catch(Exception $e) {
  143. JSON::fail($e->getMessage());
  144. }
  145. }
  146. JSON::fail("Invalid request.");
  147. ?>