180 lines
5.9 KiB
PHP
180 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Ranges\RangeCategoryEnum;
|
|
use App\Lib\ToolboxHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class Range extends Model
|
|
{
|
|
use CastsEnums;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'slug',
|
|
'seo_title',
|
|
'seo_description',
|
|
'description',
|
|
'category',
|
|
'picture',
|
|
'picture_title',
|
|
'picture_alt',
|
|
'picture_thumb',
|
|
'picture_thumb_title',
|
|
'picture_thumb_alt',
|
|
'status',
|
|
'publish_at',
|
|
'unpublish_at',
|
|
'link1',
|
|
'link2',
|
|
];
|
|
|
|
/**
|
|
* Map attribute names to enum classes.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'category' => RangeCategoryEnum::class
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'status' => 'bool',
|
|
'publish_at' => 'datetime',
|
|
'unpublish_at' => 'datetime'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
// phpcs:enable
|
|
static::creating(function (self $model) {
|
|
static::assertTitleIsUnique($model->title);
|
|
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
|
|
$model->slug = Str::slug($model->title);
|
|
$model->description = clean($model->description);
|
|
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
|
|
$model->picture_thumb = ToolboxHelper::storeImage($model, $model->picture_thumb);
|
|
if (!$model->status) {
|
|
$model->publish_at = null;
|
|
} else {
|
|
$model->unpublish_at = null;
|
|
}
|
|
if ($model->link1) {
|
|
self::validateLinkAttribute('link1', $model->link1);
|
|
}
|
|
if ($model->link2) {
|
|
self::validateLinkAttribute('link2', $model->link2);
|
|
}
|
|
});
|
|
static::updating(function (self $model) {
|
|
static::assertTitleIsUnique($model->title, $model->id);
|
|
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
|
|
if ($model->picture != $model->getOriginal('picture')) {
|
|
$model->picture = ToolboxHelper::storeImage($model, $model->picture);
|
|
}
|
|
if ($model->picture_thumb != $model->getOriginal('picture_thumb')) {
|
|
$model->picture_thumb = ToolboxHelper::storeImage($model, $model->picture_thumb);
|
|
}
|
|
// * If status changed
|
|
if (\boolval($model->getOriginal('status')) !== $model->status) {
|
|
if ($model->status) {
|
|
$model->publish_at = null;
|
|
Session::flash('info', trans('La gamme est à présent publié'));
|
|
} else {
|
|
$model->unpublish_at = null;
|
|
Session::flash('warning', trans('La gamme est à présent dépublié'));
|
|
}
|
|
}
|
|
if ($model->link1) {
|
|
self::validateLinkAttribute('link1', $model->link1);
|
|
}
|
|
if ($model->link2) {
|
|
self::validateLinkAttribute('link2', $model->link2);
|
|
}
|
|
});
|
|
static::updated(function (self $model) {
|
|
if ($model->picture != $model->getOriginal('picture')) {
|
|
$model->picture = ToolboxHelper::removeOldImage($model, 'picture');
|
|
}
|
|
if ($model->picture_v != $model->getOriginal('picture_v')) {
|
|
$model->picture_v = ToolboxHelper::removeOldImage($model, 'picture_v');
|
|
}
|
|
});
|
|
static::deleting(function (self $model) {
|
|
ToolboxHelper::removeImage($model, 'picture');
|
|
ToolboxHelper::removeImage($model, 'picture_v');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Asserts using validation that the title is unique
|
|
*
|
|
* @param mixed $title
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If an actu title already exists.
|
|
*/
|
|
private static function assertTitleIsUnique($title, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
|
|
}
|
|
|
|
/**
|
|
* Validates an internal link
|
|
*
|
|
* @param string $attribute
|
|
* @param string $value
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If the $attribute link is invalid.
|
|
*/
|
|
public static function validateLinkAttribute(string $attribute, string $value): void
|
|
{
|
|
// Validate case internal url.
|
|
$params = collect(\explode('#', \ltrim(\rtrim($value, '#'), '#')));
|
|
if (
|
|
!\in_array(
|
|
$params->get(0),
|
|
[Breeder::class, Recipe::class]
|
|
)
|
|
) {
|
|
throw ValidationException::withMessages([
|
|
$attribute => trans('Le lien contient un modèle invalide \':model\'', [
|
|
'model' => $params->get(0)
|
|
])
|
|
]);
|
|
}
|
|
// Check if model id exists. Param 0 is class, 1 is id.
|
|
if (!\forward_static_call([$params->get(0), 'find'], $params->get(1))) {
|
|
throw ValidationException::withMessages([
|
|
$attribute => trans('Le lien n\'est plus valide.')
|
|
]);
|
|
}
|
|
}
|
|
}
|