> */ 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 */ 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 $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 $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 $section * @return array{content: list>, 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> $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; } }