Seiten: Bausteine und Vorlagen (0.22.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m50s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m33s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m11s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m24s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m17s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m50s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m33s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m11s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m24s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m17s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
8817727283
commit
2166a7e11a
129 changed files with 1381 additions and 267 deletions
|
|
@ -23,6 +23,7 @@ use Illuminate\Validation\Rule;
|
|||
use Illuminate\Validation\Rules\File;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Inhaus\Plugins\Pages\Models\ContentSnippet;
|
||||
use Inhaus\Plugins\Pages\Models\Page;
|
||||
use Inhaus\Plugins\Pages\Models\PageFile;
|
||||
use Inhaus\Plugins\Pages\Models\PageRevision;
|
||||
|
|
@ -46,6 +47,7 @@ class PageEditorController extends Controller
|
|||
$data = $request->validate([
|
||||
'title' => ['required', 'string', 'max:200'],
|
||||
'parent_id' => ['nullable', 'integer', Rule::exists('pages', 'id')],
|
||||
'template_id' => ['nullable', 'integer', Rule::exists('content_snippets', 'id')->where('kind', ContentSnippet::TEMPLATE)],
|
||||
]);
|
||||
|
||||
$parent = isset($data['parent_id']) ? Page::query()->findOrFail($data['parent_id']) : null;
|
||||
|
|
@ -58,7 +60,13 @@ class PageEditorController extends Controller
|
|||
'created_by' => $request->user()->id,
|
||||
]);
|
||||
|
||||
Audit::log('page.created', ['page' => $page->id, 'title' => $page->title]);
|
||||
// Vorlage: Inhalt als erster Entwurf (Kopie – spätere Änderungen an der Vorlage wirken nicht zurück)
|
||||
if (isset($data['template_id'])) {
|
||||
$template = ContentSnippet::query()->findOrFail($data['template_id']);
|
||||
$page->forceFill(['draft' => $this->content->sanitize($template->content), 'draft_title' => $page->title, 'draft_updated_at' => now()])->save();
|
||||
}
|
||||
|
||||
Audit::log('page.created', ['page' => $page->id, 'title' => $page->title, 'template' => $data['template_id'] ?? null]);
|
||||
|
||||
return to_route('pages.editor', $page);
|
||||
}
|
||||
|
|
@ -358,7 +366,7 @@ class PageEditorController extends Controller
|
|||
|
||||
foreach ($request->input('blocks') as $block) {
|
||||
if (is_string($block['id'] ?? null)) {
|
||||
$props[$block['id']] = $this->content->preview($block, $request->user(), ['page_id' => $page->id]);
|
||||
$props[$block['id']] = $this->content->preview($block, $request->user(), ['page_id' => $page->id, 'editing' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -413,6 +421,7 @@ class PageEditorController extends Controller
|
|||
'delete' => $user->can('delete', $page),
|
||||
'create_child' => $user->can('create', [Page::class, $page]),
|
||||
'navigation' => $page->parent_id === null && $user->can('pages.manage'),
|
||||
'snippets' => $user->can('pages.manage'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
@ -422,7 +431,7 @@ class PageEditorController extends Controller
|
|||
*
|
||||
* @return array{role: list<array{value: string, label: string}>, department: list<string>, location: list<string>}
|
||||
*/
|
||||
private function audienceOptions(): array
|
||||
public static function audienceOptions(): array
|
||||
{
|
||||
return Cache::remember('pagebuilder.audience-options', 300, fn () => [
|
||||
'role' => Role::query()->orderBy('name')->get(['id', 'name'])->map(fn (Role $role) => ['value' => (string) $role->id, 'label' => $role->name])->all(),
|
||||
|
|
|
|||
139
plugins-core/pages/src/Http/Controllers/SnippetController.php
Normal file
139
plugins-core/pages/src/Http/Controllers/SnippetController.php
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Pages\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Media\MediaLibrary;
|
||||
use App\Pagebuilder\Blocks;
|
||||
use App\Pagebuilder\Content;
|
||||
use App\Support\Audit;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\Rules\File;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Inhaus\Plugins\Pages\Models\ContentSnippet;
|
||||
use Inhaus\Plugins\Pages\Snippets;
|
||||
|
||||
/**
|
||||
* Bausteine und Vorlagen („Redaktion → Bausteine & Vorlagen“). Pflegen darf, wer
|
||||
* alle Seiten verwalten darf; Bausteine einsetzen darf jede Redaktion.
|
||||
*/
|
||||
class SnippetController extends Controller
|
||||
{
|
||||
public function __construct(private readonly Content $content) {}
|
||||
|
||||
/**
|
||||
* Auswahl für Editor und „Neue Seite“.
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate(['kind' => ['nullable', Rule::in(array_keys(ContentSnippet::KINDS))]]);
|
||||
|
||||
return response()->json(['snippets' => ContentSnippet::query()
|
||||
->when($request->query('kind'), fn ($q, $kind) => $q->where('kind', $kind))
|
||||
->orderBy('name')
|
||||
->get(['id', 'kind', 'name', 'description'])]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorizeManage($request);
|
||||
$data = $request->validate([
|
||||
'kind' => ['required', Rule::in(array_keys(ContentSnippet::KINDS))],
|
||||
'name' => ['required', 'string', 'max:120'],
|
||||
'description' => ['nullable', 'string', 'max:300'],
|
||||
'content' => ['present', 'array'],
|
||||
], [], ['name' => 'Name']);
|
||||
|
||||
$snippet = ContentSnippet::query()->create([
|
||||
...$data,
|
||||
'content' => $this->content->sanitize($data['content']),
|
||||
'created_by' => $request->user()->id,
|
||||
'updated_by' => $request->user()->id,
|
||||
]);
|
||||
|
||||
Audit::log('snippet.created', ['snippet' => $snippet->id, 'kind' => $snippet->kind, 'name' => $snippet->name]);
|
||||
|
||||
return response()->json(['snippet' => $snippet->only(['id', 'kind', 'name', 'description']), 'edit_url' => route('snippets.edit', $snippet)], 201);
|
||||
}
|
||||
|
||||
public function edit(Request $request, ContentSnippet $snippet, Blocks $blocks, Snippets $snippets): Response
|
||||
{
|
||||
$this->authorizeManage($request);
|
||||
|
||||
return Inertia::render('pages::SnippetEditor', [
|
||||
'snippet' => [
|
||||
...$snippet->only(['id', 'kind', 'name', 'description']),
|
||||
'content' => $snippet->content ?: Content::empty(),
|
||||
'updated_at' => $snippet->updated_at?->toIso8601String(),
|
||||
'editor' => $snippet->editor?->name,
|
||||
],
|
||||
'kinds' => ContentSnippet::KINDS,
|
||||
'usages' => $snippet->kind === ContentSnippet::BLOCK ? ($snippets->usages()[$snippet->id] ?? []) : [],
|
||||
// Ein Baustein darf sich nicht selbst enthalten
|
||||
'blockTypes' => array_values(array_filter($blocks->forEditor(), fn (array $type) => $snippet->kind === ContentSnippet::TEMPLATE || $type['key'] !== Snippets::BLOCK_TYPE)),
|
||||
'blockGroups' => Blocks::GROUPS,
|
||||
'audienceOptions' => PageEditorController::audienceOptions(),
|
||||
'uploadsUrl' => rtrim(Storage::disk('uploads')->url(''), '/'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, ContentSnippet $snippet): JsonResponse
|
||||
{
|
||||
$this->authorizeManage($request);
|
||||
$data = $request->validate([
|
||||
'name' => ['required', 'string', 'max:120'],
|
||||
'description' => ['nullable', 'string', 'max:300'],
|
||||
'content' => ['present', 'array'],
|
||||
], [], ['name' => 'Name']);
|
||||
|
||||
$snippet->update([...$data, 'content' => $this->content->sanitize($data['content']), 'updated_by' => $request->user()->id]);
|
||||
Audit::log('snippet.updated', ['snippet' => $snippet->id, 'name' => $snippet->name]);
|
||||
|
||||
return response()->json(['snippet' => $snippet->only(['id', 'name', 'description']), 'updated_at' => $snippet->updated_at?->toIso8601String()]);
|
||||
}
|
||||
|
||||
public function destroy(Request $request, ContentSnippet $snippet): RedirectResponse
|
||||
{
|
||||
$this->authorizeManage($request);
|
||||
$snippet->delete();
|
||||
Audit::log('snippet.deleted', ['snippet' => $snippet->id, 'name' => $snippet->name]);
|
||||
|
||||
return back()->with('status', ContentSnippet::KINDS[$snippet->kind].' „'.$snippet->name.'“ gelöscht.');
|
||||
}
|
||||
|
||||
public function uploadImage(Request $request, MediaLibrary $library): JsonResponse
|
||||
{
|
||||
$this->authorizeManage($request);
|
||||
$request->validate(['image' => ['required', File::image()->types(['jpg', 'jpeg', 'png', 'webp', 'gif'])->max(15 * 1024)]]);
|
||||
|
||||
$media = $library->store($request->file('image'), 'pagebuilder/snippets', $request->user());
|
||||
|
||||
return response()->json(['path' => $media->path, 'url' => Blocks::imageUrl($media->path), 'media' => $media->toLibraryArray()]);
|
||||
}
|
||||
|
||||
public function preview(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorizeManage($request);
|
||||
$request->validate(['blocks' => ['required', 'array', 'max:100'], 'blocks.*' => ['array']]);
|
||||
|
||||
$props = [];
|
||||
|
||||
foreach ($request->input('blocks') as $block) {
|
||||
if (is_string($block['id'] ?? null)) {
|
||||
$props[$block['id']] = $this->content->preview($block, $request->user(), ['editing' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['props' => (object) $props]);
|
||||
}
|
||||
|
||||
private function authorizeManage(Request $request): void
|
||||
{
|
||||
abort_unless($request->user()->can('pages.manage'), 403);
|
||||
}
|
||||
}
|
||||
35
plugins-core/pages/src/Models/ContentSnippet.php
Normal file
35
plugins-core/pages/src/Models/ContentSnippet.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Pages\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Baustein (kind = block): ein Inhalt, der auf vielen Seiten eingebunden wird –
|
||||
* Änderungen wirken überall. Vorlage (kind = template): Ausgangsinhalt für neue Seiten.
|
||||
*/
|
||||
#[Fillable(['kind', 'name', 'description', 'content', 'created_by', 'updated_by'])]
|
||||
class ContentSnippet extends Model
|
||||
{
|
||||
public const BLOCK = 'block';
|
||||
|
||||
public const TEMPLATE = 'template';
|
||||
|
||||
public const KINDS = [self::BLOCK => 'Baustein', self::TEMPLATE => 'Vorlage'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['content' => 'array'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
public function editor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'updated_by');
|
||||
}
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ 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;
|
||||
|
|
@ -89,6 +90,16 @@ class PagesPlugin extends Plugin
|
|||
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->singleton(Snippets::class);
|
||||
$this->registerBlocks($this->app->make(Blocks::class));
|
||||
$this->registerMediaUsages($this->app->make(MediaUsages::class));
|
||||
}
|
||||
|
|
@ -126,10 +137,51 @@ class PagesPlugin extends Plugin
|
|||
}, 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([
|
||||
|
|
|
|||
75
plugins-core/pages/src/Snippets.php
Normal file
75
plugins-core/pages/src/Snippets.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Pages;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Pagebuilder\Content;
|
||||
use Inhaus\Plugins\Pages\Models\ContentSnippet;
|
||||
use Inhaus\Plugins\Pages\Models\Page;
|
||||
|
||||
/**
|
||||
* Bausteine im Pagebuilder: Anzeige (mit Schutz vor Endlosschleifen) und Verwendungsnachweis.
|
||||
*/
|
||||
class Snippets
|
||||
{
|
||||
public const BLOCK_TYPE = 'pages.snippet';
|
||||
|
||||
/** @var list<int> gerade angezeigte Bausteine (verschachtelt) */
|
||||
private array $stack = [];
|
||||
|
||||
/**
|
||||
* Daten für den Block „Baustein“ – null blendet ihn aus.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function resolve(array $data, ?User $user, array $context): ?array
|
||||
{
|
||||
$id = (int) ($data['snippet'] ?? 0);
|
||||
$snippet = $id ? ContentSnippet::query()->where('kind', ContentSnippet::BLOCK)->find($id) : null;
|
||||
|
||||
if (! $snippet || in_array($id, $this->stack, true) || count($this->stack) >= 3) {
|
||||
// Im Editor einen Hinweis zeigen, auf der Seite nichts
|
||||
return ($context['editing'] ?? false) ? ['missing' => true] : null;
|
||||
}
|
||||
|
||||
$this->stack[] = $id;
|
||||
|
||||
try {
|
||||
$blocks = app(Content::class)->render($snippet->content, $user, [...$context, 'snippet_id' => $id])['blocks'];
|
||||
} finally {
|
||||
array_pop($this->stack);
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $snippet->name,
|
||||
'blocks' => $blocks,
|
||||
'edit_url' => $user?->can('pages.manage') ? route('snippets.edit', $snippet) : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Seiten, die einen Baustein einbinden (veröffentlicht oder im Entwurf).
|
||||
*
|
||||
* @return array<int, list<array{id: int, title: string}>> Baustein => Seiten
|
||||
*/
|
||||
public function usages(): array
|
||||
{
|
||||
$content = app(Content::class);
|
||||
$usages = [];
|
||||
|
||||
Page::query()->get(['id', 'title', 'content', 'draft'])->each(function (Page $page) use ($content, &$usages) {
|
||||
$ids = [];
|
||||
|
||||
foreach ([$page->content, $page->draft] as $tree) {
|
||||
array_push($ids, ...$content->collect($tree, fn (array $block) => ($block['type'] ?? null) === self::BLOCK_TYPE ? [(int) ($block['data']['snippet'] ?? 0)] : []));
|
||||
}
|
||||
|
||||
foreach (array_unique(array_filter($ids)) as $id) {
|
||||
$usages[$id][] = ['id' => $page->id, 'title' => $page->title];
|
||||
}
|
||||
});
|
||||
|
||||
return $usages;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue