Racks: devices side by side in a holder - a rack item that holds devices across and wraps into rows for stacks, with slots to add to, drag between and Undo, the holder moving and removing what is in it, the slot shown on assets, the export and the API, and database 4.1.3 for rack_units slots, holder and position

This commit is contained in:
johnnyq
2026-11-22 14:50:45 -05:00
parent bea12a54f9
commit bb20447b48
25 changed files with 1002 additions and 100 deletions

View File

@@ -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
*/

View File

@@ -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);
}

View File

@@ -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']})";

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -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.");
}