user_ajax.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // Get projects
  69. $projects = Project::load(array_column($location_test_array, "project_id"));
  70. // Start output
  71. foreach($locations as $location) {
  72. // Filter by distance
  73. $distance = Geolocation::getDistance($address->getGeolocation(), $location->getGeolocation());
  74. if($distance > $max_distance) continue;
  75. // Get test
  76. $test = $location_test_array[$location->id] ?? false;
  77. // Get customer
  78. $customer = $location->getCustomer();
  79. $customer_slug = str_replace("-", "_", Util::slug($customer->name));
  80. // Test
  81. $tester = null;
  82. $formatted_start_dts = null;
  83. $formatted_due_dts = null;
  84. $past_due = false;
  85. // Icon
  86. $icon = null;
  87. $icon_file = "{$customer_slug}_none.png";
  88. // Process test
  89. if($test) {
  90. $tester = $test->getTester();
  91. $project = $test->getProject();
  92. if(!is_null($test->dts_start)) {
  93. $unix_start = strtotime($test->dts_start);
  94. $formatted_start_dts = date("d/m/Y H:i", $unix_start);
  95. }
  96. if(is_null($test->pass)) {
  97. $icon_file = "{$customer_slug}_pending.png";
  98. // Has due date
  99. if(!is_null($test->dts_due)) {
  100. $unix_due = strtotime($test->dts_due);
  101. $formatted_due_dts = date("d/m/Y H:i", $unix_due);
  102. if($tester && time() > $unix_due) {
  103. $icon_file = "{$customer_slug}_past_due.png";
  104. $past_due = true;
  105. }
  106. }
  107. }
  108. else if($test->pass) {
  109. $icon_file = "{$customer_slug}_pass.png";
  110. }
  111. else if(!$test->pass) {
  112. $icon_file = "{$customer_slug}_fail.png";
  113. }
  114. }
  115. if($icon_file && file_exists("public/img/markers/$icon_file")) {
  116. $icon = $icon_file;
  117. }
  118. $data = [
  119. "id" => (int) $location->id,
  120. "internalId" => (int) $location->internal_id,
  121. "name" => $location->name,
  122. "address" => $location->getDisplayAddress(),
  123. "distance" => $distance,
  124. "latitude" => (double) $location->latitude,
  125. "longitude" => (double) $location->longitude,
  126. "icon" => $icon,
  127. "test" => null,
  128. ];
  129. if($test) {
  130. $pass = is_null($test->pass) ? null : intval($test->pass);
  131. $data["test"] = [
  132. "id" => (int) $test->id,
  133. "pass" => $pass,
  134. "formattedStartDts" => $formatted_start_dts,
  135. "formattedDueDts" => $formatted_due_dts,
  136. "tester" => null,
  137. "pastDue" => $past_due,
  138. ];
  139. if($project) {
  140. $data["test"]["project"] = [
  141. "id" => (int) $project->id,
  142. "name" => $project->name,
  143. ];
  144. }
  145. if($tester) {
  146. $data["test"]["tester"] = [
  147. "id" => (int) $tester->id,
  148. "name" => $tester->getDisplayName(),
  149. ];
  150. }
  151. }
  152. $output[] = $data;
  153. }
  154. JSON::success([
  155. "name" => $address->name,
  156. "latitude" => (double) $address->latitude,
  157. "longitude" => (double) $address->longitude,
  158. "locations" => $output
  159. ]);
  160. break;
  161. }