Files
fewo-stommer/app/Http/Controllers/SwitchPropertyController.php
LarsandClaude Opus 5.5 8bcda9a4bc Wohnungsumschalter im Verwaltungsmenue und wohnungsbezogene Listen
Oben im Seitenmenue waehlt man die Wohnung; Listen, neue Eintraege, Belegungsplan
und Preise & Angebote folgen ihr. Ein geoeffneter Eintrag einer anderen Wohnung
schaltet auf diese um. Galerie und Ersatz-Titelbild der Website zeigen nur Fotos
der Wohnung des Bausteins und Fotos ohne Wohnung.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 18:40:13 +01:00

61 lines
2.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Filament\Resources\Properties\PropertyResource;
use App\Models\Property;
use App\Support\CurrentProperty;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* Apartment switcher in the admin menu: remembers the choice and returns to the page the admin came from.
* A record page of the previous apartment is left for its list; the settings of the previous apartment
* lead to the settings of the chosen one.
*/
class SwitchPropertyController extends Controller
{
public function __invoke(Request $request, CurrentProperty $current): RedirectResponse
{
$property = Property::query()->findOrFail($request->integer('property'));
$current->select($property);
return redirect()->to($this->target($request->input('return', url()->previous()), $property));
}
private function target(string $previous, Property $property): string
{
$fallback = route('filament.admin.pages.dashboard');
if (! Str::startsWith($previous, url('/admin'))) {
return $fallback;
}
try {
$route = Route::getRoutes()->match(Request::create($previous));
} catch (HttpException) {
return $fallback;
}
$name = (string) $route->getName();
if (Str::startsWith($name, 'filament.admin.resources.properties.')) {
return PropertyResource::getUrl('edit', ['record' => $property]);
}
if (Str::endsWith($name, ['.edit', '.view'])) {
$index = Str::beforeLast($name, '.').'.index';
return Route::has($index) ? route($index) : $fallback;
}
// A page's own apartment parameter would undo the switch.
$query = [];
parse_str((string) parse_url($previous, PHP_URL_QUERY), $query);
unset($query['wohnung']);
return strtok($previous, '?').($query !== [] ? '?'.http_build_query($query) : '');
}
}