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>
This commit is contained in:
LarsandClaude Opus 5.5 committed 2026-10-29 18:40:13 +01:00
1 parent e74033a958
commit 8bcda9a4bc
37 files changed
+520 -35

No files matched your search

+86
View File
@@ -0,0 +1,86 @@
<?php
namespace App\Support;
use App\Models\Property;
use App\Tenancy\TenantContext;
/**
* The apartment the admin is working on (switcher at the top of the admin menu). Lists, new records and the
* pages for one apartment follow it. Stored in the session per landlord; without a valid choice the first
* active apartment applies, then any apartment.
*/
class CurrentProperty
{
private const SESSION_KEY = 'admin.current_property';
public function get(): ?Property
{
$chosen = $this->chosenId();
$property = $chosen !== null ? Property::query()->find($chosen) : null;
return $property
?? Property::query()->where('is_active', true)->orderBy('id')->first()
?? Property::query()->orderBy('id')->first();
}
public function id(): ?int
{
return $this->get()?->id;
}
public function select(Property $property): void
{
if (app()->bound('session')) {
session()->put(self::SESSION_KEY.'.'.$this->tenantKey(), $property->id);
}
}
/**
* An apartment named in a page URL (old links, e-mails) becomes the choice; another landlord's apartment is not found.
*/
public function followUrl(?int $propertyId): ?int
{
if ($propertyId === null) {
return $this->id();
}
$this->select(Property::query()->findOrFail($propertyId));
return $propertyId;
}
/**
* Whether the landlord has more than one apartment to choose from.
*/
public function hasChoice(): bool
{
return Property::query()->count() > 1;
}
/**
* @return array<int, string>
*/
public function options(): array
{
return Property::query()->orderBy('name')->get()
->mapWithKeys(fn (Property $property): array => [$property->id => $property->is_active ? $property->name : $property->name.' (inaktiv)'])
->all();
}
private function chosenId(): ?int
{
if (! app()->bound('session')) {
return null;
}
$id = session()->get(self::SESSION_KEY.'.'.$this->tenantKey());
return is_numeric($id) ? (int) $id : null;
}
private function tenantKey(): string
{
return (string) (app(TenantContext::class)->id() ?? 'none');
}
}