ldc-legaulois/app/Models/KeyNumber.php
2024-03-29 14:19:59 +01:00

75 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property string $picto Picto path.
* @property string $picto_title Picto title attribute.
* @property string $picto_alt Picto alt attribute.
* @property string $text Legend of picto.
* @property string $source The origin of the text.
* @property integer $order Change order request.
* @property boolean $published Picto publish status.
* @property-read \Illuminate\Support\Carbon $created_at Created date.
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
*/
class KeyNumber extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'text',
'picto',
'picto_alt',
'picto_title',
'source',
'order',
'published',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $keyNumber) {
self::setOrder($keyNumber);
$keyNumber->picto = FileStorageHelper::storeFile($keyNumber, $keyNumber->picto, true);
});
static::updating(function (self $keyNumber) {
$keyNumber->picto = FileStorageHelper::storeFile($keyNumber, $keyNumber->picto, true);
});
static::updated(function (self $keyNumber) {
FileStorageHelper::removeOldFile($keyNumber, 'picto');
});
static::deleted(function (self $keyNumber) {
FileStorageHelper::removeFile($keyNumber, 'picto');
});
}
// * METHODS
/**
* Set order after the last element of the list.
*
* @param self $model
* @return void
*/
private static function setOrder(self $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
}