506 lines
16 KiB
PHP
506 lines
16 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\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Scout\Searchable;
|
|
use Spatie\SchemaOrg\Schema;
|
|
use Spatie\Sitemap\Contracts\Sitemapable;
|
|
use Uwil\SchemaOrg\Laravel\Interfaces\SchemaOrgInterface;
|
|
use Uwil\SchemaOrg\Laravel\Traits\SchemaOrgTrait;
|
|
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
|
|
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
|
|
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
|
use Uwil\Seo\Laravel\Objects\SeoItem;
|
|
use Uwil\Seo\Laravel\Traits\SeoTrait;
|
|
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
|
|
|
/**
|
|
* @property string $seo_title Product seo title.
|
|
* @property string $seo_description Product seo description.
|
|
* @property string $label Product label.
|
|
* @property string $slug Product slug.
|
|
* @property string $description Product description.
|
|
* @property string $details Product details.
|
|
* @property string $preparation Product preparation.
|
|
* @property string $ingredients Product ingredients.
|
|
* @property string $picture Product picture.
|
|
* @property string $picture_alt Product picture_alt.
|
|
* @property string $picture_title Product picture_title.
|
|
* @property string $banner Product banner.
|
|
* @property string $banner_alt Product banner_alt.
|
|
* @property string $banner_title Product banner_title.
|
|
* @property string $banner Product packaging.
|
|
* @property string $packaging_alt Product packaging_alt.
|
|
* @property string $packaging_title Product packaging_title.
|
|
* @property \App\Models\Approach|null $approach_id Product approach_id.
|
|
* @property \App\Models\Range|null $range_id Product range_id.
|
|
* @property \App\Models\Subrange|null $subrange_id Product subrange_id.
|
|
* @property \App\Models\Meat|null $meat_id Product meat_id.
|
|
* @property \App\Models\Submeat|null $submeat_id Product submeat_id.
|
|
* @property boolean $new Product new.
|
|
* @property boolean $discover Product discover.
|
|
* @property boolean $published Product published.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class Product extends Model implements SeoInterface, Sitemapable, SchemaOrgInterface, RedirectableInterface
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use SeoTrait;
|
|
use Searchable;
|
|
use SchemaOrgTrait;
|
|
use IsRedirectable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'label',
|
|
'slug',
|
|
'claim_title',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'banner',
|
|
'banner_alt',
|
|
'banner_title',
|
|
'packaging',
|
|
'packaging_alt',
|
|
'packaging_title',
|
|
'description',
|
|
'details',
|
|
'preparation',
|
|
'ingredients',
|
|
'approach_id',
|
|
'range_id',
|
|
'subrange_id',
|
|
'meat_id',
|
|
'submeat_id',
|
|
'new',
|
|
'discover',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'new' => 'boolean',
|
|
'discover' => 'boolean',
|
|
'published' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Get model's setting to apply on scout search (meilisearch).
|
|
*
|
|
* @var array
|
|
*/
|
|
public static $searchSettings = [
|
|
'filterableAttributes' => ['label', 'description', 'preparation', 'ingredients', 'published'],
|
|
'sortableAttributes' => ['label', 'description', 'preparation', 'ingredients']
|
|
];
|
|
|
|
/**
|
|
* Get sortable virtual column.
|
|
*
|
|
* @var array
|
|
*/
|
|
public static $sortableVirtualColumn = [
|
|
'ratings_avg_rate',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $product) {
|
|
self::assertLabelIsUnique($product->label);
|
|
self::manageFields($product);
|
|
});
|
|
static::created(function (self $product) {
|
|
self::resetCache();
|
|
Rating::resetCache($product);
|
|
});
|
|
static::updating(function (self $product) {
|
|
self::assertLabelIsUnique($product->label, $product->getKey());
|
|
self::manageFields($product);
|
|
});
|
|
static::updated(function (self $product) {
|
|
FileStorageHelper::removeOldFile($product, 'picture');
|
|
FileStorageHelper::removeOldFile($product, 'banner');
|
|
FileStorageHelper::removeOldFile($product, 'packaging');
|
|
self::resetCache();
|
|
Rating::resetCache($product);
|
|
});
|
|
static::deleted(function (self $product) {
|
|
FileStorageHelper::removeFile($product, 'picture');
|
|
FileStorageHelper::removeFile($product, 'banner');
|
|
FileStorageHelper::removeFile($product, 'packaging');
|
|
self::resetCache();
|
|
Rating::resetCache($product);
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $product
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $product): void
|
|
{
|
|
$product->slug = Str::slug($product->label);
|
|
$product->picture = FileStorageHelper::storeFile($product, $product->picture, true);
|
|
$product->banner = FileStorageHelper::storeFile($product, $product->banner, true);
|
|
$product->packaging = FileStorageHelper::storeFile($product, $product->packaging, 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): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param string|null $slug
|
|
* @return array<string>
|
|
*/
|
|
public static function getProductsViewsByRange(string|null $slug = null): array
|
|
{
|
|
$rangeModel = !is_null($slug) ?
|
|
Range::query()->where([['slug', $slug], ['published', true]])->first() :
|
|
Range::query()->where('published', true)->orderBy('slug', 'ASC')->first();
|
|
return Product::query()
|
|
->whereHas('subrange', function (Builder $q) use ($rangeModel) {
|
|
$q->where('published', true)->whereHas('range', function (Builder $q) use ($rangeModel) {
|
|
$q->where('id', $rangeModel->getKey());
|
|
});
|
|
})->where('published', true)
|
|
->orWhereHas('range', function (Builder $q) use ($rangeModel) {
|
|
$q->where('id', $rangeModel->getKey());
|
|
})->where('published', true)
|
|
->inRandomOrder()
|
|
->limit(6)
|
|
->get()
|
|
->map(function ($productModel) {
|
|
return view('fo.partials.product-card', ['model' => $productModel])->render();
|
|
})->toArray();
|
|
}
|
|
|
|
/**
|
|
* Transform to seoModel.
|
|
*
|
|
* @return \Uwil\Seo\Laravel\Objects\SeoItem
|
|
*/
|
|
public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem
|
|
{
|
|
return new SeoItem(
|
|
'product',
|
|
$this->seo_title,
|
|
$this->seo_description,
|
|
$this->published ?? false,
|
|
\route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this),
|
|
route('fo.products.show', $this->slug)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('publishedDiscoveredProducts');
|
|
self::getPublishedDiscovered();
|
|
}
|
|
|
|
/**
|
|
* Get all published products with discover === true.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public static function getPublishedDiscovered(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::remember('publishedDiscoveredProducts', 86400, function () {
|
|
return self::query()->where('published', true)->where('discover', true)->get();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* toSitemapTag method
|
|
*
|
|
* @return \Spatie\Sitemap\Tags\Url|string|array
|
|
*/
|
|
public function toSitemapTag(): \Spatie\Sitemap\Tags\Url|string|array
|
|
{
|
|
return SitemapHelper::generateUrl('fo.products.show', ['slug' => $this->slug], $this);
|
|
}
|
|
|
|
/**
|
|
* Determine if the model should be searchable.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function shouldBeSearchable(): bool
|
|
{
|
|
return $this->published === true;
|
|
}
|
|
|
|
/**
|
|
* Create a SchemaOrg.
|
|
*
|
|
* @param boolean $asReference
|
|
* @return \Spatie\SchemaOrg\Product
|
|
*/
|
|
public function toSchemaOrg(bool $asReference = false): \Spatie\SchemaOrg\Product
|
|
{
|
|
$model = $this->load([
|
|
'recipes',
|
|
'claims',
|
|
'productTypes',
|
|
'meat',
|
|
'submeat',
|
|
'range',
|
|
'subrange',
|
|
'videos',
|
|
'ratings'
|
|
]);
|
|
|
|
// Main.
|
|
$schema = Schema::Product()
|
|
->name($model->seo_title)
|
|
->description($model->seo_description)
|
|
->url(route('fo.products.show', $model->slug))
|
|
->keywords('produit')
|
|
->releaseDate($model->created_at)
|
|
->image([
|
|
Schema::imageObject()
|
|
->url(asset($model->picture))
|
|
->headline($model->picture_title),
|
|
Schema::imageObject()
|
|
->url(asset($model->packaging))
|
|
->headline($model->packaging_title),
|
|
])
|
|
->brand($model->getBrandSchema());
|
|
|
|
// Ratings.
|
|
if ($model->ratings->count() && Rating::getAverageRating($model->ratings) > 2.0) {
|
|
$schema->aggregateRating(
|
|
Rating::toSchemaOrg($model->ratings)
|
|
);
|
|
}
|
|
|
|
// Recipes associated.
|
|
if ($model->recipes->count() && !$asReference) {
|
|
$schema->subjectOf($model->recipes->where('published', true)->map(function (Recipe $recipe) {
|
|
return $recipe->toSchemaOrg(true);
|
|
})->toArray());
|
|
}
|
|
|
|
// Claim associated.
|
|
if ($model->claims->count()) {
|
|
$schema->subjectOf($model->claims->where('published', true)->map(function (Claim $claim) {
|
|
return $claim->toSchemaOrg();
|
|
})->toArray());
|
|
}
|
|
|
|
// Product types associated.
|
|
if ($model->productTypes->count()) {
|
|
$schema->subjectOf($model->productTypes->where('published', true)->map(function (ProductType $productType) {
|
|
return $productType->toSchemaOrg();
|
|
})->toArray());
|
|
}
|
|
|
|
// Range/Subrange & Meat/Submeat associated.
|
|
$categories = [];
|
|
if ($model->range_id) {
|
|
array_push($categories, $model->range->toSchemaOrg());
|
|
} else {
|
|
array_push($categories, $model->subrange->toSchemaOrg());
|
|
}
|
|
if ($model->meat_id) {
|
|
array_push($categories, $model->meat->toSchemaOrg());
|
|
} else {
|
|
array_push($categories, $model->submeat->toSchemaOrg());
|
|
}
|
|
$schema->category($categories);
|
|
|
|
if ($model->videos->count()) {
|
|
$model->videos->map(function (Video $videoModel) use ($schema) {
|
|
$schema->subjectOf(
|
|
Schema::creativeWork()->associatedMedia($videoModel->toSchemaOrg())
|
|
);
|
|
});
|
|
}
|
|
|
|
return $schema;
|
|
}
|
|
|
|
// * REDIRECTIONS.
|
|
|
|
/**
|
|
* Get the front url of this model.
|
|
* Should return the model.show route, with all binded elements if needed.
|
|
*
|
|
* @param string $modelKey
|
|
* @return string
|
|
*/
|
|
public function getFrontUrl(string $modelKey): string
|
|
{
|
|
return route('fo.products.show', $modelKey);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get the range that are assigned this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function range(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Range::class);
|
|
}
|
|
|
|
/**
|
|
* Get the subrange that are assigned this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function subrange(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Subrange::class);
|
|
}
|
|
|
|
/**
|
|
* Get the meat that are assigned this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function meat(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Meat::class);
|
|
}
|
|
|
|
/**
|
|
* Get the submeat that are assigned this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function submeat(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Submeat::class);
|
|
}
|
|
|
|
/**
|
|
* Get all of the recipes that are assigned this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function recipes(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(Recipe::class, 'productable');
|
|
}
|
|
|
|
/**
|
|
* Get the demarch assigned to this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function approach(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Approach::class);
|
|
}
|
|
|
|
/**
|
|
* Get all labels that are assigned to this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function labels(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(Approach::class, 'productable');
|
|
}
|
|
|
|
/**
|
|
* Get all product types that are assigned to this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function productTypes(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(ProductType::class, 'productable');
|
|
}
|
|
|
|
/**
|
|
* Get all product types that are assigned to this product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
*/
|
|
public function claims(): \Illuminate\Database\Eloquent\Relations\MorphToMany
|
|
{
|
|
return $this->morphedByMany(Claim::class, 'productable');
|
|
}
|
|
|
|
/**
|
|
* Get the videos for the product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function videos(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Video::class);
|
|
}
|
|
|
|
/**
|
|
* Get the nutritionnal values for the product.
|
|
*
|
|
* @return \App\Models\IncoNutritionalValue|null
|
|
*/
|
|
public function getNutritionnalValue(): \App\Models\IncoNutritionalValue|null
|
|
{
|
|
return $this->hasMany(IncoNutritionalValue::class)->first();
|
|
}
|
|
|
|
/**
|
|
* Get the nutritionnal values for the product.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
*/
|
|
public function ratings(): \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
{
|
|
return $this->morphMany(Rating::class, 'rating');
|
|
}
|
|
}
|