*/ private static array $compositions = []; /** * @param Closure(Get): array $changes the adjustments currently in the form * @return list */ public static function make(Booking $booking, DocumentKind $kind, Closure $changes, ?Closure $visible = null): array { $compose = fn (Get $get): DocumentComposition => self::compose($booking, $kind, $changes($get)); return [ View::make('filament.document-composition') ->viewData(fn (Get $get): array => self::viewData($compose($get), $changes($get))), Checkbox::make('acknowledged') ->label('Ich habe die Hinweise geprüft und möchte das Dokument so senden.') ->visible(fn (Get $get): bool => ($visible === null || $visible($get)) && $compose($get)->toConfirm() !== []) ->accepted(), ]; } /** * One composition per form state and request (the view and the confirmation box both need it). * * @param array $changes */ public static function compose(Booking $booking, DocumentKind $kind, array $changes): DocumentComposition { $key = $booking->id.':'.$kind->value.':'.md5(serialize($changes)).':'.$booking->updated_at?->getTimestamp(); return self::$compositions[$key] ??= app(DocumentComposer::class)->compose($booking, $kind, $changes); } /** * @param array $changes * @return array */ private static function viewData(DocumentComposition $composition, array $changes): array { $user = auth()->user(); return [ 'composition' => $composition, 'expert' => $user instanceof User && $user->adminMode() === AdminMode::Expert, 'mailPreview' => self::mailPreview($composition), 'documentPreview' => self::documentPreview($composition), 'pdfUrl' => route('filament.admin.document-preview', ['booking' => $composition->draft->getRouteKey(), 'kind' => $composition->kind->value, 'changes' => base64_encode(json_encode($changes) ?: '[]')]), ]; } private static function mailPreview(DocumentComposition $composition): string { $draft = $composition->draft; if ($draft->guest === null) { return '

Keine Vorschau: Der Gast ist noch nicht bekannt.

'; } try { return match ($composition->kind) { DocumentKind::Offer => (new OfferMade($draft, '#'))->render(), DocumentKind::ExternalRecord => (new BookingDocumentation($draft))->render(), DocumentKind::StayConfirmation => (new StayConfirmed($draft))->render(), default => (new BookingConfirmed($draft))->render(), }; } catch (Throwable $exception) { report($exception); return '

Die Vorschau der E-Mail ist gerade nicht möglich.

'; } } private static function documentPreview(DocumentComposition $composition): string { try { [$view, $data] = app(RentalContract::class)->view($composition->draft, $composition->kind, draft: $composition->kind === DocumentKind::Offer); return view($view, $data)->render(); } catch (Throwable $exception) { report($exception); return '

Die Vorschau des Dokuments ist gerade nicht möglich.

'; } } }