1
0

user_ajax.php 5.5 KB

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