user_ajax.php 5.4 KB

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