Files
netwatch/app/modules/racks/save.php
fixclean f4ce7f360d feat: implement package 1 session and validation feedback
- add session validation_errors bootstrap initialization

- render global flash + validation messages in header

- remove footer alert-based flash handling

- persist structured validation errors across save handlers

- mark NEXT_STEPS package 1 tasks as done
2026-02-18 09:40:59 +01:00

75 lines
1.7 KiB
PHP

<?php
/**
* app/modules/racks/save.php
*
* Speichert / aktualisiert ein Rack
*/
// Nur POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ?module=racks&action=list');
exit;
}
// =========================
// Daten einlesen
// =========================
$rackId = (int)($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');
$floorId = (int)($_POST['floor_id'] ?? 0);
$heightHe = (int)($_POST['height_he'] ?? 42);
$comment = trim($_POST['comment'] ?? '');
// =========================
// Validierung
// =========================
$errors = [];
if (empty($name)) {
$errors[] = "Name ist erforderlich";
}
if ($floorId <= 0) {
$errors[] = "Stockwerk ist erforderlich";
}
if ($heightHe < 1) {
$errors[] = "Höhe muss mindestens 1 HE sein";
}
// Falls Fehler: zurück zum Edit-Formular
if (!empty($errors)) {
$_SESSION['error'] = implode(', ', $errors);
$_SESSION['validation_errors'] = $errors;
$redirectUrl = $rackId ? "?module=racks&action=edit&id=$rackId" : "?module=racks&action=edit";
header("Location: $redirectUrl");
exit;
}
// =========================
// In DB speichern
// =========================
if ($rackId > 0) {
// UPDATE
$sql->set(
"UPDATE racks SET name = ?, floor_id = ?, height_he = ?, comment = ? WHERE id = ?",
"siisi",
[$name, $floorId, $heightHe, $comment, $rackId]
);
} else {
// INSERT
$sql->set(
"INSERT INTO racks (name, floor_id, height_he, comment) VALUES (?, ?, ?, ?)",
"siis",
[$name, $floorId, $heightHe, $comment]
);
}
$_SESSION['success'] = "Rack gespeichert";
// =========================
// Redirect
// =========================
header('Location: ?module=racks&action=list');
exit;