ldc-legaulois/app/Models/Picto.php
2026-09-25 16:25:31 +02:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Picto extends Model
{
public const MIN_PICTOS = 4;
public const MAX_PICTOS = 6;
public const MIN_WIDTH = 60;
public const MIN_HEIGHT = 60;
protected $fillable = [
'title',
'description',
'picture_alt',
'picture_title',
'picture',
'picture_width',
'picture_height',
'url',
'section_id',
'order',
];
public function section(): BelongsTo
{
return $this->belongsTo(SectionPage::class, 'section_id');
}
protected static function booted(): void
{
static::creating(function (self $picto) {
$picto->picture = FileStorageHelper::storeFile($picto, $picto->picture, true);
$maxOrder = self::where('section_id', $picto->section_id)->max('order');
$picto->order = $maxOrder !== null ? $maxOrder + 1 : 1;
});
static::updating(function (self $picto) {
$picto->picture = FileStorageHelper::storeFile($picto, $picto->picture, true);
if ($picto->isDirty('section_id')) {
$maxOrder = self::where('section_id', $picto->section_id)->max('order');
$picto->order = $maxOrder !== null ? $maxOrder + 1 : 1;
}
});
static::deleting(function (self $picto) {
FileStorageHelper::storeFile($picto, $picto->picture, true);
});
static::deleted(function (self $picto) {
self::where('section_id', $picto->section_id)
->where('order', '>', $picto->order)
->decrement('order');
});
}
}