map_overview_ajax.php 6.6 KB

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