ldc-legaulois/app/Models/Product.php
2024-06-12 11:56:58 +02:00

211 lines
7.7 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Vite;
/**
* @property string $label Product label.
* @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 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
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'label',
'picture',
'picture_alt',
'picture_title',
'banner',
'banner_alt',
'banner_title',
'packaging',
'packaging_alt',
'packaging_title',
'description',
'details',
'preparation',
'ingredients',
'approach_id',
'new',
'discover',
'published',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'new' => 'boolean',
'discover' => 'boolean',
'published' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $product) {
$product->picture = FileStorageHelper::storeFile($product, $product->picture, true);
$product->banner = FileStorageHelper::storeFile($product, $product->banner, true);
$product->packaging = FileStorageHelper::storeFile($product, $product->packaging, true);
});
static::updating(function (self $product) {
$product->picture = FileStorageHelper::storeFile($product, $product->picture, true);
$product->banner = FileStorageHelper::storeFile($product, $product->banner, true);
$product->packaging = FileStorageHelper::storeFile($product, $product->packaging, true);
});
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
/**
* Display a listing of the resource.
*
* @param string|null $slug
* @return array<string>
*/
public static function getProductsViewsByRange(string|null $slug = null): array
{
/** FIXME: replace 'first-range' by the first Range slug publicated with order by ASC. */
$rangeSlug = !is_null($slug) ? $slug : "first-range";
/** FIXME: replace this part of 'productModels' values by products associated to the range (limit 6, random). */
$productModels = [];
for ($i = 0; $i < 5; $i++) {
$productModels[] = (object)[
'label' => "Produit $i de la gamme $rangeSlug",
'packaging' => Vite::asset('resources/assets/images/tmp/packaging.png'),
'packaging_alt' => "lorem ipsum",
'packaging_title' => "lorem ipsum",
'picture' => Vite::asset('resources/assets/images/tmp/product-picture.png'),
'picture_alt' => "lorem ipsum",
'picture_title' => "lorem ipsum",
'tag' => Vite::asset('resources/assets/images/tmp/product-tag.png'),
'tag_alt' => "lorem ipsum",
'tag_title' => "lorem ipsum",
'isFavorite' => ($i % 2) ? true : false,
'isNew' => ($i % 2) ? false : true,
];
}
foreach ($productModels as $productModel) {
$productViews[] = view('fo.partials.product-card', compact('productModel'))->render();
} //end foreach
return $productViews;
}
// * RELATIONSHIPS
/**
* Get all of the ranges that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function ranges(): \Illuminate\Database\Eloquent\Relations\MorphToMany
{
return $this->morphedByMany(Range::class, 'productable');
}
/**
* Get all of the subranges that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function subranges(): \Illuminate\Database\Eloquent\Relations\MorphToMany
{
return $this->morphedByMany(Subrange::class, 'productable');
}
/**
* Get all of the meats that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function meats(): \Illuminate\Database\Eloquent\Relations\MorphToMany
{
return $this->morphedByMany(Meat::class, 'productable');
}
/**
* Get all of the submeats that are assigned this product.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function submeats(): \Illuminate\Database\Eloquent\Relations\MorphToMany
{
return $this->morphedByMany(Submeat::class, 'productable');
}
/**
* 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');
}
}