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>
175 lines
6.3 KiB
PHP
175 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Cms;
|
|
|
|
use App\Domain\Cms\Sections\PageSections;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* Überträgt einen Abschnitt auf eine andere Seite: als Kopie (neue Kennungen) oder als Verschiebung (Kennungen
|
|
* bleiben). Die Zielseite erhält eine neue, unveröffentlichte Revision; veröffentlicht wird nichts. Das Entfernen
|
|
* aus der Quellseite beim Verschieben ist Sache des Aufrufers (Seiteneditor), weil dort der Arbeitsstand liegt.
|
|
*/
|
|
class SectionTransfer
|
|
{
|
|
public const START = 'start';
|
|
|
|
public const END = 'end';
|
|
|
|
public function __construct(private readonly PageService $pages, private readonly PageSections $sections) {}
|
|
|
|
/**
|
|
* Abschnitte, die der Editor der Seite zeigt (Entwurf vor Veröffentlichung).
|
|
*
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function sectionsOf(Page $page): array
|
|
{
|
|
return ($page->latestRevision ?? $page->publishedRevision)?->sections() ?? [];
|
|
}
|
|
|
|
/**
|
|
* Wählbare Positionen auf der Zielseite: Anfang, Ende und hinter jedem vorhandenen Abschnitt.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function positions(Page $target): array
|
|
{
|
|
$sections = $this->sectionsOf($target);
|
|
|
|
if ($sections === []) {
|
|
return [self::END => 'Als ersten Abschnitt einfügen (die Seite hat noch keinen)'];
|
|
}
|
|
|
|
$options = [self::START => 'Am Anfang der Seite', self::END => 'Am Ende der Seite'];
|
|
|
|
foreach ($sections as $index => $section) {
|
|
$options['after:'.($index + 1)] = 'Hinter Abschnitt '.($index + 1).': '.$this->label($section);
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* Name des Abschnitts, sonst die erste Überschrift darin, sonst das Raster.
|
|
*
|
|
* @param array<string, mixed> $section
|
|
*/
|
|
public function label(array $section): string
|
|
{
|
|
$name = PageSections::cleanName($section['name'] ?? null);
|
|
|
|
if ($name !== null) {
|
|
return Str::limit($name, 50);
|
|
}
|
|
|
|
foreach ((array) ($section['slots'] ?? []) as $widget) {
|
|
$data = is_array($widget) && is_array($widget['data'] ?? null) ? $widget['data'] : [];
|
|
|
|
foreach (['heading', 'title', 'headline'] as $field) {
|
|
if (is_string($data[$field] ?? null) && trim(strip_tags($data[$field])) !== '') {
|
|
return Str::limit(trim(strip_tags($data[$field])), 50);
|
|
}
|
|
}
|
|
}
|
|
|
|
return 'ohne Namen';
|
|
}
|
|
|
|
/**
|
|
* Fügt den Abschnitt auf der Zielseite ein. Wirft eine ValidationException, wenn die Zielseite damit ungültig
|
|
* würde (zu viele Abschnitte oder Widgets); dann ist nichts geschrieben.
|
|
*
|
|
* @param array<string, mixed> $section Abschnitt im Modell (wie PageSections::read liefert)
|
|
* @return array{revision: PageRevision, position: int, anchor: ?string, anchorChanged: bool}
|
|
*/
|
|
public function insert(Page $target, array $section, string $position, bool $fresh, ?User $author): array
|
|
{
|
|
$plan = $this->plan($target, $section, $position, $fresh);
|
|
$revision = ($target->latestRevision ?? $target->publishedRevision);
|
|
|
|
$saved = $this->pages->saveRevision($target, $plan['content'], $revision?->seo_title, $revision?->seo_description, $author);
|
|
$target->unsetRelation('latestRevision');
|
|
|
|
return ['revision' => $saved, 'position' => $plan['position'], 'anchor' => $plan['anchor'], 'anchorChanged' => $plan['anchorChanged']];
|
|
}
|
|
|
|
/**
|
|
* Prüft, ob die Zielseite mit dem Abschnitt gültig bleibt, ohne zu schreiben.
|
|
*
|
|
* @param array<string, mixed> $section
|
|
* @return array{content: list<array<string, mixed>>, position: int, anchor: ?string, anchorChanged: bool}
|
|
*/
|
|
public function plan(Page $target, array $section, string $position, bool $fresh): array
|
|
{
|
|
$existing = $this->sectionsOf($target);
|
|
|
|
if (count($existing) >= PageSections::MAX_SECTIONS) {
|
|
throw ValidationException::withMessages(['sections' => 'Eine Seite hat höchstens '.PageSections::MAX_SECTIONS.' Abschnitte. Die Zielseite ist voll.']);
|
|
}
|
|
|
|
if ($fresh) {
|
|
$section['id'] = null;
|
|
$section['slots'] = array_map(fn ($widget) => is_array($widget) ? array_replace($widget, ['id' => null]) : $widget, array_values((array) ($section['slots'] ?? [])));
|
|
}
|
|
|
|
[$anchor, $changed] = $this->freeAnchor($section['anchor'] ?? null, $existing);
|
|
$section['anchor'] = $anchor;
|
|
|
|
$at = $this->index($position, count($existing));
|
|
array_splice($existing, $at, 0, [$section]);
|
|
|
|
// Prüft die ganze Zielseite (Abschnitts- und Widget-Höchstzahl, Regeln der Bausteine); Fehler bricht ab.
|
|
$this->sections->normalize($existing);
|
|
|
|
return ['content' => $existing, 'position' => $at + 1, 'anchor' => $anchor, 'anchorChanged' => $changed];
|
|
}
|
|
|
|
/**
|
|
* Die Sprungmarke bleibt, wenn sie auf der Zielseite frei ist; sonst wird sie mit „-2“, „-3“ … eindeutig gemacht.
|
|
*
|
|
* @param list<array<string, mixed>> $existing
|
|
* @return array{0: ?string, 1: bool}
|
|
*/
|
|
private function freeAnchor(mixed $anchor, array $existing): array
|
|
{
|
|
$anchor = PageSections::cleanAnchor($anchor);
|
|
|
|
if ($anchor === null) {
|
|
return [null, false];
|
|
}
|
|
|
|
$taken = array_values(array_filter(array_map(fn (array $section): ?string => PageSections::cleanAnchor($section['anchor'] ?? null), $existing)));
|
|
|
|
if (PageSections::anchorProblem($anchor, $taken) === null) {
|
|
return [$anchor, false];
|
|
}
|
|
|
|
for ($number = 2; $number <= 50; $number++) {
|
|
$suffix = '-'.$number;
|
|
$candidate = rtrim(substr($anchor, 0, PageSections::MAX_ANCHOR - strlen($suffix)), '-').$suffix;
|
|
|
|
if (PageSections::anchorProblem($candidate, $taken) === null) {
|
|
return [$candidate, true];
|
|
}
|
|
}
|
|
|
|
return [null, true];
|
|
}
|
|
|
|
private function index(string $position, int $count): int
|
|
{
|
|
if ($position === self::START) {
|
|
return 0;
|
|
}
|
|
|
|
if (preg_match('/^after:(\d+)$/', $position, $match) === 1) {
|
|
return max(0, min($count, (int) $match[1]));
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
}
|