map_overview_ajax.php 6.7 KB

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