map_overview_ajax.php 6.1 KB

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