Loheide-eu-mitarbeiterplatt.../app/Support/ContactCards.php
Friederich Loheide b276e1c6d0
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m24s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m46s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m31s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Globale Live-Suche, Kontaktkarten-Personensuche, neue Beitragsansicht, Termine mit Seitenleiste, Demo-Inhalte, Plugin-Upload-Fix
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:07:13 +00:00

79 lines
2.9 KiB
PHP

<?php
namespace App\Support;
use App\Models\User;
/**
* Kontaktkarten: Person mit ausgewählten Profilfeldern (Kern- und eigene Felder).
* Genutzt von der Personensuche auf der Startseite und von Plugins (z. B. QM-Ansprechpartner).
*/
class ContactCards
{
/** Kernfelder des Profils, die auf Karten erscheinen können. */
public const CORE_FIELDS = [
'job_title' => 'Funktion',
'department' => 'Abteilung',
'location' => 'Standort',
'phone' => 'Telefon',
'mobile' => 'Mobil',
'email' => 'E-Mail-Adresse',
];
/** Felder der Personensuche auf der Startseite (Einstellung) */
public const PEOPLE_SEARCH_FIELDS = 'people.search_fields';
public const DEFAULT_PEOPLE_SEARCH_FIELDS = ['email', 'phone', 'mobile', 'location'];
public function __construct(private readonly ProfileFields $profileFields) {}
/**
* @return array<string, string> auswählbare Felder für Einstellungen
*/
public function options(): array
{
return self::CORE_FIELDS + collect($this->profileFields->options())->mapWithKeys(fn ($label, $key) => ['custom.'.$key => $label.' (Profilfeld)'])->all();
}
/**
* @param iterable<User> $people
* @param list<string> $fields Schlüssel aus options()
* @return list<array{id: int, name: string, initials: string, avatar_url: ?string, subtitle: ?string, fields: list<array{key: string, label: string, value: string, type: string}>}>
*/
public function cards(iterable $people, ?User $viewer, array $fields): array
{
$result = [];
foreach ($people as $person) {
if (! $person->is_active) {
continue;
}
$values = [];
$custom = collect($this->profileFields->visibleValues($person, $viewer))->keyBy('key');
foreach ($fields as $field) {
if (isset(self::CORE_FIELDS[$field])) {
$value = $person->{$field};
if (filled($value)) {
$values[] = ['key' => $field, 'label' => self::CORE_FIELDS[$field], 'value' => (string) $value, 'type' => in_array($field, ['phone', 'mobile'], true) ? 'phone' : ($field === 'email' ? 'email' : 'text')];
}
} elseif (str_starts_with($field, 'custom.') && ($item = $custom->get(substr($field, 7)))) {
$values[] = ['key' => $field, 'label' => $item['label'], 'value' => (string) $item['value'], 'type' => $item['type'] ?? 'text'];
}
}
$result[] = [
'id' => $person->id,
'name' => $person->name,
'initials' => $person->initials,
'avatar_url' => $person->avatarUrl(160),
'subtitle' => collect([$person->job_title, $person->department])->filter()->implode(' · ') ?: null,
'fields' => $values,
];
}
return $result;
}
}