user_ajax.php 4.7 KB

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