304 lines
9.5 KiB
PHP
304 lines
9.5 KiB
PHP
<?php
|
|
/**
|
|
* app/modules/devices/edit.php
|
|
*
|
|
* Konkretes Gerät anlegen / bearbeiten
|
|
* - Name, Seriennummer
|
|
* - Gerätetyp wählen
|
|
* - Standort (Rack, HE-Position)
|
|
*/
|
|
|
|
// =========================
|
|
// Kontext bestimmen
|
|
// =========================
|
|
$deviceId = (int)($_GET['id'] ?? 0);
|
|
$device = null;
|
|
|
|
if ($deviceId > 0) {
|
|
$device = $sql->single(
|
|
"SELECT d.* FROM devices d WHERE d.id = ?",
|
|
"i",
|
|
[$deviceId]
|
|
);
|
|
}
|
|
|
|
$isEdit = !empty($device);
|
|
$pageTitle = $isEdit ? "Gerät bearbeiten: " . htmlspecialchars($device['name']) : "Neues Gerät";
|
|
$dependencyCounts = [
|
|
'port_count' => 0,
|
|
'module_count' => 0,
|
|
'connection_count' => 0
|
|
];
|
|
|
|
if ($isEdit) {
|
|
$dependencyCounts = $sql->single(
|
|
"SELECT
|
|
(
|
|
SELECT COUNT(*)
|
|
FROM device_ports dp
|
|
WHERE dp.device_id = ?
|
|
) AS port_count,
|
|
(
|
|
SELECT COUNT(*)
|
|
FROM device_port_modules dpm
|
|
JOIN device_ports dp2 ON dp2.id = dpm.device_port_id
|
|
WHERE dp2.device_id = ?
|
|
) AS module_count,
|
|
(
|
|
SELECT COUNT(*)
|
|
FROM connections c
|
|
WHERE (c.port_a_type = 'device' AND c.port_a_id IN (
|
|
SELECT dp3.id FROM device_ports dp3 WHERE dp3.device_id = ?
|
|
))
|
|
OR (c.port_b_type = 'device' AND c.port_b_id IN (
|
|
SELECT dp4.id FROM device_ports dp4 WHERE dp4.device_id = ?
|
|
))
|
|
) AS connection_count",
|
|
"iiii",
|
|
[$deviceId, $deviceId, $deviceId, $deviceId]
|
|
) ?: $dependencyCounts;
|
|
}
|
|
|
|
// =========================
|
|
// Optionen laden
|
|
// =========================
|
|
$deviceTypes = $sql->get("SELECT id, name, category FROM device_types ORDER BY name", "", []);
|
|
$racks = $sql->get("SELECT id, name FROM racks ORDER BY name", "", []);
|
|
|
|
?>
|
|
|
|
<div class="device-edit">
|
|
<h1><?php echo $pageTitle; ?></h1>
|
|
|
|
<form method="post" action="?module=devices&action=save" class="edit-form">
|
|
|
|
<?php if ($isEdit): ?>
|
|
<input type="hidden" name="id" value="<?php echo $deviceId; ?>">
|
|
<?php endif; ?>
|
|
|
|
<!-- =========================
|
|
Basisdaten
|
|
========================= -->
|
|
<fieldset>
|
|
<legend>Allgemein</legend>
|
|
|
|
<div class="form-group">
|
|
<label for="name">Name <span class="required">*</span></label>
|
|
<input type="text" id="name" name="name" required
|
|
value="<?php echo htmlspecialchars($device['name'] ?? ''); ?>"
|
|
placeholder="z.B. Core Switch 1">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="device_type_id">Gerätetyp <span class="required">*</span></label>
|
|
<select id="device_type_id" name="device_type_id" required>
|
|
<option value="">- Wählen -</option>
|
|
<?php foreach ($deviceTypes as $type): ?>
|
|
<option value="<?php echo $type['id']; ?>"
|
|
<?php echo ($device['device_type_id'] ?? 0) == $type['id'] ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($type['name']); ?>
|
|
<small>(<?php echo $type['category']; ?>)</small>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="serial_number">Seriennummer</label>
|
|
<input type="text" id="serial_number" name="serial_number"
|
|
value="<?php echo htmlspecialchars($device['serial_number'] ?? ''); ?>"
|
|
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"
|
|
placeholder="Notizen zu diesem Gerät"><?php echo htmlspecialchars($device['comment'] ?? ''); ?></textarea>
|
|
</div>
|
|
</fieldset>
|
|
|
|
<!-- =========================
|
|
Standort im Rack
|
|
========================= -->
|
|
<fieldset>
|
|
<legend>Standort</legend>
|
|
|
|
<div class="form-group">
|
|
<label for="rack_id">Rack <span class="required">*</span></label>
|
|
<select id="rack_id" name="rack_id" required>
|
|
<option value="">- Wählen -</option>
|
|
<?php foreach ($racks as $rack): ?>
|
|
<option value="<?php echo $rack['id']; ?>"
|
|
<?php echo ($device['rack_id'] ?? 0) == $rack['id'] ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($rack['name']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<small>Wählen Sie das Rack, in dem sich das Gerät befindet.</small>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="rack_position_he">Position im Rack (HE) <span class="required">*</span></label>
|
|
<input type="number" id="rack_position_he" name="rack_position_he" required min="1"
|
|
value="<?php echo htmlspecialchars($device['rack_position_he'] ?? ''); ?>"
|
|
placeholder="Höheneinheit von oben">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="rack_height_he">Höhe (HE) <span class="required">*</span></label>
|
|
<input type="number" id="rack_height_he" name="rack_height_he" required min="1"
|
|
value="<?php echo htmlspecialchars($device['rack_height_he'] ?? '1'); ?>"
|
|
placeholder="Anzahl Höheneinheiten">
|
|
</div>
|
|
</fieldset>
|
|
|
|
<!-- =========================
|
|
Aktionen
|
|
========================= -->
|
|
<fieldset class="form-actions">
|
|
<button type="submit" class="button button-primary">Speichern</button>
|
|
<a href="?module=devices&action=list" class="button">Abbrechen</a>
|
|
<?php if ($isEdit): ?>
|
|
<a href="?module=devices&action=delete&id=<?php echo (int)$deviceId; ?>" class="button button-danger" onclick="return confirmDelete(event, <?php echo (int)$deviceId; ?>, <?php echo (int)$dependencyCounts['connection_count']; ?>, <?php echo (int)$dependencyCounts['port_count']; ?>, <?php echo (int)$dependencyCounts['module_count']; ?>)">Löschen</a>
|
|
<?php endif; ?>
|
|
</fieldset>
|
|
|
|
</form>
|
|
</div>
|
|
|
|
<style>
|
|
.device-edit {
|
|
max-width: 800px;
|
|
margin: 20px auto;
|
|
padding: 20px;
|
|
}
|
|
|
|
.edit-form {
|
|
background: white;
|
|
padding: 20px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.edit-form fieldset {
|
|
margin: 20px 0;
|
|
padding: 15px;
|
|
border: 1px solid #e0e0e0;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
.edit-form legend {
|
|
padding: 0 10px;
|
|
font-weight: bold;
|
|
font-size: 1.1em;
|
|
}
|
|
|
|
.form-group {
|
|
margin: 15px 0;
|
|
}
|
|
|
|
.form-group label {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
font-weight: bold;
|
|
}
|
|
|
|
.form-group input[type="text"],
|
|
.form-group input[type="number"],
|
|
.form-group select,
|
|
.form-group textarea {
|
|
width: 100%;
|
|
padding: 8px 12px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
font-family: inherit;
|
|
}
|
|
|
|
.form-group textarea {
|
|
resize: vertical;
|
|
}
|
|
|
|
.form-group small {
|
|
display: block;
|
|
margin-top: 5px;
|
|
color: #666;
|
|
}
|
|
|
|
.required {
|
|
color: red;
|
|
}
|
|
|
|
.form-actions {
|
|
display: flex;
|
|
gap: 10px;
|
|
margin-top: 30px;
|
|
}
|
|
|
|
.button {
|
|
padding: 10px 15px;
|
|
background: #007bff;
|
|
color: white;
|
|
text-decoration: none;
|
|
border-radius: 4px;
|
|
border: none;
|
|
cursor: pointer;
|
|
font-size: 0.95em;
|
|
}
|
|
|
|
.button-primary {
|
|
background: #28a745;
|
|
}
|
|
|
|
.button-danger {
|
|
background: #dc3545;
|
|
}
|
|
|
|
.button:hover {
|
|
opacity: 0.8;
|
|
}
|
|
</style>
|
|
|
|
<script>
|
|
function confirmDelete(event, id, connectionCount, portCount, moduleCount) {
|
|
if (event && typeof event.preventDefault === 'function') {
|
|
event.preventDefault();
|
|
}
|
|
|
|
if (confirm('Dieses Gerät wirklich löschen?')) {
|
|
const hasDependencies = (connectionCount > 0) || (portCount > 0) || (moduleCount > 0);
|
|
if (hasDependencies) {
|
|
const details = [];
|
|
if (connectionCount > 0) {
|
|
details.push(connectionCount + ' Verbindungen');
|
|
}
|
|
if (portCount > 0) {
|
|
details.push(portCount + ' Ports');
|
|
}
|
|
if (moduleCount > 0) {
|
|
details.push(moduleCount + ' Port-Module');
|
|
}
|
|
const dependencyMessage = 'Es gibt abhängige Daten (' + details.join(', ') + '). Diese auch löschen?';
|
|
if (!confirm(dependencyMessage)) {
|
|
return false;
|
|
}
|
|
window.location.href = '?module=devices&action=delete&id=' + encodeURIComponent(id) + '&force=1';
|
|
return false;
|
|
}
|
|
|
|
window.location.href = '?module=devices&action=delete&id=' + encodeURIComponent(id);
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
</script>
|
|
|