138 lines
4.5 KiB
PHP
138 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Range extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'status' => 'bool',
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'name', 'description',
|
|
'picture', 'status',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
$flashStatus = function (self $model, bool $old = false) {
|
|
if ($old and $model->getOriginal('status') == $model->status) {
|
|
return;
|
|
}
|
|
if ($model->status) {
|
|
session()->flash('info', trans(':model est maintenant publié', [
|
|
'model' => trans('models.classes.ranges')
|
|
]));
|
|
} else {
|
|
session()->flash('warning', trans(':model est maintenant dépublié', [
|
|
'model' => trans('models.classes.ranges')
|
|
]));
|
|
}
|
|
};
|
|
// * EVENT CLOSURES.
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
static::assertNameIsUnique($model->name);
|
|
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->name);
|
|
OldSlugable::removeSameSlugs($model, $model->slug);
|
|
$model->description = clean($model->description);
|
|
$model->description = FileStorageHelper::storeModelFilesFromHtml($model, $model->description);
|
|
if ($model->picture) {
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
});
|
|
static::created(function (self $model) use ($flashStatus) {
|
|
$flashStatus($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
static::assertNameIsUnique($model->name, $model->id);
|
|
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->name);
|
|
if ($model->slug != $model->getOriginal('slug')) {
|
|
OldSlugable::removeSameSlugs($model, $model->getOriginal('slug'));
|
|
}
|
|
OldSlugable::insertSlug($model);
|
|
$model->description = clean($model->description);
|
|
$model->description = FileStorageHelper::storeModelFilesFromHtml($model, $model->description);
|
|
if ($model->picture) {
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
});
|
|
static::updated(function (self $model) use ($flashStatus) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldModelFilesFromHtml($model->description, $model->getOriginal('description'));
|
|
$flashStatus($model, true);
|
|
});
|
|
static::deleted(function (self $model) {
|
|
$model->oldSlugs()->deleteOrFail();
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeModelFilesOfHtml($model->description);
|
|
});
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* hasMany customer relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function products()
|
|
{
|
|
return $this->hasMany('App\Models\Product', 'range_id');
|
|
}
|
|
|
|
/**
|
|
* MorphMany old_slugable relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
*/
|
|
public function oldSlugs()
|
|
{
|
|
return $this->morphMany(OldSlugable::class, 'old_slugable');
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Asserts using validation that the name is unique
|
|
*
|
|
* @param mixed $name
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an actu name already exists.
|
|
*/
|
|
private static function assertNameIsUnique($name, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id);
|
|
}
|
|
}
|