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