Loheide-eu-mitarbeiterplatt.../plugins-core/news/src/NewsPlugin.php

157 lines
7.2 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 Inhaus\Plugins\News;
use App\Dashboard\Widgets;
use App\Editorial\Editorial;
use App\Media\ImagePaths;
use App\Media\MediaUsages;
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\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Schema;
use Inhaus\Plugins\News\Http\Resources\NewsPostResource;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Inhaus\Plugins\News\Policies\NewsCategoryPolicy;
use Inhaus\Plugins\News\Policies\NewsPostPolicy;
class NewsPlugin extends Plugin
{
public function register(): void
{
$this->app->make(GlobalSearch::class)->add('news', 'Neuigkeiten', 'newspaper', fn (...$args) => app(NewsSearch::class)(...$args), sort: 10);
$this->app->make(Navigation::class)->add('news', 'Neuigkeiten', '/neuigkeiten', active: ['news.index', 'news.show'], sort: 20);
$abilities = $this->app->make(Abilities::class);
$abilities->define('news.manage', 'Neuigkeiten: alle Kategorien verwalten', 'Neuigkeiten');
$abilities->define('news.post.create', 'Beiträge schreiben', 'Neuigkeiten', resource: 'news_category');
$abilities->define('news.post.publish', 'Beiträge veröffentlichen', 'Neuigkeiten', resource: 'news_category');
$abilities->define('news.comments.moderate', 'Kommentare anderer löschen', 'Neuigkeiten');
$abilities->resourceType('news_category', 'Kategorie', fn () => NewsCategory::query()->orderBy('sort')->pluck('name', 'id')->all());
Gate::policy(NewsCategory::class, NewsCategoryPolicy::class);
Gate::policy(NewsPost::class, NewsPostPolicy::class);
$this->registerWidgets($this->app->make(Widgets::class));
$this->registerMediaUsages($this->app->make(MediaUsages::class));
$this->app->make(Editorial::class)->add(
'news',
'Neuigkeiten',
'news::EditorialNews',
10,
fn (User $user) => app(NewsPostPolicy::class)->viewAny($user),
fn (User $user) => EditorialNews::props($user),
);
$this->app->make(Blocks::class)->add(BlockType::make('news.latest', 'Neuigkeiten', 'newspaper', 'dynamic', 'news::LatestNews')
->description('Die neuesten Beiträge – optional nur aus einer Kategorie.')
->fields([
Field::text('title', 'Überschrift', 150, 'Neuigkeiten'),
Field::select('category', 'Kategorie', fn () => ['' => 'Alle Kategorien'] + NewsCategory::query()->orderBy('sort')->pluck('name', 'slug')->all(), ''),
Field::number('count', 'Anzahl', 1, 12, 3),
Field::select('layout', 'Darstellung', ['cards' => 'Kacheln', 'list' => 'Liste'], 'cards'),
])
->resolveUsing(function (array $data, ?User $user) {
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')
->when($data['category'], fn ($q, $slug) => $q->whereHas('category', fn ($c) => $c->where('slug', $slug)))
->latest('published_at')->limit($data['count'])->get();
return [
'posts' => NewsPostResource::collection($posts)->resolve(),
'more_url' => '/neuigkeiten'.($data['category'] ? '?kategorie='.$data['category'] : ''),
];
}));
}
/**
* Mediathek „Eingebunden in“: Titelbild, Bilder im Text und – sobald Beiträge
* mit dem Pagebuilder geschrieben werden – Bilder in der Spalte „content“.
*/
private function registerMediaUsages(MediaUsages $usages): void
{
$usages->add('news', 'Neuigkeiten', function (NewsPost $post) {
$base = ['label' => $post->title, 'url' => route('news.editor.edit', $post, false), 'draft' => $post->status !== NewsPost::PUBLISHED];
$entries = [
[...$base, 'context' => 'Titelbild', 'paths' => [$post->cover_path]],
[...$base, 'context' => ImagePaths::HTML_CONTEXT, 'paths' => [...ImagePaths::fromHtml($post->body_html), ...ImagePaths::fromHtml($post->body)]],
];
if (self::hasContentColumn()) {
$content = $post->getAttribute('content');
$content = is_string($content) ? json_decode($content, true) : $content;
foreach (ImagePaths::fromContent(is_array($content) ? $content : null) as $context => $paths) {
$entries[] = [...$base, 'context' => $context, 'paths' => $paths];
}
}
return $entries;
}, fn (callable $each) => NewsPost::query()->chunkById(100, fn ($posts) => $posts->each($each)));
$usages->watch('news', NewsPost::class);
}
private static function hasContentColumn(): bool
{
static $has;
return $has ??= Schema::hasColumn('news_posts', 'content');
}
private function registerWidgets(Widgets $widgets): void
{
// Slider: hervorgehobene Beiträge mit Bild, sonst die neuesten mit Bild.
$widgets->add('news.hero', 'news::Hero', 'hero', sort: 10, props: function (User $user) {
$featured = $this->featured($user);
return $featured->isEmpty() ? null : ['posts' => NewsPostResource::collection($featured)->resolve()];
}, label: 'Slider mit hervorgehobenen Beiträgen', mobileSort: 10);
$widgets->add('news.latest', 'news::Latest', 'feed', sort: 10, props: function (User $user, Request $request) {
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')
->whereKeyNot($this->featured($user)->modelKeys())
->latest('published_at')->limit(4)->get();
return [
'posts' => NewsPostResource::collection($posts)->resolve(),
'canWrite' => $user->can('news.manage'),
];
}, label: 'Neuigkeiten', mobileSort: 20);
$widgets->add('news.areas', 'news::Areas', 'main', sort: 20, props: function (User $user) {
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')
->whereKeyNot($this->featured($user)->modelKeys())
->latest('published_at')->skip(4)->limit(8)->get();
return $posts->isEmpty() ? null : ['posts' => NewsPostResource::collection($posts)->resolve()];
}, label: 'Aktuelles aus den Bereichen', mobileSort: 50);
}
/**
* @return Collection<int, NewsPost>
*/
private function featured(User $user)
{
static $cache = [];
return $cache[$user->id] ??= (function () use ($user) {
$featured = NewsPost::query()->published()->visibleTo($user)->with('category')
->where('is_featured', true)->whereNotNull('cover_path')
->latest('published_at')->limit(5)->get();
return $featured->isNotEmpty() ? $featured : NewsPost::query()->published()->visibleTo($user)->with('category')
->whereNotNull('cover_path')->latest('published_at')->limit(3)->get();
})();
}
}