user_ajax.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. JSON::header();
  3. $user = User::get($id);
  4. if(!$user) JSON::fail("User not found.");
  5. switch($action) {
  6. case "get-addresses":
  7. $addresses = $user->getAddresses();
  8. $output = [];
  9. foreach($addresses as $address) {
  10. $output[] = [
  11. "name" => $address->name,
  12. "latitude" => (float) $address->latitude,
  13. "longitude" => (float) $address->longitude
  14. ];
  15. }
  16. JSON::success([
  17. "addresses" => $output
  18. ]);
  19. break;
  20. case "get-locations":
  21. $output = [];
  22. $address_id = http::value("address_id", "int");
  23. $max_distance = http::value("max_distance", "int");
  24. if($max_distance <= 0) $max_distance = 50;
  25. $date_start = http::value("date_start", "db");
  26. $date_end = http::value("date_end", "db");
  27. $pending_tests = http::value("pending_tests", "int");
  28. $address = UserAddress::queryOne([
  29. "id" => $address_id,
  30. "user_id" => $id,
  31. ]);
  32. if(!$address) throw new Exception("Cannot find address.");
  33. // Get locations according to bounds
  34. $bounds = Geolocation::getBounds($address->getGeolocation(), $max_distance);
  35. list($min_lat, $min_lng) = $bounds->southWest;
  36. list($max_lat, $max_lng) = $bounds->northEast;
  37. $locations = Location::query([
  38. "latitude" => (new Constraint("BETWEEN"))->setValues($min_lat, $max_lat),
  39. "longitude" => (new Constraint("BETWEEN"))->setValues($min_lng, $max_lng),
  40. "closed" => 0
  41. ]);
  42. // Get customers
  43. $customers = Customer::load(array_column($locations, "customer_id"));
  44. // Sort locations by distance
  45. usort($locations, function($a, $b) use ($address) {
  46. /**
  47. * @var Location $a
  48. * @var Location $b
  49. */
  50. return
  51. Geolocation::getDistance($address->getGeolocation(), $a->getGeolocation())
  52. <=>
  53. Geolocation::getDistance($address->getGeolocation(), $b->getGeolocation());
  54. });
  55. // Get tests
  56. $test_nvp = [
  57. "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
  58. "location_id" => array_column($locations, "id")
  59. ];
  60. if($pending_tests) {
  61. $test_nvp["status"] = [Test::STATUS_UNASSIGNED, Test::STATUS_ASSIGNED];
  62. }
  63. else {
  64. $test_nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($date_start, $date_end);
  65. }
  66. $tests = Test::query($test_nvp);
  67. // Get projects
  68. $projects = Project::load(array_column($tests, "project_id"));
  69. $location_test_array = [];
  70. foreach($tests as $test) {
  71. $exists = isset($location_test_array[$test->location_id]);
  72. if(!$exists) {
  73. $location_test_array[$test->location_id] = $test;
  74. }
  75. else {
  76. $existing = $location_test_array[$test->location_id];
  77. $unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC");
  78. $unix_start_current = is_null($test->dts_start) ? 0 : strtotime($test->dts_start . " UTC");
  79. if($unix_start_current > $unix_start_existing) {
  80. $location_test_array[$test->location_id] = $test;
  81. }
  82. }
  83. }
  84. // Start output
  85. foreach($locations as $location) {
  86. // Filter by distance
  87. $distance = Geolocation::getDistance($address->getGeolocation(), $location->getGeolocation());
  88. if($distance > $max_distance) continue;
  89. /** @var Test $test */
  90. $test = $location_test_array[$location->id] ?? null;
  91. if($pending_tests) {
  92. if(!$test) continue;
  93. if($test->dts_start) continue;
  94. }
  95. // Get customer
  96. $customer = $location->getCustomer();
  97. $customer_slug = str_replace("-", "_", Util::slug($customer->name));
  98. // Test
  99. /** @var User $tester */
  100. $tester = null;
  101. $formatted_start_dts = null;
  102. $formatted_due_dts = null;
  103. $past_due = false;
  104. // Icon
  105. $icon = null;
  106. $icon_file = "{$customer_slug}_none.png";
  107. // Process test
  108. if($test) {
  109. $tester = $test->getTester();
  110. $project = $test->getProject();
  111. if(!is_null($test->dts_start)) {
  112. $unix_start = strtotime($test->dts_start);
  113. $formatted_start_dts = date("d/m/Y H:i", $unix_start);
  114. }
  115. if(is_null($test->pass)) {
  116. $icon_file = "{$customer_slug}_pending.png";
  117. // Has due date
  118. if(!is_null($test->dts_due)) {
  119. $unix_due = strtotime($test->dts_due);
  120. $formatted_due_dts = date("d/m/Y H:i", $unix_due);
  121. if($tester && time() > $unix_due) {
  122. $icon_file = "{$customer_slug}_past_due.png";
  123. $past_due = true;
  124. }
  125. }
  126. }
  127. else if($test->pass) {
  128. $icon_file = "{$customer_slug}_pass.png";
  129. }
  130. else if(!$test->pass) {
  131. $icon_file = "{$customer_slug}_fail.png";
  132. }
  133. }
  134. if($icon_file && file_exists("public/img/markers/$icon_file")) {
  135. $icon = $icon_file;
  136. }
  137. $data = [
  138. "id" => (int) $location->id,
  139. "internalId" => (int) $location->internal_id,
  140. "name" => $location->name,
  141. "address" => $location->getDisplayAddress(),
  142. "distance" => $distance,
  143. "latitude" => (double) $location->latitude,
  144. "longitude" => (double) $location->longitude,
  145. "icon" => $icon,
  146. "customer" => [
  147. "id" => $customer->id,
  148. "name" => $customer->name,
  149. ],
  150. "test" => null,
  151. ];
  152. if($test) {
  153. $pass = is_null($test->pass) ? null : intval($test->pass);
  154. $data["test"] = [
  155. "id" => (int) $test->id,
  156. "pass" => $pass,
  157. "formattedStartDts" => $formatted_start_dts,
  158. "formattedDueDts" => $formatted_due_dts,
  159. "tester" => null,
  160. "pastDue" => $past_due,
  161. ];
  162. if($project) {
  163. $data["test"]["project"] = [
  164. "id" => (int) $project->id,
  165. "name" => $project->name,
  166. ];
  167. }
  168. if($tester) {
  169. $data["test"]["tester"] = [
  170. "id" => (int) $tester->id,
  171. "name" => $tester->getDisplayName(),
  172. ];
  173. }
  174. }
  175. $output[] = $data;
  176. }
  177. JSON::success([
  178. "name" => $address->name,
  179. "latitude" => (double) $address->latitude,
  180. "longitude" => (double) $address->longitude,
  181. "locations" => $output
  182. ]);
  183. break;
  184. }