Übertrage Abschnitte auf eine andere Seite: kopieren oder verschieben aus dem Abschnittskopf
Zwei Schalter im Abschnittskopf öffnen einen Dialog mit Zielseite (Auswahlfeld) und Position (Anfang, Ende, hinter einem Abschnitt). Die Zielseite erhält einen neuen Entwurf; beim Verschieben wird die Quellseite als Entwurf gespeichert. Vor dem Schreiben werden Ziel und Quelle geprüft. Vergebene Sprungmarken werden auf der Zielseite nummeriert. Kein neues Seitenformat (ADR-65). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7e7019b717
commit
5bd72dedab
15 files changed
+659
No files matched your search
@@ -5,6 +5,8 @@
|
||||
use App\Domain\Catalog\PillarService;
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\PageService;
|
||||
use App\Domain\Cms\Sections\PageSections;
|
||||
use App\Domain\Cms\SectionTransfer;
|
||||
use App\Filament\Resources\Pages\PageResource;
|
||||
use App\Filament\Support\ChoosesLinkTargets;
|
||||
use App\Filament\Support\ChoosesWebsiteImages;
|
||||
@@ -241,6 +243,101 @@ public function saveFromBlock(): void
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abschnitt auf eine andere Seite kopieren oder verschieben (Schalter im Abschnittskopf). Der Abschnitt stammt aus
|
||||
* dem Arbeitsstand dieser Seite. Die Zielseite erhält eine neue Entwurfsrevision. Beim Verschieben wird er danach
|
||||
* aus dem Arbeitsstand entfernt und die Seite als Entwurf gespeichert. Geprüft wird vor dem ersten Schreiben:
|
||||
* scheitert eine Seite, bleibt beides unverändert.
|
||||
*/
|
||||
public function transferSection(string $key, int $targetId, string $position, bool $move): void
|
||||
{
|
||||
$this->forceRender();
|
||||
$raw = is_array($this->data['sections'] ?? null) ? $this->data['sections'] : [];
|
||||
$target = Page::query()->find($targetId);
|
||||
|
||||
if (! isset($raw[$key]) || $target === null || $target->is($this->record)) {
|
||||
Notification::make()->title('Zielseite oder Abschnitt nicht gefunden')->danger()->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
abort_unless(auth()->user()?->can('update', $target) ?? false, 403);
|
||||
|
||||
// Der Arbeitsstand in der Form wie beim Speichern (formatierte Texte als HTML, Leeres entfernt).
|
||||
try {
|
||||
$state = $this->form->getState()['sections'] ?? [];
|
||||
} catch (ValidationException) {
|
||||
Notification::make()->title($move ? 'Abschnitt nicht verschoben' : 'Abschnitt nicht kopiert')->body('Auf dieser Seite gibt es noch Eingabefehler. Bitte zuerst beheben.')->danger()->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Die Schlüssel des Formulars gehen beim Aufbereiten verloren; die Reihenfolge bleibt.
|
||||
$index = array_search($key, array_map('strval', array_keys($raw)), true);
|
||||
$items = array_values(is_array($state) ? $state : []);
|
||||
|
||||
if ($index === false || ! isset($items[$index])) {
|
||||
Notification::make()->title('Zielseite oder Abschnitt nicht gefunden')->danger()->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$transfer = app(SectionTransfer::class);
|
||||
$section = SectionEditor::fromEditorState([$items[$index]])[0] ?? [];
|
||||
$remaining = $items;
|
||||
unset($remaining[$index]);
|
||||
|
||||
try {
|
||||
$transfer->plan($target, $section, $position, ! $move);
|
||||
|
||||
if ($move) {
|
||||
// Die Quellseite muss ohne den Abschnitt gültig bleiben, sonst wird nichts geschrieben.
|
||||
app(PageSections::class)->normalize(SectionEditor::fromEditorState($remaining));
|
||||
}
|
||||
|
||||
$result = $transfer->insert($target, $section, $position, ! $move, auth()->user());
|
||||
} catch (ValidationException $exception) {
|
||||
Notification::make()
|
||||
->title($move ? 'Abschnitt nicht verschoben' : 'Abschnitt nicht kopiert')
|
||||
->body(collect($exception->errors())->flatten()->first() ?? 'Die Seite wäre danach ungültig.')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($move) {
|
||||
unset($raw[$key]);
|
||||
$this->data['sections'] = $raw;
|
||||
|
||||
try {
|
||||
$this->save(shouldRedirect: false, shouldSendSavedNotification: false);
|
||||
} catch (ValidationException $exception) {
|
||||
foreach ($exception->errors() as $errorKey => $messages) {
|
||||
foreach ((array) $messages as $message) {
|
||||
$this->addError($errorKey, $message);
|
||||
}
|
||||
}
|
||||
Notification::make()->title('Auf der Zielseite eingefügt, hier nicht gespeichert')->body('Bitte die markierten Fehler beheben und „Entwurf speichern“ wählen; sonst steht der Abschnitt auf beiden Seiten.')->warning()->send();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$anchorNote = match (true) {
|
||||
! $result['anchorChanged'] => '',
|
||||
$result['anchor'] !== null => ' Die Sprungmarke war dort vergeben und heißt jetzt „'.$result['anchor'].'“.',
|
||||
default => ' Die Sprungmarke war dort vergeben; der Abschnitt hat keine mehr.',
|
||||
};
|
||||
|
||||
Notification::make()
|
||||
->title($move ? 'Abschnitt verschoben' : 'Abschnitt kopiert')
|
||||
->body('Als Abschnitt '.$result['position'].' auf '.$target->path.' eingefügt (Entwurf, noch nicht veröffentlicht).'.$anchorNote)
|
||||
->success()
|
||||
->actions([Action::make('open')->label('Zielseite öffnen')->url(PageResource::getUrl('edit', ['record' => $target]))->button()])
|
||||
->send();
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [];
|
||||
|
||||
Reference in new issue
Block a user