Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m51s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m48s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m16s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m21s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m30s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 9m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m49s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m37s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m8s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
241 lines
11 KiB
PHP
241 lines
11 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Pages;
|
||
|
||
use App\Accessibility\EasyLanguage;
|
||
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\Content;
|
||
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\ContentSnippet;
|
||
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);
|
||
|
||
// Leichte Sprache als zweite Fassung von Seiten
|
||
$this->app->make(EasyLanguage::class)->register('pages', function (int $id, User $user) {
|
||
$page = Page::query()->find($id);
|
||
|
||
return $page ? [
|
||
'can_view' => $page->isPublished() && app(PageTree::class)->isVisible($page->id, $user),
|
||
'can_edit' => $user->can('update', $page),
|
||
'title' => $page->title,
|
||
'text' => fn () => app(Content::class)->plainText($page->content, publicOnly: true),
|
||
] : null;
|
||
});
|
||
Page::deleted(fn (Page $page) => app(EasyLanguage::class)->forget('pages', $page->id));
|
||
|
||
$abilities = $this->app->make(Abilities::class);
|
||
$abilities->define('pages.manage', 'Seiten: alle Seiten verwalten', 'Seiten');
|
||
$abilities->define('pages.edit', 'Seiten bearbeiten', 'Seiten', resource: 'page', portal: true);
|
||
$abilities->define('pages.publish', 'Seiten veröffentlichen', 'Seiten', resource: 'page', portal: true);
|
||
$abilities->define('pages.create', 'Unterseiten anlegen', 'Seiten', resource: 'page', portal: true);
|
||
$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->app->make(Editorial::class)->add(
|
||
'pages.snippets',
|
||
'Bausteine & Vorlagen',
|
||
'pages::EditorialSnippets',
|
||
25,
|
||
fn (User $user) => $user->can('pages.manage'),
|
||
fn (User $user) => $this->snippetList(),
|
||
);
|
||
$this->app->make(Editorial::class)->meta('pages', icon: 'file-text',
|
||
todo: function (User $user) {
|
||
// Eigene Entwürfe, die noch nicht veröffentlicht sind
|
||
$count = Page::query()->whereNotNull('draft_updated_at')->whereNull('scheduled_at')->where('draft_updated_by', $user->id)->count();
|
||
|
||
return ['count' => $count, 'label' => $count === 1 ? 'Seite mit unveröffentlichten Änderungen' : 'Seiten mit unveröffentlichten Änderungen'];
|
||
});
|
||
$this->app->make(Editorial::class)->meta('pages.snippets', icon: 'blocks');
|
||
|
||
$this->app->singleton(Snippets::class);
|
||
$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']);
|
||
|
||
$usages->add('snippets', 'Bausteine & Vorlagen', function (ContentSnippet $snippet) {
|
||
$entries = [];
|
||
|
||
foreach (ImagePaths::fromContent($snippet->content) as $context => $paths) {
|
||
$entries[] = ['context' => $context, 'label' => ContentSnippet::KINDS[$snippet->kind].': '.$snippet->name, 'url' => route('snippets.edit', $snippet, false), 'draft' => false, 'paths' => $paths];
|
||
}
|
||
|
||
return $entries;
|
||
}, fn (callable $each) => ContentSnippet::query()->chunkById(50, fn ($snippets) => $snippets->each($each)));
|
||
|
||
$usages->watch('snippets', ContentSnippet::class, ['content', 'name']);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function snippetList(): array
|
||
{
|
||
$usages = $this->app->make(Snippets::class)->usages();
|
||
|
||
return ['snippets' => ContentSnippet::query()->with('editor:id,name')->orderBy('kind')->orderBy('name')->get()->map(fn (ContentSnippet $snippet) => [
|
||
'id' => $snippet->id,
|
||
'kind' => $snippet->kind,
|
||
'name' => $snippet->name,
|
||
'description' => $snippet->description,
|
||
'blocks' => count($snippet->content['blocks'] ?? []),
|
||
'usages' => $usages[$snippet->id] ?? [],
|
||
'edit_url' => route('snippets.edit', $snippet),
|
||
'updated_at' => $snippet->updated_at?->toIso8601String(),
|
||
'editor' => $snippet->editor?->name,
|
||
])->all()];
|
||
}
|
||
|
||
private function registerBlocks(Blocks $blocks): void
|
||
{
|
||
// Baustein: gemeinsamer Inhalt, der auf vielen Seiten gleich erscheint
|
||
$blocks->add(BlockType::make(Snippets::BLOCK_TYPE, 'Baustein', 'component', 'layout', 'pages::Snippet')
|
||
->description('Wiederverwendbarer Inhalt – Änderungen am Baustein wirken auf allen Seiten.')
|
||
->fields([
|
||
Field::select('snippet', 'Baustein', fn () => ContentSnippet::query()->where('kind', ContentSnippet::BLOCK)->orderBy('name')->pluck('name', 'id')->all())
|
||
->help('Bausteine legen Sie unter Redaktion → Bausteine & Vorlagen an – oder direkt aus einem Block („Als Baustein speichern“).'),
|
||
])
|
||
->resolveUsing(fn (array $data, ?User $user, array $context) => $this->app->make(Snippets::class)->resolve($data, $user, $context)));
|
||
|
||
$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) {
|
||
// Nur Dateien der eigenen Seite – außerhalb einer Seite (Beitrag, Startseite) keine,
|
||
// sonst ließen sich Namen von Dateien fremder Seiten per Nummer auslesen.
|
||
if (! isset($context['page_id'])) {
|
||
return ['files' => []];
|
||
}
|
||
|
||
$files = PageFile::query()->whereIn('id', $data['files'])->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))
|
||
: []];
|
||
}));
|
||
}
|
||
}
|