Phase 4: Seiten mit Pagebuilder, Redaktion und Freigaben
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m40s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m40s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
- Seiten-Plugin: Seitenbaum mit Adressen, Seitennavigation, Brotkrumen und optional Hauptnavigation; Sichtbarkeit vererbt sich auf Unterseiten - Pagebuilder (Block-Registry im Kern, Plugins tragen Blöcke bei): Abschnitt, Spalten, Tabs, Akkordeon, Box, Überschrift, Text (TipTap), Zitat, Schaltflächen, Symbolliste, Tabelle, Trennlinie, Abstand, Bild, Galerie, Video (erst nach Klick), Titelbild, Ansprechpartner, Downloads, Unterseiten, Neuigkeiten, Termine; Sichtbarkeit je Block; Bereinigung beim Speichern - Editor im Portal: Ziehen und Ablegen, Autospeichern als Entwurf, Rückgängig, Gerätevorschau, Versionen, Zeitsteuerung, Bearbeitungssperre, Freigabe anfordern - Rechte „inkl. Unterseiten“ werden im Rechte-Cache aufgelöst - Neuigkeiten: Schreiben im Portal, Freigabe-Workflow mit Benachrichtigungen - Bereich „Redaktion“, Benachrichtigungen im Portal - storage/.migrations-fingerprint nicht mehr ausliefern Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
6269d3302c
commit
f3987e8386
328 changed files with 10618 additions and 389 deletions
66
app/Editorial/Editorial.php
Normal file
66
app/Editorial/Editorial.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace App\Editorial;
|
||||
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Bereich „Redaktion“ im Portal: Plugins melden hier ihre Arbeitslisten an
|
||||
* (z. B. „Meine Seiten“, „Zur Freigabe“). Der Menüpunkt erscheint nur, wenn
|
||||
* mindestens ein Abschnitt für die Person in Frage kommt.
|
||||
*/
|
||||
class Editorial
|
||||
{
|
||||
/** @var array<string, array{label: string, component: string, sort: int, visible: Closure, props: Closure}> */
|
||||
private array $sections = [];
|
||||
|
||||
/**
|
||||
* @param Closure(User): bool $visible schnelle Rechteprüfung (ohne Abfragen)
|
||||
* @param Closure(User): array<string, mixed> $props
|
||||
*/
|
||||
public function add(string $key, string $label, string $component, int $sort, Closure $visible, Closure $props): void
|
||||
{
|
||||
$this->sections[$key] = compact('label', 'component', 'sort', 'visible', 'props');
|
||||
}
|
||||
|
||||
public function remove(string $key): void
|
||||
{
|
||||
unset($this->sections[$key]);
|
||||
}
|
||||
|
||||
public function visibleFor(User $user): bool
|
||||
{
|
||||
foreach ($this->sections as $section) {
|
||||
if (($section['visible'])($user)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{key: string, label: string, component: string, props: array<string, mixed>}>
|
||||
*/
|
||||
public function resolve(User $user): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach (collect($this->sections)->sortBy('sort') as $key => $section) {
|
||||
if (! ($section['visible'])($user)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$result[] = ['key' => $key, 'label' => $section['label'], 'component' => $section['component'], 'props' => ($section['props'])($user)];
|
||||
} catch (Throwable $e) {
|
||||
Log::error("Redaktionsbereich {$key} fehlgeschlagen: ".$e->getMessage(), ['exception' => $e]);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||
*/
|
||||
class RoleGrantSelect
|
||||
{
|
||||
public static function make(string $name, string $label, string $ability, string $resourceType): Select
|
||||
public static function make(string $name, string $label, string $ability, string $resourceType, bool $includeChildren = false): Select
|
||||
{
|
||||
return Select::make($name)
|
||||
->label($label)
|
||||
|
|
@ -23,14 +23,14 @@ class RoleGrantSelect
|
|||
->afterStateHydrated(fn (Select $component, ?Model $record) => $component->state($record
|
||||
? Grant::query()->where('ability', $ability)->where('resource_type', $resourceType)->where('resource_id', $record->getKey())->pluck('role_id')->all()
|
||||
: []))
|
||||
->saveRelationshipsUsing(function (Model $record, ?array $state) use ($ability, $resourceType) {
|
||||
->saveRelationshipsUsing(function (Model $record, ?array $state) use ($ability, $resourceType, $includeChildren) {
|
||||
$roleIds = array_map('intval', $state ?? []);
|
||||
$existing = Grant::query()->where('ability', $ability)->where('resource_type', $resourceType)->where('resource_id', $record->getKey());
|
||||
|
||||
(clone $existing)->whereNotIn('role_id', $roleIds)->get()->each->delete();
|
||||
|
||||
foreach (array_diff($roleIds, (clone $existing)->pluck('role_id')->all()) as $roleId) {
|
||||
Grant::query()->create(['role_id' => $roleId, 'ability' => $ability, 'resource_type' => $resourceType, 'resource_id' => $record->getKey()]);
|
||||
Grant::query()->create(['role_id' => $roleId, 'ability' => $ability, 'resource_type' => $resourceType, 'resource_id' => $record->getKey(), 'include_children' => $includeChildren]);
|
||||
}
|
||||
})
|
||||
->dehydrated(false);
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ use Filament\Forms\Components\Repeater;
|
|||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
|
|
@ -89,7 +90,7 @@ class RoleResource extends Resource
|
|||
->schema([
|
||||
Repeater::make('object_grants')
|
||||
->hiddenLabel()
|
||||
->columns(2)
|
||||
->columns(3)
|
||||
->addActionLabel('Recht hinzufügen')
|
||||
->defaultItems(0)
|
||||
->schema([
|
||||
|
|
@ -111,13 +112,19 @@ class RoleResource extends Resource
|
|||
|
||||
return ['*' => 'Alle'] + $options;
|
||||
}),
|
||||
Toggle::make('include_children')
|
||||
->label('Inkl. Unterseiten')
|
||||
->inline(false)
|
||||
->default(true)
|
||||
->visible(fn (Get $get) => $abilities->isHierarchical($abilities->all()[$get('ability')]['resource'] ?? null)),
|
||||
])
|
||||
->afterStateHydrated(fn (Repeater $component, ?Role $record) => $component->state(
|
||||
$record?->grants()->whereNotNull('resource_type')->get()
|
||||
->groupBy('ability')
|
||||
->map(fn ($grants, $ability) => [
|
||||
'ability' => $ability,
|
||||
->groupBy(fn (Grant $g) => $g->ability.'|'.(int) $g->include_children)
|
||||
->map(fn ($grants) => [
|
||||
'ability' => $grants->first()->ability,
|
||||
'resource_ids' => $grants->map(fn (Grant $g) => $g->resource_id === null ? '*' : (string) $g->resource_id)->values()->all(),
|
||||
'include_children' => (bool) $grants->first()->include_children,
|
||||
])->values()->all() ?? []
|
||||
))
|
||||
->saveRelationshipsUsing(function (Role $record, ?array $state) use ($abilities) {
|
||||
|
|
@ -132,8 +139,10 @@ class RoleResource extends Resource
|
|||
|
||||
$ids = in_array('*', (array) $item['resource_ids'], true) ? [null] : array_map('intval', (array) $item['resource_ids']);
|
||||
|
||||
$inherit = $abilities->isHierarchical($type) && ($item['include_children'] ?? false);
|
||||
|
||||
foreach (array_unique($ids) as $id) {
|
||||
$record->grants()->create(['ability' => $item['ability'], 'resource_type' => $type, 'resource_id' => $id]);
|
||||
$record->grants()->create(['ability' => $item['ability'], 'resource_type' => $type, 'resource_id' => $id, 'include_children' => $inherit && $id !== null]);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
20
app/Http/Controllers/EditorialController.php
Normal file
20
app/Http/Controllers/EditorialController.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Editorial\Editorial;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class EditorialController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Editorial $editorial): Response
|
||||
{
|
||||
abort_unless($editorial->visibleFor($request->user()), 403);
|
||||
|
||||
return Inertia::render('Editorial/Index', [
|
||||
'sections' => $editorial->resolve($request->user()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
50
app/Http/Controllers/NotificationController.php
Normal file
50
app/Http/Controllers/NotificationController.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Notifications\DatabaseNotification;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Glocke im Portal-Header: Benachrichtigungen anzeigen und als gelesen markieren.
|
||||
*/
|
||||
class NotificationController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$notifications = $request->user()->notifications()->limit(20)->get();
|
||||
|
||||
return response()->json([
|
||||
'notifications' => $notifications->map(fn (DatabaseNotification $n) => [
|
||||
'id' => $n->id,
|
||||
'title' => (string) ($n->data['title'] ?? ''),
|
||||
'body' => (string) ($n->data['body'] ?? ''),
|
||||
'icon' => (string) ($n->data['icon'] ?? 'bell'),
|
||||
'has_url' => filled($n->data['url'] ?? null),
|
||||
'read' => $n->read_at !== null,
|
||||
'created_at' => $n->created_at?->toIso8601String(),
|
||||
]),
|
||||
'unread' => $request->user()->unreadNotifications()->count(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function open(Request $request, string $id): RedirectResponse
|
||||
{
|
||||
$notification = $request->user()->notifications()->whereKey($id)->firstOrFail();
|
||||
$notification->markAsRead();
|
||||
$url = (string) ($notification->data['url'] ?? '');
|
||||
|
||||
// Nur Adressen innerhalb des Portals ansteuern.
|
||||
return redirect()->to($url !== '' && (Str::startsWith($url, '/') && ! Str::startsWith($url, '//') || Str::startsWith($url, url('/'))) ? $url : '/');
|
||||
}
|
||||
|
||||
public function markAllRead(Request $request): JsonResponse
|
||||
{
|
||||
$request->user()->unreadNotifications()->update(['read_at' => now()]);
|
||||
|
||||
return response()->json(['unread' => 0]);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ use App\Http\Resources\PersonResource;
|
|||
use App\Models\User;
|
||||
use App\Support\ProfileFields;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Inertia\Inertia;
|
||||
|
|
@ -55,6 +56,30 @@ class PeopleController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Personenauswahl in Formularen (z. B. Block „Ansprechpartner“): Suche oder Abruf per ID.
|
||||
*/
|
||||
public function search(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'ids' => ['nullable', 'array', 'max:50'],
|
||||
'ids.*' => ['integer'],
|
||||
]);
|
||||
|
||||
$search = trim($data['q'] ?? '');
|
||||
$like = '%'.str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $search).'%';
|
||||
|
||||
$people = User::query()->active()
|
||||
->when(isset($data['ids']), fn (Builder $q) => $q->whereIn('id', $data['ids']))
|
||||
->when(! isset($data['ids']), fn (Builder $q) => $q->where(fn (Builder $q) => $q->where('name', 'like', $like)->orWhere('email', 'like', $like)))
|
||||
->orderBy('name')
|
||||
->limit(20)
|
||||
->get();
|
||||
|
||||
return response()->json(['people' => PersonResource::collection($people)->resolve()]);
|
||||
}
|
||||
|
||||
public function show(User $user): Response
|
||||
{
|
||||
abort_unless($user->is_active, 404);
|
||||
|
|
|
|||
|
|
@ -38,7 +38,8 @@ class HandleInertiaRequests extends Middleware
|
|||
'can_admin' => $user->canAccessAdmin(),
|
||||
] : null,
|
||||
],
|
||||
'navigation' => fn () => $user ? $this->navigation->items($request) : [],
|
||||
'navigation' => fn () => $user ? $this->navigation->items($request, $user) : [],
|
||||
'notifications' => fn () => $user ? ['unread' => $user->unreadNotifications()->count()] : null,
|
||||
'flash' => [
|
||||
'status' => fn () => $request->session()->get('status'),
|
||||
],
|
||||
|
|
|
|||
40
app/Notifications/PortalNotification.php
Normal file
40
app/Notifications/PortalNotification.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Notifications\Notification;
|
||||
|
||||
/**
|
||||
* Benachrichtigung im Portal (Glocke im Header). Kern und Plugins nutzen dieselbe
|
||||
* Klasse – E-Mail und Push kommen später als weitere Kanäle hinzu.
|
||||
*
|
||||
* $user->notify(new PortalNotification('Beitrag freigegeben', '„Sommerfest“ ist online.', '/neuigkeiten/sommerfest', 'check'));
|
||||
*/
|
||||
class PortalNotification extends Notification
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function __construct(
|
||||
public readonly string $title,
|
||||
public readonly string $body = '',
|
||||
public readonly ?string $url = null,
|
||||
public readonly string $icon = 'bell',
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return ['database'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(object $notifiable): array
|
||||
{
|
||||
return ['title' => $this->title, 'body' => $this->body, 'url' => $this->url, 'icon' => $this->icon];
|
||||
}
|
||||
}
|
||||
94
app/Pagebuilder/BlockType.php
Normal file
94
app/Pagebuilder/BlockType.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace App\Pagebuilder;
|
||||
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Eine Blockart des Pagebuilders.
|
||||
*
|
||||
* Container (Abschnitt, Spalten, Tabs, Akkordeon, Box) haben „items“: jeweils
|
||||
* ein Bereich mit eigener Blockliste, bei Tabs und Akkordeon mit Titel.
|
||||
*/
|
||||
final class BlockType
|
||||
{
|
||||
/**
|
||||
* @param list<Field> $fields
|
||||
* @param array{min: int, max: int, titled: bool, label: string}|null $container
|
||||
* @param (Closure(array<string, mixed>, User|null, array<string, mixed>): (array<string, mixed>|null))|null $resolve
|
||||
* Daten für die Anzeige (z. B. Bild-URL, aktuelle Beiträge); null = Block ausblenden
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly string $key,
|
||||
public readonly string $label,
|
||||
public readonly string $icon,
|
||||
public readonly string $group,
|
||||
public readonly string $component,
|
||||
public readonly array $fields = [],
|
||||
public readonly ?array $container = null,
|
||||
public readonly ?Closure $resolve = null,
|
||||
public readonly string $description = '',
|
||||
) {}
|
||||
|
||||
public static function make(string $key, string $label, string $icon, string $group, ?string $component = null): self
|
||||
{
|
||||
return new self($key, $label, $icon, $group, $component ?? str_replace(' ', '', ucwords(str_replace(['-', '_', '.'], ' ', $key))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<Field> $fields
|
||||
*/
|
||||
public function fields(array $fields): self
|
||||
{
|
||||
return new self($this->key, $this->label, $this->icon, $this->group, $this->component, $fields, $this->container, $this->resolve, $this->description);
|
||||
}
|
||||
|
||||
public function container(int $min = 1, int $max = 1, bool $titled = false, string $label = 'Bereich'): self
|
||||
{
|
||||
return new self($this->key, $this->label, $this->icon, $this->group, $this->component, $this->fields, ['min' => $min, 'max' => $max, 'titled' => $titled, 'label' => $label], $this->resolve, $this->description);
|
||||
}
|
||||
|
||||
public function resolveUsing(Closure $resolve): self
|
||||
{
|
||||
return new self($this->key, $this->label, $this->icon, $this->group, $this->component, $this->fields, $this->container, $resolve, $this->description);
|
||||
}
|
||||
|
||||
public function description(string $description): self
|
||||
{
|
||||
return new self($this->key, $this->label, $this->icon, $this->group, $this->component, $this->fields, $this->container, $this->resolve, $description);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function defaults(): array
|
||||
{
|
||||
$data = [];
|
||||
|
||||
foreach ($this->fields as $field) {
|
||||
$data[$field->name] = $field->default();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'key' => $this->key,
|
||||
'label' => $this->label,
|
||||
'icon' => $this->icon,
|
||||
'group' => $this->group,
|
||||
'component' => $this->component,
|
||||
'description' => $this->description,
|
||||
'fields' => array_map(fn (Field $field) => $field->toArray(), $this->fields),
|
||||
'container' => $this->container,
|
||||
'defaults' => $this->defaults(),
|
||||
'dynamic' => $this->resolve !== null,
|
||||
];
|
||||
}
|
||||
}
|
||||
239
app/Pagebuilder/Blocks.php
Normal file
239
app/Pagebuilder/Blocks.php
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
<?php
|
||||
|
||||
namespace App\Pagebuilder;
|
||||
|
||||
use App\Http\Resources\PersonResource;
|
||||
use App\Models\User;
|
||||
use App\Plugins\Hooks;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* 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',
|
||||
];
|
||||
|
||||
/** @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>>
|
||||
*/
|
||||
public function forEditor(): array
|
||||
{
|
||||
return array_values(array_map(fn (BlockType $type) => $type->toArray(), $this->all()));
|
||||
}
|
||||
|
||||
public static function imageUrl(?string $path): ?string
|
||||
{
|
||||
return $path ? Storage::disk('uploads')->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()];
|
||||
}));
|
||||
}
|
||||
}
|
||||
292
app/Pagebuilder/Content.php
Normal file
292
app/Pagebuilder/Content.php
Normal file
|
|
@ -0,0 +1,292 @@
|
|||
<?php
|
||||
|
||||
namespace App\Pagebuilder;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Support\Permissions\Audiences;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Inhalt einer Seite: ein Baum aus Blöcken.
|
||||
*
|
||||
* {"blocks": [{"id": "…", "type": "columns", "data": {…}, "visibility": {…},
|
||||
* "items": [{"id": "…", "title": "…", "blocks": [ … ]}]}]}
|
||||
*
|
||||
* sanitize() läuft beim Speichern: unbekannte Blöcke und Felder fallen weg,
|
||||
* HTML wird bereinigt, Adressen geprüft. render() läuft bei der Anzeige:
|
||||
* blendet Blöcke nach Sichtbarkeit aus und ergänzt dynamische Daten.
|
||||
*/
|
||||
class Content
|
||||
{
|
||||
public const MAX_DEPTH = 6;
|
||||
|
||||
public const MAX_BLOCKS = 800;
|
||||
|
||||
private int $count = 0;
|
||||
|
||||
public function __construct(private readonly Blocks $blocks) {}
|
||||
|
||||
/**
|
||||
* @return array{blocks: list<array<string, mixed>>}
|
||||
*/
|
||||
public static function empty(): array
|
||||
{
|
||||
return ['blocks' => []];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<mixed>|null $content
|
||||
* @return array{blocks: list<array<string, mixed>>}
|
||||
*/
|
||||
public function sanitize(?array $content): array
|
||||
{
|
||||
$this->count = 0;
|
||||
|
||||
return ['blocks' => $this->sanitizeList($content['blocks'] ?? [], 1)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Anzeigefertiger Inhalt für eine Person.
|
||||
*
|
||||
* @param array<mixed>|null $content bereits bereinigter Inhalt
|
||||
* @return array{blocks: list<array<string, mixed>>}
|
||||
*/
|
||||
public function render(?array $content, ?User $user, array $context = []): array
|
||||
{
|
||||
return ['blocks' => $this->renderList($content['blocks'] ?? [], $user, $context)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ein einzelner Block (Editor-Vorschau dynamischer Blöcke).
|
||||
*
|
||||
* @param array<mixed> $block
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public function preview(array $block, ?User $user, array $context = []): ?array
|
||||
{
|
||||
$this->count = 0;
|
||||
$clean = $this->sanitizeList([$block], 1)[0] ?? null;
|
||||
|
||||
if (! $clean) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$type = $this->blocks->get($clean['type']);
|
||||
|
||||
return $type?->resolve ? $this->resolve($type, $clean, $user, $context) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Werte eines Feldtyps im Baum, z. B. alle Bildpfade (für Aufräumarbeiten).
|
||||
*
|
||||
* @param array<mixed>|null $content
|
||||
* @return list<mixed>
|
||||
*/
|
||||
public function collect(?array $content, callable $picker): array
|
||||
{
|
||||
$result = [];
|
||||
$walk = function (array $blocks) use (&$walk, &$result, $picker) {
|
||||
foreach ($blocks as $block) {
|
||||
array_push($result, ...(array) $picker($block));
|
||||
|
||||
foreach ($block['items'] ?? [] as $item) {
|
||||
$walk($item['blocks'] ?? []);
|
||||
}
|
||||
}
|
||||
};
|
||||
$walk($content['blocks'] ?? []);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reiner Text des Inhalts, z. B. für die Suche.
|
||||
*
|
||||
* @param array<mixed>|null $content
|
||||
*/
|
||||
public function plainText(?array $content): string
|
||||
{
|
||||
$parts = $this->collect($content, function (array $block) {
|
||||
$texts = [];
|
||||
|
||||
array_walk_recursive($block['data'], function ($value) use (&$texts) {
|
||||
if (is_string($value) && ! str_starts_with($value, 'pagebuilder/')) {
|
||||
$texts[] = strip_tags($value);
|
||||
}
|
||||
});
|
||||
|
||||
foreach ($block['items'] ?? [] as $item) {
|
||||
$texts[] = $item['title'] ?? '';
|
||||
}
|
||||
|
||||
return [implode(' ', $texts)];
|
||||
});
|
||||
|
||||
return trim(preg_replace('/\s+/u', ' ', html_entity_decode(implode(' ', $parts))) ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function sanitizeList(mixed $blocks, int $depth): array
|
||||
{
|
||||
if (! is_array($blocks) || $depth > self::MAX_DEPTH) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach (array_values($blocks) as $block) {
|
||||
if (! is_array($block) || ++$this->count > self::MAX_BLOCKS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$type = $this->blocks->get((string) ($block['type'] ?? ''));
|
||||
|
||||
if (! $type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = is_array($block['data'] ?? null) ? $block['data'] : [];
|
||||
$clean = [
|
||||
'id' => self::id($block['id'] ?? null),
|
||||
'type' => $type->key,
|
||||
'data' => [],
|
||||
];
|
||||
|
||||
foreach ($type->fields as $field) {
|
||||
$clean['data'][$field->name] = $field->sanitize(array_key_exists($field->name, $data) ? $data[$field->name] : $field->default());
|
||||
}
|
||||
|
||||
$visibility = $this->sanitizeVisibility($block['visibility'] ?? null);
|
||||
|
||||
if ($visibility !== null) {
|
||||
$clean['visibility'] = $visibility;
|
||||
}
|
||||
|
||||
if ($type->container) {
|
||||
$clean['items'] = $this->sanitizeItems($type, $clean['data'], $block['items'] ?? [], $depth);
|
||||
}
|
||||
|
||||
$result[] = $clean;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function sanitizeItems(BlockType $type, array $data, mixed $items, int $depth): array
|
||||
{
|
||||
$items = is_array($items) ? array_values(array_filter($items, 'is_array')) : [];
|
||||
$container = $type->container;
|
||||
|
||||
// Spalten: Anzahl folgt der Aufteilung. Überzählige Spalten werden an die letzte angehängt, nichts geht verloren.
|
||||
$count = $type->key === 'columns'
|
||||
? count(explode('-', (string) ($data['layout'] ?? '1-1')))
|
||||
: max($container['min'], min($container['max'], count($items)));
|
||||
|
||||
if (count($items) > $count && $count > 0) {
|
||||
$overflow = array_merge(...array_map(fn ($item) => (array) ($item['blocks'] ?? []), array_slice($items, $count)));
|
||||
$items = array_slice($items, 0, $count);
|
||||
$items[$count - 1]['blocks'] = array_merge((array) ($items[$count - 1]['blocks'] ?? []), $overflow);
|
||||
}
|
||||
|
||||
$items = array_pad($items, $count, []);
|
||||
|
||||
return array_map(fn (array $item) => array_filter([
|
||||
'id' => self::id($item['id'] ?? null),
|
||||
'title' => $container['titled'] ? mb_substr(trim((string) ($item['title'] ?? '')), 0, 120) : null,
|
||||
'blocks' => $this->sanitizeList($item['blocks'] ?? [], $depth + 1),
|
||||
], fn ($value) => $value !== null), $items);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{role?: list<string>, department?: list<string>, location?: list<string>}|null
|
||||
*/
|
||||
private function sanitizeVisibility(mixed $visibility): ?array
|
||||
{
|
||||
if (! is_array($visibility)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$clean = [];
|
||||
|
||||
foreach (['role', 'department', 'location'] as $rule) {
|
||||
$values = array_values(array_unique(array_filter(array_map(
|
||||
fn ($value) => is_scalar($value) ? mb_substr(trim((string) $value), 0, 150) : '',
|
||||
(array) ($visibility[$rule] ?? []),
|
||||
))));
|
||||
|
||||
if ($values !== []) {
|
||||
$clean[$rule] = array_slice($values, 0, 50);
|
||||
}
|
||||
}
|
||||
|
||||
return $clean === [] ? null : $clean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $blocks
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function renderList(array $blocks, ?User $user, array $context): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
$type = $this->blocks->get($block['type'] ?? '');
|
||||
|
||||
// Blöcke deaktivierter Plugins und fremde Zielgruppen einfach auslassen.
|
||||
if (! $type || ! Audiences::matches($block['visibility'] ?? null, $user)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($type->resolve) {
|
||||
$props = $this->resolve($type, $block, $user, $context);
|
||||
|
||||
if ($props === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$block['props'] = $props;
|
||||
}
|
||||
|
||||
unset($block['visibility']);
|
||||
$block['component'] = $type->component;
|
||||
|
||||
if (isset($block['items'])) {
|
||||
$block['items'] = array_map(fn (array $item) => [...$item, 'blocks' => $this->renderList($item['blocks'] ?? [], $user, $context)], $block['items']);
|
||||
}
|
||||
|
||||
$result[] = $block;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $block
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
private function resolve(BlockType $type, array $block, ?User $user, array $context): ?array
|
||||
{
|
||||
try {
|
||||
return ($type->resolve)($block['data'], $user, $context);
|
||||
} catch (Throwable $e) {
|
||||
// Ein fehlerhafter Plugin-Block darf die Seite nicht mitreißen.
|
||||
Log::warning("Pagebuilder-Block {$type->key} konnte nicht geladen werden: ".$e->getMessage());
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static function id(mixed $id): string
|
||||
{
|
||||
return is_string($id) && preg_match('/^[A-Za-z0-9_-]{4,40}$/', $id) ? $id : Str::random(12);
|
||||
}
|
||||
}
|
||||
209
app/Pagebuilder/Field.php
Normal file
209
app/Pagebuilder/Field.php
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
<?php
|
||||
|
||||
namespace App\Pagebuilder;
|
||||
|
||||
use App\Support\QuickLinks;
|
||||
use App\Support\RichText;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* Ein Eingabefeld eines Blocks. Beschreibt zugleich das Formular im Editor
|
||||
* (toArray) und die Bereinigung beim Speichern (sanitize) – Plugin-Blöcke
|
||||
* bekommen so Editor und Absicherung ohne eigenen Code.
|
||||
*/
|
||||
final class Field
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
private function __construct(
|
||||
public readonly string $name,
|
||||
public readonly string $type,
|
||||
public readonly string $label,
|
||||
private readonly array $options = [],
|
||||
) {}
|
||||
|
||||
public static function text(string $name, string $label, int $max = 200, string $default = '', ?string $placeholder = null): self
|
||||
{
|
||||
return new self($name, 'text', $label, ['max' => $max, 'default' => $default, 'placeholder' => $placeholder]);
|
||||
}
|
||||
|
||||
public static function textarea(string $name, string $label, int $max = 2000, string $default = ''): self
|
||||
{
|
||||
return new self($name, 'textarea', $label, ['max' => $max, 'default' => $default]);
|
||||
}
|
||||
|
||||
/** Formatierter Text – im Editor direkt im Block bearbeitet (TipTap). */
|
||||
public static function html(string $name, string $label): self
|
||||
{
|
||||
return new self($name, 'html', $label, ['default' => '']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string>|Closure(): array<string, string> $choices
|
||||
*/
|
||||
public static function select(string $name, string $label, array|Closure $choices, ?string $default = null): self
|
||||
{
|
||||
return new self($name, 'select', $label, ['choices' => $choices, 'default' => $default]);
|
||||
}
|
||||
|
||||
public static function toggle(string $name, string $label, bool $default = false): self
|
||||
{
|
||||
return new self($name, 'toggle', $label, ['default' => $default]);
|
||||
}
|
||||
|
||||
public static function number(string $name, string $label, int $min, int $max, int $default): self
|
||||
{
|
||||
return new self($name, 'number', $label, ['min' => $min, 'max' => $max, 'default' => $default]);
|
||||
}
|
||||
|
||||
public static function url(string $name, string $label, string $default = ''): self
|
||||
{
|
||||
return new self($name, 'url', $label, ['default' => $default]);
|
||||
}
|
||||
|
||||
/** Hochgeladenes Bild (Pfad im Upload-Speicher). */
|
||||
public static function image(string $name, string $label): self
|
||||
{
|
||||
return new self($name, 'image', $label, ['default' => null]);
|
||||
}
|
||||
|
||||
/** Personen aus dem Verzeichnis (IDs). */
|
||||
public static function people(string $name, string $label, int $max = 24): self
|
||||
{
|
||||
return new self($name, 'people', $label, ['max' => $max, 'default' => []]);
|
||||
}
|
||||
|
||||
/** Einfache Tabelle: Zeilen aus Textzellen. */
|
||||
public static function table(string $name, string $label): self
|
||||
{
|
||||
return new self($name, 'table', $label, ['default' => [['', ''], ['', '']]]);
|
||||
}
|
||||
|
||||
/** Symbol aus dem Symbolsatz der Schnellzugriffe. */
|
||||
public static function icon(string $name, string $label, string $default = 'link'): self
|
||||
{
|
||||
return new self($name, 'icon', $label, ['default' => $default, 'choices' => QuickLinks::ICONS]);
|
||||
}
|
||||
|
||||
/** Liste gleichartiger Einträge, z. B. Schaltflächen oder Galeriebilder. */
|
||||
public static function repeater(string $name, string $label, array $fields, int $max = 20, string $itemLabel = 'Eintrag'): self
|
||||
{
|
||||
return new self($name, 'repeater', $label, ['fields' => $fields, 'max' => $max, 'default' => [], 'itemLabel' => $itemLabel]);
|
||||
}
|
||||
|
||||
/** Beliebige Liste von IDs, deren Einträge der Block selbst verwaltet (z. B. Dateien). */
|
||||
public static function ids(string $name, string $label, string $kind, int $max = 50): self
|
||||
{
|
||||
return new self($name, 'ids', $label, ['kind' => $kind, 'max' => $max, 'default' => []]);
|
||||
}
|
||||
|
||||
public function default(): mixed
|
||||
{
|
||||
if ($this->type === 'repeater') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->options['default'] ?? null;
|
||||
}
|
||||
|
||||
public function help(string $help): self
|
||||
{
|
||||
return new self($this->name, $this->type, $this->label, [...$this->options, 'help' => $help]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed> Beschreibung für das Editor-Formular
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$options = $this->options;
|
||||
|
||||
if (isset($options['choices'])) {
|
||||
$choices = $options['choices'] instanceof Closure ? ($options['choices'])() : $options['choices'];
|
||||
$options['choices'] = collect($choices)->map(fn ($label, $value) => ['value' => (string) $value, 'label' => $label])->values()->all();
|
||||
}
|
||||
|
||||
if (isset($options['fields'])) {
|
||||
$options['fields'] = array_map(fn (Field $field) => $field->toArray(), $options['fields']);
|
||||
}
|
||||
|
||||
return ['name' => $this->name, 'type' => $this->type, 'label' => $this->label, ...$options];
|
||||
}
|
||||
|
||||
public function sanitize(mixed $value): mixed
|
||||
{
|
||||
return match ($this->type) {
|
||||
'text', 'textarea' => mb_substr(trim(is_scalar($value) ? (string) $value : ''), 0, $this->options['max']),
|
||||
'html' => app(RichText::class)->sanitize(is_string($value) ? mb_substr($value, 0, 200_000) : ''),
|
||||
'select' => $this->sanitizeChoice($value),
|
||||
'toggle' => (bool) $value,
|
||||
'number' => max($this->options['min'], min($this->options['max'], is_numeric($value) ? (int) $value : $this->options['default'])),
|
||||
'url' => self::safeUrl($value),
|
||||
'image' => is_string($value) && preg_match('#^pagebuilder/[A-Za-z0-9/_.-]+$#', $value) && ! str_contains($value, '..') ? $value : null,
|
||||
'people', 'ids' => array_values(array_slice(array_unique(array_filter(array_map('intval', is_array($value) ? $value : []))), 0, $this->options['max'])),
|
||||
'table' => $this->sanitizeTable($value),
|
||||
'icon' => is_string($value) && array_key_exists($value, QuickLinks::ICONS) ? $value : $this->options['default'],
|
||||
'repeater' => $this->sanitizeRepeater($value),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public static function safeUrl(mixed $value): string
|
||||
{
|
||||
$url = trim(is_string($value) ? $value : '');
|
||||
|
||||
if ($url === '' || mb_strlen($url) > 2000) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Relative Adressen im Portal, Anker oder http(s)/mailto/tel – nichts anderes (kein javascript:).
|
||||
if (preg_match('#^(/(?!/)|\#)#', $url) || preg_match('#^(https?://|mailto:|tel:)#i', $url)) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
return preg_match('#^[a-z0-9.-]+\.[a-z]{2,}(/|$)#i', $url) ? 'https://'.$url : '';
|
||||
}
|
||||
|
||||
private function sanitizeChoice(mixed $value): ?string
|
||||
{
|
||||
$choices = $this->options['choices'] instanceof Closure ? ($this->options['choices'])() : $this->options['choices'];
|
||||
$value = is_scalar($value) ? (string) $value : null;
|
||||
|
||||
return $value !== null && array_key_exists($value, $choices) ? $value : $this->options['default'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<list<string>>
|
||||
*/
|
||||
private function sanitizeTable(mixed $value): array
|
||||
{
|
||||
$rows = array_slice(is_array($value) ? array_values($value) : [], 0, 100);
|
||||
$width = min(10, max(1, ...array_map(fn ($row) => is_array($row) ? count($row) : 1, $rows ?: [[]])));
|
||||
|
||||
return array_map(function ($row) use ($width) {
|
||||
$cells = array_slice(is_array($row) ? array_values($row) : [], 0, $width);
|
||||
|
||||
return array_pad(array_map(fn ($cell) => mb_substr(is_scalar($cell) ? (string) $cell : '', 0, 1000), $cells), $width, '');
|
||||
}, $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function sanitizeRepeater(mixed $value): array
|
||||
{
|
||||
$items = array_slice(is_array($value) ? array_values($value) : [], 0, $this->options['max']);
|
||||
|
||||
return array_map(function ($item) {
|
||||
$item = is_array($item) ? $item : [];
|
||||
$clean = [];
|
||||
|
||||
foreach ($this->options['fields'] as $field) {
|
||||
$clean[$field->name] = $field->sanitize($item[$field->name] ?? $field->default());
|
||||
}
|
||||
|
||||
return $clean;
|
||||
}, $items);
|
||||
}
|
||||
}
|
||||
34
app/Pagebuilder/VideoEmbed.php
Normal file
34
app/Pagebuilder/VideoEmbed.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Pagebuilder;
|
||||
|
||||
/**
|
||||
* Wandelt Video-Adressen in datenschutzfreundliche Einbettungen um.
|
||||
* YouTube über youtube-nocookie.com; geladen wird erst nach einem Klick.
|
||||
*/
|
||||
final class VideoEmbed
|
||||
{
|
||||
/**
|
||||
* @return array{provider: string, src: string}|null
|
||||
*/
|
||||
public static function from(string $url): ?array
|
||||
{
|
||||
if ($url === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (preg_match('~(?:youtube\.com/(?:watch\?(?:.*&)?v=|embed/|shorts/)|youtu\.be/)([A-Za-z0-9_-]{11})~', $url, $m)) {
|
||||
return ['provider' => 'YouTube', 'src' => 'https://www.youtube-nocookie.com/embed/'.$m[1].'?rel=0'];
|
||||
}
|
||||
|
||||
if (preg_match('~vimeo\.com/(?:video/)?(\d+)~', $url, $m)) {
|
||||
return ['provider' => 'Vimeo', 'src' => 'https://player.vimeo.com/video/'.$m[1].'?dnt=1'];
|
||||
}
|
||||
|
||||
if (preg_match('~\.(mp4|webm)(\?.*)?$~i', $url)) {
|
||||
return ['provider' => 'file', 'src' => $url];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,13 +4,16 @@ namespace App\Providers;
|
|||
|
||||
use App\Dashboard\CoreWidgets;
|
||||
use App\Dashboard\Widgets;
|
||||
use App\Editorial\Editorial;
|
||||
use App\Models\User;
|
||||
use App\Pagebuilder\Blocks;
|
||||
use App\Support\Abilities;
|
||||
use App\Support\Avatars;
|
||||
use App\Support\BackgroundTasks;
|
||||
use App\Support\Branding;
|
||||
use App\Support\Installation;
|
||||
use App\Support\License\LicenseStore;
|
||||
use App\Support\Navigation;
|
||||
use App\Support\Permissions\PermissionResource;
|
||||
use App\Support\Permissions\Permissions;
|
||||
use App\Support\ProfileFields;
|
||||
|
|
@ -45,6 +48,8 @@ class AppServiceProvider extends ServiceProvider
|
|||
$this->app->singleton(LicenseStore::class);
|
||||
$this->app->singleton(Permissions::class);
|
||||
$this->app->singleton(ProfileFields::class);
|
||||
$this->app->singleton(Blocks::class);
|
||||
$this->app->singleton(Editorial::class);
|
||||
$this->app->singleton(BackgroundTasks::class);
|
||||
$this->app->singleton(InstalledVersion::class);
|
||||
$this->app->singleton(UpdateProgress::class);
|
||||
|
|
@ -55,6 +60,9 @@ class AppServiceProvider extends ServiceProvider
|
|||
{
|
||||
CoreWidgets::register($this->app->make(Widgets::class));
|
||||
|
||||
$this->app->make(Navigation::class)->add('editorial', 'Redaktion', '/redaktion', active: ['editorial', 'pages.editor', 'news.editor.*'], sort: 85,
|
||||
visible: fn (User $user) => $this->app->make(Editorial::class)->visibleFor($user));
|
||||
|
||||
// N+1-Abfragen in Entwicklung und Tests sofort sichtbar machen (docs/PLAN.md, Performance).
|
||||
Model::preventLazyLoading(! $this->app->isProduction());
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class Abilities
|
|||
/** @var array<string, array{label: string, group: string, resource: string|null}> */
|
||||
private array $abilities = [];
|
||||
|
||||
/** @var array<string, array{label: string, options: callable}> */
|
||||
/** @var array<string, array{label: string, options: callable, descendants: callable|null}> */
|
||||
private array $resourceTypes = [];
|
||||
|
||||
public function __construct()
|
||||
|
|
@ -36,11 +36,20 @@ class Abilities
|
|||
/**
|
||||
* Objekttyp, auf den sich Rechte beziehen können (z. B. „news_category“).
|
||||
*
|
||||
* Hierarchische Typen (z. B. Seiten) geben $descendants an: Rechte mit
|
||||
* „inkl. Unterobjekten“ gelten dann auch für alle Nachfahren.
|
||||
*
|
||||
* @param callable(): array<int|string, string> $options ID => Bezeichnung
|
||||
* @param (callable(list<int>): list<int>)|null $descendants IDs => IDs aller Nachfahren
|
||||
*/
|
||||
public function resourceType(string $type, string $label, callable $options): void
|
||||
public function resourceType(string $type, string $label, callable $options, ?callable $descendants = null): void
|
||||
{
|
||||
$this->resourceTypes[$type] = ['label' => $label, 'options' => $options];
|
||||
$this->resourceTypes[$type] = ['label' => $label, 'options' => $options, 'descendants' => $descendants];
|
||||
}
|
||||
|
||||
public function isHierarchical(?string $type): bool
|
||||
{
|
||||
return $type !== null && isset($this->resourceTypes[$type]['descendants']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -68,7 +77,7 @@ class Abilities
|
|||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{label: string, options: callable}>
|
||||
* @return array<string, array{label: string, options: callable, descendants: callable|null}>
|
||||
*/
|
||||
public function resourceTypes(): array
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\Drivers\Gd\Driver;
|
||||
use Intervention\Image\Encoders\WebpEncoder;
|
||||
use Intervention\Image\ImageManager;
|
||||
|
|
@ -46,6 +48,31 @@ class Images
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bild für Inhalte (Pagebuilder, Beiträge): Seitenverhältnis bleibt, höchstens
|
||||
* 2000 Pixel breit, als WebP. GIFs bleiben unverändert (Animationen).
|
||||
*
|
||||
* @return string Pfad im Speicher „uploads“
|
||||
*/
|
||||
public function storeContentImage(UploadedFile $file, string $directory): string
|
||||
{
|
||||
$disk = Storage::disk('uploads');
|
||||
$name = $directory.'/'.now()->format('Y/m').'/'.Str::random(24);
|
||||
|
||||
if ($file->getMimeType() === 'image/gif') {
|
||||
$disk->putFileAs(dirname($name), $file, basename($name).'.gif');
|
||||
|
||||
return $name.'.gif';
|
||||
}
|
||||
|
||||
self::raiseMemoryLimitFor($file->getRealPath());
|
||||
$image = ImageManager::usingDriver(Driver::class)->decodePath($file->getRealPath());
|
||||
$image->scaleDown(width: 2000);
|
||||
$disk->put($name.'.webp', $image->encode(new WebpEncoder(quality: 84))->toString());
|
||||
|
||||
return $name.'.webp';
|
||||
}
|
||||
|
||||
public function deleteWithVariants(?string $path): void
|
||||
{
|
||||
if (! $path) {
|
||||
|
|
|
|||
|
|
@ -4,18 +4,28 @@ namespace App\Support;
|
|||
|
||||
use App\Models\User;
|
||||
use App\Plugins\Hooks;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Hauptnavigation des Portals als Registry: Kern und Plugins tragen Einträge ein.
|
||||
*
|
||||
* app(Navigation::class)->add('news', 'Neuigkeiten', '/neuigkeiten', active: 'news.*', sort: 20);
|
||||
*
|
||||
* Dynamische Einträge (z. B. Seiten aus dem Seitenbaum) liefert ein Provider:
|
||||
*
|
||||
* app(Navigation::class)->provider('pages', fn (Request $request, ?User $user) => [
|
||||
* ['key' => 'page-3', 'label' => 'MAV', 'href' => '/seiten/mav', 'active' => true, 'sort' => 40],
|
||||
* ]);
|
||||
*/
|
||||
class Navigation
|
||||
{
|
||||
/** @var array<string, array{label: string, href: string, active: list<string>, sort: int, ability: string|null}> */
|
||||
/** @var array<string, array{label: string, href: string, active: list<string>, sort: int, ability: string|null, visible: Closure|null}> */
|
||||
private array $items = [];
|
||||
|
||||
/** @var array<string, Closure> */
|
||||
private array $providers = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->add('home', 'Startseite', '/', active: 'dashboard', sort: 0);
|
||||
|
|
@ -24,15 +34,24 @@ class Navigation
|
|||
|
||||
/**
|
||||
* @param string|list<string> $active Routennamen (mit *), bei denen der Eintrag aktiv ist
|
||||
* @param (Closure(User): bool)|null $visible zusätzliche Bedingung je Person
|
||||
*/
|
||||
public function add(string $key, string $label, string $href, string|array $active = [], int $sort = 50, ?string $ability = null): void
|
||||
public function add(string $key, string $label, string $href, string|array $active = [], int $sort = 50, ?string $ability = null, ?Closure $visible = null): void
|
||||
{
|
||||
$this->items[$key] = ['label' => $label, 'href' => $href, 'active' => (array) $active, 'sort' => $sort, 'ability' => $ability];
|
||||
$this->items[$key] = ['label' => $label, 'href' => $href, 'active' => (array) $active, 'sort' => $sort, 'ability' => $ability, 'visible' => $visible];
|
||||
}
|
||||
|
||||
public function remove(string $key): void
|
||||
{
|
||||
unset($this->items[$key]);
|
||||
unset($this->items[$key], $this->providers[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure(Request, User|null): list<array{key: string, label: string, href: string, active: bool, sort: int}> $items
|
||||
*/
|
||||
public function provider(string $key, Closure $items): void
|
||||
{
|
||||
$this->providers[$key] = $items;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -42,13 +61,21 @@ class Navigation
|
|||
{
|
||||
$items = collect($this->items)
|
||||
->filter(fn (array $item) => $item['ability'] === null || ($user?->can($item['ability']) ?? false))
|
||||
->sortBy('sort')
|
||||
->filter(fn (array $item) => $item['visible'] === null || ($user !== null && ($item['visible'])($user)))
|
||||
->map(fn (array $item, string $key) => [
|
||||
'key' => $key,
|
||||
'label' => $item['label'],
|
||||
'href' => $item['href'],
|
||||
'active' => $item['active'] !== [] && $request->routeIs(...$item['active']),
|
||||
])
|
||||
'sort' => $item['sort'],
|
||||
]);
|
||||
|
||||
foreach ($this->providers as $provider) {
|
||||
$items = $items->merge($provider($request, $user));
|
||||
}
|
||||
|
||||
$items = $items->sortBy('sort')
|
||||
->map(fn (array $item) => ['key' => $item['key'], 'label' => $item['label'], 'href' => $item['href'], 'active' => (bool) $item['active']])
|
||||
->values()
|
||||
->all();
|
||||
|
||||
|
|
|
|||
|
|
@ -76,6 +76,47 @@ class Audiences
|
|||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Regeln eines Typs auf einmal – für gecachte Strukturen wie den Seitenbaum.
|
||||
*
|
||||
* @return array<int, array{role: list<string>, department: list<string>, location: list<string>}> ID => Regeln
|
||||
*/
|
||||
public static function allOfType(string $type): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach (Audience::query()->where('resource_type', $type)->get(['resource_id', 'rule_type', 'rule_value']) as $audience) {
|
||||
$result[$audience->resource_id] ??= ['role' => [], 'department' => [], 'location' => []];
|
||||
$result[$audience->resource_id][$audience->rule_type][] = $audience->rule_value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dieselbe Prüfung wie constrain()/allows(), aber in PHP auf bereits geladenen Regeln.
|
||||
*
|
||||
* @param array{role?: list<int|string>, department?: list<string>, location?: list<string>}|null $rules
|
||||
*/
|
||||
public static function matches(?array $rules, ?User $user): bool
|
||||
{
|
||||
$rules = array_filter((array) $rules);
|
||||
|
||||
if ($rules === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($user === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$roleIds = array_map('strval', $user->permissions()->roleIds);
|
||||
|
||||
return array_intersect(array_map('strval', $rules['role'] ?? []), $roleIds) !== []
|
||||
|| (filled($user->department) && in_array($user->department, $rules['department'] ?? [], true))
|
||||
|| (filled($user->location) && in_array($user->location, $rules['location'] ?? [], true));
|
||||
}
|
||||
|
||||
private static function matching(QueryBuilder $rules, User $user): void
|
||||
{
|
||||
$roleIds = array_map('strval', $user->permissions()->roleIds);
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace App\Support\Permissions;
|
|||
|
||||
use App\Models\Grant;
|
||||
use App\Models\User;
|
||||
use App\Support\Abilities;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
|
|
@ -48,14 +49,33 @@ class Permissions
|
|||
$roleIds = DB::table('role_user')->where('user_id', $user->id)->distinct()->pluck('role_id')->map(fn ($id) => (int) $id)->all();
|
||||
$global = [];
|
||||
$objects = [];
|
||||
$inherited = [];
|
||||
|
||||
foreach (Grant::query()->whereIn('role_id', $roleIds)->get(['ability', 'resource_type', 'resource_id']) as $grant) {
|
||||
foreach (Grant::query()->whereIn('role_id', $roleIds)->get(['ability', 'resource_type', 'resource_id', 'include_children']) as $grant) {
|
||||
if ($grant->resource_type === null) {
|
||||
$global[$grant->ability] = true;
|
||||
} elseif ($grant->resource_id === null) {
|
||||
$objects[$grant->ability][$grant->resource_type] = true;
|
||||
} elseif (($objects[$grant->ability][$grant->resource_type] ?? null) !== true) {
|
||||
$objects[$grant->ability][$grant->resource_type][] = $grant->resource_id;
|
||||
|
||||
if ($grant->include_children) {
|
||||
$inherited[$grant->resource_type][$grant->ability][] = $grant->resource_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// „Inkl. Unterobjekten“: Nachfahren einmalig auflösen – die Prüfung bleibt ein Array-Zugriff.
|
||||
// Hierarchische Typen leeren den Rechte-Cache, wenn sich ihr Baum ändert.
|
||||
$types = app(Abilities::class)->resourceTypes();
|
||||
|
||||
foreach ($inherited as $type => $byAbility) {
|
||||
$descendants = $types[$type]['descendants'] ?? null;
|
||||
|
||||
foreach ($descendants ? $byAbility : [] as $ability => $ids) {
|
||||
if (is_array($objects[$ability][$type] ?? null)) {
|
||||
$objects[$ability][$type] = array_merge($objects[$ability][$type], array_map('intval', $descendants($ids)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,14 @@ class RichText
|
|||
return $this->sanitizer()->sanitize($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTML aus dem Portal-Editor (TipTap) bereinigen.
|
||||
*/
|
||||
public function sanitize(?string $html): string
|
||||
{
|
||||
return blank($html) ? '' : $this->sanitizer()->sanitize((string) $html);
|
||||
}
|
||||
|
||||
public function readingMinutes(?string $html): int
|
||||
{
|
||||
$words = str_word_count(strip_tags((string) $html), 0, 'ÄÖÜäöüß');
|
||||
|
|
|
|||
|
|
@ -9,5 +9,5 @@ namespace App;
|
|||
*/
|
||||
final class Version
|
||||
{
|
||||
public const VERSION = '0.4.0';
|
||||
public const VERSION = '0.5.0';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue