feat: Formular-URLs in edit.php und save.php aktualisiert, Filter- und Validierungslogik in list.php hinzugefügt

This commit is contained in:
Troy Grunt
2026-02-07 23:16:40 +01:00
parent de352e0335
commit 2f341bff9f
3 changed files with 365 additions and 118 deletions

View File

@@ -36,7 +36,7 @@ $ports = []; // TODO: Ports vorbereiten
<h2>Gerät bearbeiten</h2> <h2>Gerät bearbeiten</h2>
<form method="post" action="/app/devices/save.php" enctype="multipart/form-data"> <form method="post" action="/devices/save" enctype="multipart/form-data">
<!-- ========================= <!-- =========================
Basisdaten Basisdaten

View File

@@ -1,111 +1,242 @@
<?php <?php
/** /**
* app/devices/list.php * modules/devices/list.php
* * Vollständige Geräteübersicht
* Übersicht aller Geräte
* - Anzeigen
* - Bearbeiten
* - Löschen
* - SVG / JPG Vorschau
*/ */
// TODO: bootstrap laden
// require_once __DIR__ . '/../../bootstrap.php';
// TODO: Auth erzwingen
// requireAuth();
// ========================= // =========================
// Geräte laden // Filter / Suche einlesen
// ========================= // =========================
// TODO: Geräte aus DB laden $search = trim($_GET['search'] ?? '');
// $devices = $sql->get("SELECT * FROM devices ORDER BY name", "", []); $typeId = (int)($_GET['type_id'] ?? 0);
$locationId = (int)($_GET['location_id'] ?? 0);
$floorId = (int)($_GET['floor_id'] ?? 0);
$rackId = (int)($_GET['rack_id'] ?? 0);
// =========================
// WHERE-Clause dynamisch bauen
// =========================
$where = [];
$types = '';
$params = [];
if ($search !== '') {
$where[] = "(d.name LIKE ? OR d.serial_number LIKE ? OR dt.name LIKE ?)";
$types .= "sss";
$params[] = "%$search%";
$params[] = "%$search%";
$params[] = "%$search%";
}
if ($typeId > 0) {
$where[] = "dt.id = ?";
$types .= "i";
$params[] = $typeId;
}
if ($locationId > 0) {
$where[] = "l.id = ?";
$types .= "i";
$params[] = $locationId;
}
if ($floorId > 0) {
$where[] = "f.id = ?";
$types .= "i";
$params[] = $floorId;
}
if ($rackId > 0) {
$where[] = "r.id = ?";
$types .= "i";
$params[] = $rackId;
}
$whereSql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
// =========================
// Geräte laden (inkl. Status-Aggregate)
// =========================
$devices = $sql->get(
"
SELECT
d.id,
d.name,
d.serial_number,
d.rack_position_he,
d.rack_height_he,
dt.name AS device_type,
dt.image_path,
dt.image_type,
l.name AS location_name,
f.name AS floor_name,
r.name AS rack_name,
COUNT(dp.id) AS total_ports,
SUM(dp.status = 'active') AS active_ports,
SUM(c.id IS NOT NULL) AS connected_ports
FROM devices d
JOIN device_types dt ON dt.id = d.device_type_id
LEFT JOIN racks r ON r.id = d.rack_id
LEFT JOIN floors f ON f.id = r.floor_id
LEFT JOIN buildings b ON b.id = f.building_id
LEFT JOIN locations l ON l.id = b.location_id
LEFT JOIN device_ports dp ON dp.device_id = d.id
LEFT JOIN connections c
ON (c.port_a_type = 'device' AND c.port_a_id = dp.id)
OR (c.port_b_type = 'device' AND c.port_b_id = dp.id)
$whereSql
GROUP BY d.id
ORDER BY l.name, f.level, r.name, d.rack_position_he, d.name
",
$types,
$params
);
// =========================
// Filter-Daten laden
// =========================
$deviceTypes = $sql->get("SELECT id, name FROM device_types ORDER BY name", "", []);
$locations = $sql->get("SELECT id, name FROM locations ORDER BY name", "", []);
$floors = $sql->get("SELECT id, name FROM floors ORDER BY level", "", []);
$racks = $sql->get("SELECT id, name FROM racks ORDER BY name", "", []);
?> ?>
<h2>Geräte</h2> <h2>Geräte</h2>
<!-- ========================= <form method="get" class="toolbar">
Toolbar <input type="hidden" name="module" value="devices">
========================= --> <input type="hidden" name="action" value="list">
<div class="toolbar"> <input type="text" name="search" placeholder="Suche…" value="<?= htmlspecialchars($search) ?>">
<a href="/?page=devices/edit" class="button">
<select name="type_id">
<option value="">Gerätetyp</option>
<?php foreach ($deviceTypes as $t): ?>
<option value="<?= $t['id'] ?>" <?= $t['id'] === $typeId ? 'selected' : '' ?>>
<?= htmlspecialchars($t['name']) ?>
</option>
<?php endforeach; ?>
</select>
<select name="location_id">
<option value="">Standort</option>
<?php foreach ($locations as $l): ?>
<option value="<?= $l['id'] ?>" <?= $l['id'] === $locationId ? 'selected' : '' ?>>
<?= htmlspecialchars($l['name']) ?>
</option>
<?php endforeach; ?>
</select>
<select name="floor_id">
<option value="">Floor</option>
<?php foreach ($floors as $f): ?>
<option value="<?= $f['id'] ?>" <?= $f['id'] === $floorId ? 'selected' : '' ?>>
<?= htmlspecialchars($f['name']) ?>
</option>
<?php endforeach; ?>
</select>
<select name="rack_id">
<option value="">Rack</option>
<?php foreach ($racks as $r): ?>
<option value="<?= $r['id'] ?>" <?= $r['id'] === $rackId ? 'selected' : '' ?>>
<?= htmlspecialchars($r['name']) ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit">Filtern</button>
<a href="/devices/edit" class="button">
+ Neues Gerät + Neues Gerät
</a> </a>
</form>
<!-- TODO: Suchfeld --> <?php if ($devices): ?>
<!-- TODO: Filter (Device-Type, Standort, Floor, Rack) -->
</div>
<!-- =========================
Geräte-Tabelle
========================= -->
<table class="device-list"> <table class="device-list">
<thead> <thead>
<tr> <tr>
<th>Vorschau</th> <th>Vorschau</th>
<th>Name</th> <th>Name</th>
<th>Gerätetyp</th> <th>Typ</th>
<th>Standort</th> <th>Standort</th>
<th>Rack</th> <th>Rack</th>
<th>Rack-Pos</th> <th>HE</th>
<th>Ports</th> <th>Ports</th>
<th>Aktionen</th> <th>Aktionen</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php /* foreach ($devices as $device): */ ?> <?php foreach ($devices as $d):
$freePorts = max(0, $d['total_ports'] - $d['connected_ports']);
?>
<tr> <tr>
<td class="preview"> <td>
<!-- TODO: SVG / JPG Thumbnail --> <?php if ($d['image_path']): ?>
<img src="<?= htmlspecialchars($d['image_path']) ?>" class="thumb">
<?php else: ?>
<?php endif; ?>
</td> </td>
<td> <td>
<!-- TODO: Name --> <strong><?= htmlspecialchars($d['name']) ?></strong><br>
Gerät XY <small><?= htmlspecialchars($d['serial_number'] ?? '') ?></small>
</td>
<td><?= htmlspecialchars($d['device_type']) ?></td>
<td>
<?= htmlspecialchars($d['location_name'] ?? '—') ?><br>
<small><?= htmlspecialchars($d['floor_name'] ?? '') ?></small>
</td>
<td><?= htmlspecialchars($d['rack_name'] ?? '—') ?></td>
<td>
<?= htmlspecialchars($d['rack_position_he'] ?? '—') ?>
<?php if ($d['rack_height_he']): ?>
<?= $d['rack_position_he'] + $d['rack_height_he'] - 1 ?>
<?php endif; ?>
</td> </td>
<td> <td>
<!-- TODO: Gerätetyp --> <?= (int)$d['connected_ports'] ?>/<?= (int)$d['total_ports'] ?>
<br>
<small><?= $freePorts ?> frei</small>
</td> </td>
<td> <td class="actions">
<!-- TODO: Standort / Floor --> <a href="/devices/ports?id=<?= $d['id'] ?>">Ports</a>
</td> <a href="/devices/edit?id=<?= $d['id'] ?>">Bearbeiten</a>
<a href="/devices/delete?id=<?= $d['id'] ?>"
<td> onclick="return confirm('Gerät wirklich löschen?')">
<!-- TODO: Rack --> Löschen
</td> </a>
<td>
<!-- TODO: Rack-Position -->
</td>
<td>
<!-- TODO: Anzahl Ports -->
</td>
<td>
<a href="/?page=devices/edit&id=1">Bearbeiten</a>
<button> Löschen </button>
</td> </td>
</tr> </tr>
<?php /* endforeach; */ ?> <?php endforeach; ?>
</tbody> </tbody>
</table> </table>
<!-- ========================= <?php else: ?>
Leerer Zustand
========================= -->
<?php /* if (empty($devices)): */ ?>
<div class="empty-state"> <div class="empty-state">
<p>Noch keine Geräte angelegt.</p> <p>Keine Geräte gefunden.</p>
<p><a href="/?page=devices/edit">Erstes Gerät anlegen</a></p>
</div> </div>
<?php /* endif; */ ?>
<?php endif; ?>

View File

@@ -1,22 +1,16 @@
<?php <?php
/** /**
* app/devices/save.php * modules/devices/save.php
* *
* Speichert: * Speichert / aktualisiert ein Gerät
* - Basisdaten (Name, Beschreibung) * - Basisdaten
* - Gerätetyp-Zuordnung * - Rack-Zuordnung
* - Standort (Floor, Rack, Rack-Position) * - Ports (automatisch aus Device-Type oder manuell)
* - Ports (vom Device-Type übernommen)
* - SVG-Positionen
* *
* POST JSON oder multipart/form-data * Erwartet POST (form-data ODER JSON)
*/ */
// TODO: bootstrap laden require_once __DIR__ . '/../../bootstrap.php';
// require_once __DIR__ . '/../../bootstrap.php';
// TODO: Auth erzwingen
// requireAuth();
// ========================= // =========================
// Request prüfen // Request prüfen
@@ -24,75 +18,197 @@
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405); http_response_code(405);
echo json_encode(['error' => 'Invalid request method']);
exit; exit;
} }
// ========================= // =========================
// Daten aus POST / JSON // Daten einlesen (JSON oder POST)
// ========================= // =========================
// TODO: Prüfen, ob multipart/form-data oder JSON $contentType = $_SERVER['CONTENT_TYPE'] ?? '';
// $data = json_decode(file_get_contents('php://input'), true);
if (str_contains($contentType, 'application/json')) {
$data = json_decode(file_get_contents('php://input'), true) ?? [];
} else {
$data = $_POST;
}
// =========================
// Basisfelder // Basisfelder
// $deviceId = $data['id'] ?? null; // =========================
// $name = $data['name'] ?? '';
// $description = $data['description'] ?? ''; $deviceId = isset($data['id']) ? (int)$data['id'] : null;
// $deviceTypeId = $data['device_type_id'] ?? null; $name = trim($data['name'] ?? '');
// $floorId = $data['floor_id'] ?? null; $comment = trim($data['comment'] ?? '');
// $rackId = $data['rack_id'] ?? null; $deviceTypeId = (int)($data['device_type_id'] ?? 0);
// $rackPosition = $data['rack_position'] ?? null; $rackId = isset($data['rack_id']) ? (int)$data['rack_id'] : null;
// $svgPositions = $data['svg_positions'] ?? []; $rackPositionHe = isset($data['rack_position_he']) ? (int)$data['rack_position_he'] : null;
// $portsData = $data['ports'] ?? []; $rackHeightHe = isset($data['rack_height_he']) ? (int)$data['rack_height_he'] : null;
$serialNumber = trim($data['serial_number'] ?? '');
$portsData = $data['ports'] ?? null;
// ========================= // =========================
// Validierung // Validierung
// ========================= // =========================
// TODO: $errors = [];
// - Name nicht leer
// - Device-Type existiert
// - Floor / Rack gültig
// - Ports valide
// ========================= if ($name === '') {
// Bild / SVG Upload (optional) $errors[] = 'Name darf nicht leer sein';
// ========================= }
// TODO: $_FILES['image'] prüfen $deviceType = $sql->single(
// - Upload in /uploads/devices "SELECT * FROM device_types WHERE id = ?",
// - SVG prüfen / sanitizen "i",
// - Pfad speichern [$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 // Device speichern
// ========================= // =========================
if (!empty($deviceId)) { if ($deviceId) {
// TODO: UPDATE devices SET ...
// $rows = $sql->set("UPDATE ...", "???", [...]); $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 { } else {
// TODO: INSERT INTO devices ...
// $deviceId = $sql->set("INSERT ...", "???", [...], true); $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 speichern / übernehmen vom Device-Type // Ports aktualisieren (optional, z. B. VLAN / Mode)
// ========================= // =========================
// TODO: if (is_array($portsData)) {
// - $portsData iterieren foreach ($portsData as $portId => $port) {
// - Positionen aus SVG übernehmen
// - port_type_id, name, VLAN, Modus speichern
// ========================= $status = $port['status'] ?? 'active';
// SVG-Positionen speichern $mode = $port['mode'] ?? null;
// ========================= $vlan = isset($port['vlan_config']) ? json_encode($port['vlan_config']) : null;
// TODO: $sql->set(
// - device_positions Tabelle? "
// - x/y Koordinaten UPDATE device_ports SET
// - Rotation / Zoom (optional) status = ?,
mode = ?,
vlan_config = ?
WHERE id = ? AND device_id = ?
",
"sssii",
[
$status,
$mode,
$vlan,
(int)$portId,
$deviceId
]
);
}
}
// ========================= // =========================
// Antwort // Antwort