282 lines
9.5 KiB
PHP
282 lines
9.5 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 Illuminate\Database\Eloquent\Builder;
|
|
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
|
|
use Uwil\Seo\Laravel\Traits\SeoTrait;
|
|
use Uwil\Seo\Laravel\Objects\SeoItem;
|
|
|
|
/**
|
|
* @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
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use SeoTrait;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'label',
|
|
'slug',
|
|
'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', FIXME: hide discover part -> uncomment when there is an evolution for this part.
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'new' => 'boolean',
|
|
// 'discover' => 'boolean', FIXME: hide discover part -> uncomment when there is an evolution for this part.
|
|
'published' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* 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::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');
|
|
});
|
|
static::deleted(function (self $product) {
|
|
FileStorageHelper::removeFile($product, 'picture');
|
|
FileStorageHelper::removeOldFile($product, 'banner');
|
|
FileStorageHelper::removeOldFile($product, 'packaging');
|
|
});
|
|
}
|
|
|
|
// * 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)
|
|
{
|
|
$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)->first() :
|
|
Range::query()->orderBy('slug', 'ASC')->first();
|
|
return Product::query()->whereHas('range', function (Builder $q) use ($rangeModel) {
|
|
$q->where('id', $rangeModel->getKey());
|
|
})->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)
|
|
);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get all of the ranges 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 all of the subranges 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 all of the meats 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 all of the submeats 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');
|
|
}
|
|
}
|