ldc-legaulois/app/Models/Range.php

232 lines
7.9 KiB
PHP

<?php
namespace App\Models;
use App\Casts\HtmlColor;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
/**
* @property string $label Range label.
* @property string $subtitle Range subtitle.
* @property string $slug Range slug.
* @property string $description Range description.
* @property string $banner Range banner picture path.
* @property string $banner_alt Banner alt attribute.
* @property string $banner_title Banner title attribute.
* @property string $banner_ambiance Range ambiance banner picture path.
* @property string $banner_ambiance_alt Ambiance banner alt attribute.
* @property string $banner_ambiance_title Ambiance banner title attribute.
* @property string $picture Range picture.
* @property string $picture_alt Picture alt attribute.
* @property string $picture_title Picture title attribute.
* @property int $color Range color.
* @property int $order Range order.
* @property boolean $published Range publish status.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Subrange $subranges Subrange Has Many relationship.
* @property-read \App\Models\Product $products Product polymorphic relationship.
*/
class Range extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'label',
'subtitle',
'slug',
'description',
'banner',
'banner_alt',
'banner_title',
'banner_ambiance',
'banner_ambiance_alt',
'banner_ambiance_title',
'picture',
'picture_alt',
'picture_title',
'color',
'published',
'order',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'color' => HtmlColor::class,
'published' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::assertLabelIsUnique($model->label);
self::setOrder($model);
self::manageFields($model);
});
static::created(function (self $model) {
self::resetCache();
});
static::updating(function (self $model) {
self::assertLabelIsUnique($model->label, $model->getKey());
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'banner');
FileStorageHelper::removeOldFile($model, 'banner_ambiance');
FileStorageHelper::removeOldFile($model, 'picture');
self::resetCache();
});
static::deleting(function (self $model) {
/** @var \Illuminate\Database\Eloquent\Collection $subranges */
$subranges = $model->subranges;
if ($subranges->isNotEmpty()) {
session()->flash('error', trans('Des sous-gammes sont associées à cette gamme.
// Veuillez supprimer les sous-gammes concernées ou les réassocier à une autre gamme.'));
return false;
}
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'banner');
FileStorageHelper::removeFile($model, 'banner_ambiance');
FileStorageHelper::removeFile($model, 'picture');
self::resetCache();
});
}
// * METHODS
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
$model->slug = Str::slug($model->label);
$model->banner = FileStorageHelper::storeFile($model, $model->banner, true);
$model->banner_ambiance = FileStorageHelper::storeFile($model, $model->banner_ambiance, true);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
/**
* Asserts using validation that the label is unique
*
* @param string|null $label
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If a range label already exists.
*/
private static function assertLabelIsUnique(string|null $label, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id);
}
/**
* 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;
}
/**
* Reset all cache elements related to this model.
* @return void
*/
private static function resetCache()
{
Cache::forget('publishedRangesOrderedByOrder');
self::getPublishedOrdered();
Cache::forget('publishedRangesOrderedBySlug');
self::getPublishedOrderedBySlug();
Cache::forget('publishedRangesOrderedByOrderWithSubranges');
self::getPublishedOrderedWithSubranges();
}
/**
* Get all published Ranges ordered by order.
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getPublishedOrdered(): \Illuminate\Database\Eloquent\Collection
{
return Cache::rememberForever('publishedRangesOrderedByOrder', function () {
return self::query()->where('published', true)->orderBy('order')->get();
});
}
/**
* Get all published Ranges ordered by slug.
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getPublishedOrderedBySlug(): \Illuminate\Database\Eloquent\Collection
{
return Cache::rememberForever('publishedRangesOrderedBySlug', function () {
return self::query()->where('published', true)->orderBy('slug')->get();
});
}
/**
* Get all published Ranges ordered by order with subranges.
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getPublishedOrderedWithSubranges(): \Illuminate\Database\Eloquent\Collection
{
return Cache::rememberForever('publishedRangesOrderedByOrderWithSubranges', function () {
return self::query()
->where('published', true)
->with('subranges')
->orderBy('order')
->get();
});
}
// * RELATIONSHIPS
/**
* Get all of the products for the range.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Product::class);
}
/**
* Get the related subranges.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subranges(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Subrange::class);
}
}