map_overview_ajax.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var string $action
  4. */
  5. $date_format = "/^2\d{3}-[01]\d-[0-3]\d$/";
  6. JSON::header();
  7. $submit = http::value("submit");
  8. if($submit) {
  9. try {
  10. Core::validateCSRFToken($submit);
  11. switch($action) {
  12. // Gets all locations for a customer
  13. case "get-customer-locations": {
  14. // Input
  15. $customer_id = http::value("customer_id", "int");
  16. $min_date = http::value("min_date");
  17. $max_date = http::value("max_date");
  18. // Get customer
  19. $customer = Customer::get($customer_id);
  20. if(!$customer) throw new RuntimeException("Customer not found.");
  21. // Get locations
  22. $locations = $customer->getLocations(["archived" => 0]);
  23. // Get tests
  24. $nvp = [
  25. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  26. "location_id" => array_column($locations, "id")
  27. ];
  28. if($min_date && $max_date) {
  29. if(preg_match($date_format, $min_date) && preg_match($date_format, $max_date)) {
  30. $min_dts = $min_date;
  31. $max_dts = $max_date;
  32. $nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
  33. }
  34. else {
  35. throw new RuntimeException("Invalid dates submitted.");
  36. }
  37. }
  38. else {
  39. [$min_dts, $max_dts] = $customer->getYearCycleDates();
  40. $nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
  41. }
  42. $tests = Test::query($nvp);
  43. $location_test_array = [];
  44. foreach($tests as $test) {
  45. $exists = isset($location_test_array[$test->location_id]);
  46. if(!$exists) {
  47. $location_test_array[$test->location_id] = $test;
  48. }
  49. else {
  50. /** @var Test $existing */
  51. $existing = $location_test_array[$test->location_id];
  52. $unix_start_existing = ($existing->dts_start === null) ? 0 : strtotime($existing->dts_start . " UTC");
  53. $unix_start_current = ($test->dts_start === null) ? 0 : strtotime($test->dts_start . " UTC");
  54. if($unix_start_current > $unix_start_existing) {
  55. $location_test_array[$test->location_id] = $test;
  56. }
  57. }
  58. }
  59. $tester_ids = array_column($location_test_array, "tester_id");
  60. User::load($tester_ids);
  61. $output = [];
  62. foreach($locations as $location) {
  63. $test = $location_test_array[$location->id] ?? false;
  64. $customer_slug = Util::slug($customer->name, "_");
  65. /** @var User $tester */
  66. $tester = null;
  67. $formatted_start_dts = null;
  68. $formatted_due_dts = null;
  69. $icon = null;
  70. $icon_file = "{$customer_slug}_none.png";
  71. $past_due = false;
  72. if($test) {
  73. $tester = $test->getTester();
  74. if($test->dts_start !== null) {
  75. $unix_start = strtotime($test->dts_start);
  76. $formatted_start_dts = date("d/m/Y H:i", $unix_start);
  77. }
  78. if($test->isPending()) {
  79. $icon_file = "{$customer_slug}_pending.png";
  80. $unix_due = null;
  81. if($test->dts_due !== null) {
  82. $unix_due = strtotime($test->dts_due);
  83. $formatted_due_dts = date("d/m/Y H:i", $unix_due);
  84. }
  85. if($tester && $unix_due && time() > $unix_due) {
  86. $icon_file = "{$customer_slug}_past_due.png";
  87. $past_due = true;
  88. }
  89. }
  90. else if($test->pass) {
  91. $icon_file = "{$customer_slug}_pass.png";
  92. }
  93. else {
  94. $icon_file = "{$customer_slug}_fail.png";
  95. }
  96. }
  97. if($location->do_not_test) {
  98. $icon_file = "do_not_test.png";
  99. }
  100. if($location->closed) {
  101. $icon_file = "closed.png";
  102. }
  103. if(file_exists("public/img/markers/$icon_file")) {
  104. $icon = $icon_file;
  105. }
  106. $data = [
  107. "id" => (int) $location->id,
  108. "internalId" => $location->internal_id,
  109. "name" => $location->name,
  110. "latitude" => (float) $location->latitude,
  111. "longitude" => (float) $location->longitude,
  112. "icon" => $icon,
  113. "test" => null,
  114. "doNotTest" => (bool) $location->do_not_test,
  115. "closed" => (bool) $location->closed,
  116. "archived" => $location->isArchived()
  117. ];
  118. if($test) {
  119. $data["test"] = [
  120. "id" => $test->id,
  121. "formattedStartDts" => $formatted_start_dts,
  122. "formattedDueDts" => $formatted_due_dts,
  123. "tester" => null,
  124. "pastDue" => $past_due,
  125. ];
  126. if($tester) {
  127. $data["test"]["tester"] = [
  128. "id" => $tester->id,
  129. "name" => $tester->getDisplayName(),
  130. ];
  131. }
  132. }
  133. $output[] = $data;
  134. }
  135. JSON::success([
  136. "locations" => $output,
  137. "testPeriod" => sprintf("%s to %s", $min_dts, $max_dts)
  138. ]);
  139. break;
  140. }
  141. // Gets all locations for a project
  142. case "get-project-locations": {
  143. $project_id = http::value("project_id", "int");
  144. $project = Project::get($project_id);
  145. if(!$project) throw new RuntimeException("Project not found.");
  146. $customer = $project->getCustomer();
  147. $tests = $project->getTests();
  148. $locations = Location::load(array_column($tests, "location_id"));
  149. $location_tests = [];
  150. foreach($tests as $test) {
  151. if($test->isCancelled()) continue;
  152. $location_tests[$test->location_id] = $test;
  153. }
  154. $output = [];
  155. foreach($locations as $location) {
  156. $test = $location_tests[$location->id] ?? false;
  157. $test_id = null;
  158. $status = 0;
  159. $status_text = "";
  160. $pass = 0;
  161. $pass_class = "";
  162. $pass_text = "";
  163. $pass_icon = "";
  164. $class = "secondary";
  165. if($test) {
  166. $test_id = $test->id;
  167. $class = $test->getStatusClass();
  168. $status = $test->status;
  169. $status_text = $test->getStatusText();
  170. $pass = $test->pass;
  171. $pass_class = $test->getPassClass();
  172. $pass_text = $test->getPassText();
  173. $pass_icon = $test->getPassIcon();
  174. }
  175. $filename = Util::slug($customer->name, "_");
  176. $icon = null;
  177. if(file_exists("public/img/markers/$filename.png")) {
  178. $icon = "$filename.png";
  179. }
  180. $output[] = (object) [
  181. "id" => (int) $location->id,
  182. "internalId" => $location->internal_id,
  183. "testId" => (int) $test_id,
  184. "name" => $location->name,
  185. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  186. "address1" => $location->address_1,
  187. "status" => $status,
  188. "statusText" => $status_text,
  189. "pass" => (int) $pass,
  190. "passClass" => $pass_class,
  191. "passText" => $pass_text,
  192. "passIcon" => $pass_icon,
  193. "class" => $class,
  194. "latitude" => (float) $location->latitude,
  195. "longitude" => (float) $location->longitude,
  196. "icon" => $icon
  197. ];
  198. }
  199. usort($output, static function(object $a, object $b): int {
  200. return $a->status <=> $b->status;
  201. });
  202. JSON::success([
  203. "locations" => $output
  204. ]);
  205. break;
  206. }
  207. default:
  208. throw new RuntimeException("Invalid request.");
  209. }
  210. }
  211. catch(Exception $e) {
  212. JSON::fail($e->getMessage());
  213. }
  214. }
  215. JSON::fail("Invalid request.");