map_overview_ajax.php 7.2 KB

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