Loheide-eu-mitarbeiterplatt.../app/Pagebuilder/Blocks.php
Friederich Loheide 2c96fcce1c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m21s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m55s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m40s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m34s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Startseite frei gestalten, Neuigkeiten-Block mit Kategorien-Filter (0.26.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 09:38:18 +00:00

248 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Pagebuilder;
use App\Http\Resources\PersonResource;
use App\Models\User;
use App\Plugins\Hooks;
use App\Support\Images;
/**
* Registry der Pagebuilder-Blöcke. Der Kern bringt Layout- und Inhaltsblöcke
* mit, Plugins tragen eigene ein (z. B. „Neueste Beiträge“):
*
* app(Blocks::class)->add(BlockType::make('news.latest', 'Neuigkeiten', 'newspaper', 'dynamic', 'news::LatestBlock')
* ->fields([...])->resolveUsing(fn (array $data, ?User $user) => [...]));
*
* Vue-Komponenten liegen unter resources/js/pagebuilder/blocks/<Komponente>.vue
* bzw. plugins-core/<slug>/resources/js/blocks/<Name>.vue („slug::Name“).
*/
class Blocks
{
public const GROUPS = [
'layout' => 'Layout',
'content' => 'Inhalt',
'media' => 'Medien',
'dynamic' => 'Dynamisch',
'homepage' => 'Startseite',
];
/** @var array<string, BlockType> */
private array $types = [];
public function __construct()
{
$this->registerLayout();
$this->registerContent();
}
public function add(BlockType $type): void
{
$this->types[$type->key] = $type;
}
public function remove(string $key): void
{
unset($this->types[$key]);
}
public function get(string $key): ?BlockType
{
return $this->types[$key] ?? null;
}
/**
* @return array<string, BlockType>
*/
public function all(): array
{
return Hooks::apply('pagebuilder.blocks', $this->types);
}
/**
* @return list<array<string, mixed>>
*/
/**
* @param bool $homepage Startseiten-Editor: auch die Blöcke der Gruppe „Startseite“ (Kacheln)
* @return list<array<string, mixed>>
*/
public function forEditor(bool $homepage = false): array
{
return array_values(array_map(fn (BlockType $type) => $type->toArray(), array_filter($this->all(), fn (BlockType $type) => $homepage || $type->group !== 'homepage')));
}
/**
* Adresse eines Bilds – null, wenn die Datei fehlt (z. B. in der Mediathek
* gelöscht): Der Block zeigt dann seinen Platzhalter.
*/
public static function imageUrl(?string $path): ?string
{
return app(Images::class)->url($path, null);
}
private function registerLayout(): void
{
$backgrounds = ['none' => 'Ohne', 'muted' => 'Grau', 'card' => 'Weiß mit Rahmen', 'brand-soft' => 'Markenfarbe hell', 'brand' => 'Markenfarbe'];
$this->add(BlockType::make('section', 'Abschnitt', 'square-dashed', 'layout')
->description('Fasst Blöcke zusammen, optional mit Hintergrund.')
->container()
->fields([
Field::select('background', 'Hintergrund', $backgrounds, 'none'),
Field::select('padding', 'Innenabstand', ['none' => 'Ohne', 'normal' => 'Normal', 'large' => 'Groß'], 'normal'),
]));
$this->add(BlockType::make('columns', 'Spalten', 'columns-3', 'layout')
->description('Zwei bis vier Spalten nebeneinander.')
->container(1, 4, label: 'Spalte')
->fields([
Field::select('layout', 'Aufteilung', [
'1-1' => '2 Spalten (1:1)',
'2-1' => '2 Spalten (2:1)',
'1-2' => '2 Spalten (1:2)',
'1-1-1' => '3 Spalten',
'1-2-1' => '3 Spalten (1:2:1)',
'1-1-1-1' => '4 Spalten',
], '1-1'),
Field::select('stack', 'Untereinander auf', ['sm' => 'Smartphones', 'md' => 'Tablets und Smartphones', 'lg' => 'Kleinen Bildschirmen'], 'md'),
Field::select('gap', 'Abstand', ['small' => 'Klein', 'normal' => 'Normal', 'large' => 'Groß'], 'normal'),
Field::select('align', 'Vertikal ausrichten', ['start' => 'Oben', 'center' => 'Mitte', 'stretch' => 'Gleiche Höhe'], 'start'),
]));
$this->add(BlockType::make('tabs', 'Tabs', 'panel-top', 'layout')
->description('Inhalte auf Reiter verteilt.')
->container(1, 12, titled: true, label: 'Tab'));
$this->add(BlockType::make('accordion', 'Akkordeon', 'list-collapse', 'layout')
->description('Aufklappbare Abschnitte, z. B. für häufige Fragen.')
->container(1, 30, titled: true, label: 'Abschnitt')
->fields([
Field::toggle('first_open', 'Ersten Abschnitt geöffnet anzeigen'),
Field::toggle('multiple', 'Mehrere gleichzeitig öffnen'),
]));
$this->add(BlockType::make('box', 'Box', 'message-square-warning', 'layout')
->description('Hervorgehobener Kasten: Info, Hinweis, Warnung …')
->container()
->fields([
Field::select('variant', 'Art', [
'info' => 'Info',
'note' => 'Hinweis',
'warning' => 'Warnung',
'success' => 'Erfolg',
'neutral' => 'Neutral',
'highlight' => 'Hervorgehoben',
], 'info'),
Field::text('title', 'Überschrift', 150),
Field::toggle('show_icon', 'Symbol anzeigen', true),
]));
}
private function registerContent(): void
{
$this->add(BlockType::make('heading', 'Überschrift', 'heading', 'content')
->fields([
Field::text('text', 'Text', 200, placeholder: 'Überschrift'),
Field::select('level', 'Größe', ['2' => 'Groß (H2)', '3' => 'Mittel (H3)', '4' => 'Klein (H4)'], '2'),
Field::select('align', 'Ausrichtung', ['left' => 'Links', 'center' => 'Zentriert'], 'left'),
]));
$this->add(BlockType::make('text', 'Text', 'text', 'content')
->description('Absätze, Listen, Links und Hervorhebungen.')
->fields([Field::html('html', 'Text')]));
$this->add(BlockType::make('quote', 'Zitat', 'quote', 'content')
->fields([
Field::textarea('text', 'Zitat', 1000),
Field::text('source', 'Quelle', 150),
]));
$this->add(BlockType::make('buttons', 'Schaltflächen', 'mouse-pointer-click', 'content')
->fields([
Field::repeater('buttons', 'Schaltflächen', [
Field::text('label', 'Beschriftung', 80),
Field::url('url', 'Link'),
Field::select('variant', 'Darstellung', ['primary' => 'Markenfarbe', 'outline' => 'Umrandet', 'link' => 'Als Link'], 'primary'),
Field::toggle('new_tab', 'In neuem Tab öffnen'),
], 6, 'Schaltfläche'),
Field::select('align', 'Ausrichtung', ['left' => 'Links', 'center' => 'Zentriert'], 'left'),
]));
$this->add(BlockType::make('icon-list', 'Symbolliste', 'list-checks', 'content')
->fields([
Field::repeater('items', 'Einträge', [
Field::icon('icon', 'Symbol'),
Field::text('title', 'Titel', 120),
Field::textarea('text', 'Text', 500),
Field::url('url', 'Link'),
], 30, 'Eintrag'),
Field::select('layout', 'Darstellung', ['list' => 'Liste', 'grid' => 'Kacheln'], 'list'),
]));
$this->add(BlockType::make('table', 'Tabelle', 'table', 'content')
->fields([
Field::table('rows', 'Tabelle'),
Field::toggle('header', 'Erste Zeile als Kopfzeile', true),
Field::text('caption', 'Beschriftung', 200),
]));
$this->add(BlockType::make('divider', 'Trennlinie', 'minus', 'content'));
$this->add(BlockType::make('spacer', 'Abstand', 'move-vertical', 'content')
->fields([Field::select('size', 'Höhe', ['small' => 'Klein', 'medium' => 'Mittel', 'large' => 'Groß'], 'medium')]));
$this->add(BlockType::make('image', 'Bild', 'image', 'media')
->fields([
Field::image('path', 'Bild'),
Field::text('alt', 'Alternativtext', 200)->help('Beschreibt das Bild für Screenreader.'),
Field::text('caption', 'Bildunterschrift', 300),
Field::select('size', 'Breite', ['full' => 'Volle Breite', 'medium' => 'Mittel', 'small' => 'Klein'], 'full'),
Field::url('url', 'Link beim Anklicken'),
])
->resolveUsing(fn (array $data) => ['url' => self::imageUrl($data['path'])]));
$this->add(BlockType::make('gallery', 'Galerie', 'images', 'media')
->fields([
Field::repeater('images', 'Bilder', [
Field::image('path', 'Bild'),
Field::text('alt', 'Alternativtext', 200),
Field::text('caption', 'Bildunterschrift', 300),
], 60, 'Bild'),
Field::select('columns', 'Spalten', ['2' => '2', '3' => '3', '4' => '4'], '3'),
])
->resolveUsing(fn (array $data) => ['urls' => array_map(fn ($image) => self::imageUrl($image['path'] ?? null), $data['images'])]));
$this->add(BlockType::make('video', 'Video', 'play', 'media')
->description('YouTube, Vimeo oder MP4-Datei. Externe Videos laden erst nach Klick.')
->fields([
Field::url('url', 'Adresse des Videos'),
Field::text('title', 'Titel', 200),
])
->resolveUsing(fn (array $data) => ['embed' => VideoEmbed::from($data['url'])]));
$this->add(BlockType::make('hero', 'Titelbild mit Text', 'panorama', 'media')
->fields([
Field::image('path', 'Hintergrundbild'),
Field::text('title', 'Titel', 150),
Field::textarea('text', 'Text', 400),
Field::text('button_label', 'Beschriftung der Schaltfläche', 80),
Field::url('button_url', 'Link der Schaltfläche'),
Field::select('height', 'Höhe', ['small' => 'Niedrig', 'medium' => 'Mittel', 'large' => 'Hoch'], 'medium'),
])
->resolveUsing(fn (array $data) => ['url' => self::imageUrl($data['path'])]));
$this->add(BlockType::make('contacts', 'Ansprechpartner', 'contact', 'dynamic')
->description('Personen aus dem Verzeichnis – immer mit aktuellen Kontaktdaten.')
->fields([
Field::text('title', 'Überschrift', 150),
Field::people('people', 'Personen'),
Field::select('layout', 'Darstellung', ['cards' => 'Karten', 'list' => 'Liste'], 'cards'),
])
->resolveUsing(function (array $data) {
$people = User::query()->active()->whereIn('id', $data['people'])->get()
->sortBy(fn (User $user) => array_search($user->id, $data['people'], true))->values();
return ['people' => PersonResource::collection($people)->resolve()];
}));
}
}