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>
<form method="post" action="/app/devices/save.php" enctype="multipart/form-data">
<form method="post" action="/devices/save" enctype="multipart/form-data">
<!-- =========================
Basisdaten

View File

@@ -1,111 +1,242 @@
<?php
/**
* app/devices/list.php
*
* Übersicht aller Geräte
* - Anzeigen
* - Bearbeiten
* - Löschen
* - SVG / JPG Vorschau
* modules/devices/list.php
* Vollständige Geräteübersicht
*/
// TODO: bootstrap laden
// require_once __DIR__ . '/../../bootstrap.php';
// TODO: Auth erzwingen
// requireAuth();
// =========================
// Geräte laden
// Filter / Suche einlesen
// =========================
// TODO: Geräte aus DB laden
// $devices = $sql->get("SELECT * FROM devices ORDER BY name", "", []);
$search = trim($_GET['search'] ?? '');
$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>
<!-- =========================
Toolbar
========================= -->
<form method="get" class="toolbar">
<input type="hidden" name="module" value="devices">
<input type="hidden" name="action" value="list">
<div class="toolbar">
<a href="/?page=devices/edit" class="button">
<input type="text" name="search" placeholder="Suche…" value="<?= htmlspecialchars($search) ?>">
<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
</a>
</form>
<!-- TODO: Suchfeld -->
<!-- TODO: Filter (Device-Type, Standort, Floor, Rack) -->
</div>
<!-- =========================
Geräte-Tabelle
========================= -->
<?php if ($devices): ?>
<table class="device-list">
<thead>
<tr>
<th>Vorschau</th>
<th>Name</th>
<th>Gerätetyp</th>
<th>Typ</th>
<th>Standort</th>
<th>Rack</th>
<th>Rack-Pos</th>
<th>HE</th>
<th>Ports</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php /* foreach ($devices as $device): */ ?>
<?php foreach ($devices as $d):
$freePorts = max(0, $d['total_ports'] - $d['connected_ports']);
?>
<tr>
<td class="preview">
<!-- TODO: SVG / JPG Thumbnail -->
<td>
<?php if ($d['image_path']): ?>
<img src="<?= htmlspecialchars($d['image_path']) ?>" class="thumb">
<?php else: ?>
<?php endif; ?>
</td>
<td>
<!-- TODO: Name -->
Gerät XY
<strong><?= htmlspecialchars($d['name']) ?></strong><br>
<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>
<!-- TODO: Gerätetyp -->
<?= (int)$d['connected_ports'] ?>/<?= (int)$d['total_ports'] ?>
<br>
<small><?= $freePorts ?> frei</small>
</td>
<td>
<!-- TODO: Standort / Floor -->
</td>
<td>
<!-- TODO: Rack -->
</td>
<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 class="actions">
<a href="/devices/ports?id=<?= $d['id'] ?>">Ports</a>
<a href="/devices/edit?id=<?= $d['id'] ?>">Bearbeiten</a>
<a href="/devices/delete?id=<?= $d['id'] ?>"
onclick="return confirm('Gerät wirklich löschen?')">
Löschen
</a>
</td>
</tr>
<?php /* endforeach; */ ?>
<?php endforeach; ?>
</tbody>
</table>
<!-- =========================
Leerer Zustand
========================= -->
<?php else: ?>
<?php /* if (empty($devices)): */ ?>
<div class="empty-state">
<p>Noch keine Geräte angelegt.</p>
<p><a href="/?page=devices/edit">Erstes Gerät anlegen</a></p>
<p>Keine Geräte gefunden.</p>
</div>
<?php /* endif; */ ?>
<?php endif; ?>

View File

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