user_ajax.php 6.9 KB

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