devices anlegen

This commit is contained in:
2026-02-11 15:46:50 +01:00
parent a6b56ffd75
commit 699ffe3d6b
6 changed files with 101 additions and 21 deletions

5
BUGS.md Normal file
View File

@@ -0,0 +1,5 @@
# gefundene bugs
- device-webconfig link ist nicht korrekt
- device löschen geht nicht
- device_types svg modul malen
- ports drag n drop funktioniert nicht

View File

@@ -76,6 +76,14 @@ $racks = $sql->get("SELECT id, name FROM racks ORDER BY name", "", []);
placeholder="Optionales Feld">
</div>
<div class="form-group">
<label for="web_config_url">Webconfig-URL</label>
<input type="url" id="web_config_url" name="web_config_url"
value="<?php echo htmlspecialchars($device['web_config_url'] ?? ''); ?>"
placeholder="https://switch.example.local">
<small>Optionaler Link zur Weboberfläche des Geräts (öffnet in neuem Tab).</small>
</div>
<div class="form-group">
<label for="comment">Kommentar</label>
<textarea id="comment" name="comment" rows="3"

View File

@@ -59,6 +59,7 @@ $devices = $sql->get(
d.serial_number,
d.rack_position_he,
d.rack_height_he,
d.web_config_url,
dt.name AS device_type,
dt.image_path,
f.name AS floor_name,
@@ -148,6 +149,7 @@ $racks = $sql->get("SELECT id, name FROM racks ORDER BY name", "", []);
<th>Rack</th>
<th>Position (HE)</th>
<th>Seriennummer</th>
<th>Webconfig</th>
<th>Aktionen</th>
</tr>
</thead>
@@ -187,6 +189,16 @@ $racks = $sql->get("SELECT id, name FROM racks ORDER BY name", "", []);
<small><?php echo htmlspecialchars($d['serial_number'] ?? '—'); ?></small>
</td>
<td>
<?php if (!empty($d['web_config_url'])): ?>
<a href="<?php echo htmlspecialchars($d['web_config_url']); ?>" target="_blank" rel="noopener noreferrer" class="button button-small">
Webconfig
</a>
<?php else: ?>
<?php endif; ?>
</td>
<td class="actions">
<a href="?module=devices&action=edit&id=<?php echo $d['id']; ?>" class="button button-small">Bearbeiten</a>
<a href="#" class="button button-small button-danger" onclick="confirmDelete(<?php echo $d['id']; ?>)">Löschen</a>

View File

@@ -24,6 +24,10 @@ $rackPositionHe = (int)($_POST['rack_position_he'] ?? 0);
$rackHeightHe = (int)($_POST['rack_height_he'] ?? 1);
$serialNumber = trim($_POST['serial_number'] ?? '');
$comment = trim($_POST['comment'] ?? '');
$webConfigUrl = trim($_POST['web_config_url'] ?? '');
if ($webConfigUrl === '') {
$webConfigUrl = null;
}
// =========================
// Validierung
@@ -61,21 +65,27 @@ if (!empty($errors)) {
// =========================
// In DB speichern
// =========================
if ($deviceId > 0) {
// UPDATE
$sql->set(
"UPDATE devices SET name = ?, device_type_id = ?, rack_id = ?, rack_position_he = ?, rack_height_he = ?, serial_number = ?, comment = ? WHERE id = ?",
"siiiissi",
[$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment, $deviceId]
$isNewDevice = $deviceId <= 0;
if ($isNewDevice) {
// INSERT
$deviceId = $sql->set(
"INSERT INTO devices (name, device_type_id, rack_id, rack_position_he, rack_height_he, serial_number, comment, web_config_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
"siiiisss",
[$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment, $webConfigUrl],
true
);
} else {
// INSERT
// UPDATE
$sql->set(
"INSERT INTO devices (name, device_type_id, rack_id, rack_position_he, rack_height_he, serial_number, comment) VALUES (?, ?, ?, ?, ?, ?, ?)",
"siiiiss",
[$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment]
"UPDATE devices SET name = ?, device_type_id = ?, rack_id = ?, rack_position_he = ?, rack_height_he = ?, serial_number = ?, comment = ?, web_config_url = ? WHERE id = ?",
"siiiisssi",
[$name, $deviceTypeId, $rackId, $rackPositionHe, $rackHeightHe, $serialNumber, $comment, $webConfigUrl, $deviceId]
);
$deviceId = $sql->h->insert_id;
}
if ($isNewDevice && $deviceId > 0) {
copyDevicePortsFromType($sql, $deviceId, $deviceTypeId);
}
$_SESSION['success'] = "Gerät gespeichert";
@@ -85,3 +95,43 @@ $_SESSION['success'] = "Gerät gespeichert";
// =========================
header('Location: ?module=devices&action=list');
exit;
function copyDevicePortsFromType($sql, $deviceId, $deviceTypeId)
{
if ($deviceId <= 0 || $deviceTypeId <= 0) {
return;
}
$ports = $sql->get(
"SELECT name, port_type_id FROM device_type_ports WHERE device_type_id = ? ORDER BY id",
"i",
[$deviceTypeId]
);
if (empty($ports)) {
return;
}
foreach ($ports as $index => $port) {
$name = trim($port['name'] ?? '');
if ($name === '') {
$name = 'Port ' . ($index + 1);
}
$portTypeId = isset($port['port_type_id']) ? (int)$port['port_type_id'] : null;
if ($portTypeId === null) {
$sql->set(
"INSERT INTO device_ports (device_id, name) VALUES (?, ?)",
"is",
[$deviceId, $name]
);
} else {
$sql->set(
"INSERT INTO device_ports (device_id, name, port_type_id) VALUES (?, ?, ?)",
"isi",
[$deviceId, $name, $portTypeId]
);
}
}
}

View File

@@ -142,11 +142,15 @@ Konkretes Gerät in der Infrastruktur.
**Verwendung**
- Instanz eines Gerätetyps
- Kann in einem Rack platziert werden
- Beim Anlegen eines Geräts werden Ports aus `device_type_ports` übernommen
**Rack-Attribute**
- `rack_position_he`
- `rack_height_he`
**Technische Attribute**
- `web_config_url`: Optionale Adresse zur Weboberfläche / Webconfig des Geräts.
---
### `device_ports`

View File

@@ -98,6 +98,7 @@ CREATE TABLE devices (
rack_height_he INT,
serial_number VARCHAR(255),
comment TEXT,
web_config_url VARCHAR(255),
FOREIGN KEY (device_type_id) REFERENCES device_types(id),
FOREIGN KEY (rack_id) REFERENCES racks(id)
ON DELETE SET NULL