221 lines
4.9 KiB
PHP
221 lines
4.9 KiB
PHP
<?php
|
|
/**
|
|
* modules/devices/save.php
|
|
*
|
|
* Speichert / aktualisiert ein Gerät
|
|
* - Basisdaten
|
|
* - Rack-Zuordnung
|
|
* - Ports (automatisch aus Device-Type oder manuell)
|
|
*
|
|
* Erwartet POST (form-data ODER JSON)
|
|
*/
|
|
|
|
require_once __DIR__ . '/../../bootstrap.php';
|
|
|
|
// =========================
|
|
// Request prüfen
|
|
// =========================
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['error' => 'Invalid request method']);
|
|
exit;
|
|
}
|
|
|
|
// =========================
|
|
// Daten einlesen (JSON oder POST)
|
|
// =========================
|
|
|
|
$contentType = $_SERVER['CONTENT_TYPE'] ?? '';
|
|
|
|
if (str_contains($contentType, 'application/json')) {
|
|
$data = json_decode(file_get_contents('php://input'), true) ?? [];
|
|
} else {
|
|
$data = $_POST;
|
|
}
|
|
|
|
// =========================
|
|
// Basisfelder
|
|
// =========================
|
|
|
|
$deviceId = isset($data['id']) ? (int)$data['id'] : null;
|
|
$name = trim($data['name'] ?? '');
|
|
$comment = trim($data['comment'] ?? '');
|
|
$deviceTypeId = (int)($data['device_type_id'] ?? 0);
|
|
$rackId = isset($data['rack_id']) ? (int)$data['rack_id'] : null;
|
|
$rackPositionHe = isset($data['rack_position_he']) ? (int)$data['rack_position_he'] : null;
|
|
$rackHeightHe = isset($data['rack_height_he']) ? (int)$data['rack_height_he'] : null;
|
|
$serialNumber = trim($data['serial_number'] ?? '');
|
|
$portsData = $data['ports'] ?? null;
|
|
|
|
// =========================
|
|
// Validierung
|
|
// =========================
|
|
|
|
$errors = [];
|
|
|
|
if ($name === '') {
|
|
$errors[] = 'Name darf nicht leer sein';
|
|
}
|
|
|
|
$deviceType = $sql->single(
|
|
"SELECT * FROM device_types WHERE id = ?",
|
|
"i",
|
|
[$deviceTypeId]
|
|
);
|
|
|
|
if (!$deviceType) {
|
|
$errors[] = 'Ungültiger Gerätetyp';
|
|
}
|
|
|
|
if ($rackId !== null) {
|
|
$rack = $sql->single(
|
|
"SELECT * FROM racks WHERE id = ?",
|
|
"i",
|
|
[$rackId]
|
|
);
|
|
|
|
if (!$rack) {
|
|
$errors[] = 'Ungültiges Rack';
|
|
} elseif ($rackHeightHe !== null && $rackPositionHe !== null) {
|
|
if ($rackPositionHe < 1 || ($rackPositionHe + $rackHeightHe - 1) > $rack['height_he']) {
|
|
$errors[] = 'Gerät passt nicht ins Rack';
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($errors) {
|
|
http_response_code(400);
|
|
echo json_encode([
|
|
'status' => 'error',
|
|
'errors' => $errors
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
// =========================
|
|
// Device speichern
|
|
// =========================
|
|
|
|
if ($deviceId) {
|
|
|
|
$sql->set(
|
|
"
|
|
UPDATE devices SET
|
|
name = ?,
|
|
comment = ?,
|
|
device_type_id = ?,
|
|
rack_id = ?,
|
|
rack_position_he = ?,
|
|
rack_height_he = ?,
|
|
serial_number = ?
|
|
WHERE id = ?
|
|
",
|
|
"ssiiii si",
|
|
[
|
|
$name,
|
|
$comment,
|
|
$deviceTypeId,
|
|
$rackId,
|
|
$rackPositionHe,
|
|
$rackHeightHe,
|
|
$serialNumber,
|
|
$deviceId
|
|
]
|
|
);
|
|
|
|
} else {
|
|
|
|
$deviceId = $sql->set(
|
|
"
|
|
INSERT INTO devices
|
|
(name, comment, device_type_id, rack_id, rack_position_he, rack_height_he, serial_number)
|
|
VALUES
|
|
(?, ?, ?, ?, ?, ?, ?)
|
|
",
|
|
"ssiiiii",
|
|
[
|
|
$name,
|
|
$comment,
|
|
$deviceTypeId,
|
|
$rackId,
|
|
$rackPositionHe,
|
|
$rackHeightHe,
|
|
$serialNumber
|
|
],
|
|
true
|
|
);
|
|
|
|
// =========================
|
|
// Ports vom Device-Type übernehmen (nur bei NEU)
|
|
// =========================
|
|
|
|
$typePorts = $sql->get(
|
|
"
|
|
SELECT name, port_type_id
|
|
FROM device_type_ports
|
|
WHERE device_type_id = ?
|
|
ORDER BY id
|
|
",
|
|
"i",
|
|
[$deviceTypeId]
|
|
);
|
|
|
|
foreach ($typePorts as $tp) {
|
|
$sql->set(
|
|
"
|
|
INSERT INTO device_ports
|
|
(device_id, name, port_type_id)
|
|
VALUES
|
|
(?, ?, ?)
|
|
",
|
|
"isi",
|
|
[
|
|
$deviceId,
|
|
$tp['name'],
|
|
$tp['port_type_id']
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
// =========================
|
|
// Ports aktualisieren (optional, z. B. VLAN / Mode)
|
|
// =========================
|
|
|
|
if (is_array($portsData)) {
|
|
foreach ($portsData as $portId => $port) {
|
|
|
|
$status = $port['status'] ?? 'active';
|
|
$mode = $port['mode'] ?? null;
|
|
$vlan = isset($port['vlan_config']) ? json_encode($port['vlan_config']) : null;
|
|
|
|
$sql->set(
|
|
"
|
|
UPDATE device_ports SET
|
|
status = ?,
|
|
mode = ?,
|
|
vlan_config = ?
|
|
WHERE id = ? AND device_id = ?
|
|
",
|
|
"sssii",
|
|
[
|
|
$status,
|
|
$mode,
|
|
$vlan,
|
|
(int)$portId,
|
|
$deviceId
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
// =========================
|
|
// Antwort
|
|
// =========================
|
|
|
|
echo json_encode([
|
|
'status' => 'ok',
|
|
'id' => $deviceId
|
|
]);
|