1
0

map_overview_ajax.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if(is_null($test->pass)) {
  41. $icon_file = "{$customer_slug}_pending.png";
  42. }
  43. else if($test->pass) {
  44. $icon_file = "{$customer_slug}_pass.png";
  45. }
  46. else if(!$test->pass) {
  47. $icon_file = "{$customer_slug}_fail.png";
  48. }
  49. }
  50. if($icon_file && file_exists("public/img/markers/$icon_file")) {
  51. $icon = $icon_file;
  52. }
  53. $data = [
  54. "id" => (int) $location->id,
  55. "internalId" => $location->internal_id,
  56. "name" => $location->name,
  57. "latitude" => (float) $location->latitude,
  58. "longitude" => (float) $location->longitude,
  59. "icon" => $icon
  60. ];
  61. $output[] = $data;
  62. }
  63. JSON::success([
  64. "locations" => $output
  65. ]);
  66. break;
  67. }
  68. // Gets all locations for a project
  69. case "get-project-locations": {
  70. $project_id = http::value("project_id", "int");
  71. $project = Project::get($project_id);
  72. if(!$project) throw new Exception("Project not found.");
  73. $customer = $project->getCustomer();
  74. $tests = $project->getTests();
  75. $locations = Location::load(array_column($tests, "location_id"));
  76. $location_tests = [];
  77. foreach($tests as $test) {
  78. if($test->status == Test::STATUS_CANCELLED) continue;
  79. $location_tests[$test->location_id] = $test;
  80. }
  81. $output = [];
  82. foreach($locations as $location) {
  83. $test = $location_tests[$location->id] ?? false;
  84. $test_id = null;
  85. $status = 0;
  86. $status_text = "";
  87. $pass = 0;
  88. $pass_class = "";
  89. $pass_text = "";
  90. $pass_icon = "";
  91. $class = "secondary";
  92. if($test) {
  93. $test_id = $test->id;
  94. $class = $test->getStatusClass();
  95. $status = $test->status;
  96. $status_text = $test->getStatusText();
  97. $pass = $test->pass;
  98. $pass_class = $test->getPassClass();
  99. $pass_text = $test->getPassText();
  100. $pass_icon = $test->getPassIcon();
  101. }
  102. $filename = str_replace("-", "_", Util::slug($customer->name));
  103. $icon = null;
  104. if(file_exists("public/img/markers/$filename.png")) {
  105. $icon = "$filename.png";
  106. }
  107. $output[] = (object) [
  108. "id" => (int) $location->id,
  109. "internalId" => $location->internal_id,
  110. "testId" => (int) $test_id,
  111. "name" => $location->name,
  112. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  113. "address1" => $location->address_1,
  114. "status" => $status,
  115. "statusText" => $status_text,
  116. "pass" => (int) $pass,
  117. "passClass" => $pass_class,
  118. "passText" => $pass_text,
  119. "passIcon" => $pass_icon,
  120. "class" => $class,
  121. "latitude" => (float) $location->latitude,
  122. "longitude" => (float) $location->longitude,
  123. "icon" => $icon
  124. ];
  125. }
  126. usort($output, function($a, $b) {
  127. return $a->status <=> $b->status;
  128. });
  129. JSON::success([
  130. "locations" => $output
  131. ]);
  132. break;
  133. }
  134. default:
  135. throw new Exception("Invalid request.");
  136. }
  137. }
  138. catch(Exception $e) {
  139. JSON::fail($e->getMessage());
  140. }
  141. }
  142. JSON::fail("Invalid request.");
  143. ?>