app->make(Knowledge::class)->add('downloads', 'Downloads', fn (...$args) => app(DownloadKnowledge::class)(...$args)); $this->app->make(GlobalSearch::class)->add('downloads', 'Downloads', 'folder-down', fn (...$args) => app(DownloadSearch::class)(...$args), sort: 40); $this->app->singleton(FolderTree::class); $this->app->make(Navigation::class)->add('downloads', 'Downloads', '/downloads', active: 'downloads.*', sort: 50, icon: 'folder-down'); $abilities = $this->app->make(Abilities::class); $abilities->define('downloads.manage', 'Downloads: gesamten Bereich verwalten', 'Downloads'); $abilities->define('downloads.edit', 'Downloads pflegen', 'Downloads', resource: 'download_folder', portal: true); $abilities->resourceType( 'download_folder', 'Download-Ordner', fn () => $this->app->make(FolderTree::class)->options(), fn (array $ids) => $this->app->make(FolderTree::class)->descendantIds($ids), ); Gate::policy(DownloadFolder::class, DownloadFolderPolicy::class); // Startseite: gemerkte Dateien – ohne Favoriten die meistgenutzten. $this->app->make(Widgets::class)->add('downloads.favorites', 'downloads::MyDownloads', 'aside', sort: 25, props: function (User $user) { $visible = $this->app->make(FolderTree::class)->visibleIds($user) ?: [0]; $favorites = Download::query()->whereIn('folder_id', $visible) ->whereIn('id', DB::table('download_favorites')->where('user_id', $user->id)->select('download_id')) ->with('currentVersion')->orderBy('title')->limit(6)->get(); $popular = $favorites->isEmpty() ? Download::query()->whereIn('folder_id', $visible)->where('download_count', '>', 0)->with('currentVersion')->orderByDesc('download_count')->limit(5)->get() : collect(); return [ 'favorites' => $favorites->map(fn (Download $d) => DownloadPresenter::row($d, null, true))->all(), 'popular' => $popular->map(fn (Download $d) => DownloadPresenter::row($d))->all(), ]; }, label: 'Meine Downloads'); $this->app->make(Blocks::class)->add(BlockType::make('downloads.folder', 'Downloads', 'file-down', 'dynamic', 'downloads::FolderBlock') ->description('Die Dateien eines Download-Ordners – immer in der aktuellen Fassung.') ->fields([ Field::text('title', 'Überschrift', 150), Field::select('folder', 'Ordner', fn () => collect($this->app->make(FolderTree::class)->options())->mapWithKeys(fn ($name, $id) => [(string) $id => $name])->all()), Field::toggle('show_description', 'Beschreibungen anzeigen', true), ]) ->resolveUsing(function (array $data, ?User $user) { $tree = $this->app->make(FolderTree::class); $folderId = (int) $data['folder']; if (! $folderId || ! $tree->isVisible($folderId, $user)) { return null; } $downloads = Download::query()->where('folder_id', $folderId)->with('currentVersion')->orderBy('title')->limit(100)->get(); return [ 'folder_url' => route('downloads.folder', $folderId), 'downloads' => $downloads->map(fn (Download $d) => DownloadPresenter::row($d))->all(), ]; })); } }