ldc-doux/app/Models/Encart.php

64 lines
1.5 KiB
PHP
Raw Normal View History

2021-12-08 17:32:56 +01:00
<?php
namespace App\Models;
use App\Enums\Page;
2021-12-09 17:31:10 +01:00
use App\Lib\Utils;
use App\Traits\CastsEnumsHasTranslations;
2021-12-08 17:32:56 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
2021-12-08 17:32:56 +01:00
class Encart extends Model
{
use HasFactory;
use CastsEnumsHasTranslations;
2021-12-08 17:32:56 +01:00
2021-12-13 16:55:12 +01:00
public $enumCasts = [
2021-12-08 17:32:56 +01:00
'page' => Page::class
];
2021-12-09 17:31:10 +01:00
protected $fillable = [
'title',
'subtitle',
'text',
2021-12-20 14:02:37 +01:00
'picture',
'alt_text_picture',
2021-12-09 17:31:10 +01:00
];
public $translatable = [
'title',
'subtitle',
'text',
'picture',
'alt_text_picture',
];
2021-12-09 17:31:10 +01:00
protected static function boot()
{
static::creating(function (Encart $encart) {
if (is_a($encart->picture,"SplFileInfo")) {
2021-12-09 17:31:10 +01:00
$encart->picture = Utils::storeImage($encart->picture, $encart->getTable());
}
});
static::updating(function (Encart $encart) {
if (is_a($encart->picture,"SplFileInfo")) {
2021-12-09 17:31:10 +01:00
$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);
}
}
}
});
2021-12-09 17:31:10 +01:00
parent::boot();
}
2021-12-08 17:32:56 +01:00
}