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", ]; $page_title = "$customer->name Locations"; Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title); $nvp = [ "customer_id" => $customer->id, "archived" => 0, "closed" => 0, ]; $id = http::value("id", "int"); $ids = http::value("ids", "int"); $internal_id = http::value("internal_id", "trim"); $name = http::value("name", "trim"); $legal_entity_name = http::value("legal_entity_name", "trim"); $min_date = http::value("min_date"); $max_date = http::value("max_date"); $include_archived = (int) http::value("include_archived"); $include_closed = (int) http::value("include_closed"); $hide_tested = (int) http::value("hide_tested"); $filter_count = 0; $where_strings = []; if($id) { $where_strings[] = "`location`.`id`='$id'"; $filter_count++; } if($ids) { $where_strings[] = sprintf("`location`.`id` IN (%s)", implode(",", $ids)); } if($internal_id) { $where_strings[] = sprintf("`location`.`internal_id` LIKE '%%%s%%'", DB::escape($internal_id)); $filter_count++; } if($name) { $where_strings[] = sprintf("`location`.`name` LIKE '%%%s%%'", DB::escape($name)); $filter_count++; } if($legal_entity_name) { $where_strings[] = sprintf("`location`.`legal_entity_name` LIKE '%%%s%%'", DB::escape($legal_entity_name)); $filter_count++; } if($include_archived) { $filter_count++; } else { $where_strings[] = "`location`.`archived`='0'"; } if($include_closed) { $filter_count++; } else { $where_strings[] = "`location`.`closed`='0'"; } if($min_date) { $filter_count++; } if($max_date) { $filter_count++; } if($hide_tested) { $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; } $where_string = ""; if($where_strings) { $where_string = "AND " . implode(" AND ", $where_strings); } [$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 `customer_id`='$customer->id' $where_string 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", "include_archived", "include_closed", "hide_tested", "filter_count", "sorts", "sort", "locations")); View::include("modal_add_locations_to_project", compact("customer")); View::includeOnce("footer");