- 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
126 lines
3.7 KiB
PHP
126 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* app/modules/rooms/save.php
|
|
*
|
|
* Speichert oder aktualisiert einen Raum.
|
|
*/
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: ?module=locations&action=list');
|
|
exit;
|
|
}
|
|
|
|
$roomId = (int)($_POST['id'] ?? 0);
|
|
$name = trim((string)($_POST['name'] ?? ''));
|
|
$number = trim((string)($_POST['number'] ?? ''));
|
|
$floorId = (int)($_POST['floor_id'] ?? 0);
|
|
$comment = trim((string)($_POST['comment'] ?? ''));
|
|
$rawPolygon = trim((string)($_POST['polygon_points'] ?? ''));
|
|
|
|
if ($name === '' || $floorId <= 0) {
|
|
$errors = [];
|
|
if ($name === '') {
|
|
$errors[] = 'Name ist erforderlich';
|
|
}
|
|
if ($floorId <= 0) {
|
|
$errors[] = 'Stockwerk ist erforderlich';
|
|
}
|
|
$_SESSION['error'] = implode(', ', $errors);
|
|
$_SESSION['validation_errors'] = $errors;
|
|
$redirect = $roomId > 0 ? "?module=rooms&action=edit&id=$roomId" : "?module=rooms&action=edit&floor_id=$floorId";
|
|
header("Location: $redirect");
|
|
exit;
|
|
}
|
|
|
|
$polygonPoints = [];
|
|
$polygonJson = null;
|
|
$x = null;
|
|
$y = null;
|
|
$width = null;
|
|
$height = null;
|
|
|
|
if ($rawPolygon !== '') {
|
|
$decoded = json_decode($rawPolygon, true);
|
|
if (is_array($decoded)) {
|
|
foreach ($decoded as $point) {
|
|
if (!is_array($point)) {
|
|
continue;
|
|
}
|
|
$px = isset($point['x']) ? (float)$point['x'] : null;
|
|
$py = isset($point['y']) ? (float)$point['y'] : null;
|
|
if (!is_finite($px) || !is_finite($py)) {
|
|
continue;
|
|
}
|
|
$polygonPoints[] = [
|
|
'x' => (int)round($px),
|
|
'y' => (int)round($py),
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count($polygonPoints) >= 3) {
|
|
$xs = array_column($polygonPoints, 'x');
|
|
$ys = array_column($polygonPoints, 'y');
|
|
$minX = min($xs);
|
|
$maxX = max($xs);
|
|
$minY = min($ys);
|
|
$maxY = max($ys);
|
|
$x = (int)$minX;
|
|
$y = (int)$minY;
|
|
$width = (int)max(1, $maxX - $minX);
|
|
$height = (int)max(1, $maxY - $minY);
|
|
$polygonJson = json_encode($polygonPoints, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
}
|
|
|
|
if (roomsHasPolygonColumn($sql)) {
|
|
if ($roomId > 0) {
|
|
$sql->set(
|
|
"UPDATE rooms
|
|
SET floor_id = ?, name = ?, number = ?, x = ?, y = ?, width = ?, height = ?, polygon_points = ?, comment = ?
|
|
WHERE id = ?",
|
|
"issiiiissi",
|
|
[$floorId, $name, $number, $x, $y, $width, $height, $polygonJson, $comment, $roomId]
|
|
);
|
|
} else {
|
|
$sql->set(
|
|
"INSERT INTO rooms (floor_id, name, number, x, y, width, height, polygon_points, comment)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
"issiiiiss",
|
|
[$floorId, $name, $number, $x, $y, $width, $height, $polygonJson, $comment]
|
|
);
|
|
}
|
|
} else {
|
|
if ($roomId > 0) {
|
|
$sql->set(
|
|
"UPDATE rooms
|
|
SET floor_id = ?, name = ?, number = ?, x = ?, y = ?, width = ?, height = ?, comment = ?
|
|
WHERE id = ?",
|
|
"issiiiisi",
|
|
[$floorId, $name, $number, $x, $y, $width, $height, $comment, $roomId]
|
|
);
|
|
} else {
|
|
$sql->set(
|
|
"INSERT INTO rooms (floor_id, name, number, x, y, width, height, comment)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
"issiiiis",
|
|
[$floorId, $name, $number, $x, $y, $width, $height, $comment]
|
|
);
|
|
}
|
|
}
|
|
|
|
$_SESSION['success'] = $roomId > 0 ? 'Raum gespeichert' : 'Raum erstellt';
|
|
header('Location: ?module=locations&action=list');
|
|
exit;
|
|
|
|
function roomsHasPolygonColumn($sql)
|
|
{
|
|
static $hasColumn = null;
|
|
if ($hasColumn !== null) {
|
|
return $hasColumn;
|
|
}
|
|
$col = $sql->single("SHOW COLUMNS FROM rooms LIKE 'polygon_points'", "", []);
|
|
$hasColumn = !empty($col);
|
|
return $hasColumn;
|
|
}
|