Loheide-eu-mitarbeiterplatt.../plugins-core/qm/src/Presenter.php
Friederich Loheide 5a360d3cc7
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m47s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m6s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m7s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 4m3s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 3m56s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 3m26s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 3m34s
CI / Build-Artefakte aktuell (pull_request) Successful in 54s
QM-Startseite: Einführungstext, Hauptansprechpartner, QM-Beauftragte
- Pflege unter QM-Einstellungen → „Startseite des Qualitätsmanagements“
- QM-Beauftragte mit Zuständigkeit und optionalem Ordner-Link,
  Kontaktangaben wie bei den Ansprechpartnern

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 16:17:17 +00:00

209 lines
7.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Inhaus\Plugins\Qm;
use App\Models\User;
use App\Support\ProfileFields;
use App\Support\Settings;
use Inhaus\Plugins\Qm\Models\QmEntry;
/**
* Daten für das Portal: Ordnerkacheln, Einträge und Ansprechpartner.
*/
class Presenter
{
public const CONTACT_FIELDS = 'qm.contact_fields';
/** Kernfelder des Profils, die bei Ansprechpartnern erscheinen können. */
public const CORE_FIELDS = [
'job_title' => '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<int, int> $counts Ordner => Anzahl Einträge
* @return array<string, mixed>
*/
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<string, mixed>
*/
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<User> $people
* @return list<array<string, mixed>>
*/
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<array<string, mixed>>
*/
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<array<string, mixed>>
*/
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<string, string> 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;
}
}