Loheide-eu-mitarbeiterplatt.../plugins-core/news/src/Models/NewsPost.php
Friederich Loheide 9320d602a8
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m0s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 8m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m10s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s
Kommentare mit Antworten unter Neuigkeiten
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:13:41 +00:00

202 lines
6.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\Models;
use App\Models\User;
use App\Support\Images;
use App\Support\Permissions\Audiences;
use App\Support\RichText;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
use Inhaus\Plugins\News\Database\Factories\NewsPostFactory;
#[Fillable([
'category_id', 'author_id', 'title', 'slug', 'teaser', 'body',
'cover_path', 'is_featured', 'published_at', 'form_id', 'content', 'comments_enabled',
])]
class NewsPost extends Model
{
use HasFactory;
public const DRAFT = 'draft';
public const PENDING = 'pending';
public const PUBLISHED = 'published';
protected static function newFactory(): NewsPostFactory
{
return NewsPostFactory::new();
}
protected static function booted(): void
{
static::saving(function (NewsPost $post): void {
if (blank($post->slug)) {
$post->slug = static::uniqueSlug($post->title, $post->getKey());
}
// Ein Veröffentlichungsdatum bedeutet „veröffentlicht“ (bzw. geplant) – auch aus dem Admin-Formular.
if ($post->published_at !== null) {
$post->status = self::PUBLISHED;
} elseif ($post->status === self::PUBLISHED || $post->status === null) {
$post->status = self::DRAFT;
}
if ($post->isDirty('body') || $post->body_html === null) {
$richText = app(RichText::class);
$post->body_html = $richText->toSafeHtml($post->body);
$post->reading_minutes = $richText->readingMinutes($post->body_html);
}
});
// Bildvarianten erzeugen bzw. beim Austausch die alten Dateien entfernen.
static::saved(function (NewsPost $post): void {
if (! $post->wasRecentlyCreated && ! $post->wasChanged('cover_path')) {
return;
}
$previous = $post->wasRecentlyCreated ? null : $post->getOriginal('cover_path');
if ($previous && $previous !== $post->cover_path) {
app(Images::class)->deleteWithVariants($previous);
}
if ($post->cover_path) {
app(Images::class)->createVariants($post->cover_path);
}
});
static::deleted(fn (NewsPost $post) => app(Images::class)->deleteWithVariants($post->cover_path));
}
protected function casts(): array
{
return [
'is_featured' => 'boolean',
'content' => 'array',
'comments_enabled' => 'boolean',
'published_at' => 'datetime',
'submitted_at' => 'datetime',
'form_id' => 'integer',
];
}
/**
* @return BelongsTo<NewsCategory, $this>
*/
public function category(): BelongsTo
{
return $this->belongsTo(NewsCategory::class);
}
/**
* @return BelongsTo<User, $this>
*/
public function reviewer(): BelongsTo
{
return $this->belongsTo(User::class, 'reviewed_by');
}
/**
* @return BelongsTo<User, $this>
*/
public function author(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Veröffentlicht = Veröffentlichungsdatum gesetzt und erreicht.
*
* @param Builder<NewsPost> $query
*/
public function scopePublished(Builder $query): void
{
$query->where('news_posts.status', self::PUBLISHED)->whereNotNull('published_at')->where('published_at', '<=', now());
}
public function isPublished(): bool
{
return $this->status === self::PUBLISHED && $this->published_at !== null && $this->published_at->isPast();
}
public function isScheduled(): bool
{
return $this->status === self::PUBLISHED && $this->published_at?->isFuture();
}
public function statusLabel(): string
{
return match (true) {
$this->status === self::PENDING => 'Zur Freigabe',
$this->isScheduled() => 'Geplant',
$this->isPublished() => 'Veröffentlicht',
default => 'Entwurf',
};
}
/**
* Inhalt als Pagebuilder-Blöcke – ältere Beiträge (nur „body“) als ein Textblock.
*
* @return array{blocks: list<array<string, mixed>>}
*/
public function blocks(): array
{
if (is_array($this->content)) {
return $this->content;
}
return ['blocks' => filled($this->body) ? [['type' => 'text', 'data' => ['html' => $this->body_html ?? $this->body]]] : []];
}
/** @return HasMany<NewsComment, $this> */
public function comments(): HasMany
{
return $this->hasMany(NewsComment::class, 'post_id');
}
public function coverUrl(string $variant = 'large'): ?string
{
return app(Images::class)->url($this->cover_path, $variant);
}
public static function uniqueSlug(string $title, ?int $ignoreId = null): string
{
$base = Str::slug($title, language: 'de') ?: 'beitrag';
$slug = $base;
for ($i = 2; static::query()->where('slug', $slug)->when($ignoreId, fn ($q) => $q->whereKeyNot($ignoreId))->exists(); $i++) {
$slug = "{$base}-{$i}";
}
return $slug;
}
/**
* Nur Beiträge, die diese Person sehen darf – die Zielgruppe hängt an der Kategorie.
*
* @param Builder<NewsPost> $query
*/
public function scopeVisibleTo(Builder $query, ?User $user): void
{
if ($user?->can('news.manage')) {
return;
}
$query->where(function (Builder $q) use ($user) {
$q->whereNull('news_posts.category_id');
$q->orWhere(fn (Builder $inner) => Audiences::constrain($inner, 'news_category', 'news_posts.category_id', $user));
});
}
public function isVisibleTo(?User $user): bool
{
return $this->category_id === null || $user?->can('news.manage') || Audiences::allows('news_category', $this->category_id, $user);
}
}