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; }