193 lines
7 KiB
PHP
193 lines
7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
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;
|
|
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
|
use Uwil\Seo\Laravel\Objects\SeoItem;
|
|
use Uwil\Seo\Laravel\Traits\SeoTrait;
|
|
|
|
/**
|
|
* @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 $label Label du produit.
|
|
* @property string $slug Slug à partir du label du produit.
|
|
* @property string $picture Path de l'image du produit (503px * 503px).
|
|
* @property string $picture_alt Attribut alt de l'image du produit.
|
|
* @property string $picture_title Attribut title de l'image du produit.
|
|
* @property string $picture_thumbnail Path de la vignette du produit (310px * 310px).
|
|
* @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 $description WYSIWYG - Description du produit.
|
|
* @property \App\Enums\Products\NutriscoreEnum $nutriscore Nutriscore du produit
|
|
* (enum \App\Enums\Products\NutriscoreEnum).
|
|
* @property string $nutritionnal_values JSON avec les valeurs nutritionnelles
|
|
* pour le produit et pour 100g.
|
|
* @property string $advice WYSIWYG - Texte conseils .
|
|
* @property string $ingredients WYSIWYG - Texte ingrédients.
|
|
* @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\Picto $pictos Pictos Many-to-many relationship.
|
|
* @property-read \App\Models\Range $range Range Belongs to relationship.
|
|
*/
|
|
class Product extends Model implements SeoInterface
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use SeoTrait;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'label',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'picture_thumbnail',
|
|
'picture_thumbnail_alt',
|
|
'picture_thumbnail_title',
|
|
'description',
|
|
'nutriscore',
|
|
'nutritionnal_values',
|
|
'advice',
|
|
'ingredients',
|
|
'published',
|
|
'order',
|
|
'range_id',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'published' => 'boolean',
|
|
'nutriscore' => \App\Enums\Products\NutriscoreEnum::class,
|
|
'nutritionnal_values' => 'array'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
self::setOrder($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldFile($model, 'picture_thumbnail');
|
|
});
|
|
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeFile($model, 'picture_thumbnail');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
// * ASSERTIONS
|
|
self::assertFieldIsUnique('label', $model->label, $model->id);
|
|
self::assertFieldIsUnique('slug', Str::slug($model->label), $model->id);
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->label);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
|
|
}
|
|
|
|
/**
|
|
* 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 pictos that belong to the product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function pictos(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Picto::class);
|
|
}
|
|
|
|
// * SEO TRAIT OVERLOAD
|
|
/**
|
|
* Transform to seoModel
|
|
*
|
|
* @return \App\Objects\SeoItem
|
|
*/
|
|
public function toSeoItem(): SeoItem
|
|
{
|
|
return new SeoItem(
|
|
\trans('models.classes.' . Str::singular($this->getTable())),
|
|
$this->seo_title,
|
|
$this->seo_description,
|
|
$this->published ?? false,
|
|
\route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this),
|
|
$this->published ? \route('fo.' . $this->getTable() . '.show', $this) : ''
|
|
);
|
|
}
|
|
}
|