map_overview_ajax.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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($icon_file && file_exists("public/img/markers/$icon_file")) {
  91. $icon = $icon_file;
  92. }
  93. $data = [
  94. "id" => (int) $location->id,
  95. "internalId" => $location->internal_id,
  96. "name" => $location->name,
  97. "latitude" => (float) $location->latitude,
  98. "longitude" => (float) $location->longitude,
  99. "icon" => $icon,
  100. "test" => null,
  101. "doNotTest" => (bool) $location->do_not_test,
  102. "closed" => (bool) $location->closed,
  103. ];
  104. if($test) {
  105. $data["test"] = [
  106. "id" => $test->id,
  107. "formattedStartDts" => $formatted_start_dts,
  108. "formattedDueDts" => $formatted_due_dts,
  109. "tester" => null,
  110. "pastDue" => $past_due,
  111. ];
  112. if($tester) {
  113. $data["test"]["tester"] = [
  114. "id" => $tester->id,
  115. "name" => $tester->getDisplayName(),
  116. ];
  117. }
  118. }
  119. $output[] = $data;
  120. }
  121. JSON::success([
  122. "locations" => $output,
  123. "testPeriod" => sprintf("%s to %s", $min_dts, $max_dts)
  124. ]);
  125. break;
  126. }
  127. // Gets all locations for a project
  128. case "get-project-locations": {
  129. $project_id = http::value("project_id", "int");
  130. $project = Project::get($project_id);
  131. if(!$project) throw new Exception("Project not found.");
  132. $customer = $project->getCustomer();
  133. $tests = $project->getTests();
  134. $locations = Location::load(array_column($tests, "location_id"));
  135. $location_tests = [];
  136. foreach($tests as $test) {
  137. if($test->status == Test::STATUS_CANCELLED) continue;
  138. $location_tests[$test->location_id] = $test;
  139. }
  140. $output = [];
  141. foreach($locations as $location) {
  142. $test = $location_tests[$location->id] ?? false;
  143. $test_id = null;
  144. $status = 0;
  145. $status_text = "";
  146. $pass = 0;
  147. $pass_class = "";
  148. $pass_text = "";
  149. $pass_icon = "";
  150. $class = "secondary";
  151. if($test) {
  152. $test_id = $test->id;
  153. $class = $test->getStatusClass();
  154. $status = $test->status;
  155. $status_text = $test->getStatusText();
  156. $pass = $test->pass;
  157. $pass_class = $test->getPassClass();
  158. $pass_text = $test->getPassText();
  159. $pass_icon = $test->getPassIcon();
  160. }
  161. $filename = str_replace("-", "_", Util::slug($customer->name));
  162. $icon = null;
  163. if(file_exists("public/img/markers/$filename.png")) {
  164. $icon = "$filename.png";
  165. }
  166. $output[] = (object) [
  167. "id" => (int) $location->id,
  168. "internalId" => $location->internal_id,
  169. "testId" => (int) $test_id,
  170. "name" => $location->name,
  171. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  172. "address1" => $location->address_1,
  173. "status" => $status,
  174. "statusText" => $status_text,
  175. "pass" => (int) $pass,
  176. "passClass" => $pass_class,
  177. "passText" => $pass_text,
  178. "passIcon" => $pass_icon,
  179. "class" => $class,
  180. "latitude" => (float) $location->latitude,
  181. "longitude" => (float) $location->longitude,
  182. "icon" => $icon
  183. ];
  184. }
  185. usort($output, function($a, $b) {
  186. return $a->status <=> $b->status;
  187. });
  188. JSON::success([
  189. "locations" => $output
  190. ]);
  191. break;
  192. }
  193. default:
  194. throw new Exception("Invalid request.");
  195. }
  196. }
  197. catch(Exception $e) {
  198. JSON::fail($e->getMessage());
  199. }
  200. }
  201. JSON::fail("Invalid request.");