Files
fewo-stommer/app/Models/Notice.php
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

53 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use App\Enums\NoticeTone;
use App\Models\Concerns\BelongsToTenant;
use Database\Factories\NoticeFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* A banner shown on the website, optionally limited to a date range.
*/
#[Fillable(['text', 'tone', 'link_url', 'link_label', 'starts_on', 'ends_on', 'is_active'])]
class Notice extends Model
{
/** @use HasFactory<NoticeFactory> */
use BelongsToTenant, HasFactory;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'tone' => NoticeTone::class,
'starts_on' => 'immutable_date',
'ends_on' => 'immutable_date',
'is_active' => 'boolean',
];
}
/**
* Active notices whose date range includes today.
*
* @param Builder<Notice> $query
* @return Builder<Notice>
*/
#[Scope]
protected function visible(Builder $query): Builder
{
return $query
->where('is_active', true)
->where(fn (Builder $query) => $query->whereNull('starts_on')->orWhereDate('starts_on', '<=', today()))
->where(fn (Builder $query) => $query->whereNull('ends_on')->orWhereDate('ends_on', '>=', today()));
}
}