Phase 6 (Teil 1): Dokumente und Organigramm
- Dokumente-Plugin: Ordnerbaum mit vererbten Rechten und Sichtbarkeit, Hochladen per Ziehen und Ablegen, Versionen, Suche, Pagebuilder-Block - Organigramm aus den Vorgesetzten, Vorgesetzte im Admin pflegbar - Personensuche im Kopf übernimmt nur noch Suchbegriffe des Verzeichnisses Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
21c706e5f5
commit
d6d5d4b0fe
110 changed files with 2209 additions and 208 deletions
177
plugins-core/documents/src/FolderTree.php
Normal file
177
plugins-core/documents/src/FolderTree.php
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Documents;
|
||||
|
||||
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 = 'documents.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('documents.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('document_folder');
|
||||
$folders = [];
|
||||
$children = [];
|
||||
|
||||
foreach (DB::table('document_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];
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue