='); } return $ready; } /** * The assets linked to one, by name - not archived, and of its client: rows with asset_id, * asset_name, asset_type, asset_make, asset_model, asset_status, asset_location_id and * location_name. Empty before the database has links. */ function assetLinked($asset_id) { global $mysqli; $asset_id = intval($asset_id); if (!$asset_id || !assetLinksReady()) { return []; } return mysqli_fetch_all(mysqli_query($mysqli, "SELECT o.asset_id, o.asset_name, o.asset_type, o.asset_make, o.asset_model, o.asset_status, o.asset_location_id, location_name FROM asset_links l JOIN assets a ON a.asset_id = l.asset_id JOIN assets o ON o.asset_id = l.linked_asset_id AND o.asset_client_id = a.asset_client_id AND o.asset_archived_at IS NULL LEFT JOIN locations ON location_id = o.asset_location_id AND o.asset_location_id > 0 WHERE l.asset_id = $asset_id ORDER BY o.asset_name ASC"), MYSQLI_ASSOC); } /** The names of the assets linked to each of these, for a list - [asset_id => [name, ...]] in one query. */ function assetLinkedNames(array $asset_ids) { global $mysqli; $ids = array_values(array_unique(array_filter(array_map('intval', $asset_ids)))); $out = []; if (!$ids || !assetLinksReady()) { return $out; } $sql = mysqli_query($mysqli, "SELECT l.asset_id, o.asset_name FROM asset_links l JOIN assets a ON a.asset_id = l.asset_id JOIN assets o ON o.asset_id = l.linked_asset_id AND o.asset_client_id = a.asset_client_id AND o.asset_archived_at IS NULL WHERE l.asset_id IN (" . implode(',', $ids) . ") ORDER BY o.asset_name ASC"); while ($row = mysqli_fetch_row($sql)) { $out[intval($row[0])][] = $row[1]; } return $out; } /** Are these two linked? */ function assetLinkedTo($asset_id, $other_id) { global $mysqli; if (!assetLinksReady()) { return false; } return (bool) mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM asset_links WHERE asset_id = " . intval($asset_id) . " AND linked_asset_id = " . intval($other_id))); } /** One line in an asset's History - with the status it has, as every History line does. */ function assetLinkHistory($asset_id, $text) { global $mysqli; $asset_id = intval($asset_id); mysqli_query($mysqli, "INSERT INTO asset_history SET asset_history_status = COALESCE((SELECT s FROM (SELECT asset_status AS s FROM assets WHERE asset_id = $asset_id) x), ''), asset_history_description = '" . escapeSql(mb_substr($text, 0, 255)) . "', asset_history_asset_id = $asset_id"); } /** * Link $ids to one asset, both ways. Only assets of its client that aren't archived, and not * itself; one already linked is skipped. The caller has checked the agent may edit its assets. * Returns the names that were linked. */ function assetLinkAdd($asset_id, array $ids) { global $mysqli, $session_name, $session_user_id; $asset_id = intval($asset_id); $source = linkRecord('asset', $asset_id); if (!$source || $source['archived_at'] !== null || !assetLinksReady()) { return []; } $client_id = intval($source['client_id']); $added = []; foreach (array_unique(array_map('intval', $ids)) as $other_id) { $other = linkRecord('asset', $other_id); if (!$other || $other_id === $asset_id || intval($other['client_id']) !== $client_id || $other['archived_at'] !== null) { continue; } mysqli_query($mysqli, "INSERT IGNORE INTO asset_links (asset_id, linked_asset_id, asset_link_created_by) VALUES ($asset_id, $other_id, " . intval($session_user_id) . "), ($other_id, $asset_id, " . intval($session_user_id) . ")"); if (mysqli_affected_rows($mysqli) < 1) { continue; } $added[] = $other['name']; $said = "$session_name linked asset {$other['name']} to asset {$source['name']}"; assetLinkHistory($asset_id, $said); assetLinkHistory($other_id, $said); logAudit("Asset", "Link", escapeSql($said), $client_id, $asset_id); } return $added; } /** Unlink two assets, both ways. Returns the other's name, or '' when they weren't linked. */ function assetLinkRemove($asset_id, $other_id) { global $mysqli, $session_name; $asset_id = intval($asset_id); $other_id = intval($other_id); $source = linkRecord('asset', $asset_id); $other = linkRecord('asset', $other_id); if (!$source || !$other || !assetLinksReady()) { return ''; } mysqli_query($mysqli, "DELETE FROM asset_links WHERE (asset_id = $asset_id AND linked_asset_id = $other_id) OR (asset_id = $other_id AND linked_asset_id = $asset_id)"); if (mysqli_affected_rows($mysqli) < 1) { return ''; } $said = "$session_name unlinked asset {$other['name']} from asset {$source['name']}"; assetLinkHistory($asset_id, $said); assetLinkHistory($other_id, $said); logAudit("Asset", "Unlink", escapeSql($said), intval($source['client_id']), $asset_id); return $other['name']; } /** * The linked assets on an asset's page: each one's name, what it is and its site - and, where its * site isn't this one's, a Move menu to put one at the other's site. $row is this asset's * (asset_id, asset_client_id, asset_location_id, location_name). */ function assetLinkedListHtml(array $row, array $linked, $can_edit) { $asset_id = intval($row['asset_id']); $client_id = intval($row['asset_client_id']); $here_id = intval($row['asset_location_id']); $here_name = (string) ($row['location_name'] ?? ''); $this_name = escapeHtml($row['asset_name']); $csrf = urlencode($_SESSION['csrf_token'] ?? ''); $html = '