= 2; $can_delete = lookupUserPermission('module_support') >= 3; // Archived - from All Client Docs, an archived client's racks count as archived too $archived = (isset($_GET['archived']) && $_GET['archived'] == 1) ? 1 : 0; if ($client_url) { $archive_query = $archived ? "rack_archived_at IS NOT NULL" : "rack_archived_at IS NULL"; } else { $archive_query = $archived ? "(rack_archived_at IS NOT NULL OR client_archived_at IS NOT NULL)" : "(rack_archived_at IS NULL AND client_archived_at IS NULL)"; } // Client filter (All Client Docs only) $client_filter = 0; if (!$client_url && !empty($_GET['client'])) { $client_filter = intval($_GET['client']); $client_query = "AND rack_client_id = $client_filter"; } // Location filter (one client only) $location_filter = 0; $location_query = ''; if ($client_url && !empty($_GET['location'])) { $location_filter = intval($_GET['location']); $location_query = "AND rack_location_id = $location_filter"; } // Type filter - the rack types (Admin > Categories), held by name $type_filter = trim(strip_tags((string) ($_GET['type'] ?? ''))); $type_query = $type_filter !== '' ? "AND rack_type = '" . escapeSql($type_filter) . "'" : ''; // Only these sort - filter_header.php will hand through any lowercase word if (!in_array($sort, ['rack_name', 'rack_units', 'client_name', 'location_name', 'rack_created_at'], true)) { $sort = 'rack_name'; } $sql = mysqli_query( $mysqli, "SELECT SQL_CALC_FOUND_ROWS client_id, client_name, location_name, rack_id, rack_client_id, rack_name, rack_type, rack_model, rack_units, rack_units_descending, rack_power_capacity, rack_physical_location, rack_archived_at FROM racks LEFT JOIN clients ON client_id = rack_client_id LEFT JOIN locations ON location_id = rack_location_id WHERE $archive_query AND (rack_name LIKE '%$q%' OR rack_type LIKE '%$q%' OR rack_model LIKE '%$q%' OR rack_description LIKE '%$q%' OR rack_physical_location LIKE '%$q%' OR location_name LIKE '%$q%' OR client_name LIKE '%$q%' OR EXISTS (SELECT 1 FROM rack_units LEFT JOIN assets ON asset_id = unit_asset_id WHERE unit_rack_id = rack_id AND (unit_device LIKE '%$q%' OR asset_name LIKE '%$q%' OR asset_serial LIKE '%$q%'))) " . clientScopeSql('rack_client_id') . " $client_query $location_query $type_query ORDER BY $sort $order LIMIT $record_from, $record_to" ); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); $racks = []; while ($row = mysqli_fetch_assoc($sql)) { $racks[intval($row['rack_id'])] = $row; } // Every item on the page in one query, not one per rack $racks_units = rackLoadUnits(array_keys($racks)); // Totals across what's listed, for the summary line $total_units = 0; $total_used = 0; $total_items = 0; foreach ($racks as $rack_id => $row) { $racks[$rack_id]['occupancy'] = rackOccupancy($racks_units[$rack_id], max(1, intval($row['rack_units']))); $total_units += max(1, intval($row['rack_units'])); $total_used += $racks[$rack_id]['occupancy']['any']; $total_items += count($racks_units[$rack_id]); } ?>