ldc-legaulois/app/Models/Picto.php
2026-06-04 14:20:18 +02:00

62 lines
1.7 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
{
const MIN_PICTOS = 4;
const MAX_PICTOS = 6;
const MIN_WIDTH = 60;
const MIN_HEIGHT = 60;
protected $fillable = [
'title',
'description',
'picture_alt',
'picture_title',
'picture',
'picture_width',
'picture_height',
'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');
});
}
}