Link assets to each other: a Linked assets card on the asset page with the link picker for any of the client's other assets, links kept both ways in asset_links (database 4.4.7) and written to both assets' History and the audit log, a menu on a linked asset at another site to move either one to the other's site, linked names on the assets list and quick look, links in the client export, and links kept between assets transferred to another client together; 26.12.6 changelog

This commit is contained in:
johnnyq
2026-12-05 02:24:14 -05:00
parent ac089c6af8
commit f750e3c00b
12 changed files with 395 additions and 3 deletions

192
functions/asset_link.php Normal file
View File

@@ -0,0 +1,192 @@
<?php
/*
* Assets linked to each other (database 4.4.7, asset_links) - a label printer and the knife unit
* on it, a server and its disk shelf, a desk phone and its sidecar: two assets of one client that
* go together but are each their own, sent off for repair or swapped on their own.
*
* A link goes both ways - each asset shows the other - and is kept as two rows, one from each, so
* either side reads it the same. An asset can be linked to any number of others of its client.
* Linking and unlinking go in both assets' History and the audit log, so where a swapped part has
* been can be read back. Moving one to the other's site is offered on the asset page, and never
* happens on its own.
*/
/** Whether the database has asset links yet (4.4.7). */
function assetLinksReady() {
global $mysqli;
static $ready = null;
if ($ready === null) {
if (defined('CURRENT_DATABASE_VERSION')) {
$version = CURRENT_DATABASE_VERSION;
} else {
$result = mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1");
$version = $result && ($row = mysqli_fetch_row($result)) ? (string) $row[0] : '0';
}
$ready = version_compare($version, '4.4.7', '>=');
}
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 = '<div class="list-group list-group-flush">';
foreach ($linked as $other) {
$other_id = intval($other['asset_id']);
$there_id = intval($other['asset_location_id']);
$there_name = (string) ($other['location_name'] ?? '');
$name = escapeHtml($other['asset_name']);
$what = escapeHtml(trim($other['asset_make'] . ' ' . $other['asset_model']) ?: (string) $other['asset_type']);
$site = $there_id !== $here_id
? '<span class="text-warning-emphasis" title="Not at this asset\'s site"><i class="fas fa-fw fa-map-marker-alt me-1"></i>' . ($there_name !== '' ? escapeHtml($there_name) : 'No site') . '</span>'
: '';
$html .= '<div class="list-group-item d-flex align-items-center gap-2">'
. '<span class="itflow-asset-icon itflow-asset-icon-sm itflow-asset-tone-' . assetTypeTone($other['asset_type']) . '" aria-hidden="true"><i class="fas fa-fw fa-' . getAssetIcon((string) $other['asset_type']) . '"></i></span>'
. '<div class="min-w-0 flex-grow-1">'
. '<a class="fw-semibold d-block text-truncate" href="asset.php?client_id=' . $client_id . '&amp;asset_id=' . $other_id . '">' . $name . '</a>'
. '<div class="small text-body-secondary d-flex flex-wrap column-gap-2"><span class="text-truncate">' . $what . '</span>' . $site
. ($other['asset_status'] ? '<span>' . escapeHtml($other['asset_status']) . '</span>' : '') . '</div>'
. '</div>';
if ($can_edit) {
$moves = '';
if ($there_id !== $here_id && $here_id) {
$moves .= '<a class="dropdown-item d-flex" href="post.php?move_linked_asset&amp;move=' . $other_id . '&amp;with=' . $asset_id . '&amp;csrf_token=' . $csrf . '">'
. '<i class="fas fa-fw fa-arrow-left text-secondary me-2 mt-1 flex-shrink-0"></i><span>Move ' . $name . ' to ' . escapeHtml($here_name) . '</span></a>';
}
if ($there_id !== $here_id && $there_id) {
$moves .= '<a class="dropdown-item d-flex" href="post.php?move_linked_asset&amp;move=' . $asset_id . '&amp;with=' . $other_id . '&amp;csrf_token=' . $csrf . '">'
. '<i class="fas fa-fw fa-arrow-right text-secondary me-2 mt-1 flex-shrink-0"></i><span>Move ' . $this_name . ' to ' . escapeHtml($there_name) . '</span></a>';
}
$html .= '<div class="dropdown flex-shrink-0">'
. '<button type="button" class="btn btn-sm btn-light" data-bs-toggle="dropdown" title="More actions" aria-label="More actions"><i class="fas fa-fw fa-ellipsis-v"></i></button>'
. '<div class="dropdown-menu dropdown-menu-end itflow-linked-asset-menu">' . $moves . ($moves ? '<div class="dropdown-divider"></div>' : '')
. '<a class="dropdown-item" href="post.php?unlink_asset_link&amp;asset_id=' . $asset_id . '&amp;other=' . $other_id . '&amp;csrf_token=' . $csrf . '">'
. '<i class="fas fa-fw fa-unlink text-secondary me-2"></i>Unlink</a>'
. '</div></div>';
}
$html .= '</div>';
}
return $html . '</div>';
}

View File

@@ -912,6 +912,16 @@ function clientExportLinks($client_id, $options) {
$rows[] = ['type' => $labels[$ft] ?? ucfirst($ft), 'name' => $fn, 'linked_type' => $labels[$tt] ?? ucfirst($tt), 'linked_name' => $tn];
}
}
// Assets linked to each other (functions/asset_link.php) - once a pair, though it is kept from both sides
if (clientExportHas($options, 'assets') && assetLinksReady()) {
$archived = $options['archived'] ? '' : ' AND ta.asset_archived_at IS NULL AND tb.asset_archived_at IS NULL';
$sql = mysqli_query($mysqli, "SELECT ta.asset_name AS a_name, tb.asset_name AS b_name FROM asset_links l
JOIN assets ta ON ta.asset_id = l.asset_id JOIN assets tb ON tb.asset_id = l.linked_asset_id
WHERE l.asset_id < l.linked_asset_id AND ta.asset_client_id = $client_id AND tb.asset_client_id = $client_id $archived");
while ($sql && ($row = mysqli_fetch_assoc($sql))) {
$rows[] = ['type' => 'Asset', 'name' => $row['a_name'], 'linked_type' => 'Asset', 'linked_name' => $row['b_name']];
}
}
usort($rows, function ($x, $y) {
return strcmp($x['type'], $y['type']) ?: strcasecmp($x['name'], $y['name']) ?: strcmp($x['linked_type'], $y['linked_type']) ?: strcasecmp($x['linked_name'], $y['linked_name']);
});

View File

@@ -15,7 +15,9 @@
* person or device each belongs to (asset_contact_id, credential_contact_id,
* credential_asset_id). One that is someone else's moves;
* - a project's tickets, a service's tickets, a ticket's services, and a
* ticket's related tickets.
* ticket's related tickets;
* - an asset's linked assets - a printer and the knife unit on it - linked
* both ways (functions/asset_link.php).
* Each goes through the same code as the windows it replaces did.
*/
@@ -28,6 +30,7 @@ const LINK_PICKER_OWN = [
'service|ticket' => ['module_support' => 2],
'ticket|service' => ['module_support' => 2],
'ticket|ticket' => ['module_support' => 2],
'asset|asset' => ['module_support' => 2],
];
/*
@@ -102,6 +105,9 @@ function linkPickerSource($from, $id, $to) {
if ($from === 'ticket' && $to === 'ticket' && !ticketLinksReady()) {
return [null, 'Related tickets need the database update.'];
}
if ($from === 'asset' && $to === 'asset' && !assetLinksReady()) {
return [null, 'Linking assets needs the database update.'];
}
return [$row, ''];
}
@@ -186,6 +192,20 @@ function linkPickerOptions($from, $id, $to) {
}
return ['title' => 'Assets of ' . $source['name'], 'options' => $options, 'count' => $count, 'linked' => $linked];
case 'asset|asset':
// The client's other assets, linked ones first - each with what it is and where it is
$source_id = intval($source['id']);
$sql = mysqli_query($mysqli, "SELECT a.asset_id, a.asset_name, a.asset_type, a.asset_make, a.asset_model, location_name, l.linked_asset_id IS NOT NULL AS linked
FROM assets a
LEFT JOIN locations ON location_id = a.asset_location_id AND a.asset_location_id > 0
LEFT JOIN asset_links l ON l.asset_id = $source_id AND l.linked_asset_id = a.asset_id
WHERE a.asset_client_id = $client_id AND a.asset_archived_at IS NULL AND a.asset_id != $source_id
ORDER BY linked DESC, a.asset_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$add($row['asset_id'], $row['asset_name'], (bool) $row['linked'], selectOptionAsset($row['asset_type'], implode(' · ', array_filter([trim($row['asset_make'] . ' ' . $row['asset_model']), (string) $row['location_name']]))));
}
return ['title' => 'Assets linked to ' . $source['name'], 'options' => $options, 'count' => $count, 'linked' => $linked];
case 'contact|credential':
case 'asset|credential':
// A credential belongs to one person and one device - one of someone else's moves here
@@ -424,6 +444,17 @@ function linkPickerSave($from, $id, $to, array $add, array $remove) {
}
break;
case 'asset|asset':
// Both ways, each in both assets' History (functions/asset_link.php)
$linked = assetLinkAdd($source_id, $ids($add));
foreach ($ids($remove) as $other_id) {
$name = assetLinkRemove($source_id, $other_id);
if ($name !== '') {
$unlinked[] = $name;
}
}
break;
case 'ticket|ticket':
$ticket = $source['ticket'];
$join = [$source_id];