Loheide-eu-mitarbeiterplatt.../plugins-core/pages/src/PagesPlugin.php
Friederich Loheide b276e1c6d0
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m24s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m46s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m31s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m18s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Globale Live-Suche, Kontaktkarten-Personensuche, neue Beitragsansicht, Termine mit Seitenleiste, Demo-Inhalte, Plugin-Upload-Fix
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:07:13 +00:00

131 lines
5.8 KiB
PHP
Raw Permalink 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 Inhaus\Plugins\Pages;
use App\Editorial\Editorial;
use App\Models\User;
use App\Pagebuilder\Blocks;
use App\Pagebuilder\BlockType;
use App\Pagebuilder\Field;
use App\Plugins\Plugin;
use App\Search\GlobalSearch;
use App\Support\Abilities;
use App\Support\Navigation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Schedule;
use Inhaus\Plugins\Pages\Models\Page;
use Inhaus\Plugins\Pages\Models\PageFile;
use Inhaus\Plugins\Pages\Policies\PagePolicy;
class PagesPlugin extends Plugin
{
public function register(): void
{
$this->app->make(GlobalSearch::class)->add('pages', 'Seiten', 'file-text', fn (...$args) => app(PageSearch::class)(...$args), sort: 30);
$this->app->singleton(PageTree::class);
$abilities = $this->app->make(Abilities::class);
$abilities->define('pages.manage', 'Seiten: alle Seiten verwalten', 'Seiten');
$abilities->define('pages.edit', 'Seiten bearbeiten', 'Seiten', resource: 'page');
$abilities->define('pages.publish', 'Seiten veröffentlichen', 'Seiten', resource: 'page');
$abilities->resourceType(
'page',
'Seite',
fn () => $this->app->make(PageTree::class)->options(),
fn (array $ids) => $this->app->make(PageTree::class)->descendantIds($ids),
);
Gate::policy(Page::class, PagePolicy::class);
// Seiten der obersten Ebene mit „In der Hauptnavigation“ erscheinen dort.
$this->app->make(Navigation::class)->provider('pages', function (Request $request, ?User $user) {
$current = trim((string) $request->route('path'), '/');
return array_map(fn (array $page) => [
'key' => 'page-'.$page['id'],
'label' => $page['title'],
'href' => '/seiten/'.$page['path'],
'active' => $request->routeIs('pages.show') && ($current === $page['path'] || str_starts_with($current, $page['path'].'/')),
'sort' => 40 + $page['sort'],
], $this->app->make(PageTree::class)->navigation($user));
});
// Menü-Editor: „Seite“ als Menüpunkt, auf Wunsch mit ihren Unterseiten als Untermenü.
$this->app->make(Navigation::class)->source('page', 'Seite', fn () => $this->app->make(PageTree::class)->options(), function (string $id, array $item, Request $request, ?User $user) {
$tree = $this->app->make(PageTree::class);
$page = $tree->get((int) $id);
if (! $page || ! $tree->isVisible($page['id'], $user)) {
return null;
}
$current = trim((string) $request->route('path'), '/');
$isActive = fn (string $path) => $request->routeIs('pages.show') && ($current === $path || str_starts_with($current, $path.'/'));
$children = [];
if (! empty($item['subpages'])) {
foreach ($tree->childIds($page['id']) as $childId) {
$child = $tree->get($childId);
if ($child && $tree->isVisible($childId, $user)) {
$children[] = ['label' => $child['title'], 'href' => '/seiten/'.$child['path'], 'active' => $isActive($child['path'])];
}
}
}
return ['label' => $page['title'], 'href' => '/seiten/'.$page['path'], 'active' => $isActive($page['path']), 'children' => $children];
});
$this->app->make(Editorial::class)->add(
'pages',
'Seiten',
'pages::EditorialPages',
20,
fn (User $user) => app(PagePolicy::class)->viewAny($user),
fn (User $user) => EditorialPages::props($user),
);
$this->registerBlocks($this->app->make(Blocks::class));
}
public function boot(): void
{
// Zeitgesteuerte Veröffentlichungen. Beim Aufruf einer Seite wird ein fälliger Stand zusätzlich sofort übernommen.
Schedule::call(function () {
Page::query()->whereNotNull('scheduled_at')->where('scheduled_at', '<=', now())->get()->each->publishIfDue();
})->everyMinute()->name('pages-scheduled')->withoutOverlapping(5);
}
private function registerBlocks(Blocks $blocks): void
{
$blocks->add(BlockType::make('downloads', 'Downloads', 'file-down', 'media', 'pages::Downloads')
->description('Dateien zum Herunterladen – nur für Personen, die die Seite sehen dürfen.')
->fields([
Field::text('title', 'Überschrift', 150),
Field::ids('files', 'Dateien', 'page-file'),
])
->resolveUsing(function (array $data, ?User $user, array $context) {
$files = PageFile::query()->whereIn('id', $data['files'])
->when(isset($context['page_id']), fn ($q) => $q->where('page_id', $context['page_id']))
->get()->keyBy('id');
return ['files' => collect($data['files'])->map(fn ($id) => $files->get($id)?->toBlockArray())->filter()->values()->all()];
}));
$blocks->add(BlockType::make('subpages', 'Unterseiten', 'folder-tree', 'dynamic', 'pages::Subpages')
->description('Verweise auf die Unterseiten dieser Seite.')
->fields([
Field::text('title', 'Überschrift', 150),
Field::select('layout', 'Darstellung', ['cards' => 'Kacheln', 'list' => 'Liste'], 'cards'),
])
->resolveUsing(function (array $data, ?User $user, array $context) {
$tree = $this->app->make(PageTree::class);
return ['pages' => isset($context['page_id'])
? array_map(fn (array $page) => ['id' => $page['id'], 'title' => $page['title'], 'url' => $page['url']], $tree->visibleTree($context['page_id'], $user, 1))
: []];
}));
}
}