|null $sections gespeicherte Abschnitte (Spalte sections) * @param array|null $blocks flache Bausteinliste (Spalte blocks), nur ohne Abschnitte verwendet * @return list, slots: list|null>}> */ public function read(?array $sections, ?array $blocks = null): array { if ($sections === null) { return $this->wrapWidgets($blocks ?? []); } $result = []; foreach (SectionStyle::migrateSpacing(array_values(array_filter($sections, 'is_array')), byVersion: true) as $section) { $result[] = $this->upcastSection($section); } return $result; } /** * Schreibpfad: prüft und bereinigt Abschnitte für eine neue Revision. Flache Widgets (ohne „slots“) werden * als einspaltige Abschnitte übernommen. Alle Fehler der Seite werden gesammelt gemeldet; Schlüssel: * „sections.N.layout“, „sections.N.style.image“, „sections.N.slots“, „sections.N.slots.M.data.feld“. * * @param array $content * @return list, slots: list|null>}> */ public function normalize(array $content): array { $items = array_values($content); if (count($items) > self::MAX_SECTIONS) { throw ValidationException::withMessages([ 'sections' => 'Eine Seite hat höchstens '.self::MAX_SECTIONS.' Abschnitte.', ]); } $errors = []; $sections = []; $seenSectionIds = []; $seenWidgetIds = []; $widgetCount = 0; // Eingaben mit Abständen der Version 1 (zum Beispiel ältere Aufrufer) werden wie beim Lesen übersetzt. $migrated = SectionStyle::migrateSpacing(array_map( fn ($item) => is_array($item) && array_key_exists('slots', $item) ? $item : ['style' => [], 'slots' => [$item]], $items, )); foreach ($items as $index => $item) { if (! is_array($item)) { $errors["sections.$index"] = ['Ein Abschnitt ist unvollständig.']; continue; } if (! array_key_exists('slots', $item)) { $item = ['id' => null, 'layout' => SectionLayouts::DEFAULT, 'style' => [], 'slots' => [$item]]; } else { $item['style'] = $migrated[$index]['style'] ?? ($item['style'] ?? []); } $sections[] = $this->normalizeSection($item, $index, $errors, $seenSectionIds, $seenWidgetIds, $widgetCount); } if ($widgetCount > self::MAX_WIDGETS) { $errors['sections'] = ['Eine Seite enthält höchstens '.self::MAX_WIDGETS.' Widgets.']; } if ($errors !== []) { throw ValidationException::withMessages($errors); } return $sections; } /** * Widgets in Lesereihenfolge (Abschnitt für Abschnitt, Slot für Slot). Wird zusätzlich als flache * Bausteinliste gespeichert, damit ein Code-Rollback auf die Fassung vor den Abschnitten die Inhalte * weiterhin untereinander darstellt. * * @param list> $sections * @return list> */ public function flatten(array $sections): array { $widgets = []; foreach ($sections as $section) { $count = SectionLayouts::slotCount($section['layout']); foreach (array_slice($section['slots'], 0, $count) as $widget) { if (is_array($widget)) { $widgets[] = $widget; } } } return $widgets; } /** * Öffentliche Medien der Seite: Widget-Bilder und Hintergrundbilder der Abschnitte. * * @param list> $sections * @return list */ public function mediaPaths(array $sections): array { $paths = []; foreach ($sections as $section) { $style = SectionStyle::fromStored($section['style'] ?? []); if (is_string($style['image']) && PublicMedia::isPublicPath($style['image'])) { $paths[] = $style['image']; } } foreach ($this->widgets->mediaPaths($this->flatten($sections)) as $path) { $paths[] = $path; } return array_values(array_unique($paths)); } /** * @param list> $sections */ public function pageTitle(array $sections): ?string { return $this->widgets->pageTitle($this->flatten($sections)); } /** * @param array $blocks * @return list, slots: list|null>}> */ private function wrapWidgets(array $blocks): array { $sections = []; foreach ($this->widgets->read($blocks) as $position => $widget) { $sections[] = [ 'id' => null, 'v' => self::VERSION, 'layout' => SectionLayouts::DEFAULT, // Bestand vor dem Raster stand in einem durchgehenden Blatt: kompakte Abstände, zwischen den Widgets // nur einmal (oben kein Abstand ab dem zweiten), wie bisher. 'style' => ['spacing_top' => $position === 0 ? 'compact' : 'none', 'spacing_bottom' => 'compact', 'gap' => 'normal'] + SectionStyle::DEFAULTS, 'slots' => [$widget], ]; } return $sections; } /** * @param array $section * @return array{id: ?string, v: int, layout: string, style: array, slots: list|null>} */ private function upcastSection(array $section): array { $layout = SectionLayouts::has($section['layout'] ?? null) ? $section['layout'] : SectionLayouts::DEFAULT; $slots = []; foreach (array_values(is_array($section['slots'] ?? null) ? $section['slots'] : []) as $slot) { $slots[] = is_array($slot) ? $this->widgets->upcast($slot) : null; } while (count($slots) < SectionLayouts::slotCount($layout)) { $slots[] = null; } return [ 'id' => is_string($section['id'] ?? null) ? $section['id'] : null, 'v' => self::VERSION, 'layout' => $layout, 'style' => SectionStyle::fromStored($section['style'] ?? []), 'slots' => $slots, ]; } /** * @param array $section * @param array> $errors * @param array $seenSectionIds * @param array $seenWidgetIds * @return array{id: string, v: int, layout: string, style: array, slots: list|null>} */ private function normalizeSection(array $section, int $index, array &$errors, array &$seenSectionIds, array &$seenWidgetIds, int &$widgetCount): array { $prefix = "sections.$index"; $layout = $section['layout'] ?? SectionLayouts::DEFAULT; if (! SectionLayouts::has($layout)) { $errors["$prefix.layout"] = ['Dieses Abschnittsraster ist nicht freigegeben.']; $layout = SectionLayouts::DEFAULT; } $style = $this->normalizeStyle(is_array($section['style'] ?? null) ? $section['style'] : [], $prefix, $errors); $units = SectionLayouts::units($layout); $rawSlots = array_values(is_array($section['slots'] ?? null) ? $section['slots'] : []); $filled = count(array_filter($rawSlots, fn ($slot): bool => is_array($slot))); if ($filled > count($units)) { $errors["$prefix.slots"] = [sprintf( 'Das Raster „%s“ hat %d %s, der Abschnitt enthält aber %d Widgets. Entfernen Sie ein Widget oder wählen Sie ein Raster mit mehr Plätzen.', SectionLayouts::short($layout), count($units), count($units) === 1 ? 'Platz' : 'Plätze', $filled, )]; } elseif (count($rawSlots) > count($units)) { // Überzählige Plätze sind leer: Widgets rücken in Reihenfolge nach vorn, leere Plätze entfallen. $rawSlots = array_values(array_filter($rawSlots, fn ($slot): bool => is_array($slot))); } $slots = []; foreach ($units as $position => $slotUnits) { $block = $rawSlots[$position] ?? null; if (! is_array($block)) { $slots[] = null; continue; } $widgetCount++; try { $widget = $this->widgets->normalizeWidget($block, "$prefix.slots.$position", $seenWidgetIds); } catch (ValidationException $exception) { foreach ($exception->errors() as $key => $messages) { $errors[$key] = $messages; } $slots[] = null; continue; } $type = $this->widgets->get($widget['type']); if (! $type->supportsUnits($slotUnits)) { $errors["$prefix.slots.$position.units"] = [sprintf( '„%s“ ist für Plätze mit %s Rastereinheiten vorgesehen; dieser Platz hat %d. Wählen Sie ein passendes Raster oder einen passenden Platz.', $type->label(), $type->unitsLabel(), $slotUnits, )]; } $slots[] = $widget; } $id = $section['id'] ?? null; if (! is_string($id) || preg_match('/^[A-Za-z0-9-]{1,64}$/', $id) !== 1 || isset($seenSectionIds[$id])) { $id = (string) Str::uuid(); } $seenSectionIds[$id] = true; return [ 'id' => $id, 'v' => self::VERSION, 'layout' => $layout, 'style' => $style, 'slots' => $slots, ]; } /** * @param array $style * @param array> $errors * @return array */ private function normalizeStyle(array $style, string $prefix, array &$errors): array { $result = SectionStyle::DEFAULTS; $enums = SectionStyle::enums(); foreach ($style as $key => $value) { if ($key === 'image' || str_starts_with((string) $key, '_')) { continue; } if (! isset($enums[$key])) { $errors["$prefix.style.$key"] = ['Diese Gestaltungsoption ist nicht freigegeben.']; continue; } if ($value === null || $value === '') { continue; } if (! is_string($value) || ! array_key_exists($value, $enums[$key])) { $errors["$prefix.style.$key"] = ['Dieser Gestaltungswert ist nicht freigegeben.']; continue; } $result[$key] = $value; } if ($result['background'] === 'image') { $widgetErrors = new WidgetErrors; $refs = ImageRefFields::clean(['image' => $style['image'] ?? null, '_upload' => $style['_upload'] ?? null], $widgetErrors, errorKey: 'image'); $image = $refs['image']; foreach ($widgetErrors->all() as $messages) { $errors["$prefix.style.image"] = $messages; } if ($image === null && ! isset($errors["$prefix.style.image"])) { $errors["$prefix.style.image"] = ['Bitte ein Hintergrundbild aus dem Medienmanager wählen oder hochladen.']; } $result['image'] = $image; } else { $result['image'] = null; } return $result; } }