>, children: array>}|null */ private ?array $data = null; /** @var array> */ 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|null */ public function get(int $id): ?array { return $this->data()['folders'][$id] ?? null; } /** * @return list */ public function childIds(?int $id): array { return $this->data()['children'][$id ?? 0] ?? []; } /** * @param list $ids * @return list */ 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> */ 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 */ 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}> */ 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 */ 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>, children: array>} */ 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]; }); } }