Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m8s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m12s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m12s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m5s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m6s
CI / Build-Artefakte aktuell (pull_request) Failing after 43s
- Neuigkeiten mit Kategorien, Titelbild (WebP-Varianten), Lesezeit, geplanter Veröffentlichung und Hervorhebung; Text wird beim Speichern bereinigt - Termine mit Ort, Online-Link, ganztägigen Terminen und .ics-Export - Startseite nach Intranet-Vorbild: Slider, Neuigkeiten, Termine, Aktuelles aus den Bereichen; mobil in sinnvoller Reihenfolge - Admin: Beiträge, Kategorien und Termine - Bildverarbeitung für Shared Hosting: nur eine Vollbild-Kopie im Speicher, Speicherlimit bei Bedarf auf 256 MB angehoben (wie WordPress) - Demo-Inhalte für die Entwicklung Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
111 lines
3.2 KiB
PHP
111 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Support\Images;
|
|
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;
|
|
|
|
#[Fillable([
|
|
'category_id', 'author_id', 'title', 'slug', 'teaser', 'body',
|
|
'cover_path', 'is_featured', 'published_at',
|
|
])]
|
|
class NewsPost extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function (NewsPost $post): void {
|
|
if (blank($post->slug)) {
|
|
$post->slug = static::uniqueSlug($post->title, $post->getKey());
|
|
}
|
|
|
|
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',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<NewsCategory, $this>
|
|
*/
|
|
public function category(): BelongsTo
|
|
{
|
|
return $this->belongsTo(NewsCategory::class);
|
|
}
|
|
|
|
/**
|
|
* @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->whereNotNull('published_at')->where('published_at', '<=', now());
|
|
}
|
|
|
|
public function isPublished(): bool
|
|
{
|
|
return $this->published_at !== null && $this->published_at->isPast();
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|