ldc-doux/app/Models/Picto.php

81 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2021-12-13 16:55:12 +01:00
<?php
namespace App\Models;
use App\Enums\Page;
2021-12-15 10:09:39 +01:00
use App\Lib\Utils;
2022-01-23 12:31:21 +01:00
use App\Traits\CastsEnumsHasTranslations;
2021-12-13 16:55:12 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
2021-12-13 16:55:12 +01:00
class Picto extends Model
{
use HasFactory;
2022-01-23 12:31:21 +01:00
use CastsEnumsHasTranslations;
2021-12-13 16:55:12 +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 = [
'page' => Page::class
];
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-13 16:55:12 +01:00
protected $fillable = [
'picture',
2021-12-20 14:02:37 +01:00
'alt_text_picture',
2021-12-13 16:55:12 +01:00
'text',
];
2022-08-29 17:23:10 +02:00
/**
* The attributes that are translatable.
*
* @var array
*/
2022-01-23 12:31:21 +01:00
public $translatable = [
'picture',
'alt_text_picture',
'text',
];
2022-08-29 17:23:10 +02:00
/**
* Perform any actions required after the model boots.
*
* @return void
*/
2021-12-15 10:09:39 +01:00
protected static function boot()
{
2026-10-27 09:12:51 +01:00
static::creating(function (Picto $picto): void {
2022-07-27 14:33:56 +02:00
if (is_a($picto->picture, "SplFileInfo")) {
2021-12-15 10:09:39 +01:00
$picto->picture = Utils::storeImage($picto->picture, $picto->getTable());
}
});
2021-12-13 16:55:12 +01:00
2026-10-27 09:12:51 +01:00
static::updating(function (Picto $picto): void {
2022-07-27 14:33:56 +02:00
if (is_a($picto->picture, "SplFileInfo")) {
2021-12-15 10:09:39 +01:00
$picto->picture = Utils::storeImage($picto->picture, $picto->getTable());
}
});
2026-10-27 09:12:51 +01:00
static::deleted(function (Picto $picto): void {
if ($picto->picture) {
$urlPicture = public_path() . $picto->picture;
if ($urlPicture) {
if (File::exists($urlPicture)) {
File::delete($urlPicture);
}
}
}
});
2021-12-15 10:09:39 +01:00
parent::boot();
}
2021-12-13 16:55:12 +01:00
}