'Funktion', 'department' => 'Abteilung', 'location' => 'Standort', 'phone' => 'Telefon', 'mobile' => 'Mobil', 'email' => 'E-Mail-Adresse', ]; public const DEFAULT_CONTACT_FIELDS = ['job_title', 'phone', 'email']; /** QM-Startseite: Einführungstext, Hauptansprechpartner, Liste der QM-Beauftragten */ public const HOME_INTRO = 'qm.home.intro'; public const HOME_CONTACTS = 'qm.home.contacts'; public const OFFICERS = 'qm.home.officers'; public function __construct(private readonly QmTree $tree, private readonly Settings $settings, private readonly ProfileFields $profileFields) {} /** * @param array $counts Ordner => Anzahl Einträge * @return array */ public function tile(int $id, array $counts = []): array { $folder = $this->tree->folder($id); $children = count($this->tree->childIds($id)); return [ 'id' => $id, 'name' => $folder['name'], 'color' => $folder['color'], 'icon' => $folder['icon'], 'folders' => $children, 'entries' => (int) ($counts[$id] ?? 0), 'url' => route('qm.folder', $id), ]; } /** * @return array */ public function entry(QmEntry $entry, bool $withFolder = false): array { $reading = $entry->relationLoaded('readingFile') ? $entry->readingFile : null; $editable = $entry->relationLoaded('editableFile') ? $entry->editableFile : null; $main = $reading ?? $editable; $open = match ($entry->type) { QmEntry::LINK => ['url' => $entry->url, 'kind' => 'link'], QmEntry::FORM => ['url' => $entry->form_id ? $this->formUrl($entry->form_id) : null, 'kind' => 'form'], default => $main ? ($main->isViewable() ? ['url' => route('qm.view', $entry), 'kind' => 'view'] : ['url' => $main->url(), 'kind' => 'download']) : ['url' => null, 'kind' => 'none'], }; return [ 'id' => $entry->id, 'type' => $entry->type, 'title' => $entry->title, 'description' => $entry->description, 'version' => $entry->version, 'valid_from' => $entry->valid_from?->toDateString(), 'size' => $main?->size, 'extension' => $main?->extension(), 'open_url' => $open['url'], 'open_kind' => $open['kind'], 'download_url' => $main?->url(), 'editable' => $editable ? ['name' => $editable->name, 'extension' => $editable->extension(), 'url' => $editable->url(), 'size' => $editable->size] : null, 'searchable' => $main ? in_array($main->text_status, ['ok', 'ocr'], true) : null, 'text_status' => $main?->text_status, 'url' => $entry->url, 'form_id' => $entry->form_id, 'folder' => $withFolder ? $this->folderLabel($entry->folder_id) : null, ]; } /** * @return array{id: int, path: string, color: string} */ public function folderLabel(int $id): array { return [ 'id' => $id, 'path' => implode(' › ', array_column($this->tree->path($id), 'name')), 'color' => $this->tree->folder($id)['color'] ?? Colors::DEFAULT, 'url' => route('qm.folder', $id), ]; } /** * Ansprechpartner mit den im Admin gewählten Profilfeldern. * * @param iterable $people * @return list> */ public function contacts(iterable $people, ?User $viewer): array { $fields = (array) $this->settings->get(self::CONTACT_FIELDS, self::DEFAULT_CONTACT_FIELDS); $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' => $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' => $item['value'], 'type' => $item['type'] ?? 'text']; } } $result[] = [ 'id' => $person->id, 'name' => $person->name, 'initials' => $person->initials, 'avatar_url' => $person->avatarUrl(160), 'fields' => $values, ]; } return $result; } /** * Hauptansprechpartner der QM-Startseite (in der gespeicherten Reihenfolge). * * @return list> */ public function homeContacts(?User $viewer): array { $ids = array_map('intval', (array) $this->settings->get(self::HOME_CONTACTS, [])); $users = User::query()->whereIn('id', $ids ?: [0])->get()->keyBy('id'); return $this->contacts(array_filter(array_map(fn (int $id) => $users[$id] ?? null, $ids)), $viewer); } /** * QM-Beauftragte mit Zuständigkeit und optional Ordner. * * @return list> */ public function officers(?User $viewer): array { $rows = array_values(array_filter((array) $this->settings->get(self::OFFICERS, []), fn ($row) => is_array($row) && ! empty($row['user_id']))); $users = User::query()->whereIn('id', array_map(fn ($row) => (int) $row['user_id'], $rows) ?: [0])->get()->keyBy('id'); $result = []; foreach ($rows as $row) { $user = $users[(int) $row['user_id']] ?? null; $contact = $user ? ($this->contacts([$user], $viewer)[0] ?? null) : null; if (! $contact) { continue; } $folderId = (int) ($row['folder_id'] ?? 0); $result[] = [ ...$contact, 'area' => filled($row['area'] ?? null) ? (string) $row['area'] : null, 'folder' => $folderId && $this->tree->isVisible($folderId, $viewer) ? $this->folderLabel($folderId) : null, ]; } return $result; } /** * @return array auswählbare Felder für die Einstellungen */ public function contactFieldOptions(): array { return self::CORE_FIELDS + collect($this->profileFields->options())->mapWithKeys(fn ($label, $key) => ['custom.'.$key => $label.' (Profilfeld)'])->all(); } private function formUrl(int $id): ?string { $class = 'Inhaus\\Plugins\\Forms\\Models\\Form'; return class_exists($class) ? $class::query()->find($id)?->url() : null; } }