Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m51s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m48s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m16s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m21s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m30s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 9m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m49s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m37s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m8s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
158 lines
5.4 KiB
PHP
158 lines
5.4 KiB
PHP
<?php
|
||
|
||
namespace App\Editorial;
|
||
|
||
use App\Models\User;
|
||
use App\Support\Icons;
|
||
use App\Support\Navigation;
|
||
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 = [];
|
||
|
||
/** @var array<string, array{icon: ?string, todo: ?Closure, create: ?Closure}> */
|
||
private array $meta = [];
|
||
|
||
/**
|
||
* @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');
|
||
}
|
||
|
||
/**
|
||
* Zusatzangaben für die Navigation der Redaktion (ab 0.42.0):
|
||
*
|
||
* $editorial->meta('news', icon: 'newspaper',
|
||
* todo: fn (User $u) => ['count' => 3, 'label' => 'Beiträge warten auf Freigabe'], // oder null
|
||
* create: fn (User $u) => ['label' => 'Neuer Beitrag', 'href' => '/redaktion/neuigkeiten/neu']);
|
||
*
|
||
* „todo“ zählt nur, was jetzt Handlung braucht – es erscheint als Zahl in der Leiste und in der Übersicht.
|
||
*
|
||
* @param (Closure(User): (array{count: int, label: string}|null))|null $todo
|
||
* @param (Closure(User): (array{label: string, href: string}|null))|null $create
|
||
*/
|
||
public function meta(string $key, ?string $icon = null, ?Closure $todo = null, ?Closure $create = null): void
|
||
{
|
||
$this->meta[$key] = compact('icon', 'todo', 'create');
|
||
}
|
||
|
||
public function remove(string $key): void
|
||
{
|
||
unset($this->sections[$key], $this->meta[$key]);
|
||
}
|
||
|
||
/**
|
||
* Bereiche für die Leiste: Bezeichnung, Symbol, offene Aufgaben – ohne die Listen selbst zu laden.
|
||
*
|
||
* @return list<array{key: string, label: string, icon: string, todo: array{count: int, label: string}|null, create: array{label: string, href: string}|null}>
|
||
*/
|
||
public function menu(User $user): array
|
||
{
|
||
$result = [];
|
||
|
||
foreach (collect($this->sections)->sortBy('sort') as $key => $section) {
|
||
if (! ($section['visible'])($user)) {
|
||
continue;
|
||
}
|
||
|
||
$meta = $this->meta[$key] ?? [];
|
||
$result[] = [
|
||
'key' => $key,
|
||
'label' => $section['label'],
|
||
'icon' => $this->icon($key, $meta['icon'] ?? null),
|
||
'todo' => $this->call($key, $meta['todo'] ?? null, $user, fn ($todo) => is_array($todo) && ($todo['count'] ?? 0) > 0),
|
||
'create' => $this->call($key, $meta['create'] ?? null, $user, fn ($create) => is_array($create) && filled($create['href'] ?? null)),
|
||
];
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* Ein einzelner Bereich mit seinen Daten – null, wenn es ihn nicht gibt oder er nicht erlaubt ist.
|
||
*
|
||
* @return array{key: string, label: string, component: string, props: array<string, mixed>}|null
|
||
*/
|
||
public function section(string $key, User $user): ?array
|
||
{
|
||
$section = $this->sections[$key] ?? null;
|
||
|
||
if (! $section || ! ($section['visible'])($user)) {
|
||
return null;
|
||
}
|
||
|
||
return ['key' => $key, 'label' => $section['label'], 'component' => $section['component'], 'props' => ($section['props'])($user)];
|
||
}
|
||
|
||
private function icon(string $key, ?string $icon): string
|
||
{
|
||
$fallback = Navigation::DEFAULT_ICONS[explode('.', $key)[0]] ?? Navigation::FALLBACK_ICON;
|
||
|
||
return Icons::has($icon) ? $icon : $fallback;
|
||
}
|
||
|
||
private function call(string $key, ?Closure $callback, User $user, Closure $valid): ?array
|
||
{
|
||
if (! $callback) {
|
||
return null;
|
||
}
|
||
|
||
try {
|
||
$value = $callback($user);
|
||
|
||
return $valid($value) ? $value : null;
|
||
} catch (Throwable $e) {
|
||
Log::error("Redaktionsbereich {$key}: ".$e->getMessage(), ['exception' => $e]);
|
||
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param string|null $except diesen Abschnitt nicht mitprüfen (verhindert Zirkelschlüsse, z. B. „media“)
|
||
*/
|
||
public function visibleFor(User $user, ?string $except = null): bool
|
||
{
|
||
foreach ($this->sections as $key => $section) {
|
||
if ($key !== $except && ($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;
|
||
}
|
||
}
|