192 lines
7.1 KiB
PHP
192 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Products\CookingModeEnum;
|
|
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\Str;
|
|
|
|
/**
|
|
* @phpcs:disable Generic.Files.LineLength.TooLong
|
|
*
|
|
* @property string $seo_title Utilisé pour la balise title de la page.
|
|
* @property string $seo_description Utilisé pour la balise meta description de la page.
|
|
* @property string $title Balise h1 de la page.
|
|
* @property string $slug Slug à partir de title.
|
|
* @property string $subtitle Sous-titre du produit.
|
|
* @property string $picture_thumbnail Path de la vignette du produit (332px * 252px).
|
|
* @property string $picture_thumbnail_alt Attribut alt de la vignette du produit.
|
|
* @property string $picture_thumbnail_title Attribut title de la vignette du produit.
|
|
* @property string $picture_ambiance Path du visuel d'ambiance du produit (774px * 515px).
|
|
* @property string $picture_ambiance_alt Attribut alt du visuel d'ambiance.
|
|
* @property string $picture_ambiance_title Attribut title du visuel d'ambiance.
|
|
* @property string $picture_packshot Path de l'image du packshot (327px * 252px).
|
|
* @property string $picture_packshot_alt Attribut alt de l'image du packshot.
|
|
* @property string $picture_packshot_title Attribut title de l'image du packshot.
|
|
* @property string $description WYSIWYG - Description du produit.
|
|
* @property \App\Enums\Products\CookingModeEnum $cooking_mode Mode de cuisson \App\Enums\Products\CookingModeEnum.
|
|
* @property string $cooking_advice WYSIWYG - Conseil de réchauffage.
|
|
* @property string $ingredients WYSIWYG - Ingrédients qui composent le produit.
|
|
* @property string $nutritionnal_values JSON des valeurs nutritionnelles pour 100g.
|
|
* @property boolean $published Le produit est-il visible ?
|
|
* @property integer $order Numéro permettant d'ordonner les produits.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\Commitment $commitments Commitments Many-to-many relationship.
|
|
* @property-read \App\Models\Range $range Range Belongs to relationship.
|
|
* @phpcs:enable
|
|
*/
|
|
class Product extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'title',
|
|
'slug',
|
|
'subtitle',
|
|
'picture_thumbnail',
|
|
'picture_thumbnail_alt',
|
|
'picture_thumbnail_title',
|
|
'picture_ambiance',
|
|
'picture_ambiance_alt',
|
|
'picture_ambiance_title',
|
|
'picture_packshot',
|
|
'picture_packshot_alt',
|
|
'picture_packshot_title',
|
|
'description',
|
|
'cooking_mode',
|
|
'cooking_advice',
|
|
'ingredients',
|
|
'nutritionnal_values',
|
|
'published',
|
|
'order',
|
|
'range_id'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'cooking_mode' => CookingModeEnum::class,
|
|
'published' => 'bool',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::assertFieldIsUnique('title', $model->title);
|
|
self::setOrder($model);
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture_thumbnail');
|
|
FileStorageHelper::removeOldFile($model, 'picture_ambiance');
|
|
FileStorageHelper::removeOldFile($model, 'picture_packshot');
|
|
});
|
|
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture_thumbnail');
|
|
FileStorageHelper::removeFile($model, 'picture_ambiance');
|
|
FileStorageHelper::removeFile($model, 'picture_packshot');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
self::setSlug($model, $model->title);
|
|
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
|
|
$model->picture_ambiance = FileStorageHelper::storeFile($model, $model->picture_ambiance, true);
|
|
$model->picture_packshot = FileStorageHelper::storeFile($model, $model->picture_packshot, true);
|
|
}
|
|
|
|
/**
|
|
* Set the slug.
|
|
*
|
|
* @param self $model
|
|
* @param string $original
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function setSlug(self $model, string $original): void
|
|
{
|
|
$model->slug = Str::slug($original);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the field is unique.
|
|
*
|
|
* @param string $nameField
|
|
* @param string $valueField
|
|
* @param integer|null $id
|
|
* @return void
|
|
*/
|
|
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* Get the range that owns the product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function range(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Range::class);
|
|
}
|
|
|
|
/**
|
|
* The commitments that belong to the product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function commitments(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Commitment::class);
|
|
}
|
|
}
|