map_overview_ajax.php 5.6 KB

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