- Tabellen media und media_usages, Modelle Media/MediaUsage - App\Media\MediaLibrary (store/register/replace/delete/rebuild) und Registry App\Media\MediaUsages (Quellen: Seiten, Neuigkeiten inkl. künftiger content-Spalte, Termine, Startseiten-Kacheln), ImagePaths-Extraktor - Alle Uploads landen in der Mediathek (Seiten-Editor direkt, übrige über Beobachter beim Speichern); Daten-Migration und media:rebuild übernehmen bestehende Bilder - Recht media.manage, Zugang media.use für alle Redaktionsbereiche - /redaktion/mediathek (Suche, Filter, Upload mit Fortschritt, Details, Eingebunden in, Ersetzen, Löschen mit Warnung, Mehrfachauswahl), Abschnitt in der Redaktion und Menüpunkt im Admin-Bereich - MediaPicker (Hochladen/Mediathek) im Pagebuilder: Bildfelder und Galerie (Mehrfachauswahl), editor.pickMedia im provide-API - Bild-URLs mit ?v=Änderungszeit, null bei fehlender Datei; Modelle löschen geteilte Bilder nicht mehr selbst Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
161 lines
7.2 KiB
PHP
161 lines
7.2 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Pages;
|
||
|
||
use App\Editorial\Editorial;
|
||
use App\Media\ImagePaths;
|
||
use App\Media\MediaUsages;
|
||
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\Http\Request;
|
||
use Illuminate\Support\Facades\Gate;
|
||
use Illuminate\Support\Facades\Schedule;
|
||
use Inhaus\Plugins\Pages\Models\Page;
|
||
use Inhaus\Plugins\Pages\Models\PageFile;
|
||
use Inhaus\Plugins\Pages\Policies\PagePolicy;
|
||
|
||
class PagesPlugin extends Plugin
|
||
{
|
||
public function register(): void
|
||
{
|
||
$this->app->make(GlobalSearch::class)->add('pages', 'Seiten', 'file-text', fn (...$args) => app(PageSearch::class)(...$args), sort: 30);
|
||
|
||
$this->app->singleton(PageTree::class);
|
||
|
||
$abilities = $this->app->make(Abilities::class);
|
||
$abilities->define('pages.manage', 'Seiten: alle Seiten verwalten', 'Seiten');
|
||
$abilities->define('pages.edit', 'Seiten bearbeiten', 'Seiten', resource: 'page');
|
||
$abilities->define('pages.publish', 'Seiten veröffentlichen', 'Seiten', resource: 'page');
|
||
$abilities->resourceType(
|
||
'page',
|
||
'Seite',
|
||
fn () => $this->app->make(PageTree::class)->options(),
|
||
fn (array $ids) => $this->app->make(PageTree::class)->descendantIds($ids),
|
||
);
|
||
|
||
Gate::policy(Page::class, PagePolicy::class);
|
||
|
||
// Seiten der obersten Ebene mit „In der Hauptnavigation“ erscheinen dort.
|
||
$this->app->make(Navigation::class)->provider('pages', function (Request $request, ?User $user) {
|
||
$current = trim((string) $request->route('path'), '/');
|
||
|
||
return array_map(fn (array $page) => [
|
||
'key' => 'page-'.$page['id'],
|
||
'label' => $page['title'],
|
||
'href' => '/seiten/'.$page['path'],
|
||
'active' => $request->routeIs('pages.show') && ($current === $page['path'] || str_starts_with($current, $page['path'].'/')),
|
||
'sort' => 40 + $page['sort'],
|
||
], $this->app->make(PageTree::class)->navigation($user));
|
||
});
|
||
|
||
// Menü-Editor: „Seite“ als Menüpunkt, auf Wunsch mit ihren Unterseiten als Untermenü.
|
||
$this->app->make(Navigation::class)->source('page', 'Seite', fn () => $this->app->make(PageTree::class)->options(), function (string $id, array $item, Request $request, ?User $user) {
|
||
$tree = $this->app->make(PageTree::class);
|
||
$page = $tree->get((int) $id);
|
||
|
||
if (! $page || ! $tree->isVisible($page['id'], $user)) {
|
||
return null;
|
||
}
|
||
|
||
$current = trim((string) $request->route('path'), '/');
|
||
$isActive = fn (string $path) => $request->routeIs('pages.show') && ($current === $path || str_starts_with($current, $path.'/'));
|
||
$children = [];
|
||
|
||
if (! empty($item['subpages'])) {
|
||
foreach ($tree->childIds($page['id']) as $childId) {
|
||
$child = $tree->get($childId);
|
||
|
||
if ($child && $tree->isVisible($childId, $user)) {
|
||
$children[] = ['label' => $child['title'], 'href' => '/seiten/'.$child['path'], 'active' => $isActive($child['path'])];
|
||
}
|
||
}
|
||
}
|
||
|
||
return ['label' => $page['title'], 'href' => '/seiten/'.$page['path'], 'active' => $isActive($page['path']), 'children' => $children];
|
||
});
|
||
|
||
$this->app->make(Editorial::class)->add(
|
||
'pages',
|
||
'Seiten',
|
||
'pages::EditorialPages',
|
||
20,
|
||
fn (User $user) => app(PagePolicy::class)->viewAny($user),
|
||
fn (User $user) => EditorialPages::props($user),
|
||
);
|
||
|
||
$this->registerBlocks($this->app->make(Blocks::class));
|
||
$this->registerMediaUsages($this->app->make(MediaUsages::class));
|
||
}
|
||
|
||
public function boot(): void
|
||
{
|
||
// Zeitgesteuerte Veröffentlichungen. Beim Aufruf einer Seite wird ein fälliger Stand zusätzlich sofort übernommen.
|
||
Schedule::call(function () {
|
||
Page::query()->whereNotNull('scheduled_at')->where('scheduled_at', '<=', now())->get()->each->publishIfDue();
|
||
})->everyMinute()->name('pages-scheduled')->withoutOverlapping(5);
|
||
}
|
||
|
||
/**
|
||
* Mediathek „Eingebunden in“: veröffentlichter Inhalt, Entwurf und geplanter Stand.
|
||
*/
|
||
private function registerMediaUsages(MediaUsages $usages): void
|
||
{
|
||
$usages->add('pages', 'Seiten', function (Page $page) {
|
||
$entries = [];
|
||
$url = route('pages.editor', $page, false);
|
||
|
||
foreach (['content' => false, 'scheduled_content' => true, 'draft' => true] as $column => $draft) {
|
||
$title = match ($column) {
|
||
'draft' => $page->draft_title ?? $page->title,
|
||
'scheduled_content' => $page->scheduled_title ?? $page->title,
|
||
default => $page->title,
|
||
};
|
||
|
||
foreach (ImagePaths::fromContent($page->{$column}) as $context => $paths) {
|
||
$entries[] = ['context' => $context, 'label' => $title, 'url' => $url, 'draft' => $draft, 'paths' => $paths];
|
||
}
|
||
}
|
||
|
||
return $entries;
|
||
}, fn (callable $each) => Page::query()->chunkById(50, fn ($pages) => $pages->each($each)));
|
||
|
||
$usages->watch('pages', Page::class, ['content', 'draft', 'scheduled_content', 'title', 'draft_title', 'scheduled_title']);
|
||
}
|
||
|
||
private function registerBlocks(Blocks $blocks): void
|
||
{
|
||
$blocks->add(BlockType::make('downloads', 'Downloads', 'file-down', 'media', 'pages::Downloads')
|
||
->description('Dateien zum Herunterladen – nur für Personen, die die Seite sehen dürfen.')
|
||
->fields([
|
||
Field::text('title', 'Überschrift', 150),
|
||
Field::ids('files', 'Dateien', 'page-file'),
|
||
])
|
||
->resolveUsing(function (array $data, ?User $user, array $context) {
|
||
$files = PageFile::query()->whereIn('id', $data['files'])
|
||
->when(isset($context['page_id']), fn ($q) => $q->where('page_id', $context['page_id']))
|
||
->get()->keyBy('id');
|
||
|
||
return ['files' => collect($data['files'])->map(fn ($id) => $files->get($id)?->toBlockArray())->filter()->values()->all()];
|
||
}));
|
||
|
||
$blocks->add(BlockType::make('subpages', 'Unterseiten', 'folder-tree', 'dynamic', 'pages::Subpages')
|
||
->description('Verweise auf die Unterseiten dieser Seite.')
|
||
->fields([
|
||
Field::text('title', 'Überschrift', 150),
|
||
Field::select('layout', 'Darstellung', ['cards' => 'Kacheln', 'list' => 'Liste'], 'cards'),
|
||
])
|
||
->resolveUsing(function (array $data, ?User $user, array $context) {
|
||
$tree = $this->app->make(PageTree::class);
|
||
|
||
return ['pages' => isset($context['page_id'])
|
||
? array_map(fn (array $page) => ['id' => $page['id'], 'title' => $page['title'], 'url' => $page['url']], $tree->visibleTree($context['page_id'], $user, 1))
|
||
: []];
|
||
}));
|
||
}
|
||
}
|