From 8bcda9a4bc3595dd00a9a349b25adc905cfd785a Mon Sep 17 00:00:00 2001 From: Lars Date: Thu, 29 Oct 2026 18:40:13 +0100 Subject: [PATCH] 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 --- .../Concerns/ListsCurrentProperty.php | 33 ++++ .../Concerns/SelectsRecordProperty.php | 34 ++++ app/Filament/Pages/OccupancyCalendar.php | 8 +- app/Filament/Pages/PricesAndOffers.php | 15 +- .../Resources/Amenities/AmenityResource.php | 3 +- .../Amenities/Pages/ListAmenities.php | 3 + .../Amenities/Schemas/AmenityForm.php | 3 +- .../BlockedPeriods/BlockedPeriodResource.php | 6 +- .../Pages/ManageBlockedPeriods.php | 3 + .../Resources/Bookings/BookingResource.php | 3 +- .../Bookings/Schemas/BookingForm.php | 3 +- .../Bookings/Tables/BookingsTable.php | 5 +- .../ChannelTasks/Pages/ListChannelTasks.php | 10 ++ .../Resources/Channels/ChannelResource.php | 3 +- .../Resources/Channels/Pages/ListChannels.php | 3 + .../Channels/Schemas/ChannelForm.php | 3 +- .../FloorPlans/FloorPlanResource.php | 3 +- .../FloorPlans/Pages/ListFloorPlans.php | 3 + .../FloorPlans/Schemas/FloorPlanForm.php | 3 +- .../Resources/Photos/Pages/ListPhotos.php | 15 +- .../Resources/Photos/PhotoResource.php | 3 +- .../Resources/Photos/Schemas/PhotoForm.php | 3 +- .../PortalEmails/Pages/ListPortalEmails.php | 10 ++ .../Pages/ManagePortalOccupations.php | 3 + .../PortalOccupationResource.php | 3 +- .../Resources/Reviews/Pages/ListReviews.php | 3 + .../Resources/Reviews/ReviewResource.php | 3 +- .../Resources/Reviews/Schemas/ReviewForm.php | 3 +- .../Controllers/SwitchPropertyController.php | 60 +++++++ app/Models/Photo.php | 14 ++ app/Providers/Filament/AdminPanelProvider.php | 7 + app/Support/CurrentProperty.php | 86 ++++++++++ app/Themes/Sections/GallerySection.php | 2 +- app/Themes/Sections/HeroSection.php | 4 +- .../pages/occupancy-calendar.blade.php | 7 - .../filament/property-switcher.blade.php | 32 ++++ tests/Feature/CurrentPropertyTest.php | 150 ++++++++++++++++++ 37 files changed, 520 insertions(+), 35 deletions(-) create mode 100644 app/Filament/Concerns/ListsCurrentProperty.php create mode 100644 app/Filament/Concerns/SelectsRecordProperty.php create mode 100644 app/Http/Controllers/SwitchPropertyController.php create mode 100644 app/Support/CurrentProperty.php create mode 100644 resources/views/filament/property-switcher.blade.php create mode 100644 tests/Feature/CurrentPropertyTest.php diff --git a/app/Filament/Concerns/ListsCurrentProperty.php b/app/Filament/Concerns/ListsCurrentProperty.php new file mode 100644 index 0000000..27388bf --- /dev/null +++ b/app/Filament/Concerns/ListsCurrentProperty.php @@ -0,0 +1,33 @@ +get(); + + return $query instanceof Builder && $property !== null ? $this->scopeToProperty($query, $property) : $query; + } + + /** + * @param Builder $query + * @return Builder + */ + protected function scopeToProperty(Builder $query, Property $property): Builder + { + return $query->where($query->qualifyColumn('property_id'), $property->id); + } +} diff --git a/app/Filament/Concerns/SelectsRecordProperty.php b/app/Filament/Concerns/SelectsRecordProperty.php new file mode 100644 index 0000000..a8d1169 --- /dev/null +++ b/app/Filament/Concerns/SelectsRecordProperty.php @@ -0,0 +1,34 @@ +id() && ($property = Property::query()->find($propertyId))) { + app(CurrentProperty::class)->select($property); + } + + return $record; + } + + public static function recordPropertyId(Model $record): ?int + { + $id = $record->getAttribute('property_id'); + + return $id !== null ? (int) $id : null; + } +} diff --git a/app/Filament/Pages/OccupancyCalendar.php b/app/Filament/Pages/OccupancyCalendar.php index a637f71..36a5a3d 100644 --- a/app/Filament/Pages/OccupancyCalendar.php +++ b/app/Filament/Pages/OccupancyCalendar.php @@ -6,6 +6,7 @@ use App\Filament\Concerns\FollowsAdminMode; use App\Models\Booking; use App\Models\Property; +use App\Support\CurrentProperty; use BackedEnum; use Carbon\CarbonImmutable; use Filament\Pages\Page; @@ -45,10 +46,15 @@ public static function canAccess(): bool return Gate::allows('viewAny', Booking::class); } + public function getSubheading(): ?string + { + return app(CurrentProperty::class)->hasChoice() ? Property::query()->find($this->propertyId)?->name : null; + } + public function mount(): void { $this->month = $this->month ?: now()->format('Y-m'); - $this->propertyId ??= Property::query()->where('is_active', true)->value('id') ?? Property::query()->value('id'); + $this->propertyId = app(CurrentProperty::class)->followUrl($this->propertyId); } #[Computed] diff --git a/app/Filament/Pages/PricesAndOffers.php b/app/Filament/Pages/PricesAndOffers.php index d937234..95e9625 100644 --- a/app/Filament/Pages/PricesAndOffers.php +++ b/app/Filament/Pages/PricesAndOffers.php @@ -9,8 +9,8 @@ use App\Filament\Resources\Properties\RelationManagers\SeasonsRelationManager; use App\Filament\Resources\Properties\RelationManagers\SpecialOffersRelationManager; use App\Models\Property; +use App\Support\CurrentProperty; use BackedEnum; -use Filament\Forms\Components\Select; use Filament\Pages\Page; use Filament\Schemas\Components\Livewire; use Filament\Schemas\Components\Tabs; @@ -49,9 +49,14 @@ public static function canAccess(): bool return (bool) auth()->user()?->hasRole(UserRole::Admin, UserRole::Rentals); } + public function getSubheading(): ?string + { + return app(CurrentProperty::class)->hasChoice() ? Property::query()->find($this->propertyId)?->name : null; + } + public function mount(): void { - $this->propertyId ??= Property::query()->where('is_active', true)->value('id') ?? Property::query()->value('id'); + $this->propertyId = app(CurrentProperty::class)->followUrl($this->propertyId); } public function content(Schema $schema): Schema @@ -64,12 +69,6 @@ public function content(Schema $schema): Schema $context = ['ownerRecord' => $property, 'pageClass' => EditProperty::class]; return $schema->components([ - Select::make('propertyId') - ->label('Wohnung') - ->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all()) - ->live() - ->afterStateUpdated(fn () => $this->redirect(static::getUrl(['wohnung' => $this->propertyId]))) - ->visible(fn (): bool => Property::query()->count() > 1), Tabs::make('prices') ->persistTabInQueryString() ->tabs([ diff --git a/app/Filament/Resources/Amenities/AmenityResource.php b/app/Filament/Resources/Amenities/AmenityResource.php index 341194b..8696a9a 100644 --- a/app/Filament/Resources/Amenities/AmenityResource.php +++ b/app/Filament/Resources/Amenities/AmenityResource.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources\Amenities; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\Amenities\Pages\CreateAmenity; use App\Filament\Resources\Amenities\Pages\EditAmenity; use App\Filament\Resources\Amenities\Pages\ListAmenities; @@ -18,7 +19,7 @@ class AmenityResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = Amenity::class; diff --git a/app/Filament/Resources/Amenities/Pages/ListAmenities.php b/app/Filament/Resources/Amenities/Pages/ListAmenities.php index 29ad95c..96e4415 100644 --- a/app/Filament/Resources/Amenities/Pages/ListAmenities.php +++ b/app/Filament/Resources/Amenities/Pages/ListAmenities.php @@ -2,12 +2,15 @@ namespace App\Filament\Resources\Amenities\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\Amenities\AmenityResource; use Filament\Actions\CreateAction; use Filament\Resources\Pages\ListRecords; class ListAmenities extends ListRecords { + use ListsCurrentProperty; + protected static string $resource = AmenityResource::class; protected function getHeaderActions(): array diff --git a/app/Filament/Resources/Amenities/Schemas/AmenityForm.php b/app/Filament/Resources/Amenities/Schemas/AmenityForm.php index b6ec50e..c8c201f 100644 --- a/app/Filament/Resources/Amenities/Schemas/AmenityForm.php +++ b/app/Filament/Resources/Amenities/Schemas/AmenityForm.php @@ -4,6 +4,7 @@ use App\Enums\AmenityArea; use App\Models\Property; +use App\Support\CurrentProperty; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Toggle; @@ -28,7 +29,7 @@ public static function configure(Schema $schema): Schema Select::make('property_id') ->label('Wohnung') ->options(fn (): array => Property::query()->pluck('name', 'id')->all()) - ->default(fn (): ?int => Property::query()->value('id')) + ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->required(), Toggle::make('is_highlight') ->label('Highlight (auf der Startseite hervorheben)'), diff --git a/app/Filament/Resources/BlockedPeriods/BlockedPeriodResource.php b/app/Filament/Resources/BlockedPeriods/BlockedPeriodResource.php index faf277b..b4fcd89 100644 --- a/app/Filament/Resources/BlockedPeriods/BlockedPeriodResource.php +++ b/app/Filament/Resources/BlockedPeriods/BlockedPeriodResource.php @@ -6,12 +6,14 @@ use App\Channels\ICal\ICalRuleEngine; use App\Enums\BlockedPeriodPurpose; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\BlockedPeriods\Pages\ManageBlockedPeriods; use App\Filament\Resources\PortalOccupations\PortalOccupationResource; use App\Models\BlockedPeriod; use App\Models\Booking; use App\Models\Channel; use App\Models\Property; +use App\Support\CurrentProperty; use BackedEnum; use Filament\Actions\Action; use Filament\Actions\DeleteAction; @@ -37,7 +39,7 @@ */ class BlockedPeriodResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = BlockedPeriod::class; @@ -59,7 +61,7 @@ public static function form(Schema $schema): Schema Select::make('property_id') ->label('Wohnung') ->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all()) - ->default(fn (): ?int => Property::query()->where('is_active', true)->value('id')) + ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->required() ->columnSpanFull() ->hidden(fn (): bool => Property::query()->count() <= 1) diff --git a/app/Filament/Resources/BlockedPeriods/Pages/ManageBlockedPeriods.php b/app/Filament/Resources/BlockedPeriods/Pages/ManageBlockedPeriods.php index 8add1c3..70b5943 100644 --- a/app/Filament/Resources/BlockedPeriods/Pages/ManageBlockedPeriods.php +++ b/app/Filament/Resources/BlockedPeriods/Pages/ManageBlockedPeriods.php @@ -2,12 +2,15 @@ namespace App\Filament\Resources\BlockedPeriods\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\BlockedPeriods\BlockedPeriodResource; use Filament\Actions\CreateAction; use Filament\Resources\Pages\ManageRecords; class ManageBlockedPeriods extends ManageRecords { + use ListsCurrentProperty; + protected static string $resource = BlockedPeriodResource::class; protected function getHeaderActions(): array diff --git a/app/Filament/Resources/Bookings/BookingResource.php b/app/Filament/Resources/Bookings/BookingResource.php index 7918273..5ffe7ee 100644 --- a/app/Filament/Resources/Bookings/BookingResource.php +++ b/app/Filament/Resources/Bookings/BookingResource.php @@ -4,6 +4,7 @@ use App\Enums\BookingStatus; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\Bookings\Pages\CreateBooking; use App\Filament\Resources\Bookings\Pages\ListBookings; use App\Filament\Resources\Bookings\Pages\ViewBooking; @@ -21,7 +22,7 @@ class BookingResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = Booking::class; diff --git a/app/Filament/Resources/Bookings/Schemas/BookingForm.php b/app/Filament/Resources/Bookings/Schemas/BookingForm.php index ecbb9f5..02bb833 100644 --- a/app/Filament/Resources/Bookings/Schemas/BookingForm.php +++ b/app/Filament/Resources/Bookings/Schemas/BookingForm.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources\Bookings\Schemas; use App\Models\Property; +use App\Support\CurrentProperty; use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\Select; use Filament\Forms\Components\Textarea; @@ -26,7 +27,7 @@ public static function configure(Schema $schema): Schema Select::make('property_id') ->label('Wohnung') ->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all()) - ->default(fn (): ?int => Property::query()->value('id')) + ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->required(), TextInput::make('guests') ->label('Personen') diff --git a/app/Filament/Resources/Bookings/Tables/BookingsTable.php b/app/Filament/Resources/Bookings/Tables/BookingsTable.php index c63fdc1..4fb0a00 100644 --- a/app/Filament/Resources/Bookings/Tables/BookingsTable.php +++ b/app/Filament/Resources/Bookings/Tables/BookingsTable.php @@ -5,6 +5,7 @@ use App\Availability\OccupancyOverview; use App\Enums\BookingStatus; use App\Models\Property; +use App\Support\CurrentProperty; use Filament\Actions\Action; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Filters\Filter; @@ -59,7 +60,9 @@ public static function configure(Table $table, ?int $guestId = null): Table TextColumn::make('property')->label('Wohnung')->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ - SelectFilter::make('property')->label('Wohnung')->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all()), + SelectFilter::make('property')->label('Wohnung')->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all()) + // The booking list starts with the apartment chosen in the menu; a guest's bookings show all apartments. + ->default(fn (): ?int => $guestId === null ? app(CurrentProperty::class)->id() : null), SelectFilter::make('kind')->label('Buchungsweg')->options(['booking' => 'Eigene Anfragen & Buchungen', 'portal' => 'Portalreservierungen']), SelectFilter::make('status')->label('Status')->multiple()->options([ ...collect(BookingStatus::cases())->mapWithKeys(fn (BookingStatus $status): array => [$status->value => $status->getLabel()])->all(), diff --git a/app/Filament/Resources/ChannelTasks/Pages/ListChannelTasks.php b/app/Filament/Resources/ChannelTasks/Pages/ListChannelTasks.php index 886ff47..4036727 100644 --- a/app/Filament/Resources/ChannelTasks/Pages/ListChannelTasks.php +++ b/app/Filament/Resources/ChannelTasks/Pages/ListChannelTasks.php @@ -2,10 +2,20 @@ namespace App\Filament\Resources\ChannelTasks\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\ChannelTasks\ChannelTaskResource; +use App\Models\Property; use Filament\Resources\Pages\ListRecords; +use Illuminate\Database\Eloquent\Builder; class ListChannelTasks extends ListRecords { + use ListsCurrentProperty; + protected static string $resource = ChannelTaskResource::class; + + protected function scopeToProperty(Builder $query, Property $property): Builder + { + return $query->whereHas('channel', fn (Builder $channel) => $channel->where('property_id', $property->id)); + } } diff --git a/app/Filament/Resources/Channels/ChannelResource.php b/app/Filament/Resources/Channels/ChannelResource.php index 51fc2a6..f20d7eb 100644 --- a/app/Filament/Resources/Channels/ChannelResource.php +++ b/app/Filament/Resources/Channels/ChannelResource.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources\Channels; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\Channels\Pages\ChannelRules; use App\Filament\Resources\Channels\Pages\CreateChannel; use App\Filament\Resources\Channels\Pages\EditChannel; @@ -20,7 +21,7 @@ class ChannelResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = Channel::class; diff --git a/app/Filament/Resources/Channels/Pages/ListChannels.php b/app/Filament/Resources/Channels/Pages/ListChannels.php index b169aa5..c658df5 100644 --- a/app/Filament/Resources/Channels/Pages/ListChannels.php +++ b/app/Filament/Resources/Channels/Pages/ListChannels.php @@ -2,12 +2,15 @@ namespace App\Filament\Resources\Channels\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\Channels\ChannelResource; use Filament\Actions\CreateAction; use Filament\Resources\Pages\ListRecords; class ListChannels extends ListRecords { + use ListsCurrentProperty; + protected static string $resource = ChannelResource::class; protected function getHeaderActions(): array diff --git a/app/Filament/Resources/Channels/Schemas/ChannelForm.php b/app/Filament/Resources/Channels/Schemas/ChannelForm.php index fb41fb2..3b495bb 100644 --- a/app/Filament/Resources/Channels/Schemas/ChannelForm.php +++ b/app/Filament/Resources/Channels/Schemas/ChannelForm.php @@ -9,6 +9,7 @@ use App\Enums\ChannelStatus; use App\Models\Channel; use App\Models\Property; +use App\Support\CurrentProperty; use Filament\Forms\Components\DateTimePicker; use Filament\Forms\Components\Select; use Filament\Forms\Components\Textarea; @@ -30,7 +31,7 @@ public static function configure(Schema $schema, bool $selectProperty = false): ->components([ ...($selectProperty ? [Select::make('property_id')->label('Wohnung')->required() ->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all()) - ->default(fn (): ?int => Property::query()->where('is_active', true)->value('id')) + ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->disabled(fn (?Channel $record): bool => $record !== null)->dehydrated()->columnSpanFull()] : []), TextEntry::make('export_url') ->label('Eigener iCal-Exportlink') diff --git a/app/Filament/Resources/FloorPlans/FloorPlanResource.php b/app/Filament/Resources/FloorPlans/FloorPlanResource.php index a79db83..ca4e409 100644 --- a/app/Filament/Resources/FloorPlans/FloorPlanResource.php +++ b/app/Filament/Resources/FloorPlans/FloorPlanResource.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources\FloorPlans; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\FloorPlans\Pages\CreateFloorPlan; use App\Filament\Resources\FloorPlans\Pages\EditFloorPlan; use App\Filament\Resources\FloorPlans\Pages\ListFloorPlans; @@ -21,7 +22,7 @@ */ class FloorPlanResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = FloorPlan::class; diff --git a/app/Filament/Resources/FloorPlans/Pages/ListFloorPlans.php b/app/Filament/Resources/FloorPlans/Pages/ListFloorPlans.php index c488192..76a5659 100644 --- a/app/Filament/Resources/FloorPlans/Pages/ListFloorPlans.php +++ b/app/Filament/Resources/FloorPlans/Pages/ListFloorPlans.php @@ -2,12 +2,15 @@ namespace App\Filament\Resources\FloorPlans\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\FloorPlans\FloorPlanResource; use Filament\Actions\CreateAction; use Filament\Resources\Pages\ListRecords; class ListFloorPlans extends ListRecords { + use ListsCurrentProperty; + protected static string $resource = FloorPlanResource::class; protected function getHeaderActions(): array diff --git a/app/Filament/Resources/FloorPlans/Schemas/FloorPlanForm.php b/app/Filament/Resources/FloorPlans/Schemas/FloorPlanForm.php index 60241e0..5420a12 100644 --- a/app/Filament/Resources/FloorPlans/Schemas/FloorPlanForm.php +++ b/app/Filament/Resources/FloorPlans/Schemas/FloorPlanForm.php @@ -6,6 +6,7 @@ use App\Models\Amenity; use App\Models\FloorPlan; use App\Models\Property; +use App\Support\CurrentProperty; use Closure; use Filament\Forms\Components\Hidden; use Filament\Forms\Components\Repeater; @@ -28,7 +29,7 @@ public static function configure(Schema $schema): Schema Select::make('property_id') ->label('Wohnung') ->options(fn (): array => Property::query()->pluck('name', 'id')->all()) - ->default(fn (): ?int => Property::query()->value('id')) + ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->unique(ignoreRecord: true) ->validationMessages(['unique' => 'Für diese Wohnung gibt es schon einen Grundriss.']) ->required() diff --git a/app/Filament/Resources/Photos/Pages/ListPhotos.php b/app/Filament/Resources/Photos/Pages/ListPhotos.php index 1af3de9..152bb50 100644 --- a/app/Filament/Resources/Photos/Pages/ListPhotos.php +++ b/app/Filament/Resources/Photos/Pages/ListPhotos.php @@ -3,9 +3,11 @@ namespace App\Filament\Resources\Photos\Pages; use App\Enums\PhotoCategory; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\Photos\PhotoResource; use App\Models\Photo; use App\Models\Property; +use App\Support\CurrentProperty; use Filament\Actions\Action; use Filament\Actions\CreateAction; use Filament\Forms\Components\FileUpload; @@ -13,12 +15,23 @@ use Filament\Notifications\Notification; use Filament\Resources\Pages\ListRecords; use Filament\Support\Icons\Heroicon; +use Illuminate\Database\Eloquent\Builder; use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; class ListPhotos extends ListRecords { + use ListsCurrentProperty; + protected static string $resource = PhotoResource::class; + /** + * Photos without an apartment (e.g. the area) belong to every apartment. + */ + protected function scopeToProperty(Builder $query, Property $property): Builder + { + return $query->where(fn (Builder $query) => $query->where('property_id', $property->id)->orWhereNull('property_id')); + } + protected function getHeaderActions(): array { return [ @@ -42,7 +55,7 @@ protected function getHeaderActions(): array ->required(), ]) ->action(function (array $data): void { - $propertyId = Property::query()->value('id'); + $propertyId = app(CurrentProperty::class)->id(); $nextSort = (int) Photo::max('sort_order'); /** @var TemporaryUploadedFile $file */ diff --git a/app/Filament/Resources/Photos/PhotoResource.php b/app/Filament/Resources/Photos/PhotoResource.php index 4a372cd..64cf4cc 100644 --- a/app/Filament/Resources/Photos/PhotoResource.php +++ b/app/Filament/Resources/Photos/PhotoResource.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources\Photos; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\Photos\Pages\CreatePhoto; use App\Filament\Resources\Photos\Pages\EditPhoto; use App\Filament\Resources\Photos\Pages\ListPhotos; @@ -18,7 +19,7 @@ class PhotoResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = Photo::class; diff --git a/app/Filament/Resources/Photos/Schemas/PhotoForm.php b/app/Filament/Resources/Photos/Schemas/PhotoForm.php index 3862cdc..6326622 100644 --- a/app/Filament/Resources/Photos/Schemas/PhotoForm.php +++ b/app/Filament/Resources/Photos/Schemas/PhotoForm.php @@ -5,6 +5,7 @@ use App\Enums\PhotoCategory; use App\Models\Photo; use App\Models\Property; +use App\Support\CurrentProperty; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; use Filament\Forms\Components\TextInput; @@ -44,7 +45,7 @@ public static function configure(Schema $schema): Schema Select::make('property_id') ->label('Wohnung') ->options(fn (): array => Property::query()->pluck('name', 'id')->all()) - ->default(fn (): ?int => Property::query()->value('id')) + ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->placeholder('– keine (z. B. Umgebung) –'), Toggle::make('is_published') ->label('Auf der Webseite zeigen') diff --git a/app/Filament/Resources/PortalEmails/Pages/ListPortalEmails.php b/app/Filament/Resources/PortalEmails/Pages/ListPortalEmails.php index e50e539..f9b949c 100644 --- a/app/Filament/Resources/PortalEmails/Pages/ListPortalEmails.php +++ b/app/Filament/Resources/PortalEmails/Pages/ListPortalEmails.php @@ -2,10 +2,20 @@ namespace App\Filament\Resources\PortalEmails\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\PortalEmails\PortalEmailResource; +use App\Models\Property; use Filament\Resources\Pages\ListRecords; +use Illuminate\Database\Eloquent\Builder; class ListPortalEmails extends ListRecords { + use ListsCurrentProperty; + protected static string $resource = PortalEmailResource::class; + + protected function scopeToProperty(Builder $query, Property $property): Builder + { + return $query->whereHas('channel', fn (Builder $channel) => $channel->where('property_id', $property->id)); + } } diff --git a/app/Filament/Resources/PortalOccupations/Pages/ManagePortalOccupations.php b/app/Filament/Resources/PortalOccupations/Pages/ManagePortalOccupations.php index 9c72830..5b02e05 100644 --- a/app/Filament/Resources/PortalOccupations/Pages/ManagePortalOccupations.php +++ b/app/Filament/Resources/PortalOccupations/Pages/ManagePortalOccupations.php @@ -2,6 +2,7 @@ namespace App\Filament\Resources\PortalOccupations\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\PortalOccupations\PortalOccupationResource; use Filament\Resources\Pages\ManageRecords; use Filament\Schemas\Components\Tabs\Tab; @@ -9,6 +10,8 @@ class ManagePortalOccupations extends ManageRecords { + use ListsCurrentProperty; + protected static string $resource = PortalOccupationResource::class; public function getTabs(): array diff --git a/app/Filament/Resources/PortalOccupations/PortalOccupationResource.php b/app/Filament/Resources/PortalOccupations/PortalOccupationResource.php index 9f67535..e01205b 100644 --- a/app/Filament/Resources/PortalOccupations/PortalOccupationResource.php +++ b/app/Filament/Resources/PortalOccupations/PortalOccupationResource.php @@ -6,6 +6,7 @@ use App\Enums\OccupationKind; use App\Filament\Actions\AssignGuestAction; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\BlockedPeriods\BlockedPeriodResource; use App\Filament\Resources\Bookings\BookingResource; use App\Filament\Resources\Guests\GuestResource; @@ -32,7 +33,7 @@ class PortalOccupationResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = BlockedPeriod::class; diff --git a/app/Filament/Resources/Reviews/Pages/ListReviews.php b/app/Filament/Resources/Reviews/Pages/ListReviews.php index 5abcab3..9b8bd52 100644 --- a/app/Filament/Resources/Reviews/Pages/ListReviews.php +++ b/app/Filament/Resources/Reviews/Pages/ListReviews.php @@ -2,12 +2,15 @@ namespace App\Filament\Resources\Reviews\Pages; +use App\Filament\Concerns\ListsCurrentProperty; use App\Filament\Resources\Reviews\ReviewResource; use Filament\Actions\CreateAction; use Filament\Resources\Pages\ListRecords; class ListReviews extends ListRecords { + use ListsCurrentProperty; + protected static string $resource = ReviewResource::class; protected function getHeaderActions(): array diff --git a/app/Filament/Resources/Reviews/ReviewResource.php b/app/Filament/Resources/Reviews/ReviewResource.php index 28aef41..8eadef8 100644 --- a/app/Filament/Resources/Reviews/ReviewResource.php +++ b/app/Filament/Resources/Reviews/ReviewResource.php @@ -4,6 +4,7 @@ use App\Enums\ReviewStatus; use App\Filament\Concerns\FollowsAdminMode; +use App\Filament\Concerns\SelectsRecordProperty; use App\Filament\Resources\Reviews\Pages\CreateReview; use App\Filament\Resources\Reviews\Pages\EditReview; use App\Filament\Resources\Reviews\Pages\ListReviews; @@ -19,7 +20,7 @@ class ReviewResource extends Resource { - use FollowsAdminMode; + use FollowsAdminMode, SelectsRecordProperty; protected static ?string $model = Review::class; diff --git a/app/Filament/Resources/Reviews/Schemas/ReviewForm.php b/app/Filament/Resources/Reviews/Schemas/ReviewForm.php index 2346bf8..17efce3 100644 --- a/app/Filament/Resources/Reviews/Schemas/ReviewForm.php +++ b/app/Filament/Resources/Reviews/Schemas/ReviewForm.php @@ -5,6 +5,7 @@ use App\Enums\ReviewStatus; use App\Models\Property; use App\Models\Review; +use App\Support\CurrentProperty; use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\Select; use Filament\Forms\Components\Textarea; @@ -54,7 +55,7 @@ public static function configure(Schema $schema): Schema Select::make('property_id') ->label('Wohnung') ->options(fn (): array => Property::query()->pluck('name', 'id')->all()) - ->default(fn (): ?int => Property::query()->value('id')) + ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->required(), ]), Section::make('Öffentliche Antwort der Gastgeber') diff --git a/app/Http/Controllers/SwitchPropertyController.php b/app/Http/Controllers/SwitchPropertyController.php new file mode 100644 index 0000000..8363ca1 --- /dev/null +++ b/app/Http/Controllers/SwitchPropertyController.php @@ -0,0 +1,60 @@ +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) : ''); + } +} diff --git a/app/Models/Photo.php b/app/Models/Photo.php index 1f1201f..801dfd2 100644 --- a/app/Models/Photo.php +++ b/app/Models/Photo.php @@ -81,6 +81,20 @@ protected function published(Builder $query): Builder return $query->where('is_published', true)->orderBy('sort_order'); } + /** + * Photos of the apartment and those without an apartment (e.g. the area); without an apartment all photos. + * + * @param Builder $query + * @return Builder + */ + #[Scope] + protected function forProperty(Builder $query, ?Property $property): Builder + { + return $property === null + ? $query + : $query->where(fn (Builder $query) => $query->where('property_id', $property->id)->orWhereNull('property_id')); + } + public function url(string $size = 'md'): string { return $this->getFirstMediaUrl(self::COLLECTION, $size); diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php index 518cf21..085727e 100644 --- a/app/Providers/Filament/AdminPanelProvider.php +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -3,6 +3,7 @@ namespace App\Providers\Filament; use App\Enums\AdminMode; +use App\Http\Controllers\SwitchPropertyController; use App\Http\Middleware\ResolveTheme; use App\Models\User; use Filament\Actions\Action; @@ -25,6 +26,7 @@ use Illuminate\Foundation\Http\Middleware\PreventRequestForgery; use Illuminate\Routing\Middleware\SubstituteBindings; use Illuminate\Session\Middleware\StartSession; +use Illuminate\Support\Facades\Route; use Illuminate\View\Middleware\ShareErrorsFromSession; class AdminPanelProvider extends PanelProvider @@ -68,6 +70,11 @@ public function panel(Panel $panel): Panel ->brandName('FeWo Stommer · Verwaltung') // As long as the site is not switched live, every page shows where the e-mails go. ->renderHook(PanelsRenderHook::BODY_START, fn (): View => view('filament.test-mode-banner')) + // Apartment switcher at the top of the menu; lists and new records follow the chosen apartment. + ->renderHook(PanelsRenderHook::SIDEBAR_NAV_START, fn (): View => view('filament.property-switcher')) + ->authenticatedRoutes(function (): void { + Route::post('wohnung-wechseln', SwitchPropertyController::class)->name('switch-property'); + }) ->navigationGroups(['Vermietung', 'Webseite', 'System']) ->colors([ 'primary' => Color::Sky, diff --git a/app/Support/CurrentProperty.php b/app/Support/CurrentProperty.php new file mode 100644 index 0000000..0a83a86 --- /dev/null +++ b/app/Support/CurrentProperty.php @@ -0,0 +1,86 @@ +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 + */ + 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'); + } +} diff --git a/app/Themes/Sections/GallerySection.php b/app/Themes/Sections/GallerySection.php index 2cbe104..fb69e41 100644 --- a/app/Themes/Sections/GallerySection.php +++ b/app/Themes/Sections/GallerySection.php @@ -48,7 +48,7 @@ public function data(Theme $theme, array $data): array return [ 'heading' => $data['heading'] ?? null, - 'photos' => $this->withSeasonalPhotos($theme, $data, 'photo_ids', Photo::query()->published()->with('media') + 'photos' => $this->withSeasonalPhotos($theme, $data, 'photo_ids', Photo::query()->published()->forProperty($theme->property())->with('media') ->when($category, fn ($query) => $query->where('category', $category)) ->when(filled($data['limit'] ?? null), fn ($query) => $query->limit((int) $data['limit'])) ->get()), diff --git a/app/Themes/Sections/HeroSection.php b/app/Themes/Sections/HeroSection.php index 2afe8aa..9dd4df0 100644 --- a/app/Themes/Sections/HeroSection.php +++ b/app/Themes/Sections/HeroSection.php @@ -51,7 +51,9 @@ public static function photoSlots(): array public function data(Theme $theme, array $data): array { - $fallback = isset($data['photo_id']) ? Photo::find($data['photo_id']) : Photo::query()->published()->first(); + $fallback = isset($data['photo_id']) + ? Photo::find($data['photo_id']) + : Photo::query()->published()->when($theme->property(), fn ($query, $property) => $query->whereBelongsTo($property))->first() ?? Photo::query()->published()->first(); $featuredOffer = ($data['show_offer'] ?? true) && $theme->property() ? app(OfferShowcase::class)->current($theme->property(), limit: 1, featuredOnly: true)->first() : null; diff --git a/resources/views/filament/pages/occupancy-calendar.blade.php b/resources/views/filament/pages/occupancy-calendar.blade.php index 07c4093..3a81724 100644 --- a/resources/views/filament/pages/occupancy-calendar.blade.php +++ b/resources/views/filament/pages/occupancy-calendar.blade.php @@ -28,13 +28,6 @@ @php($calendar = $this->calendar)
- ← Zurück Weiter → diff --git a/resources/views/filament/property-switcher.blade.php b/resources/views/filament/property-switcher.blade.php new file mode 100644 index 0000000..81e75d1 --- /dev/null +++ b/resources/views/filament/property-switcher.blade.php @@ -0,0 +1,32 @@ +@php + /** Apartment switcher at the top of the admin menu (App\Support\CurrentProperty). */ + $current = app(\App\Support\CurrentProperty::class); + $property = $current->get(); +@endphp +@if ($property) +
+ + @if ($current->hasChoice()) +
+ @csrf + + + + @foreach ($current->options() as $id => $name) + + @endforeach + + + +
+ @else +

{{ $property->name }}

+ @endif + @can('update', $property) + + Einstellungen der Wohnung + + @endcan +
+
+@endif diff --git a/tests/Feature/CurrentPropertyTest.php b/tests/Feature/CurrentPropertyTest.php new file mode 100644 index 0000000..89c9f47 --- /dev/null +++ b/tests/Feature/CurrentPropertyTest.php @@ -0,0 +1,150 @@ +actingAs(User::factory()->create()); + $this->coast = Property::factory()->create(['name' => 'Küstenwohnung']); + $this->mountains = Property::factory()->create(['name' => 'Bergwohnung']); + } + + public function test_menu_shows_the_switcher_with_all_apartments_and_starts_with_the_first_active_one(): void + { + $this->get(OccupancyCalendar::getUrl()) + ->assertOk() + ->assertSee('Wohnung wählen') + ->assertSeeInOrder(['Bergwohnung', 'Küstenwohnung']) + ->assertSee('Einstellungen der Wohnung') + ->assertSee(PropertyResource::getUrl('edit', ['record' => $this->coast]), false); + } + + public function test_a_single_apartment_is_named_without_a_choice(): void + { + $this->mountains->delete(); + + $this->get(OccupancyCalendar::getUrl())->assertOk()->assertSee('Küstenwohnung')->assertDontSee('Wohnung wählen'); + } + + public function test_switching_returns_to_the_page_and_lists_follow_the_chosen_apartment(): void + { + Amenity::factory()->for($this->coast)->create(['name' => 'Strandkorb']); + Amenity::factory()->for($this->mountains)->create(['name' => 'Kachelofen']); + $list = AmenityResource::getUrl('index'); + + Livewire::test(ListAmenities::class)->assertSee('Strandkorb')->assertDontSee('Kachelofen'); + + $this->post(route('filament.admin.switch-property'), ['property' => $this->mountains->id, 'return' => $list])->assertRedirect($list); + + $this->assertSame($this->mountains->id, app(CurrentProperty::class)->id()); + Livewire::test(ListAmenities::class)->assertSee('Kachelofen')->assertDontSee('Strandkorb'); + } + + public function test_switching_leaves_a_record_page_for_its_list_and_the_apartment_settings_for_the_chosen_ones(): void + { + $amenity = Amenity::factory()->for($this->coast)->create(); + + $this->post(route('filament.admin.switch-property'), ['property' => $this->mountains->id, 'return' => AmenityResource::getUrl('edit', ['record' => $amenity])]) + ->assertRedirect(AmenityResource::getUrl('index')); + $this->post(route('filament.admin.switch-property'), ['property' => $this->coast->id, 'return' => PropertyResource::getUrl('edit', ['record' => $this->mountains])]) + ->assertRedirect(PropertyResource::getUrl('edit', ['record' => $this->coast])); + $this->post(route('filament.admin.switch-property'), ['property' => $this->coast->id, 'return' => OccupancyCalendar::getUrl(['monat' => '2026-11', 'wohnung' => $this->mountains->id])]) + ->assertRedirect(OccupancyCalendar::getUrl(['monat' => '2026-11'])); + $this->post(route('filament.admin.switch-property'), ['property' => $this->coast->id, 'return' => 'https://example.com/admin']) + ->assertRedirect(route('filament.admin.pages.dashboard')); + } + + public function test_another_landlords_apartment_cannot_be_chosen(): void + { + $foreign = Property::factory()->create(['tenant_id' => Tenant::factory()->create()->id]); + + $this->post(route('filament.admin.switch-property'), ['property' => $foreign->id])->assertNotFound(); + $this->assertSame($this->coast->id, app(CurrentProperty::class)->id()); + } + + public function test_opening_a_record_of_another_apartment_switches_to_it(): void + { + $amenity = Amenity::factory()->for($this->mountains)->create(); + + $this->get(AmenityResource::getUrl('edit', ['record' => $amenity]))->assertOk(); + + $this->assertSame($this->mountains->id, app(CurrentProperty::class)->id()); + } + + public function test_new_records_belong_to_the_chosen_apartment(): void + { + app(CurrentProperty::class)->select($this->mountains); + + Livewire::test(CreateAmenity::class)->assertSchemaStateSet(['property_id' => $this->mountains->id]); + } + + public function test_photo_list_shows_the_apartment_and_the_shared_area_photos(): void + { + app(CurrentProperty::class)->select($this->mountains); + $own = Photo::factory()->create(['property_id' => $this->mountains->id, 'alt_text' => 'Blick auf die Alpen']); + $area = Photo::factory()->create(['property_id' => null, 'category' => PhotoCategory::Area, 'alt_text' => 'Wanderweg']); + $other = Photo::factory()->create(['property_id' => $this->coast->id, 'alt_text' => 'Blick auf die Ostsee']); + + Livewire::test(ListPhotos::class)->assertCanSeeTableRecords([$own, $area])->assertCanNotSeeTableRecords([$other]); + } + + public function test_pages_for_one_apartment_follow_the_choice_and_name_it(): void + { + app(CurrentProperty::class)->select($this->mountains); + + Livewire::test(PricesAndOffers::class)->assertSet('propertyId', $this->mountains->id)->assertSee('Bergwohnung'); + $this->get(OccupancyCalendar::getUrl(['wohnung' => $this->coast->id]))->assertOk(); + $this->assertSame($this->coast->id, app(CurrentProperty::class)->id()); + } + + public function test_website_gallery_shows_only_the_photos_of_the_sections_apartment(): void + { + $this->withoutVite(); + $site = Site::factory()->create(['domain' => 'wohnungen.test', 'is_default' => true]); + $this->coast->update(['site_id' => $site->id]); + $this->mountains->update(['site_id' => $site->id]); + Photo::factory()->create(['property_id' => $this->coast->id, 'alt_text' => 'Blick auf die Ostsee']); + Photo::factory()->create(['property_id' => $this->mountains->id, 'alt_text' => 'Blick auf die Alpen']); + Photo::factory()->create(['property_id' => null, 'alt_text' => 'Gemeinsamer Ort']); + Page::factory()->for($site)->create([ + 'slug' => Page::HOME, + 'sections' => [['type' => 'gallery', 'data' => ['heading' => 'Bilder', 'property_id' => $this->mountains->id]]], + ]); + + $this->get('http://wohnungen.test/') + ->assertOk() + ->assertSee('Blick auf die Alpen') + ->assertSee('Gemeinsamer Ort') + ->assertDontSee('Blick auf die Ostsee'); + } +}