ldc-doux/app/Models/Encart.php
2022-08-29 17:23:10 +02:00

83 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Page;
use App\Lib\Utils;
use App\Traits\CastsEnumsHasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
class Encart extends Model
{
use HasFactory;
use CastsEnumsHasTranslations;
/**
* Map attribute names to enum classes.
*
* @var array
*/
public $enumCasts = [
'page' => Page::class
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'subtitle',
'text',
'picture',
'alt_text_picture',
];
/**
* The attributes that are translatable.
*
* @var array
*/
public $translatable = [
'title',
'subtitle',
'text',
'picture',
'alt_text_picture',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function boot()
{
static::creating(function (Encart $encart) {
if (is_a($encart->picture, "SplFileInfo")) {
$encart->picture = Utils::storeImage($encart->picture, $encart->getTable());
}
});
static::updating(function (Encart $encart) {
if (is_a($encart->picture, "SplFileInfo")) {
$encart->picture = Utils::storeImage($encart->picture, $encart->getTable());
}
});
static::deleted(function (Encart $encart) {
if ($encart->picture) {
$urlPicture = public_path() . $encart->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
parent::boot();
}
}