| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- /**
- * @var string $action
- */
- use Helpers\LocationHelper;
- $date_format = "/^2\d{3}-[01]\d-[0-3]\d$/";
- JSON::header();
- $submit = http::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- switch($action) {
- // Gets all locations for a customer
- case "get-customer-locations": {
- // Input
- $customer_id = http::value("customer_id", "int");
- $min_date = http::value("min_date");
- $max_date = http::value("max_date");
- // Get customer
- $customer = Customer::get($customer_id);
- if(!$customer) {
- throw new RuntimeException("Customer not found.");
- }
- // Get locations
- $locations = $customer->getLocations(["archived" => 0]);
- $output = LocationHelper::getMarkerDataForLocations($locations, $min_date, $max_date);
- if($min_date && $max_date) {
- if(preg_match($date_format, $min_date) && preg_match($date_format, $max_date)) {
- $min_dts = $min_date;
- $max_dts = $max_date;
- }
- else {
- throw new RuntimeException("Invalid dates submitted.");
- }
- }
- else {
- [$min_dts, $max_dts] = $customer->getYearCycleDates();
- }
- JSON::success([
- "locations" => $output,
- "testPeriod" => sprintf("%s to %s", $min_dts, $max_dts)
- ]);
- break;
- }
- // Gets all locations for a project
- case "get-project-locations": {
- $project_id = http::value("project_id", "int");
- $project = Project::get($project_id);
- if(!$project) throw new RuntimeException("Project not found.");
- $customer = $project->getCustomer();
- $tests = $project->getTests();
- $locations = Location::load(array_column($tests, "location_id"));
- $location_tests = [];
- foreach($tests as $test) {
- if($test->isCancelled()) continue;
- $location_tests[$test->location_id] = $test;
- }
- $output = [];
- foreach($locations as $location) {
- $test = $location_tests[$location->id] ?? false;
- $test_id = null;
- $status = 0;
- $status_text = "";
- $pass = 0;
- $pass_class = "";
- $pass_text = "";
- $pass_icon = "";
- $class = "secondary";
- if($test) {
- $test_id = $test->id;
- $class = $test->getStatusClass();
- $status = $test->status;
- $status_text = $test->getStatusText();
- $pass = $test->pass;
- $pass_class = $test->getPassClass();
- $pass_text = $test->getPassText();
- $pass_icon = $test->getPassIcon();
- }
- $filename = Util::slug($customer->name, "_");
- $icon = null;
- if(file_exists("public/img/markers/$filename.png")) {
- $icon = "$filename.png";
- }
- $output[] = (object) [
- "id" => (int) $location->id,
- "internalId" => $location->internal_id,
- "testId" => (int) $test_id,
- "name" => $location->name,
- "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
- "address1" => $location->address_1,
- "status" => $status,
- "statusText" => $status_text,
- "pass" => (int) $pass,
- "passClass" => $pass_class,
- "passText" => $pass_text,
- "passIcon" => $pass_icon,
- "class" => $class,
- "latitude" => (float) $location->latitude,
- "longitude" => (float) $location->longitude,
- "icon" => $icon
- ];
- }
- usort($output, static function(object $a, object $b): int {
- return $a->status <=> $b->status;
- });
- JSON::success([
- "locations" => $output
- ]);
- break;
- }
- default:
- throw new RuntimeException("Invalid request.");
- }
- }
- catch(Exception $e) {
- JSON::fail($e->getMessage());
- }
- }
- JSON::fail("Invalid request.");
|