137 lines
4.5 KiB
PHP
137 lines
4.5 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;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
/**
|
|
* @property \Carbon\Carbon $year Historical milestone year.
|
|
* @property string $title Historical milestone title.
|
|
* @property string $description Historical milestone description.
|
|
* @property string|null $picture Historical milestone picture.
|
|
* @property string|null $picture_alt Picture alt attribute.
|
|
* @property string|null $picture_title Picture title attribute.
|
|
* @property string|null $video_id Historical milestone video ID.
|
|
* @property string|null $picto Historical milestone picto.
|
|
* @property string|null $picto_alt picto alt attribute.
|
|
* @property string|null $picto_title picto title attribute.
|
|
* @property boolean $published Historical milestone publish status.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\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',
|
|
'title',
|
|
'description',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'video_id',
|
|
'picto',
|
|
'picto_alt',
|
|
'picto_title',
|
|
'picto_placement',
|
|
'order',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'year' => 'integer',
|
|
'published' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $historicalMilestone) {
|
|
self::setImage($historicalMilestone, 'picture');
|
|
self::setImage($historicalMilestone, 'picto');
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $historicalMilestone) {
|
|
self::setImage($historicalMilestone, 'picture');
|
|
self::setImage($historicalMilestone, 'picto');
|
|
});
|
|
static::updated(function (self $historicalMilestone) {
|
|
FileStorageHelper::removeOldFile($historicalMilestone, 'picture');
|
|
FileStorageHelper::removeOldFile($historicalMilestone, 'picto');
|
|
self::resetCache();
|
|
});
|
|
static::deleted(function (self $historicalMilestone) {
|
|
FileStorageHelper::removeFile($historicalMilestone, 'picture');
|
|
FileStorageHelper::removeFile($historicalMilestone, 'picto');
|
|
self::resetCache();
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Set model's published date.
|
|
*
|
|
* @param self $historicalMilestone
|
|
* @param string $field
|
|
* @return void
|
|
*/
|
|
private static function setImage(self $historicalMilestone, string $field): void
|
|
{
|
|
if (is_null($historicalMilestone->{$field})) {
|
|
$historicalMilestone->{$field . "_alt"} = null;
|
|
$historicalMilestone->{$field . "_title"} = null;
|
|
} else {
|
|
$historicalMilestone->{$field} = FileStorageHelper::storeFile(
|
|
$historicalMilestone,
|
|
$historicalMilestone->{$field},
|
|
true
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('publishedHistoricalMilestonesOrdered');
|
|
self::getPublishedOrdered();
|
|
}
|
|
|
|
/**
|
|
* Get all published HistoricalMilestones ordered.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getPublishedOrdered(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::rememberForever('publishedHistoricalMilestonesOrdered', function () {
|
|
return self::query()->where('published', true)->orderBy('order')->get();
|
|
});
|
|
}
|
|
}
|