ldc-agis/app/Models/Range.php

211 lines
6.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Products\CookingModeEnum;
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 Spatie\Sitemap\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
use Uwil\Seo\Laravel\Objects\SeoItem;
use Uwil\Seo\Laravel\Traits\SeoTrait;
use Uwil\SitemapGenerator\Lib\SitemapHelper;
// phpcs:disable Generic.Files.LineLength.TooLong
/**
* @property string $seo_title SEO title.
* @property string $seo_description SEO description.
* @property string $title Title.
* @property string $slug Slug of the title.
* @property string $subtitle Subtitle.
* @property string $tagline Tagline.
* @property string $description WYSIWYG - Description.
* @property string $picture Path of the picture (466px * 440px).
* @property string $picture_alt Picture alt attribute.
* @property string $picture_title Picture title attribute.
* @property string $picture_productsheet Path of the picture of the product sheet (680px * 300px).
* @property string $picture_productsheet_alt Picture alt attribute of the product sheet.
* @property string $picture_productsheet_title Picture title attribute of the product sheet.
* @property string $picture_thumbnail Path of the thumbnail (246px * 337px).
* @property string $picture_thumbnail_alt Thumbnail alt attribute.
* @property string $picture_thumbnail_title Thumbnail title attribute.
* @property boolean $published Publication status.
* @property integer $order Order.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Products $products Products Has Many relationship.
*/
// phpcs:enable
class Range extends Model implements SeoInterface, Sitemapable
{
use ActivityLog;
use HasFactory;
use SeoTrait;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'seo_title',
'seo_description',
'title',
'slug',
'subtitle',
'tagline',
'description',
'picture',
'picture_alt',
'picture_title',
'picture_productsheet',
'picture_productsheet_alt',
'picture_productsheet_title',
'picture_thumbnail',
'picture_thumbnail_alt',
'picture_thumbnail_title',
'published',
'order',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'cooking_mode' => CookingModeEnum::class,
'published' => 'bool',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::assertFieldIsUnique('title', $model->title);
self::setOrder($model);
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'picture_productsheet');
FileStorageHelper::removeOldFile($model, 'picture_thumbnail');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'picture_productsheet');
FileStorageHelper::removeFile($model, 'picture_thumbnail');
});
}
// * METHODS
/**
* Manage model fields.
*
* @param self $model
* @return void
*/
private static function manageFields(self $model): void
{
self::setSlug($model, $model->title);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->picture_productsheet = FileStorageHelper::storeFile($model, $model->picture_productsheet, true);
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
}
/**
* Set the slug.
*
* @param self $model
* @param string $original
*
* @return void
*/
private static function setSlug(self $model, string $original): void
{
$model->slug = Str::slug($original);
}
/**
* 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 products in the range.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Product::class);
}
// * SEO TRAIT OVERLOAD
/**
* Transform to seoModel
*
* @return \Uwil\Seo\Laravel\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) : ''
);
}
/**
* toSitemapTag method
*
* @return Url|string|array
*/
public function toSitemapTag(): Url | string | array
{
$model = $this;
$route = 'fo.ranges.show';
$params = ['slug' => $model->slug];
return SitemapHelper::generateUrl($route, $params, $model);
}
}