@@ -247,6 +260,7 @@ if (mysqli_num_rows($sql) == 0) {
= $item['type_label'] ?>
+ · holds = $item['slots'] ?> across
· = $item['label'] ?>
· = $item['notes'] ?>
@@ -465,6 +479,8 @@ if (mysqli_num_rows($sql) == 0) {
+
+
diff --git a/agent/racks.php b/agent/racks.php
index 27a63596c..cd0802677 100644
--- a/agent/racks.php
+++ b/agent/racks.php
@@ -230,7 +230,8 @@ foreach ($racks as $rack_id => $row) {
diff --git a/api/v1/rack_units/create.php b/api/v1/rack_units/create.php
index a3c9d1e8b..268b47492 100644
--- a/api/v1/rack_units/create.php
+++ b/api/v1/rack_units/create.php
@@ -14,10 +14,13 @@
* asset_id The client's asset - or unit_device for an item that isn't one
* unit_device A name for the item
* unit_type optional - Without an asset: Device (the default), Patch Panel, Cable Management, Shelf, PDU, UPS, KVM / Console or Blanking Panel
- * unit_start_number required - The lowest unit it takes
+ * unit_start_number required - The lowest unit it takes (not in a holder)
* unit_height optional - How many units, 1 if left out
* unit_face optional - front (the default) or rear
* unit_full_depth optional - 1: it takes the front and the rear
+ * unit_slots optional - It holds devices side by side, this many across (a bracket, shelf or chassis)
+ * unit_parent_id optional - The holder it goes in instead - it takes the holder's units, face and depth
+ * unit_position optional - Its slot in the holder, 1 the first; the first free one if left out
* unit_power_watts, unit_notes optional
*/
diff --git a/api/v1/rack_units/delete.php b/api/v1/rack_units/delete.php
index f3fed327b..f2eee33d1 100644
--- a/api/v1/rack_units/delete.php
+++ b/api/v1/rack_units/delete.php
@@ -3,7 +3,8 @@
* API - Rack Units - Delete
* POST /api/v1/rack_units/delete.php
*
- * Take an item out of its rack, as Remove does - the asset stays.
+ * Take an item out of its rack, as Remove does - the asset stays. A holder takes
+ * what is in it out with it.
*
* Parameters (POST, JSON body): api_key, unit_id, client_id (the rack's client -
* needed when the key's user is limited to certain clients)
@@ -18,11 +19,12 @@ require_once 'unit_find.php';
if ($unit) {
+ $children = rackChildCount($unit_id);
mysqli_query($mysqli, "DELETE FROM rack_units WHERE unit_id = $unit_id");
$delete_count = mysqli_affected_rows($mysqli);
if ($delete_count > 0) {
- $what = escapeSql(rackUnitName($unit['asset_name'], $unit['unit_device'], $unit['unit_type']) . ' from rack ' . $rack['rack_name']);
+ $what = escapeSql(rackUnitName($unit['asset_name'], $unit['unit_device'], $unit['unit_type']) . ($children ? ' and the ' . rackDeviceCount($children) . ' in it' : '') . ' from rack ' . $rack['rack_name']);
logAudit("Rack", "Edit", "Removed $what via API ($api_key_name)", $client_id, $rack_id);
logAudit("API", "Success", "Removed $what via API ($api_key_name)", $client_id);
}
diff --git a/api/v1/rack_units/unit_model.php b/api/v1/rack_units/unit_model.php
index 137fe5249..6542e93f2 100644
--- a/api/v1/rack_units/unit_model.php
+++ b/api/v1/rack_units/unit_model.php
@@ -2,8 +2,8 @@
// An item in a rack from POST, checked as the rack item form checks it - with what
// the item has now ($unit) for those not sent, on an update. $rack is its rack and
-// $client_id the rack's client. Sets $unit_set (the SET clause) and $unit_label, or
-// turns the request down with 400 and why.
+// $client_id the rack's client. Sets $unit_set (the SET clause), $unit_label and
+// $unit_place (rackPlacement()), or turns the request down with 400 and why.
if (!function_exists('apiDeny')) {
http_response_code(404);
@@ -26,7 +26,10 @@ $face = (string) $sent('unit_face', $unit['unit_face'] ?? 'front');
$full_depth = intval($sent('unit_full_depth', $unit['unit_full_depth'] ?? 0)) === 1 ? 1 : 0;
$power_watts = intval($sent('unit_power_watts', $unit['unit_power_watts'] ?? 0));
$notes = mb_substr(trim((string) $sent('unit_notes', $unit['unit_notes'] ?? '')), 0, 255);
-$rack_units = intval($rack['rack_units']);
+// In a holder at a slot, or in the rack - and holding devices side by side, or not
+$holder_id = intval($sent('unit_parent_id', $unit['unit_parent_id'] ?? 0));
+$position = intval($sent('unit_position', $unit['unit_position'] ?? 0));
+$slots = intval($sent('unit_slots', $unit['unit_slots'] ?? 0));
if ($unit_type === '' || !array_key_exists($unit_type, rackItemTypes())) {
if (array_key_exists('unit_type', $_POST)) {
@@ -37,8 +40,11 @@ if ($unit_type === '' || !array_key_exists($unit_type, rackItemTypes())) {
if (!in_array($face, ['front', 'rear'], true)) {
$rack_refuse('unit_face is front or rear.');
}
-if ($start < 1 || $end > $rack_units) {
- $rack_refuse("That doesn't fit - this rack runs from U1 to U$rack_units.");
+if ($slots < 0 || $slots > RACK_SLOTS_MAX) {
+ $rack_refuse('unit_slots is how many it holds side by side - 0 to ' . RACK_SLOTS_MAX . '.');
+}
+if ($holder_id && $slots) {
+ $rack_refuse("An item in a holder (unit_parent_id) can't hold devices of its own (unit_slots).");
}
// The asset has to be the client's, and in one place at a time
@@ -58,13 +64,16 @@ if ($asset_id) {
$rack_refuse('Send asset_id, or a name for the item in unit_device.');
}
-$conflict = rackUnitConflict($rack['rack_id'], $start, $end, $face, $full_depth, $unit_id);
-if ($conflict) {
- $rack_refuse(html_entity_decode(rackPositionLabel($start, $end)) . " on the $face is taken by {$conflict['name']} (" . html_entity_decode(rackPositionLabel($conflict['start'], $conflict['end'])) . ").");
+// Units, overlaps and slots - functions/rack.php
+$unit_place = rackPlacement($rack['rack_id'], $unit_id, ['holder' => $holder_id, 'position' => $position,
+ 'start' => $start, 'end' => $end, 'face' => $face, 'full_depth' => $full_depth, 'slots' => $slots]);
+if (isset($unit_place['error'])) {
+ $rack_refuse(strip_tags($unit_place['error']) . '.');
}
// A linked asset is drawn by its own asset type, so only unlinked items keep one
$unit_set = "unit_device = '" . escapeSql(mb_substr($label, 0, 200)) . "', unit_asset_id = " . ($asset_id ?: 'NULL') . ", unit_type = " . ($asset_id ? 'NULL' : "'" . escapeSql($unit_type) . "'") . ",
- unit_start_number = $start, unit_end_number = $end, unit_face = '$face', unit_full_depth = $full_depth,
+ " . rackPlacementSet($unit_place) . ", unit_slots = " . intval($unit_place['slots']) . ",
unit_power_watts = " . ($power_watts > 0 ? $power_watts : 'NULL') . ", unit_notes = '" . escapeSql($notes) . "'";
-$unit_label = rackUnitName($asset_name, $label, $unit_type) . ' at ' . html_entity_decode(rackPositionLabel($start, $end)) . " ($face)";
+$unit_label = rackUnitName($asset_name, $label, $unit_type) . ($unit_place['holder'] ? " in slot {$unit_place['position']} of {$unit_place['holder_name']}" : '')
+ . ' at ' . html_entity_decode(rackPositionLabel($unit_place['start'], $unit_place['end'])) . " ({$unit_place['face']})";
diff --git a/api/v1/rack_units/update.php b/api/v1/rack_units/update.php
index 02aef9093..19bd396ac 100644
--- a/api/v1/rack_units/update.php
+++ b/api/v1/rack_units/update.php
@@ -4,7 +4,7 @@
* POST /api/v1/rack_units/update.php
*
* Move or change an item in its rack, as Edit does on it - only what is sent,
- * with the same checks as mounting it.
+ * with the same checks as mounting it. A holder takes what is in it along.
*
* Parameters (POST, JSON body):
* api_key required - Your API key
@@ -26,6 +26,9 @@ if ($unit) {
if (mysqli_query($mysqli, "UPDATE rack_units SET $unit_set WHERE unit_id = $unit_id")) {
$update_count = 1;
+ if ($unit_place['slots']) {
+ rackSyncChildren($unit_id);
+ }
$what = escapeSql("$unit_label in rack " . $rack['rack_name']);
logAudit("Rack", "Edit", "Updated $what via API ($api_key_name)", $client_id, $rack_id);
logAudit("API", "Success", "Updated $what via API ($api_key_name)", $client_id);
diff --git a/api/v1/racks/read.php b/api/v1/racks/read.php
index 43ae893ff..b6ef48e57 100644
--- a/api/v1/racks/read.php
+++ b/api/v1/racks/read.php
@@ -5,7 +5,9 @@
*
* A client's racks - archived ones too. One, by rack_id, also has what is mounted
* in it (units): each item's position (unit_start_number to unit_end_number), face,
- * whether it is full depth, and the asset it is, or its own name and type.
+ * whether it is full depth, and the asset it is, or its own name and type. A holder
+ * says how many it holds side by side (unit_slots); a device in one, which holder
+ * (unit_parent_id) and its slot there (unit_position).
*
* rack_id optional - Just this rack, with its units
* client_id optional - Just this client's
@@ -19,6 +21,12 @@ if (isset($_GET['rack_id'])) {
$id = intval($_GET['rack_id']);
$sql = mysqli_query($mysqli, "SELECT * FROM racks WHERE rack_id = $id " . apiClientScopeSql('rack_client_id'));
if ($sql && ($row = mysqli_fetch_assoc($sql))) {
+ // From U1 up - a holder, then what is in it by slot
+ $units = rackLoadUnits([$id])[$id];
+ usort($units, function ($a, $b) {
+ return [intval($a['unit_start_number']), intval($a['unit_parent_id'] ?: $a['unit_id']), intval($a['unit_position'])]
+ <=> [intval($b['unit_start_number']), intval($b['unit_parent_id'] ?: $b['unit_id']), intval($b['unit_position'])];
+ });
$row['units'] = array_map(function ($unit) {
return [
'unit_id' => intval($unit['unit_id']),
@@ -30,11 +38,14 @@ if (isset($_GET['rack_id'])) {
'unit_type' => $unit['unit_type'],
'unit_power_watts' => $unit['unit_power_watts'] !== null ? intval($unit['unit_power_watts']) : null,
'unit_notes' => $unit['unit_notes'],
+ 'unit_slots' => intval($unit['unit_slots']),
+ 'unit_parent_id' => $unit['unit_parent_id'] !== null ? intval($unit['unit_parent_id']) : null,
+ 'unit_position' => $unit['unit_parent_id'] !== null ? intval($unit['unit_position']) : null,
'asset_id' => $unit['asset_id'] !== null ? intval($unit['asset_id']) : null,
'asset_name' => $unit['asset_name'],
'asset_type' => $unit['asset_type'],
];
- }, array_reverse(rackLoadUnits([$id])[$id]));
+ }, $units);
echo json_encode(['success' => 'True', 'count' => 1, 'data' => [$row]]);
exit();
}
diff --git a/api/v1/racks/update.php b/api/v1/racks/update.php
index 087511800..794a7b908 100644
--- a/api/v1/racks/update.php
+++ b/api/v1/racks/update.php
@@ -33,7 +33,7 @@ if ($rack) {
$units = intval($rack_set['rack_units']);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT unit_device, unit_type, unit_end_number, asset_name FROM rack_units
LEFT JOIN assets ON asset_id = unit_asset_id
- WHERE unit_rack_id = $rack_id AND unit_end_number > $units ORDER BY unit_end_number DESC LIMIT 1"));
+ WHERE unit_rack_id = $rack_id AND unit_end_number > $units AND unit_parent_id IS NULL ORDER BY unit_end_number DESC LIMIT 1"));
if ($row) {
$rack_refuse("Can't shrink the rack to {$units}U - " . rackUnitName($row['asset_name'], $row['unit_device'], $row['unit_type']) . " is mounted up to U" . intval($row['unit_end_number']) . ". Move or remove it first.");
}
diff --git a/css/itflow_custom.css b/css/itflow_custom.css
index eef192c31..e0fd5df8d 100644
--- a/css/itflow_custom.css
+++ b/css/itflow_custom.css
@@ -4013,15 +4013,166 @@ button.itflow-rack-item-body:hover {
cursor: default;
}
+/* A holder - a bracket, shelf or chassis - with the devices in it side by side,
+ in rows when it wraps. Its tab at the left is the holder itself; each slot is
+ a device, or an empty "add here". */
+.itflow-rack-item.is-holder {
+ display: flex;
+}
+.itflow-rack-holder-tab {
+ display: flex;
+ flex: 0 0 1.4rem;
+ align-items: center;
+ justify-content: center;
+ padding: 0;
+ border: 0;
+ border-right: 1px solid rgba(0, 0, 0, .25);
+ background: transparent;
+ font-size: .7rem;
+ color: inherit;
+ text-decoration: none;
+}
+a.itflow-rack-holder-tab:hover,
+button.itflow-rack-holder-tab:hover {
+ color: inherit;
+ background-color: rgba(255, 255, 255, .1);
+}
+.itflow-rack-holder-tab:focus-visible {
+ outline: 2px solid #fff;
+ outline-offset: -2px;
+}
+.itflow-rack-holder-slots {
+ display: grid;
+ flex: 1 1 auto;
+ min-width: 0;
+ gap: 2px;
+ padding: 2px;
+}
+.itflow-rack-cell {
+ position: relative;
+ display: flex;
+ min-width: 0;
+ min-height: 0;
+ overflow: hidden;
+ border-radius: .2rem;
+ color: #fff;
+ box-shadow: inset 0 1px 0 rgba(255, 255, 255, .14), 0 1px 1px rgba(0, 0, 0, .35);
+ container-type: inline-size;
+}
+.itflow-rack-cell.is-empty {
+ align-items: center;
+ justify-content: center;
+ padding: 0;
+ border: 1px dashed rgba(255, 255, 255, .22);
+ background-color: rgba(0, 0, 0, .25);
+ box-shadow: none;
+ font-size: .6rem;
+ color: rgba(255, 255, 255, .8);
+}
+.itflow-rack-cell.is-empty i {
+ opacity: 0;
+ transition: opacity .12s ease-in-out;
+}
+button.itflow-rack-cell.is-empty:hover,
+button.itflow-rack-cell.is-empty:focus-visible {
+ border-color: rgba(255, 255, 255, .5);
+ background-color: rgba(255, 255, 255, .1);
+ outline: 0;
+}
+button.itflow-rack-cell.is-empty:hover i,
+button.itflow-rack-cell.is-empty:focus-visible i,
+.itflow-rack-dragging .itflow-rack-cell.is-empty i {
+ opacity: 1;
+}
+.itflow-rack-cell.is-target {
+ border: 2px dashed rgba(117, 214, 157, .9);
+ background-color: rgba(117, 214, 157, .25);
+}
+.itflow-rack-cell-body {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ width: 100%;
+ min-width: 0;
+ padding: 0 .3rem;
+ border: 0;
+ background: transparent;
+ font: inherit;
+ line-height: 1.1;
+ text-align: left;
+ color: inherit;
+ text-decoration: none;
+}
+a.itflow-rack-cell-body:hover,
+button.itflow-rack-cell-body:hover {
+ color: inherit;
+ background-color: rgba(255, 255, 255, .1);
+}
+.itflow-rack-cell-body:focus-visible {
+ outline: 2px solid #fff;
+ outline-offset: -2px;
+}
+.itflow-rack-cell-name,
+.itflow-rack-cell-meta {
+ overflow: hidden;
+ white-space: nowrap;
+ text-overflow: ellipsis;
+}
+.itflow-rack-cell-name {
+ font-size: .66rem;
+ font-weight: 600;
+}
+.itflow-rack-cell-meta {
+ font-size: .6rem;
+ opacity: .78;
+}
+.itflow-rack-cell-edit {
+ position: absolute;
+ top: 50%;
+ right: .15rem;
+ display: none;
+ align-items: center;
+ justify-content: center;
+ width: 1.15rem;
+ height: 1.05rem;
+ margin-top: -.525rem;
+ padding: 0;
+ border: 0;
+ border-radius: .2rem;
+ background-color: rgba(0, 0, 0, .4);
+ font-size: .55rem;
+ color: #fff;
+}
+.itflow-rack-cell-edit:hover {
+ background-color: rgba(0, 0, 0, .6);
+}
+/* The pen only where the slot is wide enough for it and the name */
+@container (min-width: 4.5rem) {
+ .itflow-rack-cell:hover .itflow-rack-cell-edit,
+ .itflow-rack-cell:focus-within .itflow-rack-cell-edit {
+ display: flex;
+ }
+}
+.itflow-rack-cell.is-archived {
+ opacity: .55;
+}
+.itflow-rack-cell.is-archived .itflow-rack-cell-name {
+ text-decoration: line-through;
+}
+
/* Dragging kit into place (agent/js/rack_drag.js): where it would go, red
where something is in the way, and the item being moved faded */
.itflow-rack-item[draggable="true"] {
cursor: grab;
}
.itflow-rack-item.is-dragging,
+.itflow-rack-cell.is-dragging,
.itflow-rack-unracked.is-dragging {
opacity: .45;
}
+.itflow-rack-cell[draggable="true"] {
+ cursor: grab;
+}
.itflow-rack-unracked[draggable="true"] {
cursor: grab;
}
@@ -4158,6 +4309,9 @@ button.itflow-rack-item-body:hover {
.itflow-rack-fit:empty {
display: none;
}
+.itflow-rack-slots-input {
+ max-width: 14rem;
+}
/* Listing cards: the whole card opens the rack, the menu sits above that link */
.itflow-rack-card {
@@ -4196,6 +4350,7 @@ button.itflow-rack-item-body:hover {
.itflow-rack-frame,
.itflow-rack-bay,
.itflow-rack-item,
+ .itflow-rack-cell,
.itflow-rack-chip,
.itflow-rack-swatch,
.itflow-rack-head-icon {
@@ -4203,7 +4358,9 @@ button.itflow-rack-item-body:hover {
print-color-adjust: exact;
}
.itflow-rack-slot span,
- .itflow-rack-item-edit {
+ .itflow-rack-item-edit,
+ .itflow-rack-cell-edit,
+ .itflow-rack-cell.is-empty i {
display: none;
}
.itflow-rack {
diff --git a/db.sql b/db.sql
index d295ff6a0..a664808cf 100644
--- a/db.sql
+++ b/db.sql
@@ -3027,15 +3027,20 @@ CREATE TABLE `rack_units` (
`unit_type` varchar(50) DEFAULT NULL,
`unit_power_watts` int(11) DEFAULT NULL,
`unit_notes` varchar(255) DEFAULT NULL,
+ `unit_slots` int(11) NOT NULL DEFAULT 0,
+ `unit_position` int(11) NOT NULL DEFAULT 0,
`unit_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`unit_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`unit_archived_at` datetime DEFAULT NULL,
`unit_asset_id` int(11) DEFAULT NULL,
`unit_rack_id` int(11) NOT NULL,
+ `unit_parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`unit_id`),
KEY `unit_rack_id` (`unit_rack_id`),
KEY `unit_asset_id` (`unit_asset_id`),
- CONSTRAINT `rack_units_ibfk_1` FOREIGN KEY (`unit_rack_id`) REFERENCES `racks` (`rack_id`) ON DELETE CASCADE
+ KEY `unit_parent_id` (`unit_parent_id`),
+ CONSTRAINT `rack_units_ibfk_1` FOREIGN KEY (`unit_rack_id`) REFERENCES `racks` (`rack_id`) ON DELETE CASCADE,
+ CONSTRAINT `rack_units_ibfk_2` FOREIGN KEY (`unit_parent_id`) REFERENCES `rack_units` (`unit_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
diff --git a/functions/asset.php b/functions/asset.php
index 4964355da..d099b2935 100644
--- a/functions/asset.php
+++ b/functions/asset.php
@@ -527,7 +527,7 @@ function assetDetailsText($asset_id) {
$add('In the building', $row['asset_physical_location']);
$rack = rackPositionForAsset($asset_id);
if ($rack) {
- $add('Rack', $rack['rack_name'] . ', ' . html_entity_decode(rackPositionLabel($rack['unit_start_number'], $rack['unit_end_number'])) . ' (' . $rack['unit_face'] . ')');
+ $add('Rack', $rack['rack_name'] . ', ' . html_entity_decode(rackPositionLabel($rack['unit_start_number'], $rack['unit_end_number'])) . ' (' . $rack['unit_face'] . ')' . rackSlotText($rack['holder_name'], $rack['unit_position']));
}
$add('Used by', $row['contact_name']);
diff --git a/functions/client_export.php b/functions/client_export.php
index d72b2904c..9de54208c 100644
--- a/functions/client_export.php
+++ b/functions/client_export.php
@@ -309,7 +309,7 @@ function clientExportDatasets() {
] + $archived],
'rack_units' => ['section' => 'racks', 'label' => 'Rack contents', 'columns' => [
'rack' => 'Rack', 'from_unit' => 'From unit', 'to_unit' => 'To unit', 'face' => 'Face', 'device' => 'Device', 'asset' => 'Asset', 'type' => 'Type',
- 'power_watts' => 'Power (W)', 'full_depth' => 'Full depth', 'notes' => 'Notes',
+ 'power_watts' => 'Power (W)', 'full_depth' => 'Full depth', 'notes' => 'Notes', 'holder' => 'In holder', 'slot' => 'Slot',
]],
'licenses' => ['section' => 'software', 'label' => 'Licenses', 'columns' => [
'id' => 'ID', 'name' => 'Name', 'version' => 'Version', 'type' => 'Type', 'license_type' => 'License type', 'key' => 'License key', 'seats' => 'Seats',
@@ -616,12 +616,16 @@ function clientExportRows($dataset, $client_id, $options) {
break;
case 'rack_units':
- foreach ($fetch("SELECT rack_units.*, rack_name FROM rack_units JOIN racks ON rack_id = unit_rack_id
- WHERE rack_client_id = $client_id" . $live('rack_archived_at') . $live('unit_archived_at') . " ORDER BY rack_name, unit_face, unit_start_number") as $row) {
+ foreach ($fetch("SELECT rack_units.*, rack_name, holder.unit_device AS holder_device, holder.unit_type AS holder_type, holder.unit_asset_id AS holder_asset_id
+ FROM rack_units JOIN racks ON rack_id = rack_units.unit_rack_id
+ LEFT JOIN rack_units holder ON holder.unit_id = rack_units.unit_parent_id
+ WHERE rack_client_id = $client_id" . $live('rack_archived_at') . $live('rack_units.unit_archived_at') . " ORDER BY rack_name, rack_units.unit_face, rack_units.unit_start_number, rack_units.unit_position") as $row) {
$asset = $name('asset', $row['unit_asset_id']);
+ $holder = $row['unit_parent_id'] ? rackUnitName($name('asset', $row['holder_asset_id']), $row['holder_device'], $row['holder_type']) : '';
$rows[] = ['rack' => $row['rack_name'], 'from_unit' => $row['unit_start_number'], 'to_unit' => $row['unit_end_number'], 'face' => ucfirst((string) $row['unit_face']),
'device' => $row['unit_device'] ?: $asset, 'asset' => $asset, 'type' => $row['unit_type'], 'power_watts' => $row['unit_power_watts'] ?: '',
- 'full_depth' => clientExportYes($row['unit_full_depth']), 'notes' => $row['unit_notes'], '_rack_id' => intval($row['unit_rack_id'])];
+ 'full_depth' => clientExportYes($row['unit_full_depth']), 'notes' => $row['unit_notes'], 'holder' => $holder, 'slot' => $holder !== '' ? intval($row['unit_position']) : '',
+ '_rack_id' => intval($row['unit_rack_id'])];
}
break;
@@ -1356,7 +1360,7 @@ function clientExportPdf($client_id, $options) {
$list = $units[$rack['name']] ?? [];
usort($list, function ($a, $b) { return intval($b['from_unit']) <=> intval($a['from_unit']); });
$write($list ? clientExportPdfTable(['Units' => 12, 'Face' => 10, 'Device' => 38, 'Type' => 16, 'Power' => 10, 'Notes' => 14], array_map(function ($u) use ($e) {
- $span = intval($u['from_unit']) === intval($u['to_unit']) ? 'U' . $u['from_unit'] : 'U' . $u['from_unit'] . '-' . $u['to_unit'];
+ $span = (intval($u['from_unit']) === intval($u['to_unit']) ? 'U' . $u['from_unit'] : 'U' . $u['from_unit'] . '-' . $u['to_unit']) . ($u['holder'] !== '' ? ', slot ' . $u['slot'] : '');
return [$e($span), $e($u['face']), '' . $e($u['device']) . ' ', $e($u['type']), $e($u['power_watts'] ? $u['power_watts'] . ' W' : ''), $e($u['notes'])];
}, $list)) : 'Nothing documented in this rack. ');
}
diff --git a/functions/rack.php b/functions/rack.php
index 8cbaaa303..2e6b4782a 100644
--- a/functions/rack.php
+++ b/functions/rack.php
@@ -12,8 +12,23 @@
* Faces: an item is mounted on the front or the rear. A full-depth item (most
* servers, a UPS) fills both faces at its units; a half-depth one (a patch
* panel, a small switch) leaves the other face free for something else.
+ *
+ * Holders: an item with unit_slots holds devices side by side - two Tiny PCs in
+ * a 1U bracket, eight PLCs on a shelf. Each device is an item of its own with
+ * unit_parent_id set to the holder and unit_position its slot: 1 is the first,
+ * counting across and then a row on, so devices past unit_slots wrap onto
+ * another row (stacks). Rows count the way the rack's units do - the first at
+ * the bottom, or at the top when U1 is. A device keeps its holder's units, face
+ * and depth in its own columns (rackSyncChildren() after the holder moves), so
+ * whatever reads where an asset is racked reads it the same way - but only the
+ * holder takes space in the rack: occupancy and clashes skip what is in one.
+ * Holders don't go in holders. Removing a holder removes what is in it (the
+ * foreign key cascades); the assets themselves stay.
*/
+/** The most devices a holder takes side by side */
+const RACK_SLOTS_MAX = 24;
+
/** Unlinked rack items - things worth documenting that are not assets. */
function rackItemTypes() {
return [
@@ -92,6 +107,10 @@ function rackOccupancy(array $units, $rack_units) {
$rear = $front;
foreach ($units as $unit) {
+ // A device in a holder is in the holder's units already
+ if (intval($unit['unit_parent_id'] ?? 0)) {
+ continue;
+ }
$start = max(1, intval($unit['unit_start_number']));
$end = min($rack_units, intval($unit['unit_end_number']));
$face = ($unit['unit_face'] ?? 'front') === 'rear' ? 'rear' : 'front';
@@ -152,7 +171,7 @@ function rackGridRow($start, $end, $rack_units, $descending) {
* The first item that would clash with an item at $start..$end on $face, or
* null if the space is free. Two items clash when their units overlap and they
* share a face - which a full-depth item always does. $exclude_unit_id skips
- * the item being moved.
+ * the item being moved. Devices in a holder never clash - their holder does.
*
* Returns the clashing item's display name and position, raw (escape it).
*/
@@ -171,6 +190,7 @@ function rackUnitConflict($rack_id, $start, $end, $face, $full_depth, $exclude_u
LEFT JOIN assets ON asset_id = unit_asset_id
WHERE unit_rack_id = $rack_id
AND unit_id != $exclude_unit_id
+ AND unit_parent_id IS NULL
AND unit_start_number <= $end AND unit_end_number >= $start
AND (unit_full_depth = 1 OR $full_depth = 1 OR unit_face = '$face')
ORDER BY unit_start_number
@@ -253,7 +273,7 @@ function rackLoadUnits(array $rack_ids) {
}
$sql = mysqli_query($mysqli, "SELECT unit_id, unit_rack_id, unit_start_number, unit_end_number, unit_device,
- unit_face, unit_full_depth, unit_type, unit_power_watts, unit_notes,
+ unit_face, unit_full_depth, unit_type, unit_power_watts, unit_notes, unit_slots, unit_parent_id, unit_position,
asset_id, asset_name, asset_type, asset_make, asset_model, asset_serial, asset_status, asset_archived_at,
(SELECT interface_ip FROM asset_interfaces
WHERE interface_asset_id = asset_id AND interface_primary = 1 AND interface_ip IS NOT NULL AND interface_ip != ''
@@ -261,7 +281,7 @@ function rackLoadUnits(array $rack_ids) {
FROM rack_units
LEFT JOIN assets ON asset_id = unit_asset_id
WHERE unit_rack_id IN (" . implode(',', $rack_ids) . ")
- ORDER BY unit_start_number DESC");
+ ORDER BY unit_start_number DESC, unit_position");
while ($row = mysqli_fetch_assoc($sql)) {
$units[intval($row['unit_rack_id'])][] = $row;
@@ -323,9 +343,234 @@ function rackUnitView(array $row) {
'tone' => $style['tone'],
'type_label' => escapeHtml($style['label']),
'position' => rackPositionLabel($start, $end),
+ 'slots' => intval($row['unit_parent_id'] ?? 0) ? 0 : max(0, intval($row['unit_slots'] ?? 0)),
+ 'parent_id' => intval($row['unit_parent_id'] ?? 0),
+ 'slot' => intval($row['unit_parent_id'] ?? 0) ? max(1, intval($row['unit_position'] ?? 0)) : 0,
];
}
+/**
+ * rackUnitView() items as they are drawn: the items in the rack itself, each
+ * holder with what is in it as 'children', in slot order. A device whose holder
+ * isn't among them (it can't be - the key cascades) is left out rather than
+ * drawn loose.
+ */
+function rackNestItems(array $items) {
+ $children = [];
+ foreach ($items as $item) {
+ if ($item['parent_id']) {
+ $children[$item['parent_id']][] = $item;
+ }
+ }
+ $nested = [];
+ foreach ($items as $item) {
+ if ($item['parent_id']) {
+ continue;
+ }
+ $kids = $children[$item['id']] ?? [];
+ usort($kids, function ($a, $b) {
+ return $a['slot'] <=> $b['slot'];
+ });
+ $nested[] = $item + ['children' => $kids];
+ }
+ return $nested;
+}
+
+/** How many rows a holder with $slots across draws, its highest slot in use being $highest */
+function rackHolderRows($slots, $highest) {
+ $slots = max(1, intval($slots));
+ return max(1, intval(ceil(max(0, intval($highest)) / $slots)));
+}
+
+/** The highest slot a device can go in: the holder's rows so far, and one row more */
+function rackSlotLimit($slots, $highest) {
+ $slots = max(1, intval($slots));
+ return (rackHolderRows($slots, $highest) + 1) * $slots;
+}
+
+/**
+ * A holder's slots as they are drawn: each ['slot', 'column', 'row' (the CSS
+ * grid row), 'item' (the device there, or null)], in slot order. Rows count
+ * the way the rack's units do - slot 1 is at the bottom left unless U1 is at
+ * the top.
+ */
+function rackHolderCells(array $holder, $descending) {
+ $slots = max(1, intval($holder['slots']));
+ $by_slot = [];
+ foreach ($holder['children'] ?? [] as $child) {
+ $by_slot[$child['slot']] = $child;
+ }
+ $rows = rackHolderRows($slots, $by_slot ? max(array_keys($by_slot)) : 0);
+ $cells = [];
+ for ($slot = 1; $slot <= $rows * $slots; $slot++) {
+ $row = intdiv($slot - 1, $slots);
+ $cells[] = [
+ 'slot' => $slot,
+ 'column' => ($slot - 1) % $slots + 1,
+ 'row' => $descending ? $row + 1 : $rows - $row,
+ 'item' => $by_slot[$slot] ?? null,
+ ];
+ }
+ return ['rows' => $rows, 'columns' => $slots, 'cells' => $cells];
+}
+
+/** How many devices are in a holder */
+function rackChildCount($unit_id) {
+ global $mysqli;
+ return intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM rack_units WHERE unit_parent_id = " . intval($unit_id)))[0]);
+}
+
+/** What a holder is called (rackUnitName()), raw - '' when it is gone */
+function rackHolderName($unit_id) {
+ global $mysqli;
+ $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT unit_device, unit_type, asset_name FROM rack_units
+ LEFT JOIN assets ON asset_id = unit_asset_id WHERE unit_id = " . intval($unit_id)));
+ return $row ? rackUnitName($row['asset_name'], $row['unit_device'], $row['unit_type']) : '';
+}
+
+/** "1 device" / "3 devices" */
+function rackDeviceCount($count) {
+ $count = intval($count);
+ return $count === 1 ? '1 device' : "$count devices";
+}
+
+/**
+ * Where an item would go, checked the way Add and Edit check it - the rack item
+ * form, a drag, Undo and the API all come through here, so they can't drift
+ * apart. $want:
+ *
+ * holder the holder (unit id) to put it in, 0 for the rack itself
+ * position its slot in the holder - 0 for the first free one
+ * start, end, face, full_depth where in the rack, when it isn't in a holder
+ * slots how many it holds side by side, 0 when it holds nothing
+ *
+ * Returns ['error' => why] (HTML - names are in , tags stripped) or
+ * where it goes: holder, position, start, end, face, full_depth, slots and
+ * holder_name (raw). rackPlacementSet() turns that into the SET.
+ */
+function rackPlacement($rack_id, $unit_id, array $want) {
+ global $mysqli;
+
+ $rack_id = intval($rack_id);
+ $unit_id = intval($unit_id);
+ $holder_id = intval($want['holder'] ?? 0);
+ $slots = max(0, min(RACK_SLOTS_MAX, intval($want['slots'] ?? 0)));
+ $children = $unit_id ? rackChildCount($unit_id) : 0;
+ $strong = function ($name) {
+ return '' . strip_tags((string) $name) . ' ';
+ };
+
+ if ($holder_id) {
+ if ($holder_id === $unit_id) {
+ return ['error' => "An item can't go in itself"];
+ }
+ $holder = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT unit_id, unit_rack_id, unit_slots, unit_parent_id, unit_start_number, unit_end_number,
+ unit_face, unit_full_depth, unit_device, unit_type, asset_name
+ FROM rack_units
+ LEFT JOIN assets ON asset_id = unit_asset_id
+ WHERE unit_id = $holder_id AND unit_rack_id = $rack_id"));
+ if (!$holder) {
+ return ['error' => "That holder isn't in this rack any more"];
+ }
+ $holder_name = rackUnitName($holder['asset_name'], $holder['unit_device'], $holder['unit_type']);
+ $holder_slots = intval($holder['unit_slots']);
+ if ($holder_slots < 1 || intval($holder['unit_parent_id'])) {
+ return ['error' => $strong($holder_name) . " doesn't hold devices"];
+ }
+ if ($children) {
+ return ['error' => 'It holds ' . rackDeviceCount($children) . " of its own - a holder can't go in another holder"];
+ }
+
+ $taken = [];
+ $sql = mysqli_query($mysqli, "SELECT unit_position, unit_device, unit_type, asset_name FROM rack_units
+ LEFT JOIN assets ON asset_id = unit_asset_id
+ WHERE unit_parent_id = $holder_id AND unit_id != $unit_id");
+ while ($row = mysqli_fetch_assoc($sql)) {
+ $taken[intval($row['unit_position'])] = rackUnitName($row['asset_name'], $row['unit_device'], $row['unit_type']);
+ }
+ $limit = rackSlotLimit($holder_slots, $taken ? max(array_keys($taken)) : 0);
+
+ $position = intval($want['position'] ?? 0);
+ if ($position < 1) {
+ $position = 1;
+ while (isset($taken[$position])) {
+ $position++;
+ }
+ }
+ if ($position > $limit) {
+ return ['error' => $strong($holder_name) . " has slots 1 to $limit - pick one of those"];
+ }
+ if (isset($taken[$position])) {
+ return ['error' => "Slot $position in " . $strong($holder_name) . ' is taken by ' . $strong($taken[$position])];
+ }
+
+ return [
+ 'holder' => $holder_id,
+ 'position' => $position,
+ 'start' => intval($holder['unit_start_number']),
+ 'end' => intval($holder['unit_end_number']),
+ 'face' => $holder['unit_face'] === 'rear' ? 'rear' : 'front',
+ 'full_depth' => intval($holder['unit_full_depth']) === 1 ? 1 : 0,
+ 'slots' => 0,
+ 'holder_name' => $holder_name,
+ ];
+ }
+
+ $rack_units = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT rack_units FROM racks WHERE rack_id = $rack_id"))[0] ?? 0);
+ $start = intval($want['start'] ?? 0);
+ $end = max($start, intval($want['end'] ?? $start));
+ $face = ($want['face'] ?? 'front') === 'rear' ? 'rear' : 'front';
+ $full_depth = intval($want['full_depth'] ?? 0) === 1 ? 1 : 0;
+
+ if ($start < 1 || $end > $rack_units) {
+ return ['error' => "That doesn't fit - this rack runs from U1 to U$rack_units"];
+ }
+
+ $conflict = rackUnitConflict($rack_id, $start, $end, $face, $full_depth, $unit_id);
+ if ($conflict) {
+ return ['error' => html_entity_decode(rackPositionLabel($start, $end)) . " on the $face is taken by " . $strong($conflict['name']) . ' (' . html_entity_decode(rackPositionLabel($conflict['start'], $conflict['end'])) . ')'];
+ }
+
+ if ($children && $slots < 1) {
+ return ['error' => 'It holds ' . rackDeviceCount($children) . ' - take ' . ($children === 1 ? 'it' : 'them') . ' out before it stops holding devices'];
+ }
+
+ return [
+ 'holder' => 0,
+ 'position' => 0,
+ 'start' => $start,
+ 'end' => $end,
+ 'face' => $face,
+ 'full_depth' => $full_depth,
+ 'slots' => $slots,
+ 'holder_name' => '',
+ ];
+}
+
+/** The SET for where rackPlacement() says an item goes - not its slots, which only Add and Edit change */
+function rackPlacementSet(array $place) {
+ return "unit_parent_id = " . (intval($place['holder']) ?: 'NULL') . ", unit_position = " . intval($place['position']) . ",
+ unit_start_number = " . intval($place['start']) . ", unit_end_number = " . intval($place['end']) . ",
+ unit_face = '" . ($place['face'] === 'rear' ? 'rear' : 'front') . "', unit_full_depth = " . (intval($place['full_depth']) ? 1 : 0);
+}
+
+/** What is in a holder takes its units, face and depth again - after the holder moves or changes */
+function rackSyncChildren($holder_id) {
+ global $mysqli;
+ $holder_id = intval($holder_id);
+ mysqli_query($mysqli, "UPDATE rack_units child
+ JOIN rack_units holder ON holder.unit_id = child.unit_parent_id
+ SET child.unit_start_number = holder.unit_start_number, child.unit_end_number = holder.unit_end_number,
+ child.unit_face = holder.unit_face, child.unit_full_depth = holder.unit_full_depth, child.unit_rack_id = holder.unit_rack_id
+ WHERE holder.unit_id = $holder_id");
+}
+
+/** " - slot 2 in Tiny bracket", raw, for where an asset is racked - '' for one in the rack itself */
+function rackSlotText($holder_name, $position) {
+ $holder_name = trim((string) $holder_name);
+ return $holder_name !== '' ? ' - slot ' . intval($position) . " in $holder_name" : '';
+}
+
/**
* How an asset of this type usually goes in a rack, to start the Add window
* from: ['height' => U, 'full_depth' => 0|1]. A server, NAS or UPS takes 2U;
@@ -339,16 +584,28 @@ function rackAssetDefaults($asset_type) {
];
}
-/** Where an asset is racked: the rack and its position, or null. Raw values. */
+/**
+ * Where an asset is racked: the rack and its position, or null. Raw values. In
+ * a holder, holder_name is the holder and unit_position its slot there.
+ */
function rackPositionForAsset($asset_id) {
global $mysqli;
$asset_id = intval($asset_id);
- $sql = mysqli_query($mysqli, "SELECT rack_id, rack_name, rack_archived_at, unit_start_number, unit_end_number, unit_face
+ $sql = mysqli_query($mysqli, "SELECT rack_id, rack_name, rack_archived_at, rack_units.unit_start_number, rack_units.unit_end_number,
+ rack_units.unit_face, rack_units.unit_position, holder.unit_id AS holder_id, holder.unit_device AS holder_device,
+ holder.unit_type AS holder_type, holder_asset.asset_name AS holder_asset_name
FROM rack_units
- JOIN racks ON rack_id = unit_rack_id
- WHERE unit_asset_id = $asset_id
+ JOIN racks ON rack_id = rack_units.unit_rack_id
+ LEFT JOIN rack_units holder ON holder.unit_id = rack_units.unit_parent_id
+ LEFT JOIN assets holder_asset ON holder_asset.asset_id = holder.unit_asset_id
+ WHERE rack_units.unit_asset_id = $asset_id
LIMIT 1");
- return mysqli_fetch_assoc($sql) ?: null;
+ $row = mysqli_fetch_assoc($sql);
+ if (!$row) {
+ return null;
+ }
+ $row['holder_name'] = $row['holder_id'] ? rackUnitName($row['holder_asset_name'], $row['holder_device'], $row['holder_type']) : '';
+ return $row;
}
diff --git a/functions/undo.php b/functions/undo.php
index cf0a387f6..d4a43f708 100644
--- a/functions/undo.php
+++ b/functions/undo.php
@@ -17,6 +17,8 @@
* ['recurring_on', recurring invoice id] a recurring invoice the change stopped, running again
* ['folder', folder id, parent before, parent after, client id] a folder back under its parent
* ['rack', unit id, start, end, face, start after, face after] a rack item back where it was, if that's free
+ * ['rack_place', unit id, [holder, slot, start, end, face, full depth] before, [holder, slot] after]
+ * a rack item back in the slot - or the units - it came from
* ['ticket_priority', ticket id, before, after] through ticketChangePriority(), as the picker
* ['ticket_category', ticket id, before, after] with the ticket's history line, as the bulk change
* ['ticket_project', ticket id, before, after] the same
@@ -386,9 +388,30 @@ function undoStep(array $step) {
return false;
}
mysqli_query($mysqli, "UPDATE rack_units SET unit_start_number = " . intval($start) . ", unit_end_number = " . intval($end) . ", unit_face = " . undoSqlValue($face) . " WHERE unit_id = " . intval($unit_id));
+ rackSyncChildren($unit_id);
logAudit("Rack", "Edit", "$session_name moved an item back to " . html_entity_decode(rackPositionLabel($start, $end)) . " ($face)", intval($row['rack_client_id']), intval($row['unit_rack_id']));
return true;
+ case 'rack_place':
+ [, $unit_id, $before, $after] = $step;
+ $row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT unit_rack_id, unit_parent_id, unit_position, rack_client_id, rack_archived_at FROM rack_units
+ JOIN racks ON rack_id = unit_rack_id WHERE unit_id = " . intval($unit_id)));
+ if (!$row || $row['rack_archived_at'] || !hasClientAccess($row['rack_client_id'])
+ || intval($row['unit_parent_id']) !== intval($after[0]) || intval($row['unit_position']) !== intval($after[1])) {
+ return false;
+ }
+ [$holder, $slot, $start, $end, $face, $full_depth] = $before;
+ $place = rackPlacement(intval($row['unit_rack_id']), $unit_id, ['holder' => $holder, 'position' => max(1, intval($slot)),
+ 'start' => $start, 'end' => $end, 'face' => $face, 'full_depth' => $full_depth]);
+ if (isset($place['error'])) {
+ return false;
+ }
+ mysqli_query($mysqli, "UPDATE rack_units SET " . rackPlacementSet($place) . " WHERE unit_id = " . intval($unit_id));
+ logAudit("Rack", "Edit", "$session_name moved an item back " . escapeSql($place['holder']
+ ? "to slot {$place['position']} of {$place['holder_name']}"
+ : 'to ' . html_entity_decode(rackPositionLabel($place['start'], $place['end'])) . " ({$place['face']})"), intval($row['rack_client_id']), intval($row['unit_rack_id']));
+ return true;
+
case 'ticket_priority':
[, $ticket_id, $before, $after] = $step;
$row = ticketFieldsRow($ticket_id);