Integration Baniere dans le backend
This commit is contained in:
@@ -4,17 +4,34 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
/** Campagne de communication : une bannière affichée sous la signature pendant une période, pour tous ou pour des groupes AD. */
|
||||
class Campaign extends Model
|
||||
{
|
||||
public const DEFAULT_WIDTH = 600;
|
||||
|
||||
/** État => libellé affiché dans le back-office. */
|
||||
public const STATUSES = [
|
||||
'running' => 'En cours',
|
||||
'scheduled' => 'Programmée',
|
||||
'inactive' => 'Inactive',
|
||||
'ended' => 'Terminée',
|
||||
];
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['starts_at' => 'datetime', 'ends_at' => 'datetime', 'active' => 'boolean'];
|
||||
return [
|
||||
'starts_at' => 'datetime', 'ends_at' => 'datetime', 'active' => 'boolean',
|
||||
'priority' => 'integer', 'width' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); }
|
||||
|
||||
public function groups(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(DirectoryGroup::class, 'campaign_group');
|
||||
@@ -24,4 +41,22 @@ class Campaign extends Model
|
||||
{
|
||||
return $q->where('active', true)->where('starts_at', '<=', now())->where('ends_at', '>=', now());
|
||||
}
|
||||
|
||||
/** running | scheduled | inactive | ended — doit rester cohérent avec scopeRunning(). */
|
||||
public function status(): string
|
||||
{
|
||||
$now = now()->startOfSecond(); // même précision que la comparaison SQL de scopeRunning()
|
||||
|
||||
return match (true) {
|
||||
$this->ends_at->lt($now) => 'ended',
|
||||
! $this->active => 'inactive',
|
||||
$this->starts_at->gt($now) => 'scheduled',
|
||||
default => 'running',
|
||||
};
|
||||
}
|
||||
|
||||
public function displayWidth(): int
|
||||
{
|
||||
return $this->width ?: self::DEFAULT_WIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user