39 lines
1.3 KiB
PHP
39 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Template extends Model
|
|
{
|
|
protected $guarded = ['id'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return ['is_default' => 'boolean', 'editor_state' => 'array', 'published_at' => 'datetime'];
|
|
}
|
|
|
|
public function tenant(): BelongsTo { return $this->belongsTo(Tenant::class); }
|
|
public function versions(): HasMany { return $this->hasMany(TemplateVersion::class); }
|
|
public function assignments(): HasMany { return $this->hasMany(Assignment::class); }
|
|
|
|
/** Publie le brouillon : copie vers le contenu servi + nouvelle version d'historique. */
|
|
public function publish(?User $by = null): void
|
|
{
|
|
$this->html_full = $this->draft_html_full ?? $this->html_full;
|
|
$this->html_short = $this->draft_html_short ?? $this->html_short;
|
|
$this->published_version++;
|
|
$this->published_at = now();
|
|
$this->save();
|
|
|
|
$this->versions()->create([
|
|
'version' => $this->published_version,
|
|
'html_full' => $this->html_full,
|
|
'html_short' => $this->html_short,
|
|
'published_by' => $by?->id,
|
|
]);
|
|
}
|
|
}
|