Loheide-eu-mitarbeiterplatt.../plugins-core/downloads/src/DownloadsPlugin.php
Friederich Loheide 8a9399b6a5
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 8m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 8m46s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 11m36s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 11m31s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 11m34s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 12m12s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 12m14s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 46s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 11m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m15s
Symbole für alle Hauptpunkte im automatischen Menü (0.40.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 18:02:11 +00:00

81 lines
3.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\Dashboard\Widgets;
use App\Models\User;
use App\Pagebuilder\Blocks;
use App\Pagebuilder\BlockType;
use App\Pagebuilder\Field;
use App\Plugins\Plugin;
use App\Search\GlobalSearch;
use App\Support\Abilities;
use App\Support\Navigation;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Inhaus\Plugins\Downloads\Models\Download;
use Inhaus\Plugins\Downloads\Models\DownloadFolder;
use Inhaus\Plugins\Downloads\Policies\DownloadFolderPolicy;
class DownloadsPlugin extends Plugin
{
public function register(): void
{
$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(),
];
}));
}
}