Loheide-eu-mitarbeiterplatt.../plugins-core/qm/src/QmTree.php
Friederich Loheide b3a057e27c
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m46s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m51s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m55s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 4m15s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m46s
CI / Build-Artefakte aktuell (pull_request) Successful in 52s
Neues Modul: Qualitätsmanagement
- Kategorien mit farbigen Ordnerkacheln; Farbe und Symbol je Ordner
  (Auswahl oder eigener Upload, SVG bereinigt), an Unterordner vererbt
- Ordnerseite: Einführungstext, Ansprechpartner (Profilfelder wählbar),
  Unterordner, Liste mit Dokument, Version, Stand, Größe, Aktion
- Einträge: Datei (Lesefassung + bearbeitbare Vorlage), Link,
  Online-Formular; neue Fassungen archivieren die bisherigen
- PDF-Betrachter im Portal (pdf.js)
- Eigene Volltextsuche (PDF, Word, Excel, PowerPoint, OpenDocument)
  mit Textausschnitt; optionale Texterkennung per Tesseract
- Rollen-Ausschluss für das ganze Modul, Pflegerecht qm.manage,
  Zielgruppen je Kategorie
- Vorbereitet: Wiedervorlage, Verantwortliche, Lesebestätigungen
- Abhängigkeit smalot/pdfparser

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

206 lines
6.7 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\Icons;
use App\Support\Permissions\Audiences;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
/**
* Ordnerstruktur des QM-Moduls, zwischengespeichert: Kategorien, Ordner, geerbte
* Farben und Sichtbarkeit (je Kategorie, vorbereitet über Zielgruppen).
*/
class QmTree
{
private const CACHE_KEY = 'qm.tree';
/** @var array<string, mixed>|null */
private ?array $data = null;
public static function flush(): void
{
Cache::forget(self::CACHE_KEY);
app(self::class)->data = null;
}
/**
* @return array{id: int, parent_id: ?int, category_id: ?int, name: string, color: string, own_color: ?string, icon: array{name?: string, url?: string, mono?: bool}, sort: int}|null
*/
public function folder(int $id): ?array
{
return $this->data()['folders'][$id] ?? null;
}
/**
* @return list<int>
*/
public function childIds(?int $id): array
{
return $this->data()['children'][$id ?? 0] ?? [];
}
/**
* @return list<array<string, mixed>> Weg von oben bis einschließlich des Ordners
*/
public function path(int $id): array
{
$path = [];
for ($folder = $this->folder($id); $folder && count($path) < 50; $folder = $folder['parent_id'] ? $this->folder($folder['parent_id']) : null) {
array_unshift($path, $folder);
}
return $path;
}
/**
* @param list<int> $ids
* @return list<int>
*/
public function descendantIds(array $ids): array
{
$result = [];
foreach ($ids as $id) {
foreach ($this->childIds($id) as $child) {
$result[] = $child;
array_push($result, ...$this->descendantIds([$child]));
}
}
return $result;
}
/**
* Kategorie eines Ordners (die des obersten Ordners im Pfad).
*/
public function categoryId(int $id): ?int
{
return $this->path($id)[0]['category_id'] ?? null;
}
public function isVisible(int $id, ?User $user): bool
{
if (! $this->folder($id)) {
return false;
}
$category = $this->categoryId($id);
return $category === null || $this->categoryVisible($category, $user);
}
public function categoryVisible(int $category, ?User $user): bool
{
return Audiences::matches($this->data()['category_audiences'][$category] ?? null, $user);
}
/**
* @return list<int>
*/
public function visibleIds(?User $user): array
{
return array_values(array_filter(array_keys($this->data()['folders']), fn (int $id) => $this->isVisible($id, $user)));
}
/**
* Kategorien mit ihren obersten Ordnern – für die Startseite.
*
* @return list<array{id: int|null, name: string, folders: list<int>}>
*/
public function categories(?User $user): array
{
$groups = [];
foreach ($this->data()['categories'] as $category) {
if (! $this->categoryVisible($category['id'], $user)) {
continue;
}
$folders = array_values(array_filter($this->childIds(null), fn (int $id) => $this->folder($id)['category_id'] === $category['id']));
if ($folders !== []) {
$groups[] = ['id' => $category['id'], 'name' => $category['name'], 'folders' => $folders];
}
}
// Oberste Ordner ohne (gültige) Kategorie
$known = array_column($this->data()['categories'], 'id');
$rest = array_values(array_filter($this->childIds(null), fn (int $id) => ! in_array($this->folder($id)['category_id'], $known, true)));
if ($rest !== []) {
$groups[] = ['id' => null, 'name' => $groups === [] ? 'Ordner' : 'Weitere', 'folders' => $rest];
}
return $groups;
}
/**
* Alle Ordner mit Einrückung – für Auswahllisten.
*
* @return array<int, string>
*/
public function options(?int $rootId = null, int $depth = 0): array
{
$options = [];
foreach ($this->childIds($rootId) as $id) {
$options[$id] = str_repeat('— ', $depth).$this->folder($id)['name'];
$options += $this->options($id, $depth + 1);
}
return $options;
}
/**
* @return array<string, mixed>
*/
private function data(): array
{
return $this->data ??= Cache::rememberForever(self::CACHE_KEY, function () {
$categories = DB::table('qm_categories')->orderBy('sort')->orderBy('name')->get(['id', 'name'])
->map(fn ($c) => ['id' => (int) $c->id, 'name' => $c->name])->all();
$rows = DB::table('qm_folders')->orderBy('sort')->orderBy('name')->get(['id', 'parent_id', 'category_id', 'name', 'color', 'icon', 'icon_path', 'icon_mono', 'sort']);
$folders = [];
$children = [];
foreach ($rows as $row) {
$folders[(int) $row->id] = [
'id' => (int) $row->id,
'parent_id' => $row->parent_id ? (int) $row->parent_id : null,
'category_id' => $row->category_id ? (int) $row->category_id : null,
'name' => $row->name,
'own_color' => Colors::has($row->color) ? $row->color : null,
'color' => Colors::DEFAULT,
'own_icon' => $row->icon_path
? ['url' => Storage::disk('uploads')->url($row->icon_path), 'mono' => (bool) $row->icon_mono]
: (Icons::has($row->icon) ? ['name' => $row->icon] : null),
'icon' => ['name' => 'folder'],
'sort' => (int) $row->sort,
];
$children[(int) ($row->parent_id ?? 0)][] = (int) $row->id;
}
// Farbe und Symbol vererben: von oben nach unten auflösen.
$resolve = function (int $parent, string $color, array $icon) use (&$resolve, &$folders, $children) {
foreach ($children[$parent] ?? [] as $id) {
$folders[$id]['color'] = $folders[$id]['own_color'] ?? $color;
$folders[$id]['icon'] = $folders[$id]['own_icon'] ?? $icon;
$resolve($id, $folders[$id]['color'], $folders[$id]['icon']);
}
};
$resolve(0, Colors::DEFAULT, ['name' => 'folder']);
return [
'categories' => $categories,
'folders' => $folders,
'children' => $children,
'category_audiences' => Audiences::allOfType('qm_category'),
];
});
}
}