Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m49s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m29s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m38s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m40s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m7s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m24s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
180 lines
6.6 KiB
PHP
180 lines
6.6 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Qm;
|
||
|
||
use App\Models\User;
|
||
use App\Support\ContactCards;
|
||
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 = ContactCards::CORE_FIELDS;
|
||
|
||
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 ContactCards $cards) {}
|
||
|
||
/**
|
||
* @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,
|
||
'requires_acknowledgement' => (bool) $entry->requires_acknowledgement,
|
||
'review' => [
|
||
'due_at' => $entry->review_due_at?->toDateString(),
|
||
'state' => Reviews::state($entry),
|
||
'interval_months' => $entry->review_interval_months,
|
||
'responsible' => $entry->responsible_id ? ['id' => $entry->responsible_id, 'name' => $entry->responsible?->name] : null,
|
||
'reviewed_at' => $entry->reviewed_at?->toIso8601String(),
|
||
],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @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);
|
||
|
||
return array_map(fn (array $card) => array_diff_key($card, ['subtitle' => true]), $this->cards->cards($people, $viewer, $fields));
|
||
}
|
||
|
||
/**
|
||
* 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 $this->cards->options();
|
||
}
|
||
|
||
private function formUrl(int $id): ?string
|
||
{
|
||
$class = 'Inhaus\\Plugins\\Forms\\Models\\Form';
|
||
|
||
return class_exists($class) ? $class::query()->find($id)?->url() : null;
|
||
}
|
||
}
|