|null */ private ?Collection $fields = null; /** * @return Collection */ public function all(): Collection { try { // Im Cache nur Arrays – Laravel deserialisiert dort keine Objekte. return $this->fields ??= ProfileField::hydrate(Cache::rememberForever( self::CACHE_KEY, fn () => ProfileField::query()->orderBy('sort')->orderBy('label')->get()->map->getAttributes()->all(), )); } catch (Throwable $e) { report($e); return collect(); } } public function flush(): void { $this->fields = null; Cache::forget(self::CACHE_KEY); } /** * @return array Schlüssel => Bezeichnung */ public function options(): array { return $this->all()->pluck('label', 'key')->all(); } /** * Felder mit Werten, die $viewer bei $person sehen darf. * * @return list */ public function visibleValues(User $person, ?User $viewer): array { $seesRestricted = $viewer && ($viewer->is($person) || $viewer->can('users.manage')); $values = (array) ($person->custom_fields ?? []); return $this->all() ->filter(fn (ProfileField $field) => $field->visibility === 'public' || $seesRestricted) ->filter(fn (ProfileField $field) => filled($values[$field->key] ?? null)) ->map(fn (ProfileField $field) => [ 'key' => $field->key, 'label' => $field->label, 'type' => $field->type, 'value' => $this->display($field, (string) $values[$field->key]), ]) ->values() ->all(); } /** * Definitionen und Werte für das Bearbeitungsformular der Person selbst. * * @param list $managed vom Identity Provider gepflegte Felder („custom.“) * @return list> */ public function formFor(User $user, array $managed): array { $values = (array) ($user->custom_fields ?? []); return $this->all()->map(fn (ProfileField $field) => [ 'key' => $field->key, 'label' => $field->label, 'type' => $field->type, 'help' => $field->help, 'restricted' => $field->visibility === 'restricted', 'value' => $values[$field->key] ?? '', 'readonly' => ! $field->editable || in_array('custom.'.$field->key, $managed, true), 'managed' => in_array('custom.'.$field->key, $managed, true), ])->values()->all(); } /** * @return array> */ public function rules(string $prefix = 'custom_fields'): array { return $this->all()->mapWithKeys(fn (ProfileField $field) => [ "{$prefix}.{$field->key}" => array_merge(['nullable'], match ($field->type) { 'email' => ['email', 'max:255'], 'url' => ['url', 'max:500'], 'date' => ['date'], 'phone' => ['string', 'max:50'], 'textarea' => ['string', 'max:5000'], default => ['string', 'max:255'], }), ])->all(); } /** * Nur die Felder übernehmen, die die Person selbst pflegen darf. * * @param array $input * @param list $managed */ public function mergeUserInput(User $user, array $input, array $managed): void { $values = (array) ($user->custom_fields ?? []); foreach ($this->all() as $field) { if ($field->editable && ! in_array('custom.'.$field->key, $managed, true) && array_key_exists($field->key, $input)) { $values[$field->key] = filled($input[$field->key]) ? (string) $input[$field->key] : null; } } $user->forceFill(['custom_fields' => array_filter($values, fn ($v) => $v !== null)])->save(); } private function display(ProfileField $field, string $value): string { if ($field->type === 'date') { try { return Carbon::parse($value)->format('d.m.Y'); } catch (Throwable) { return $value; } } return $value; } }