ldc-loue/app/Models/Engagement.php

97 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2022-09-19 09:10:04 +00:00
<?php
namespace App\Models;
use App\Enums\Engagements\EngagementType;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2022-09-23 17:19:00 +02:00
use App\Traits\Models\MiniCrudModel;
2022-09-19 09:10:04 +00:00
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 = [
2022-09-23 17:19:00 +02:00
'data' => 'array',
'banner_dimensions' => 'array'
2022-09-19 09:10:04 +00:00
];
/**
* 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',
2022-09-23 17:19:00 +02:00
'title_simple',
2022-09-19 09:10:04 +00:00
'slug',
'seo_title',
'seo_description',
'banner',
'banner_title',
'banner_alt',
2022-09-23 17:19:00 +02:00
'banner_dimensions',
2022-09-19 09:10:04 +00:00
'type',
'data'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
2022-09-23 17:19:00 +02:00
$model->data = clean($model->data);
$model->banner = FileStorageHelper::storeFile($model, $model->banner);
2022-09-19 09:10:04 +00:00
});
static::updating(function (self $model) {
2022-09-23 17:19:00 +02:00
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');
}
2022-09-19 09:10:04 +00:00
});
static::deleted(function (self $model) {
2022-09-23 17:19:00 +02:00
FileStorageHelper::removeFile($model, 'banner');
ToolboxHelper::removeImagesOnCkEditor($model, 'title');
2022-09-19 09:10:04 +00:00
});
}
}