Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 2m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m40s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m35s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 2m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m38s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
- Seiten-Plugin: Seitenbaum mit Adressen, Seitennavigation, Brotkrumen und optional Hauptnavigation; Sichtbarkeit vererbt sich auf Unterseiten - Pagebuilder (Block-Registry im Kern, Plugins tragen Blöcke bei): Abschnitt, Spalten, Tabs, Akkordeon, Box, Überschrift, Text (TipTap), Zitat, Schaltflächen, Symbolliste, Tabelle, Trennlinie, Abstand, Bild, Galerie, Video (erst nach Klick), Titelbild, Ansprechpartner, Downloads, Unterseiten, Neuigkeiten, Termine; Sichtbarkeit je Block; Bereinigung beim Speichern - Editor im Portal: Ziehen und Ablegen, Autospeichern als Entwurf, Rückgängig, Gerätevorschau, Versionen, Zeitsteuerung, Bearbeitungssperre, Freigabe anfordern - Rechte „inkl. Unterseiten“ werden im Rechte-Cache aufgelöst - Neuigkeiten: Schreiben im Portal, Freigabe-Workflow mit Benachrichtigungen - Bereich „Redaktion“, Benachrichtigungen im Portal - storage/.migrations-fingerprint nicht mehr ausliefern Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
178 lines
5.4 KiB
PHP
178 lines
5.4 KiB
PHP
<?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\Support\Str;
|
||
use Inhaus\Plugins\News\Database\Factories\NewsPostFactory;
|
||
|
||
#[Fillable([
|
||
'category_id', 'author_id', 'title', 'slug', 'teaser', 'body',
|
||
'cover_path', 'is_featured', 'published_at',
|
||
])]
|
||
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',
|
||
'published_at' => 'datetime',
|
||
'submitted_at' => 'datetime',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @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',
|
||
};
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|