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

@@ -0,0 +1,33 @@
<?php
namespace App\Filament\Concerns;
use App\Models\Property;
use App\Support\CurrentProperty;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
/**
* Limits a resource list to the apartment chosen in the admin menu (CurrentProperty).
* Records stay reachable by URL; opening one switches to its apartment (SelectsRecordProperty).
*/
trait ListsCurrentProperty
{
protected function getTableQuery(): Builder|Relation|null
{
$query = parent::getTableQuery();
$property = app(CurrentProperty::class)->get();
return $query instanceof Builder && $property !== null ? $this->scopeToProperty($query, $property) : $query;
}
/**
* @param Builder<Model> $query
* @return Builder<Model>
*/
protected function scopeToProperty(Builder $query, Property $property): Builder
{
return $query->where($query->qualifyColumn('property_id'), $property->id);
}
}
@@ -0,0 +1,34 @@
<?php
namespace App\Filament\Concerns;
use App\Models\Property;
use App\Support\CurrentProperty;
use Closure;
use Illuminate\Database\Eloquent\Model;
/**
* Opening a record of another apartment (e.g. from a notification e-mail) switches the admin to that apartment,
* so the menu always shows the apartment the open record belongs to.
*/
trait SelectsRecordProperty
{
public static function resolveRecordRouteBinding(int|string $key, ?Closure $modifyQuery = null): ?Model
{
$record = parent::resolveRecordRouteBinding($key, $modifyQuery);
$propertyId = $record !== null ? static::recordPropertyId($record) : null;
if ($propertyId !== null && $propertyId !== app(CurrentProperty::class)->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;
}
}
+7 -1
View File
@@ -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]
+7 -8
View File
@@ -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([
@@ -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;
@@ -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
@@ -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)'),
@@ -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)
@@ -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
@@ -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;
@@ -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')
@@ -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(),
@@ -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));
}
}
@@ -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;
@@ -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
@@ -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')
@@ -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;
@@ -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
@@ -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()
@@ -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 */
@@ -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;
@@ -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')
@@ -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));
}
}
@@ -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
@@ -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;
@@ -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
@@ -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;
@@ -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')
@@ -0,0 +1,60 @@
<?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) : '');
}
}
+14
View File
@@ -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<Photo> $query
* @return Builder<Photo>
*/
#[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);
@@ -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,
+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');
}
}
+1 -1
View File
@@ -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()),
+3 -1
View File
@@ -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;
@@ -28,13 +28,6 @@
@php($calendar = $this->calendar)
<div class="occupancy-plan">
<div class="occupancy-controls">
<label>Wohnung
<select wire:model.live="propertyId">
@foreach (\App\Models\Property::query()->orderBy('name')->get() as $property)
<option value="{{ $property->id }}">{{ $property->name }}</option>
@endforeach
</select>
</label>
<label>Monat <input type="month" wire:model.live="month" required></label>
<x-filament::button wire:click="moveMonth(-1)" color="gray" aria-label="Vorheriger Monat">← Zurück</x-filament::button>
<x-filament::button wire:click="moveMonth(1)" color="gray" aria-label="Nächster Monat">Weiter →</x-filament::button>
@@ -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)
<div class="fi-property-switcher" style="margin-bottom:1rem">
<x-filament::section compact icon="heroicon-o-home-modern" heading="Wohnung">
@if ($current->hasChoice())
<form method="POST" action="{{ route('filament.admin.switch-property') }}">
@csrf
<input type="hidden" name="return" value="{{ request()->fullUrl() }}">
<x-filament::input.wrapper>
<x-filament::input.select name="property" aria-label="Wohnung wählen" x-data x-on:change="$el.form.submit()">
@foreach ($current->options() as $id => $name)
<option value="{{ $id }}" @selected($id === $property->id)>{{ $name }}</option>
@endforeach
</x-filament::input.select>
</x-filament::input.wrapper>
<noscript><x-filament::button type="submit" size="sm" style="margin-top:.5rem">Wechseln</x-filament::button></noscript>
</form>
@else
<p style="font-weight:600">{{ $property->name }}</p>
@endif
@can('update', $property)
<x-filament::link :href="\App\Filament\Resources\Properties\PropertyResource::getUrl('edit', ['record' => $property])" size="sm" style="margin-top:.5rem">
Einstellungen der Wohnung
</x-filament::link>
@endcan
</x-filament::section>
</div>
@endif
+150
View File
@@ -0,0 +1,150 @@
<?php
namespace Tests\Feature;
use App\Enums\PhotoCategory;
use App\Filament\Pages\OccupancyCalendar;
use App\Filament\Pages\PricesAndOffers;
use App\Filament\Resources\Amenities\AmenityResource;
use App\Filament\Resources\Amenities\Pages\CreateAmenity;
use App\Filament\Resources\Amenities\Pages\ListAmenities;
use App\Filament\Resources\Photos\Pages\ListPhotos;
use App\Filament\Resources\Properties\PropertyResource;
use App\Models\Amenity;
use App\Models\Page;
use App\Models\Photo;
use App\Models\Property;
use App\Models\Site;
use App\Models\Tenant;
use App\Models\User;
use App\Support\CurrentProperty;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class CurrentPropertyTest extends TestCase
{
use RefreshDatabase;
private Property $coast;
private Property $mountains;
protected function setUp(): void
{
parent::setUp();
$this->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');
}
}