166 lines
5 KiB
PHP
166 lines
5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\HasTranslations;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property string $name Name of the range.
|
|
* @property string $slug Slug of the name for the range.
|
|
* @property string $picture Picture path for the range.
|
|
* @property string $color HTML Color string.
|
|
* @property integer $width Width or the range to display
|
|
* (1 or 2 columns, 2 is full width).
|
|
* @property integer $order Order for the range to display.
|
|
* @property boolean $shelf Les enfant sont rendus sur un étagere.
|
|
* @property boolean $status Visible ? (présent dans l'export).
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\Brand $brand Brand Belongs-to.
|
|
* @property-read \App\Models\Font $font Font Belongs-to.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Product> $products Products Has-many.
|
|
*/
|
|
class Range extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'picture',
|
|
'brand_id',
|
|
'font_id',
|
|
'color',
|
|
'width',
|
|
'order',
|
|
'shelf',
|
|
'status',
|
|
];
|
|
|
|
/**
|
|
* Translatable fields array
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
public $translatable = [
|
|
'name',
|
|
'slug',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'shelf' => 'bool',
|
|
'status' => 'bool',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertNameIsUnique($model->name);
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->name);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
self::setOrder($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
self::assertNameIsUnique($model->name, $model->id);
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->name);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldFile($model, 'font');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeFile($model, 'font');
|
|
});
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get the brand that owns the Range
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function brand(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Brand::class);
|
|
}
|
|
|
|
/**
|
|
* Get the font that owns the Range
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function font(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Font::class);
|
|
}
|
|
|
|
/**
|
|
* Get all of the products for the Brand
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function products(): HasMany
|
|
{
|
|
return $this->hasMany(Product::class);
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Asserts using validation that the name is unique
|
|
*
|
|
* @param string|null $name
|
|
* @param integer|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If name already exists.
|
|
*/
|
|
private static function assertNameIsUnique(string|null $name, ?int $id = null)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $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;
|
|
}
|
|
}
|