ldc-tregalette/app/Models/HistoricalMilestone.php
2023-12-22 15:09:55 +01:00

112 lines
3.9 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property \Carbon\Carbon $year Année de l'évènement.
* @property string $text Descriptif du jalon historique. Min 10 caractères, max 15 mots.
* @property string $picture1 Path de l'image 1 (431px * 300px).
* @property string $picture1_alt Attribut alt de l'image 1.
* @property string $picture1_title Attribut title de l'image 1.
* @property string|null $picture2 Path de l'image 2 (431px * 300px).
* @property string|null $picture2_alt Attribut alt de l'image 2.
* @property string|null $picture2_title Attribut title de l'image 2.
* @property string|null $picture3 Path de l'image 3 (431px * 300px).
* @property string|null $picture3_alt Attribut alt de l'image 3
* @property string|null $picture3_title Attribut title de l'image 3.
* @property boolean $button_status Le bouton est-il visible ?.
* @property string $button_label Libellé du bouton, max 35 caractères.
* Obligatoire si button_status est true.
* @property string $button_url Url du bouton, obligatoire si button_status est true.
* @property boolean $button_new_tab Le lien du bouton doit-il s'ouvrir dans un nouvel onglet ?
* @property boolean $published Le jalon historique est-il visible ?
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class HistoricalMilestone extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'year',
'text',
'picture1',
'picture1_alt',
'picture1_title',
'picture2',
'picture2_alt',
'picture2_title',
'picture3',
'picture3_alt',
'picture3_title',
'button_status',
'button_label',
'button_url',
'button_new_tab',
'published',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'year' => 'integer',
'button_status' => 'boolean',
'button_new_tab' => 'boolean',
'published' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture1');
FileStorageHelper::removeOldFile($model, 'picture2');
FileStorageHelper::removeOldFile($model, 'picture3');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture1');
FileStorageHelper::removeFile($model, 'picture2');
FileStorageHelper::removeFile($model, 'picture3');
});
}
// * METHODS
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
$model->picture1 = FileStorageHelper::storeFile($model, $model->picture1, true);
$model->picture2 = FileStorageHelper::storeFile($model, $model->picture2, true);
$model->picture3 = FileStorageHelper::storeFile($model, $model->picture3, true);
}
}