name); $sorts = [ "name" => "Name", "internal-id" => "Internal ID", "last-added" => "Most recently added first", "test-count" => "Highest test count first", "retest-count" => "Highest retest count first", ]; $filter_options = [ "" => "No Filter", "untested-current" => "Untested Only (Current Cycle)", "failed-current" => "Failed Only (Current Cycle)", "untested-all" => "Untested Only (All Time)", "failed-all" => "Failed Only (All Time)", ]; $page_title = "$customer->name Locations"; Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title); $nvp = [ "customer_id" => $customer->id, "archived" => 0, "closed" => 0, ]; $search = new LocationSearch($customer); $id = http::value("id", "int"); $search->setId($id); $ids = http::value("ids", "int"); $search->setIds($ids); $internal_id = http::value("internal_id", "trim"); $search->setInternalId($internal_id); $name = http::value("name", "trim"); $search->setName($name); $legal_entity_name = http::value("legal_entity_name", "trim"); $search->setLegalEntityName($legal_entity_name); $min_date = http::value("min_date"); $search->setMinTestDate($min_date); $max_date = http::value("max_date"); $search->setMaxTestDate($max_date); $include_archived = (int) http::value("include_archived"); $search->setIncludeArchived($include_archived); $include_closed = (int) http::value("include_closed"); $search->setIncludeClosed($include_closed); $filter = http::value("filter"); $search->setFilter($filter); $filter_count = 0; $where_strings = []; if($id) { $filter_count++; } if($internal_id) { $filter_count++; } if($name) { $filter_count++; } if($legal_entity_name) { $filter_count++; } if($include_archived) { $filter_count++; } if($include_closed) { $filter_count++; } if($min_date) { $filter_count++; } if($max_date) { $filter_count++; } if($filter) { $filter_count++; } $sort = http::value("sort"); switch($sort) { case "last-added": $query_sort = "id"; $order = "desc"; break; case "test-count": $query_sort = "test_count"; $order = "desc"; break; case "retest-count": $query_sort = "retest_count"; $order = "desc"; break; default: $query_sort = "name"; $order = "asc"; break; } $locations = $search->run(); $location_ids = array_column($locations, "id") ?: [0]; $location_ids_csv = implode(", ", $location_ids); [$date_start, $date_end] = $customer->getYearCycleDates(); $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count` FROM `location` LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end' WHERE `location`.`id` IN ($location_ids_csv) GROUP BY `location`.`id` ORDER BY $query_sort $order "; $locations = Location::findBySql($sql); if($sort === "internal-id") { usort($locations, static function(Location $a, Location $b) { return strnatcasecmp($a->internal_id, $b->internal_id); }); } $action = http::value("action"); if(!empty($action)) { switch($action) { case "export": if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource."); $export = new LocationCsvExport($locations); $export->export("$customer_slug-export.csv"); exit(); case "export-kml": $export = new LocationKmlExport($locations); $export->setCustomer($customer); $export->export("$customer_slug-export.kml"); exit; } } View::includeOnce("header", compact("page_title")); View::include("locations", compact("customer", "search", "filter_count", "sorts", "sort", "filter_options", "locations")); View::include("modal_add_locations_to_project", compact("customer")); View::includeOnce("footer");