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;
|
2022-01-22 18:32:04 +01:00
|
|
|
use App\Traits\CastsEnumsHasTranslations;
|
2021-12-08 17:32:56 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-01-07 17:29:55 +01:00
|
|
|
use Illuminate\Support\Facades\File;
|
2021-12-08 17:32:56 +01:00
|
|
|
|
|
|
|
|
class Encart extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2022-01-22 18:32:04 +01:00
|
|
|
use CastsEnumsHasTranslations;
|
2021-12-08 17:32:56 +01:00
|
|
|
|
2022-08-29 17:23:10 +02:00
|
|
|
/**
|
|
|
|
|
* Map attribute names to enum classes.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
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
|
|
|
|
2022-08-29 17:23:10 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2026-10-27 09:12:51 +01:00
|
|
|
#[\Override]
|
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
|
|
|
];
|
|
|
|
|
|
2022-08-29 17:23:10 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are translatable.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2022-01-22 18:32:04 +01:00
|
|
|
public $translatable = [
|
|
|
|
|
'title',
|
|
|
|
|
'subtitle',
|
|
|
|
|
'text',
|
|
|
|
|
'picture',
|
|
|
|
|
'alt_text_picture',
|
|
|
|
|
];
|
|
|
|
|
|
2022-08-29 17:23:10 +02:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-12-09 17:31:10 +01:00
|
|
|
protected static function boot()
|
|
|
|
|
{
|
2026-10-27 09:12:51 +01:00
|
|
|
static::creating(function (Encart $encart): void {
|
2022-07-27 14:33:56 +02:00
|
|
|
if (is_a($encart->picture, "SplFileInfo")) {
|
2021-12-09 17:31:10 +01:00
|
|
|
$encart->picture = Utils::storeImage($encart->picture, $encart->getTable());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-27 09:12:51 +01:00
|
|
|
static::updating(function (Encart $encart): void {
|
2022-07-27 14:33:56 +02:00
|
|
|
if (is_a($encart->picture, "SplFileInfo")) {
|
2021-12-09 17:31:10 +01:00
|
|
|
$encart->picture = Utils::storeImage($encart->picture, $encart->getTable());
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-01-07 17:29:55 +01:00
|
|
|
|
2026-10-27 09:12:51 +01:00
|
|
|
static::deleted(function (Encart $encart): void {
|
2022-01-07 17:29:55 +01:00
|
|
|
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
|
|
|
}
|