user = $user; } public function getAddresses(): void { $output = []; foreach($this->user->getAddresses() as $address) { $output[] = [ "name" => $address->name, "latitude" => (float) $address->latitude, "longitude" => (float) $address->longitude ]; } JSON::success([ "addresses" => $output ]); } /** * @return void */ public function getLocations(): void { $output = []; $address_id = http::value("address_id", "int"); $max_distance = http::value("max_distance", "int"); if($max_distance <= 0) { $max_distance = 50; } $date_start = http::value("date_start", "db"); $date_end = http::value("date_end", "db"); $pending_tests = http::value("pending_tests", "int"); $address = UserAddress::queryOne([ "id" => $address_id, "user_id" => $this->user->id, ]); if(!$address) { throw new RuntimeException("Cannot find address."); } // Get locations according to bounds $bounds = Geolocation::getBounds($address->getGeolocation(), $max_distance); [$min_lat, $min_lng] = $bounds->southWest; [$max_lat, $max_lng] = $bounds->northEast; $locations = Location::query([ "latitude" => (new Constraint("BETWEEN"))->setValues($min_lat, $max_lat), "longitude" => (new Constraint("BETWEEN"))->setValues($min_lng, $max_lng), "archived" => 0, "closed" => 0 ]); // Get customers Customer::load(array_column($locations, "customer_id")); // Sort locations by distance usort($locations, static function(Location $a, Location $b) use ($address) { return Geolocation::getDistance($address->getGeolocation(), $a->getGeolocation()) <=> Geolocation::getDistance($address->getGeolocation(), $b->getGeolocation()); }); // Get tests $test_nvp = [ "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED), "location_id" => array_column($locations, "id") ]; if($pending_tests) { $test_nvp["status"] = [Test::STATUS_UNASSIGNED, Test::STATUS_ASSIGNED]; } else { $test_nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($date_start, $date_end); } $tests = Test::query($test_nvp); // Get projects Project::load(array_column($tests, "project_id")); foreach($tests as $test) { $exists = isset($this->locationTests[$test->location_id]); if(!$exists) { $this->locationTests[$test->location_id] = $test; } else { $existing = $this->locationTests[$test->location_id]; $unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC"); $unix_start_current = is_null($test->dts_start) ? 0 : strtotime($test->dts_start . " UTC"); if($unix_start_current > $unix_start_existing) { $this->locationTests[$test->location_id] = $test; } } } $locations = array_filter($locations, function(Location $location) use ($address, $max_distance, $pending_tests) { // Filter by distance $distance = Geolocation::getDistance($address->getGeolocation(), $location->getGeolocation()); if($distance > $max_distance) { return false; } // Filter by tests that have not started if($pending_tests) { $test = $this->locationTests[$location->id] ?? null; if(!$test) { return false; } if($test->dts_start) { return false; } } return true; }); // Start output foreach($locations as $location) { $output[] = $this->getLocationData($location, $address); } JSON::success([ "name" => $address->name, "latitude" => (double) $address->latitude, "longitude" => (double) $address->longitude, "locations" => $output ]); } public function getAssignedLocations(): void { $pending_tests = $this->user->getPendingTests(); $location_ids = array_column($pending_tests, "location_id"); $locations = Location::query(["id" => $location_ids]); $output = []; foreach($locations as $location) { $output[] = $this->getLocationData($location, null); } JSON::success([ "locations" => $output ]); } private function getLocationData(Location $location, ?UserAddress $address): array { // Get customer $customer = $location->getCustomer(); $customer_slug = Util::slug($customer->name, "_"); // Test /** @var User $tester */ $tester = null; $formatted_start_dts = null; $formatted_due_dts = null; $past_due = false; // Icon $icon = null; $icon_file = "{$customer_slug}.png"; // Distance $distance = null; if($address) { $distance = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation()); } // Process test $test = $this->locationTests[$location->id] ?? null; if($test) { $tester = $test->getTester(); $project = $test->getProject(); if($test->dts_start !== null) { $unix_start = strtotime($test->dts_start); $formatted_start_dts = date("d/m/Y H:i", $unix_start); } if($test->pass === null) { $icon_file = "{$customer_slug}_pending.png"; // Has due date if($test->dts_due !== null) { $unix_due = strtotime($test->dts_due); $formatted_due_dts = date("d/m/Y H:i", $unix_due); if($tester && time() > $unix_due) { $icon_file = "{$customer_slug}_past_due.png"; $past_due = true; } } } else if($test->pass) { $icon_file = "{$customer_slug}_pass.png"; } else if(!$test->pass) { $icon_file = "{$customer_slug}_fail.png"; } } if(file_exists("public/img/markers/$icon_file")) { $icon = $icon_file; } $data = [ "id" => (int) $location->id, "internalId" => (int) $location->internal_id, "name" => $location->name, "address" => $location->getDisplayAddress(), "distance" => $distance, "latitude" => (double) $location->latitude, "longitude" => (double) $location->longitude, "icon" => $icon, "customer" => [ "id" => $customer->id, "name" => $customer->name, ], "test" => null, ]; if($test) { $data["test"] = [ "id" => (int) $test->id, "pass" => $test->pass !== null ? (int) $test->pass : null, "formattedStartDts" => $formatted_start_dts, "formattedDueDts" => $formatted_due_dts, "tester" => null, "pastDue" => $past_due, ]; if($project) { $data["test"]["project"] = [ "id" => (int) $project->id, "name" => $project->name, ]; } if($tester) { $data["test"]["tester"] = [ "id" => (int) $tester->id, "name" => $tester->getDisplayName(), ]; } } return $data; } }; switch($action) { case "get-addresses": $controller->getAddresses(); break; case "get-locations": $controller->getLocations(); break; case "get-assigned-locations": $controller->getAssignedLocations(); break; }