ldc-loue/app/Models/Engagement.php

96 lines
2.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Engagements\EngagementType;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\MiniCrudModel;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Engagement extends Model
{
use CastsEnums;
use MiniCrudModel;
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'type';
/**
* Indicates if the IDs are auto-incrementing.
*
* @var boolean
*/
public $incrementing = false;
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'data' => 'array',
'banner_dimensions' => 'array'
];
/**
* The attributes that should be cast to Enums.
*
* @var array
*/
protected $enumCasts = [
'type' => EngagementType::class
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'title_simple',
'slug',
'seo_title',
'seo_description',
'banner',
'banner_title',
'banner_alt',
'banner_dimensions',
'type',
'data'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
$model->data = clean($model->data);
$model->banner = FileStorageHelper::storeFile($model, $model->banner);
});
static::updating(function (self $model) {
if ($model->banner != $model->getOriginal('banner')) {
$model->banner = FileStorageHelper::storeFile($model, $model->banner);
}
});
static::updated(function (self $model) {
ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'title');
if ($model->banner != $model->getOriginal('banner')) {
$model->banner = FileStorageHelper::removeOldFile($model, 'banner');
}
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'banner');
ToolboxHelper::removeImagesOnCkEditor($model, 'title');
});
}
}