74 lines
1.7 KiB
PHP
74 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);
|
|
$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;
|