[setting, value that means yes, value that means no]. */ private const LEGACY = [ self::COLLECTS_PAYMENT => ['payments', 'portal', 'landlord'], self::PROVIDES_CONTRACT_DOCUMENTS => ['documents', 'portal', 'landlord'], self::MESSAGES_GUEST => ['guest_messages', 'portal', 'landlord'], ]; /** * @return array */ public static function definitions(): array { return [ self::COLLECTS_PAYMENT => ['label' => 'Inkasso', 'question' => 'Zieht das Portal das Geld des Gastes ein?'], self::PROVIDES_CONTRACT_DOCUMENTS => ['label' => 'Vertragsunterlagen', 'question' => 'Stellt das Portal dem Gast die verbindlichen Vertragsunterlagen bereit?'], self::MESSAGES_GUEST => ['label' => 'Gastkommunikation', 'question' => 'Schreibt das Portal den Gast an (Anreise-Information, Erinnerungen)?'], self::SENDS_CONFIRMATION_MAIL => ['label' => 'Bestätigungsmail', 'question' => 'Schickt das Portal Ihnen eine Buchungsbestätigung per E-Mail?'], ]; } /** * The capabilities of a channel; a draft (key => bool|null) overrides what is stored, to show what a change would do. * * @param array|null $draft * @return array */ public function resolve(Channel $channel, ?array $draft = null): array { $stored = (array) (($channel->settings ?? [])['capabilities'] ?? []); $legacy = (array) (($channel->process_overrides ?? [])['booking'] ?? []); $suggested = (array) (config('channels.portal_profiles.'.app(ChannelProcessConfiguration::class)->profileKey($channel).'.capabilities') ?? []); $result = []; foreach (array_keys(self::definitions()) as $key) { if ($draft !== null && array_key_exists($key, $draft) && $draft[$key] !== null && $draft[$key] !== '') { // A value the landlord only left as shown keeps its origin; a changed one is a pending confirmation. $value = filter_var($draft[$key], FILTER_VALIDATE_BOOLEAN); $base = $this->resolve($channel)[$key]; if ($base->value !== $value) { $result[$key] = new Capability($key, $value, Capability::CONFIRMED, null, now()->toIso8601String(), auth()->user()?->name); continue; } } if (isset($stored[$key]['value'])) { $result[$key] = new Capability($key, (bool) $stored[$key]['value'], Capability::CONFIRMED, null, $stored[$key]['at'] ?? null, $stored[$key]['by'] ?? null); continue; } if (isset(self::LEGACY[$key]) && filled($legacy[self::LEGACY[$key][0]] ?? null)) { [$setting, $yes, $no] = self::LEGACY[$key]; $value = (string) $legacy[$setting]; if (in_array($value, [$yes, $no], true)) { $result[$key] = new Capability($key, $value === $yes, Capability::DERIVED, $value === $yes ? 'Portal' : 'wir'); continue; } } $result[$key] = array_key_exists($key, $suggested) && $suggested[$key] !== null ? new Capability($key, (bool) $suggested[$key], Capability::SUGGESTED) : new Capability($key, null, Capability::UNKNOWN); } return $result; } public function value(Channel $channel, string $key): ?bool { return $this->resolve($channel)[$key]->value; } /** * What is stored to confirm the capabilities of a draft, and the readable list of what changed. * * @param array $draft * @return array{capabilities: array, changes: list} */ public function confirm(Channel $channel, array $draft, ?string $by): array { $stored = (array) (($channel->settings ?? [])['capabilities'] ?? []); $changes = []; foreach (self::definitions() as $key => $definition) { if (! array_key_exists($key, $draft) || $draft[$key] === null || $draft[$key] === '') { continue; } $value = filter_var($draft[$key], FILTER_VALIDATE_BOOLEAN); if (isset($stored[$key]['value']) && (bool) $stored[$key]['value'] === $value) { continue; } $changes[] = $definition['label'].': '.(isset($stored[$key]['value']) ? ($stored[$key]['value'] ? 'ja' : 'nein').' → ' : '').($value ? 'ja' : 'nein'); $stored[$key] = ['value' => $value, 'at' => now()->toIso8601String(), 'by' => $by]; } return ['capabilities' => $stored, 'changes' => $changes]; } }