ldc-volena/app/Models/Strip.php
2026-04-24 15:06:55 +02:00

110 lines
3.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Strips\PictoEnum;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use App\Traits\Models\PlanificatedStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property App\Enums\Strips\PictoEnum|null $picto Picto.
* @property string|null $picture Strip picture path.
* @property string|null $picture_alt Strip picture alt attribute.
* @property string|null $picture_title Strip picture title attribute.
* @property string $text WYSIWYG restrained (blod and underline only). Text.
* @property boolean $published Publication status.
* @property \Carbon\Carbon|null $publish_at Publication date.
* @property \Carbon\Carbon|null $unpublish_at Unpublication status update date.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
* @property-read \App\Models\News $news News Belongs to relationship.
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
class Strip extends Model
{
use ActivityLog;
/** @use HasFactory<\Database\Factories\StripFactory> */
use HasFactory;
use PlanificatedStatus;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'picto',
'picture',
'picture_alt',
'picture_title',
'text',
'published',
'publish_at',
'unpublish_at',
'news_id',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'picto' => PictoEnum::class,
'published' => 'bool',
'publish_at' => 'datetime',
'unpublish_at' => 'datetime',
];
/**
* 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, 'picture');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS.
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
self::makeStatusSense($model);
if ($model->picture) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
}
// * RELATIONS.
/**
* Get the news that owns the strip.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\News,$this>
*/
public function news(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(News::class);
}
}