Loheide-eu-mitarbeiterplatt.../plugins-core/downloads/src/FolderTree.php
Friederich Loheide f78cc96be7
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m38s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m54s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m50s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m55s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m18s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
„Dokumente“ heißt jetzt „Downloads“
Der Name „Dokumente“ bleibt dem kommenden QM-Modul (QM-Dokumente und
-Formulare) vorbehalten.

- Plugin plugins-core/downloads (Inhaus\Plugins\Downloads), Adresse
  /downloads, Rechte downloads.*, Objekttyp download_folder,
  Block downloads.folder, Kachel downloads.favorites
- Migration für bestehende Installationen: Tabellen und Spalten,
  Rechte, Sichtbarkeiten, Menü, Startseiten-Anordnung, Blöcke in
  Seiten und Fassungen, Plugin-Zustand; mit Test
- Alte Links unter /dokumente leiten dauerhaft weiter

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

177 lines
4.9 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\Downloads;
use App\Models\User;
use App\Support\Permissions\Audiences;
use App\Support\Permissions\Permissions;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
/**
* Ordnerbaum als gecachte Struktur (nur Arrays). Sichtbar ist ein Ordner, wenn
* die Person seine Zielgruppe und die aller übergeordneten Ordner erfüllt.
*/
class FolderTree
{
private const CACHE_KEY = 'downloads.tree';
/** @var array{folders: array<int, array<string, mixed>>, children: array<int, list<int>>}|null */
private ?array $data = null;
/** @var array<int, array<int, bool>> */
private array $visible = [];
public static function flush(bool $structureChanged = false): void
{
Cache::forget(self::CACHE_KEY);
app(self::class)->data = null;
app(self::class)->visible = [];
if ($structureChanged) {
Permissions::flush();
}
}
/**
* @return array<string, mixed>|null
*/
public function get(int $id): ?array
{
return $this->data()['folders'][$id] ?? null;
}
/**
* @return list<int>
*/
public function childIds(?int $id): array
{
return $this->data()['children'][$id ?? 0] ?? [];
}
/**
* @param list<int> $ids
* @return list<int>
*/
public function descendantIds(array $ids): array
{
$result = [];
$queue = $ids;
while ($queue !== []) {
foreach ($this->childIds(array_shift($queue)) as $child) {
if (! in_array($child, $result, true)) {
$result[] = $child;
$queue[] = $child;
}
}
}
return $result;
}
/**
* @return list<array<string, mixed>>
*/
public function ancestors(int $id): array
{
$result = [];
$parent = $this->get($id)['parent_id'] ?? null;
while ($parent !== null && ($folder = $this->get($parent)) && count($result) < 50) {
array_unshift($result, $folder);
$parent = $folder['parent_id'];
}
return $result;
}
public function isVisible(int $id, ?User $user): bool
{
if ($user?->can('downloads.manage')) {
return $this->get($id) !== null;
}
$key = $user?->id ?? 0;
if (isset($this->visible[$key][$id])) {
return $this->visible[$key][$id];
}
$folder = $this->get($id);
$visible = $folder !== null
&& Audiences::matches($folder['audience'], $user)
&& ($folder['parent_id'] === null || $this->isVisible($folder['parent_id'], $user));
return $this->visible[$key][$id] = $visible;
}
/**
* IDs aller sichtbaren Ordner – für die Suche.
*
* @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)));
}
/**
* @return list<array{id: int, name: string, children: list<mixed>}>
*/
public function visibleTree(?int $rootId, ?User $user, int $maxDepth = 8): array
{
if ($maxDepth <= 0) {
return [];
}
$result = [];
foreach ($this->childIds($rootId) as $id) {
if ($this->isVisible($id, $user)) {
$result[] = ['id' => $id, 'name' => $this->get($id)['name'], 'children' => $this->visibleTree($id, $user, $maxDepth - 1)];
}
}
return $result;
}
/**
* @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->get($id)['name'];
$options += $this->options($id, $depth + 1);
}
return $options;
}
/**
* @return array{folders: array<int, array<string, mixed>>, children: array<int, list<int>>}
*/
private function data(): array
{
return $this->data ??= Cache::rememberForever(self::CACHE_KEY, function () {
$audiences = Audiences::allOfType('download_folder');
$folders = [];
$children = [];
foreach (DB::table('download_folders')->orderBy('sort')->orderBy('name')->get(['id', 'parent_id', 'name']) as $row) {
$folders[$row->id] = [
'id' => (int) $row->id,
'parent_id' => $row->parent_id !== null ? (int) $row->parent_id : null,
'name' => $row->name,
'audience' => $audiences[$row->id] ?? null,
];
$children[(int) ($row->parent_id ?? 0)][] = (int) $row->id;
}
return ['folders' => $folders, 'children' => $children];
});
}
}