diff --git a/CHANGELOG.md b/CHANGELOG.md
index 711dc8dc0..9057e7327 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow.
### Upgrading to 26.12.6
-Update from Maintenance > Update as usual. This release has three database changes: 4.4.4 adds the settings for Warranty Lookup, 4.4.5 puts the email and phone links back in Knowledge Base articles and documents written in Markdown, and 4.4.6 adds the Vendors permission to roles.
+Update from Maintenance > Update as usual. This release has four database changes: 4.4.4 adds the settings for Warranty Lookup, 4.4.5 puts the email and phone links back in Knowledge Base articles and documents written in Markdown, 4.4.6 adds the Vendors permission to roles, and 4.4.7 adds linked assets.
- If you have switched a module off in Admin > Settings > Modules - IT documentation, ticketing, invoicing and accounting or any of the others - check that you meant it before updating. Its pages, bookmarks to them and links in old emails stop opening, and the nightly run stops doing its work: with ticketing off, no recurring tickets are raised and resolved tickets aren't closed; with invoicing and accounting off, no recurring invoices or expenses are made, no autopay is charged and no payment reminders go out; with IT documentation off, no domain, certificate, warranty or service review notifications, and shared links - logins, files, documents and IT documentation exports sent to clients - don't open. Switch it back on and the next run picks up what is due, and shared links that haven't run out open again.
- API keys get a 403 saying which module is off for anything of a module that is switched off - tickets with ticketing off, assets and credentials with IT documentation off, invoices with invoicing off.
@@ -28,6 +28,7 @@ Update from Maintenance > Update as usual. This release has three database chang
- A `.md` already in Files shows as it reads in its preview and quick look, with its pictures that are in the client's files, and **Make it a document**, in its menu and quick look, makes it a document: the pictures it uses are copied in, and the document takes the file's place and its links.
- Copy a Knowledge Base article's link from its **⋮** menu, on its page or beside it in the list - to send it to someone, or to link to it from another article. It offers the links that suit who can read it: **Copy Public Link** for an article for anyone, **Copy Portal Link** for one in the client portal, and **Copy Link** to its page here for one for agents only. A link that won't open yet is greyed out with the reason under it, such as **Publish it first** or **The public help center is off**. Agents who can read articles but not edit them get the menu with these links too.
- A **Vendors** permission in Admin > Roles, for your own vendors - the ones with no client: suppliers, distributors, the colo - at **None**, **Read**, **Modify** or **Full**. Set a role to **None** to keep its agents out of your suppliers' numbers, accounts and contacts: **Vendors** in the menu opens your clients' vendors instead, your own vendors' pages and their export are refused, search leaves them out, and where a client's license, network, ticket or service names one of yours, they see only its name. A client's vendors stay with the **Clients** permission. Adding, editing and deleting your own vendors moves from **Financial** to **Vendors** too.
+- Link assets to each other - a label printer and its cutter, a server and its disk shelf, a desk phone and its sidecar - so a part that is swapped or sent off for repair on its own can be its own asset, with its own serial number and warranty, and still be found with what it goes on. **Link**, on the asset's new **Linked assets** card, picks from the client's other assets, of any type. A link goes both ways, each asset showing the other, and an asset can be linked to as many as it needs. Linking and unlinking are noted in both assets' History, so you can see which part was on which device and when it was swapped. A linked asset at another site says where it is, and its **⋮** menu moves either one to the other's site - nothing moves on its own. The assets list and the asset's quick look show what each asset is linked to, the client export lists the links, and assets transferred to another client together stay linked.
### Bug Fixes
@@ -55,6 +56,7 @@ Update from Maintenance > Update as usual. This release has three database chang
- `purifierUriSchemes()` (`functions/sanitize.php`) is the one list of address schemes HTMLPurifier lets through - `http`, `https`, `mailto`, `tel` and `data` - for `documentHtmlPurify()` and the ticket views (`agent/ticket.php`, `agent/modals/ticket/ticket.php`, `client/ticket.php`, `guest/guest_view_ticket.php`, `guest/guest_approve_ticket_task.php`), each of which set `http`/`https`/`data` only. HTMLPurifier still checks each: a `mailto:` must be an address, a `tel:` is cut down to its digits, and `javascript:`, `vbscript:`, `file:` and the rest go. Database 4.4.5 makes `document_content` and `kb_article_content` (with their `_raw` search text) again from the Markdown of those with `mailto:` or `tel:` in it, keeping `_updated_at`; it stops, to be run again, if the process still has the functions from before it.
- `kbCopyLinks($row)` (`functions/kb.php`) gives an article's links for its readers - `[label, icon, url, why not, what it is]`, from `kbPublicUrl()`, `kbPortalArticleUrl()` and the new `kbAgentArticleUrl()` - with why one won't open yet (a draft, `kbPublicOn()` or `moduleOn('portal')` off); `kbCopyLinksMenuHtml()` makes them `.clipboardjs` dropdown items, or disabled ones with the reason in `.itflow-dropdown-why`. `agent/kb_article.php` and the rows of `agent/kb.php` show their ⋮ menu for `kbCan(1)` as well as `kbCan(2)` - with only these items for those who can't edit.
- Vendors permission: `module_vendor` (database 4.4.6, module 9 on a fresh install, `setup/seed_data.php` - Technician Read, Accountant Full); 4.4.6 gives every role `max(1, its module_financial level)`. In `functions/vendor.php`, `vendorPermissionReady()` (4.4.6) switches `vendorModule(0)` from `module_financial` to `module_vendor`, and `vendorCan(0, 1)` from always-yes to the permission; `vendorEnforce(0, $level)` enforces it at every level. `vendorIsOurs($vendor_id)` (one query a page) and `vendorCanOpen($vendor_id)` decide whether a mention of a vendor opens: `vendorLink()` and the new `vendorNameHtml()` show the name without a link, and `vendorRedact($row, $vendor_id)` blanks a row's `vendor_phone`, `vendor_account_number`, `vendor_code`, `vendor_email`, contacts and hours - used by `agent/network.php`, `agent/license.php`, `agent/ticket.php`, `agent/service.php` and the network, license and service quick looks. `vendorLink()` no longer puts a client's id on a page link to one of ours. The ours list (`agent/vendors.php`), its export, the vendor quick look and global search's Vendors group check it; the side nav's Vendors goes to `vendors.php?view=clients` without it. `roleModules()` lists modules in `roleModuleInfo()`'s order, then by id.
+- Linked assets: `asset_links` (database 4.4.7) keeps each link as two rows, (`asset_id`, `linked_asset_id`) and the reverse, both `ON DELETE CASCADE`. `functions/asset_link.php` has `assetLinksReady()` (4.4.7), `assetLinked($asset_id)` (the client's assets, not archived), `assetLinkedNames($asset_ids)` for a list in one query, `assetLinkedTo()`, `assetLinkAdd($asset_id, $ids)` and `assetLinkRemove($asset_id, $other_id)` - each writes an `asset_history` row on both sides and `logAudit('Asset', 'Link')` or `'Unlink'` - and `assetLinkedListHtml()` for the card. The link picker (`functions/link_picker.php`) takes `asset|asset` in `LINK_PICKER_OWN`, with Support at Modify; it is kept out of `LINK_RELATIONS`, whose code assumes two different kinds. `agent/post/asset.php` adds `unlink_asset_link` and `move_linked_asset`, which sets one asset's location to the other's through `assetQuickSet()`, and `bulk_transfer_client_asset` copies the links between the assets it moves to their new copies. `clientExportLinks()` lists each pair once.
## [26.12.5]
diff --git a/admin/database_updates/4.4.7.php b/admin/database_updates/4.4.7.php
new file mode 100644
index 000000000..72b153adf
--- /dev/null
+++ b/admin/database_updates/4.4.7.php
@@ -0,0 +1,25 @@
+= 2;
$can_delete = lookupUserPermission('module_support') >= 3;
+ // The assets it's linked to - a part on it, or what it sits on (functions/asset_link.php)
+ $asset_linked = assetLinked($asset_id);
$can_credentials = lookupUserPermission('module_credential') >= 1;
$can_credentials_edit = lookupUserPermission('module_credential') >= 2;
@@ -804,6 +806,23 @@ if (mysqli_num_rows($sql) == 0) {
+
+
+
+
+
+ = assetLinkedListHtml($asset_row, $asset_linked, $can_edit && !$asset_archived_at) ?>
+
+
An asset that goes with this one - a part on it, or what it's fitted to. Each shows the other, and swaps go in both histories.
+
+
+
+
+
+
+
+
+ $linked_asset) { ?>= $n ? ', ' : '' ?>
= escapeHtml($linked_asset['asset_name']) ?>
+
+ = linkPickerButton('asset', $asset_id, 'asset', $asset_linked ? 'Change' : 'Link an asset', 'small' . ($asset_linked ? ' ms-1' : ''), '', 'Link it to another of this client\'s assets') ?>
+
+
+
+
diff --git a/agent/post/asset.php b/agent/post/asset.php
index ca1660c17..791679a2f 100644
--- a/agent/post/asset.php
+++ b/agent/post/asset.php
@@ -655,6 +655,7 @@ if (isset($_POST['bulk_transfer_client_asset'])) {
// Get Count
$asset_count = count($_POST['asset_ids']);
+ $transferred = []; // old asset ID => new
foreach($_POST['asset_ids'] as $current_asset_id) {
$current_asset_id = intval($current_asset_id);
@@ -683,6 +684,7 @@ if (isset($_POST['bulk_transfer_client_asset'])) {
WHERE asset_id = $current_asset_id
");
$new_asset_id = mysqli_insert_id($mysqli);
+ $transferred[$current_asset_id] = $new_asset_id;
// Transfer all Interfaces over too - with their type, so a port stays a port and an SFP cage a cage
$sql_interfaces = mysqli_query($mysqli, "SELECT interface_mac, interface_name, interface_type, interface_primary FROM asset_interfaces WHERE interface_asset_id = $current_asset_id");
@@ -730,6 +732,17 @@ if (isset($_POST['bulk_transfer_client_asset'])) {
} // End Transfer to Client Loop
+ // Assets moved together stay linked to each other (functions/asset_link.php) - a link to one
+ // left behind stays with the old, archived asset
+ if (count($transferred) > 1 && assetLinksReady()) {
+ $old_ids = implode(',', array_keys($transferred));
+ $sql_links = mysqli_query($mysqli, "SELECT asset_id, linked_asset_id FROM asset_links WHERE asset_id IN ($old_ids) AND linked_asset_id IN ($old_ids)");
+ while ($row = mysqli_fetch_row($sql_links)) {
+ mysqli_query($mysqli, "INSERT IGNORE INTO asset_links (asset_id, linked_asset_id, asset_link_created_by)
+ VALUES (" . $transferred[intval($row[0])] . ", " . $transferred[intval($row[1])] . ", " . intval($session_user_id) . ")");
+ }
+ }
+
logAudit("Asset", "Bulk Transfer", "$session_name transferred $asset_count assets to $new_client_name", $new_client_id);
flashAlert("Transferred $asset_count asset" . ($asset_count === 1 ? '' : 's') . " to " . stripslashes($new_client_name) . "");
@@ -1042,6 +1055,60 @@ if (isset($_POST['bulk_delete_assets'])) {
// Licenses are linked in post/link.php (link_software_to_asset)
+/*
+ * Linked assets (functions/asset_link.php) - unlinking one from the asset's page, and moving one
+ * of two linked assets to the other's site: what the Move menu beside it offers, never done on
+ * its own.
+ */
+if (isset($_GET['unlink_asset_link'])) {
+
+ validateCSRFToken();
+
+ enforceUserPermission('module_support', 2);
+
+ $asset_id = intval($_GET['asset_id'] ?? 0);
+ $other_id = intval($_GET['other'] ?? 0);
+ $row = linkRecord('asset', $asset_id);
+ if (!$row) {
+ flashAlert('That asset no longer exists.', 'error');
+ redirect();
+ }
+ $client_id = intval($row['client_id']);
+ enforceClientAccess();
+
+ $name = assetLinkRemove($asset_id, $other_id);
+ flashAlert($name !== '' ? 'Unlinked ' . escapeHtml($name) . ' from ' . escapeHtml($row['name']) . '' : 'They weren\'t linked.', $name !== '' ? 'error' : 'info');
+ redirect();
+
+}
+
+if (isset($_GET['move_linked_asset'])) {
+
+ validateCSRFToken();
+
+ enforceUserPermission('module_support', 2);
+
+ $move_id = intval($_GET['move'] ?? 0);
+ $with_id = intval($_GET['with'] ?? 0);
+ $move = assetQuickRow($move_id);
+ $with = assetQuickRow($with_id);
+ if (!$move || !$with) {
+ flashAlert('That asset no longer exists.', 'error');
+ redirect();
+ }
+ $client_id = intval($move['asset_client_id']);
+ enforceClientAccess();
+ if (intval($with['asset_client_id']) !== $client_id || !assetLinkedTo($move_id, $with_id)) {
+ flashAlert('Those two aren\'t linked.', 'error');
+ redirect();
+ }
+
+ [$ok, $message] = assetQuickSet($move_id, 'asset_location', intval($with['asset_location_id']));
+ flashAlert($ok ? '' . escapeHtml($move['asset_name']) . ': ' . escapeHtml($message ?: 'already there') : escapeHtml($message), $ok ? 'success' : 'error');
+ redirect();
+
+}
+
if (isset($_GET['unlink_credential_from_asset'])) {
validateCSRFToken();
diff --git a/css/itflow_custom.css b/css/itflow_custom.css
index 1c854d9d1..30b0b6636 100644
--- a/css/itflow_custom.css
+++ b/css/itflow_custom.css
@@ -5339,6 +5339,15 @@ tr:hover .itflow-files-rename,
padding: .2rem .25rem;
border-bottom: 1px solid var(--bs-border-color-translucent);
}
+/* A linked asset's menu - its card sits in the page's narrow column, so long names wrap
+ rather than the menu reaching under the side menu */
+.itflow-linked-asset-menu {
+ max-width: 16rem;
+}
+.itflow-linked-asset-menu .dropdown-item {
+ white-space: normal;
+}
+
/* A menu item that can't be used yet, and why - under its label, lined up with it */
.dropdown-item .itflow-dropdown-why {
display: block;
diff --git a/db.sql b/db.sql
index df506afe8..09737a550 100644
--- a/db.sql
+++ b/db.sql
@@ -374,6 +374,25 @@ CREATE TABLE `asset_interfaces` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
+--
+-- Table structure for table `asset_links`
+--
+
+DROP TABLE IF EXISTS `asset_links`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8mb4 */;
+CREATE TABLE `asset_links` (
+ `asset_id` int(11) NOT NULL,
+ `linked_asset_id` int(11) NOT NULL,
+ `asset_link_created_by` int(11) NOT NULL DEFAULT 0,
+ `asset_link_created_at` datetime NOT NULL DEFAULT current_timestamp(),
+ PRIMARY KEY (`asset_id`,`linked_asset_id`),
+ KEY `asset_links_linked` (`linked_asset_id`),
+ CONSTRAINT `asset_links_asset` FOREIGN KEY (`asset_id`) REFERENCES `assets` (`asset_id`) ON DELETE CASCADE,
+ CONSTRAINT `asset_links_linked_asset` FOREIGN KEY (`linked_asset_id`) REFERENCES `assets` (`asset_id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
--
-- Table structure for table `asset_notes`
--
diff --git a/functions.php b/functions.php
index 2b260e7fd..8c4f70db8 100644
--- a/functions.php
+++ b/functions.php
@@ -30,6 +30,7 @@ require_once __DIR__ . '/functions/api_keys.php';
require_once __DIR__ . '/functions/asset_interface.php';
require_once __DIR__ . '/functions/rack.php';
require_once __DIR__ . '/functions/asset.php';
+require_once __DIR__ . '/functions/asset_link.php';
require_once __DIR__ . '/functions/warranty.php';
require_once __DIR__ . '/functions/asset_type.php';
require_once __DIR__ . '/functions/asset_type_custom.php';
diff --git a/functions/asset_link.php b/functions/asset_link.php
new file mode 100644
index 000000000..83a6878aa
--- /dev/null
+++ b/functions/asset_link.php
@@ -0,0 +1,192 @@
+=');
+ }
+ 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 = '';
+ 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
+ ? '
' . ($there_name !== '' ? escapeHtml($there_name) : 'No site') . ''
+ : '';
+ $html .= '
';
+ }
+ return $html . '
';
+}
diff --git a/functions/client_export.php b/functions/client_export.php
index 6e37749da..e58df8a6b 100644
--- a/functions/client_export.php
+++ b/functions/client_export.php
@@ -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']);
});
diff --git a/functions/link_picker.php b/functions/link_picker.php
index 6fe906a6e..941a38c0e 100644
--- a/functions/link_picker.php
+++ b/functions/link_picker.php
@@ -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];