= 2; $can_delete = lookupUserPermission('module_support') >= 3; //Asset Type from GET $asset_type_tabs = [ 'workstation' => ['label' => 'Workstations', 'icon' => 'fa-desktop', 'query' => "asset_type IN ('Desktop', 'Laptop')"], 'server' => ['label' => 'Servers', 'icon' => 'fa-server', 'query' => "asset_type = 'Server'"], 'virtual' => ['label' => 'Virtual', 'icon' => 'fa-cloud', 'query' => "asset_type = 'Virtual Machine'"], 'network' => ['label' => 'Network', 'icon' => 'fa-network-wired', 'query' => "asset_type IN ('Firewall/Router', 'Switch', 'Access Point')"], 'printer' => ['label' => 'Printers', 'icon' => 'fa-print', 'query' => "asset_type = 'Printer'"], 'other' => ['label' => 'Other', 'icon' => 'fa-tag', 'query' => "asset_type NOT IN ('Desktop', 'Laptop', 'Server', 'Virtual Machine', 'Firewall/Router', 'Switch', 'Access Point', 'Printer')"], ]; $type_filter = (isset($_GET['type']) && isset($asset_type_tabs[$_GET['type']])) ? $_GET['type'] : ''; $_GET['type'] = $type_filter; $type_query = $type_filter ? $asset_type_tabs[$type_filter]['query'] : "1 = 1"; $client = ''; if (!$client_url) { // Client Filter if (!empty($_GET['client'])) { $client = intval($_GET['client']); $client_query = "AND asset_client_id = $client"; } } // Location Filter if ($client_url && !empty($_GET['location'])) { $location_filter = intval($_GET['location']); $location_query = "AND asset_location_id = $location_filter"; } else { // Default - any $location_query = ''; $location_filter = 0; } // Status Filter - the asset statuses (Admin > Categories), held by name $status_filter = trim(strip_tags((string) ($_GET['status'] ?? ''))); $status_query = $status_filter !== '' ? "AND asset_status = '" . escapeSql($status_filter) . "'" : ''; // Tags Filter if (isset($_GET['tags']) && is_array($_GET['tags']) && !empty($_GET['tags'])) { $tag_filter = implode(",", array_map('intval', $_GET['tags'])); $tag_query = "AND asset_id IN (SELECT asset_tag_asset_id FROM asset_tags WHERE asset_tag_tag_id IN ($tag_filter))"; } else { $tag_filter = 0; $tag_query = ''; } // Expiring In Filter if (!empty($_GET['expire_days'])) { if ($_GET['expire_days'] == "expired") { $expire_days = "expired"; $expire_query = "AND (asset_warranty_expire IS NOT NULL AND asset_warranty_expire != '0000-00-00' AND asset_warranty_expire < CURDATE())"; } else { $expire_days = intval($_GET['expire_days']); $expire_query = "AND (asset_warranty_expire IS NOT NULL AND asset_warranty_expire != '0000-00-00' AND asset_warranty_expire BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL $expire_days DAY))"; } } else { // Default - any $expire_days = ''; $expire_query = ''; } // Extra columns, picked from the filter bar $extra_column_options = [ 'Mac_Address' => 'MAC address', 'Purchase_Date' => 'Purchase date', 'Install_Date' => 'Install date', 'Warranty_Expire' => 'Warranty', ]; $extra_columns = array_values(array_intersect(array_keys($extra_column_options), (array) ($_GET['show_column'] ?? []))); $show_mac = in_array('Mac_Address', $extra_columns, true); $show_purchase = in_array('Purchase_Date', $extra_columns, true); $show_install = in_array('Install_Date', $extra_columns, true); $show_warranty = in_array('Warranty_Expire', $extra_columns, true); // Assigned To only means something for workstations and the like - printers are shared $show_contact = !in_array($type_filter, ['network', 'server', 'printer', 'other'], true); $show_model = $type_filter !== 'virtual'; // Only these sort - filter_header.php will hand through any lowercase word, and // an unknown column takes the page down with a mysqli exception $sortable_columns = [ 'asset_name', 'asset_type', 'asset_make', 'interface_ip', 'interface_mac', 'asset_purchase_date', 'asset_install_date', 'asset_warranty_expire', 'contact_name', 'location_name', 'asset_status', 'client_name', 'asset_created_at' ]; if (!in_array($sort, $sortable_columns, true)) { $sort = 'asset_name'; } // Addresses sort by value - .9 before .10 - and this works for IPv4 and IPv6 alike $order_by = $sort === 'interface_ip' ? "INET6_ATON(interface_ip) $order, interface_ip $order" : "$sort $order"; // Tab counts - within the client, status, tag and archive scope, before the search narrows it $row = mysqli_fetch_assoc(mysqli_query($mysqli, " SELECT COUNT(*) AS all_count, SUM(asset_type IN ('Laptop', 'Desktop')) AS workstation_count, SUM(asset_type = 'Server') AS server_count, SUM(asset_type = 'Virtual Machine') AS virtual_count, SUM(asset_type IN ('Firewall/Router', 'Switch', 'Access Point')) AS network_count, SUM(asset_type = 'Printer') AS printer_count, SUM(asset_type NOT IN ('Laptop', 'Desktop', 'Server', 'Virtual Machine', 'Firewall/Router', 'Switch', 'Access Point', 'Printer')) AS other_count FROM assets LEFT JOIN clients ON client_id = asset_client_id WHERE $archive_query $status_query $tag_query " . clientScopeSql('asset_client_id') . " $client_query ")); $tab_counts = [ '' => intval($row['all_count']), 'workstation' => intval($row['workstation_count']), 'server' => intval($row['server_count']), 'virtual' => intval($row['virtual_count']), 'network' => intval($row['network_count']), 'printer' => intval($row['printer_count']), 'other' => intval($row['other_count']), ]; $sql = mysqli_query( $mysqli, "SELECT SQL_CALC_FOUND_ROWS asset_archived_at, asset_client_id, asset_contact_id, asset_description, asset_favorite, asset_id, asset_install_date, asset_location_id, asset_make, asset_model, asset_name, asset_os, asset_physical_location, asset_purchase_date, asset_serial, asset_status, asset_type, asset_uri, asset_uri_2, asset_uri_client, asset_warranty_expire, client_id, client_name, contact_archived_at, contact_name, contact_photo, interface_ip, interface_ipv6, interface_mac, location_archived_at, location_name FROM assets LEFT JOIN clients ON asset_client_id = client_id LEFT JOIN contacts ON asset_contact_id = contact_id LEFT JOIN locations ON asset_location_id = location_id LEFT JOIN asset_interfaces ON interface_asset_id = asset_id AND interface_primary = 1 WHERE $archive_query $tag_query AND (asset_name LIKE '%$q%' OR asset_description LIKE '%$q%' OR asset_type LIKE '%$q%' OR interface_ip LIKE '%$q%' OR interface_ipv6 LIKE '%$q%' OR interface_mac LIKE '%$q%' OR asset_make LIKE '%$q%' OR asset_model LIKE '%$q%' OR asset_serial LIKE '%$q%' OR asset_os LIKE '%$q%' OR contact_name LIKE '%$q%' OR location_name LIKE '%$q%' OR client_name LIKE '%$q%' OR asset_id IN (SELECT asset_tag_asset_id FROM asset_tags JOIN tags ON tag_id = asset_tag_tag_id WHERE tag_name LIKE '%$q%')) AND ($type_query) " . clientScopeSql('asset_client_id') . " $status_query $location_query $expire_query $client_query GROUP BY asset_id ORDER BY $order_by LIMIT $record_from, $record_to" ); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); $assets = []; while ($row = mysqli_fetch_assoc($sql)) { $assets[intval($row['asset_id'])] = $row; } // Tags and rack positions for the whole page in two queries, not two per row $assets_tags = tagsForRecords('asset', array_keys($assets)); $assets_racked = []; if ($assets) { $sql_racked = mysqli_query($mysqli, "SELECT unit_asset_id, unit_start_number, unit_end_number, rack_id, rack_name FROM rack_units JOIN racks ON rack_id = unit_rack_id WHERE unit_asset_id IN (" . implode(',', array_keys($assets)) . ")"); while ($row = mysqli_fetch_assoc($sql_racked)) { $assets_racked[intval($row['unit_asset_id'])] = $row; } } $search_noun = $type_filter ? $asset_type_tabs[$type_filter]['label'] : 'assets'; ?>