getLocations(); $min_date = http::value("min_date"); $max_date = http::value("max_date"); $nvp = [ "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED), "location_id" => array_column($locations, "id") ]; $date_format = "/^2[0-9]{3}\-[0-1][0-9]\-[0-3][0-9]$/"; if($min_date && preg_match($date_format, $min_date)) { $min_dts = "{$min_date} 00:00:00"; $nvp["dts_start"] = (new Constraint("MORE_THAN_OR_EQUAL"))->setValues($min_dts); } if($max_date && preg_match($date_format, $max_date)) { $max_dts = "{$max_date} 23:59:59"; $nvp["dts_start"] = (new Constraint("LESS_THAN_OR_EQUAL"))->setValues($max_dts); } $tests = Test::query($nvp); $location_test_array = []; foreach($tests as $test) { $exists = isset($location_test_array[$test->location_id]); if(!$exists) { $location_test_array[$test->location_id] = $test; } else { $existing = $location_test_array[$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) { $location_test_array[$test->location_id] = $test; } } } $tester_ids = array_column($location_test_array, "tester_id"); User::load($tester_ids); $output = []; foreach($locations as $location) { $test = $location_test_array[$location->id] ?? false; $customer_slug = str_replace("-", "_", Util::slug($customer->name)); $tester = null; $formatted_start_dts = null; $formatted_due_dts = null; $icon = null; $icon_file = "{$customer_slug}_none.png"; $past_due = false; if($test) { $tester = $test->getTester(); if(!is_null($test->dts_start)) { $unix_start = strtotime($test->dts_start); $formatted_start_dts = date("d/m/Y H:i", $unix_start); } if(is_null($test->pass)) { $icon_file = "{$customer_slug}_pending.png"; if(!is_null($test->dts_due)) { $unix_due = strtotime($test->dts_due); $formatted_due_dts = date("d/m/Y H:i", $unix_due); } if($tester && $test->dts_due && 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($icon_file && file_exists("public/img/markers/$icon_file")) { $icon = $icon_file; } $data = [ "id" => (int) $location->id, "internalId" => $location->internal_id, "name" => $location->name, "latitude" => (float) $location->latitude, "longitude" => (float) $location->longitude, "icon" => $icon, "test" => null, ]; if($test) { $data["test"] = [ "id" => $test->id, "formattedStartDts" => $formatted_start_dts, "formattedDueDts" => $formatted_due_dts, "tester" => null, "pastDue" => $past_due, ]; if($tester) { $data["test"]["tester"] = [ "id" => $tester->id, "name" => $tester->getDisplayName(), ]; } } $output[] = $data; } JSON::success([ "locations" => $output ]); 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 Exception("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->status == Test::STATUS_CANCELLED) 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 = str_replace("-", "_", 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, function($a, $b) { return $a->status <=> $b->status; }); JSON::success([ "locations" => $output ]); break; } default: throw new Exception("Invalid request."); } } catch(Exception $e) { JSON::fail($e->getMessage()); } } JSON::fail("Invalid request."); ?>