Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m10s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 6m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m25s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m28s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m27s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m27s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m12s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
66 lines
3 KiB
PHP
66 lines
3 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\News;
|
||
|
||
use App\Models\User;
|
||
use Inhaus\Plugins\News\Models\NewsPost;
|
||
|
||
/**
|
||
* „Redaktion → Neuigkeiten“: eigene Entwürfe, Beiträge zur Freigabe und Geplantes.
|
||
*/
|
||
final class EditorialNews
|
||
{
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
public static function props(User $user): array
|
||
{
|
||
$publishable = $user->can('news.manage') ? true : $user->permissions()->ids('news.post.publish', 'news_category');
|
||
$writable = $user->can('news.manage') ? true : $user->permissions()->ids('news.post.create', 'news_category');
|
||
|
||
$map = fn (NewsPost $post) => [
|
||
'id' => $post->id,
|
||
'title' => $post->title,
|
||
'category' => $post->category?->name,
|
||
'author' => $post->author?->name,
|
||
'status' => $post->status,
|
||
'status_label' => $post->statusLabel(),
|
||
'review_note' => $post->review_note,
|
||
'date' => ($post->published_at ?? $post->submitted_at ?? $post->updated_at)?->toIso8601String(),
|
||
'edit_url' => route('news.editor.edit', $post),
|
||
];
|
||
|
||
$review = $publishable === [] ? collect() : NewsPost::query()->with(['category', 'author'])
|
||
->where('status', NewsPost::PENDING)
|
||
->when($publishable !== true, fn ($q) => $q->whereIn('category_id', $publishable))
|
||
->oldest('submitted_at')->limit(20)->get();
|
||
|
||
$mine = NewsPost::query()->with(['category', 'author'])
|
||
->where('author_id', $user->id)
|
||
->where(fn ($q) => $q->whereIn('status', [NewsPost::DRAFT, NewsPost::PENDING])->orWhere('published_at', '>', now()))
|
||
->latest('updated_at')->limit(20)->get();
|
||
|
||
$scheduled = NewsPost::query()->with(['category', 'author'])
|
||
->where('status', NewsPost::PUBLISHED)->where('published_at', '>', now())
|
||
->when($writable !== true, fn ($q) => $q->whereIn('category_id', $writable ?: [0]))
|
||
->whereKeyNot($mine->modelKeys())
|
||
->oldest('published_at')->limit(20)->get();
|
||
|
||
// Zuletzt veröffentlicht – zum Nachbessern, ohne das Backend zu brauchen
|
||
$published = $writable === [] ? collect() : NewsPost::query()->with(['category', 'author'])
|
||
->published()
|
||
->when($writable !== true, fn ($q) => $q->whereIn('category_id', $writable))
|
||
->withCount('likes')
|
||
->latest('published_at')->limit(15)->get()
|
||
->filter(fn (NewsPost $post) => $user->can('update', $post));
|
||
$readers = app(Reach::class)->counts($published->modelKeys());
|
||
|
||
return [
|
||
'review' => $review->map($map)->all(),
|
||
'published' => $published->map(fn (NewsPost $post) => [...$map($post), 'readers' => $readers[$post->id] ?? 0, 'likes' => (int) $post->likes_count])->values()->all(),
|
||
'mine' => $mine->map($map)->all(),
|
||
'scheduled' => $scheduled->map($map)->all(),
|
||
'canCreate' => app(Policies\NewsPostPolicy::class)->create($user),
|
||
];
|
||
}
|
||
}
|