#13 räume definieren

This commit is contained in:
2026-02-16 10:59:29 +01:00
parent f80ab4aaa9
commit 2a1732323d
9 changed files with 1007 additions and 178 deletions

115
app/modules/rooms/save.php Normal file
View File

@@ -0,0 +1,115 @@
<?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) {
$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]
);
}
}
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;
}