map_overview_ajax.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $tester_ids = array_column($location_test_array, "tester_id");
  34. User::load($tester_ids);
  35. $output = [];
  36. foreach($locations as $location) {
  37. $test = $location_test_array[$location->id] ?? false;
  38. $customer_slug = str_replace("-", "_", Util::slug($customer->name));
  39. $tester = null;
  40. $formatted_due_dts = null;
  41. $icon = null;
  42. $icon_file = "{$customer_slug}_none.png";
  43. $past_due = false;
  44. if($test) {
  45. $tester = $test->getTester();
  46. $unix_due = strtotime($test->dts_due);
  47. $formatted_due_dts = date("d/m/Y H:i", $unix_due);
  48. if(is_null($test->pass)) {
  49. $icon_file = "{$customer_slug}_pending.png";
  50. if($tester && $test->dts_due && time() > $unix_due) {
  51. $icon_file = "{$customer_slug}_past_due.png";
  52. $past_due = true;
  53. }
  54. }
  55. else if($test->pass) {
  56. $icon_file = "{$customer_slug}_pass.png";
  57. }
  58. else if(!$test->pass) {
  59. $icon_file = "{$customer_slug}_fail.png";
  60. }
  61. }
  62. if($icon_file && file_exists("public/img/markers/$icon_file")) {
  63. $icon = $icon_file;
  64. }
  65. $data = [
  66. "id" => (int) $location->id,
  67. "internalId" => $location->internal_id,
  68. "name" => $location->name,
  69. "latitude" => (float) $location->latitude,
  70. "longitude" => (float) $location->longitude,
  71. "icon" => $icon,
  72. "test" => null,
  73. ];
  74. if($test) {
  75. $data["test"] = [
  76. "id" => $test->id,
  77. "formattedDueDts" => $formatted_due_dts,
  78. "tester" => null,
  79. "pastDue" => $past_due,
  80. ];
  81. if($tester) {
  82. $data["test"]["tester"] = [
  83. "id" => $tester->id,
  84. "name" => $tester->getDisplayName(),
  85. ];
  86. }
  87. }
  88. $output[] = $data;
  89. }
  90. JSON::success([
  91. "locations" => $output
  92. ]);
  93. break;
  94. }
  95. // Gets all locations for a project
  96. case "get-project-locations": {
  97. $project_id = http::value("project_id", "int");
  98. $project = Project::get($project_id);
  99. if(!$project) throw new Exception("Project not found.");
  100. $customer = $project->getCustomer();
  101. $tests = $project->getTests();
  102. $locations = Location::load(array_column($tests, "location_id"));
  103. $location_tests = [];
  104. foreach($tests as $test) {
  105. if($test->status == Test::STATUS_CANCELLED) continue;
  106. $location_tests[$test->location_id] = $test;
  107. }
  108. $output = [];
  109. foreach($locations as $location) {
  110. $test = $location_tests[$location->id] ?? false;
  111. $test_id = null;
  112. $status = 0;
  113. $status_text = "";
  114. $pass = 0;
  115. $pass_class = "";
  116. $pass_text = "";
  117. $pass_icon = "";
  118. $class = "secondary";
  119. if($test) {
  120. $test_id = $test->id;
  121. $class = $test->getStatusClass();
  122. $status = $test->status;
  123. $status_text = $test->getStatusText();
  124. $pass = $test->pass;
  125. $pass_class = $test->getPassClass();
  126. $pass_text = $test->getPassText();
  127. $pass_icon = $test->getPassIcon();
  128. }
  129. $filename = str_replace("-", "_", Util::slug($customer->name));
  130. $icon = null;
  131. if(file_exists("public/img/markers/$filename.png")) {
  132. $icon = "$filename.png";
  133. }
  134. $output[] = (object) [
  135. "id" => (int) $location->id,
  136. "internalId" => $location->internal_id,
  137. "testId" => (int) $test_id,
  138. "name" => $location->name,
  139. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  140. "address1" => $location->address_1,
  141. "status" => $status,
  142. "statusText" => $status_text,
  143. "pass" => (int) $pass,
  144. "passClass" => $pass_class,
  145. "passText" => $pass_text,
  146. "passIcon" => $pass_icon,
  147. "class" => $class,
  148. "latitude" => (float) $location->latitude,
  149. "longitude" => (float) $location->longitude,
  150. "icon" => $icon
  151. ];
  152. }
  153. usort($output, function($a, $b) {
  154. return $a->status <=> $b->status;
  155. });
  156. JSON::success([
  157. "locations" => $output
  158. ]);
  159. break;
  160. }
  161. default:
  162. throw new Exception("Invalid request.");
  163. }
  164. }
  165. catch(Exception $e) {
  166. JSON::fail($e->getMessage());
  167. }
  168. }
  169. JSON::fail("Invalid request.");
  170. ?>