mirror of
https://github.com/itflow-org/itflow
synced 2026-12-05 18:49:01 +00:00
193 lines
10 KiB
PHP
193 lines
10 KiB
PHP
<?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 . '&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&move=' . $other_id . '&with=' . $asset_id . '&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&move=' . $asset_id . '&with=' . $other_id . '&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&asset_id=' . $asset_id . '&other=' . $other_id . '&csrf_token=' . $csrf . '">'
|
|
. '<i class="fas fa-fw fa-unlink text-secondary me-2"></i>Unlink</a>'
|
|
. '</div></div>';
|
|
}
|
|
$html .= '</div>';
|
|
}
|
|
return $html . '</div>';
|
|
}
|