ldc-nature-and-respect/app/Models/Product.php
2025-07-18 17:18:56 +02:00

283 lines
7.6 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\Support\Str;
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
use Uwil\Seo\Laravel\Traits\SeoTrait;
use Spatie\SchemaOrg\Schema;
use Spatie\Sitemap\Contracts\Sitemapable;
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
use Uwil\Seo\Laravel\Objects\SeoItem;
use Uwil\SitemapGenerator\Lib\SitemapHelper;
/**
* @property integer $range_id
* @property string $seo_title
* @property string $seo_description
* @property string $label
* @property string $slug
* @property string $description
* @property string|null $marinade
* @property string $picture
* @property array $certifications
* @property array $nutritionnal_values
* @property array $list
* @property boolean $status
* @property integer $order
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class Product extends Model implements SeoInterface, SchemaOrgInterface, Sitemapable
{
use HasFactory;
use HasTranslations;
use SeoTrait;
use SchemaOrgTrait;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'range_id',
'seo_title',
'seo_description',
'label',
'slug',
'description',
'marinade',
'picture',
'picture_alt',
'picture_title',
'picture_arrow',
'picture_arrow_alt',
'picture_arrow_title',
'certifications',
'nutritionnal_values',
'list',
'status',
'order',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'certifications' => 'array',
'nutritionnal_values' => 'array',
'list' => 'array',
];
/**
* The translatable columns.
*
* @var array
*/
public $translatable = [
'seo_title',
'seo_description',
'label',
'slug',
'description',
'marinade',
'picture',
'picture_alt',
'picture_title',
'picture_arrow',
'picture_arrow_alt',
'picture_arrow_title',
'nutritionnal_values',
'list',
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::assertFieldIsUnique('label', $model->label);
self::manageFields($model);
self::setOrder($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS
/**
* Undocumented function
*
* @param self $model
* @return void
*/
private static function manageFields(self $model)
{
self::setSlug($model);
if (is_array($model->list)) {
foreach ($model->list as $itemList) {
ToolboxHelper::assertNoInternalBoUrls('list', $itemList['value'] ?? '');
}
}
if (is_array($model->nutritionnal_values)) {
foreach ($model->nutritionnal_values as $itemNutritionnalValues) {
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $itemNutritionnalValues['label'] ?? '');
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $itemNutritionnalValues['value'] ?? '');
}
}
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
/**
* Set the slug.
*
* @param self $model
*
* @return void
*/
private static function setSlug(self $model)
{
$model->slug = Str::slug($model->label);
}
/**
* 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)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $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;
}
/**
* Create a SchemaOrg.
*
* @return \Spatie\SchemaOrg\Product
*/
public function toSchemaOrg(): \Spatie\SchemaOrg\Product
{
$model = $this->load(['range']);
// Main.
$schema = Schema::Product()
->name($model->seo_title)
->description($model->seo_description)
->url(route('product.show', $model->slug))
->keywords('produit')
->releaseDate($model->created_at)
->image([
Schema::imageObject()
->url(asset($model->picture))
->headline($model->picture_title),
])
->brand($model->getBrandSchema());
// Range associated.
if ($model->range->status) {
$schema->subjectOf($model->range->toSchemaOrg()->toArray());
}
return $schema;
}
/**
* Get the SEO object.
*
* @return \Uwil\Seo\Laravel\Objects\SeoItem
*/
public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem
{
return new SeoItem(
\trans($this->getTable()),
$this->seo_title,
$this->seo_description,
$this->status,
\route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this),
\route(Str::singular($this->getTable()) . '.show', $this)
);
}
/**
* To sitemap tag .
*
* @return \Spatie\Sitemap\Tags\Url|string|array
*/
public function toSitemapTag(): \Spatie\Sitemap\Tags\Url|string|array
{
return SitemapHelper::generateUrl('product.show', ['publishedProduct' => $this], $this);
}
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function recipes()
{
return $this->belongsToMany('App\Models\Recipe', 'recipes_products');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function range()
{
return $this->belongsTo('App\Models\Range');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function homes1()
{
return $this->hasMany('App\Models\Home', 'part2_product1_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function homes2()
{
return $this->hasMany('App\Models\Home', 'part2_product2_id');
}
}