map_overview_ajax.php 6.2 KB

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