user_ajax.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. "archived" => 0,
  41. "closed" => 0
  42. ]);
  43. // Get customers
  44. $customers = Customer::load(array_column($locations, "customer_id"));
  45. // Sort locations by distance
  46. usort($locations, function($a, $b) use ($address) {
  47. /**
  48. * @var Location $a
  49. * @var Location $b
  50. */
  51. return
  52. Geolocation::getDistance($address->getGeolocation(), $a->getGeolocation())
  53. <=>
  54. Geolocation::getDistance($address->getGeolocation(), $b->getGeolocation());
  55. });
  56. // Get tests
  57. $test_nvp = [
  58. "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
  59. "location_id" => array_column($locations, "id")
  60. ];
  61. if($pending_tests) {
  62. $test_nvp["status"] = [Test::STATUS_UNASSIGNED, Test::STATUS_ASSIGNED];
  63. }
  64. else {
  65. $test_nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($date_start, $date_end);
  66. }
  67. $tests = Test::query($test_nvp);
  68. // Get projects
  69. $projects = Project::load(array_column($tests, "project_id"));
  70. $location_test_array = [];
  71. foreach($tests as $test) {
  72. $exists = isset($location_test_array[$test->location_id]);
  73. if(!$exists) {
  74. $location_test_array[$test->location_id] = $test;
  75. }
  76. else {
  77. $existing = $location_test_array[$test->location_id];
  78. $unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC");
  79. $unix_start_current = is_null($test->dts_start) ? 0 : strtotime($test->dts_start . " UTC");
  80. if($unix_start_current > $unix_start_existing) {
  81. $location_test_array[$test->location_id] = $test;
  82. }
  83. }
  84. }
  85. // Start output
  86. foreach($locations as $location) {
  87. // Filter by distance
  88. $distance = Geolocation::getDistance($address->getGeolocation(), $location->getGeolocation());
  89. if($distance > $max_distance) continue;
  90. /** @var Test $test */
  91. $test = $location_test_array[$location->id] ?? null;
  92. if($pending_tests) {
  93. if(!$test) continue;
  94. if($test->dts_start) continue;
  95. }
  96. // Get customer
  97. $customer = $location->getCustomer();
  98. $customer_slug = Util::slug($customer->name, "_");
  99. // Test
  100. /** @var User $tester */
  101. $tester = null;
  102. $formatted_start_dts = null;
  103. $formatted_due_dts = null;
  104. $past_due = false;
  105. // Icon
  106. $icon = null;
  107. $icon_file = "{$customer_slug}_none.png";
  108. // Process test
  109. if($test) {
  110. $tester = $test->getTester();
  111. $project = $test->getProject();
  112. if(!is_null($test->dts_start)) {
  113. $unix_start = strtotime($test->dts_start);
  114. $formatted_start_dts = date("d/m/Y H:i", $unix_start);
  115. }
  116. if(is_null($test->pass)) {
  117. $icon_file = "{$customer_slug}_pending.png";
  118. // Has due date
  119. if(!is_null($test->dts_due)) {
  120. $unix_due = strtotime($test->dts_due);
  121. $formatted_due_dts = date("d/m/Y H:i", $unix_due);
  122. if($tester && time() > $unix_due) {
  123. $icon_file = "{$customer_slug}_past_due.png";
  124. $past_due = true;
  125. }
  126. }
  127. }
  128. else if($test->pass) {
  129. $icon_file = "{$customer_slug}_pass.png";
  130. }
  131. else if(!$test->pass) {
  132. $icon_file = "{$customer_slug}_fail.png";
  133. }
  134. }
  135. if($icon_file && file_exists("public/img/markers/$icon_file")) {
  136. $icon = $icon_file;
  137. }
  138. $data = [
  139. "id" => (int) $location->id,
  140. "internalId" => (int) $location->internal_id,
  141. "name" => $location->name,
  142. "address" => $location->getDisplayAddress(),
  143. "distance" => $distance,
  144. "latitude" => (double) $location->latitude,
  145. "longitude" => (double) $location->longitude,
  146. "icon" => $icon,
  147. "customer" => [
  148. "id" => $customer->id,
  149. "name" => $customer->name,
  150. ],
  151. "test" => null,
  152. ];
  153. if($test) {
  154. $pass = is_null($test->pass) ? null : intval($test->pass);
  155. $data["test"] = [
  156. "id" => (int) $test->id,
  157. "pass" => $pass,
  158. "formattedStartDts" => $formatted_start_dts,
  159. "formattedDueDts" => $formatted_due_dts,
  160. "tester" => null,
  161. "pastDue" => $past_due,
  162. ];
  163. if($project) {
  164. $data["test"]["project"] = [
  165. "id" => (int) $project->id,
  166. "name" => $project->name,
  167. ];
  168. }
  169. if($tester) {
  170. $data["test"]["tester"] = [
  171. "id" => (int) $tester->id,
  172. "name" => $tester->getDisplayName(),
  173. ];
  174. }
  175. }
  176. $output[] = $data;
  177. }
  178. JSON::success([
  179. "name" => $address->name,
  180. "latitude" => (double) $address->latitude,
  181. "longitude" => (double) $address->longitude,
  182. "locations" => $output
  183. ]);
  184. break;
  185. }