ldc-lionor/app/Models/Range.php

165 lines
6 KiB
PHP
Raw Permalink Normal View History

2023-02-24 15:20:58 +00:00
<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasFlashStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Spatie\Sitemap\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;
use Uwil\SitemapGenerator\Lib\SitemapHelper;
2023-02-24 15:20:58 +00:00
/**
* Gamme
*
* @property string $name The name.
* @property string $slug The slug.
* @property string $subtitle The subtitle.
* @property string $seo_title The seo title for referencement.
* @property string $seo_description The seo description.
* @property string $banner The banner path.
* @property string $banner_alt The banner alternative text.
* @property string $banner_title The banner alternative title.
* @property string $picture The picture path.
* @property string $picture_alt The picture alternative text.
* @property string $picture_title The picture alternative title.
* @property string $sticker The sticker path.
* @property string $sticker_alt The sticker alternative text.
* @property string $sticker_title The sticker alternative title.
* @property string $description The description html text.
* @property string|null $preparation The preparation html text.
* @property string $ingredients The ingredients html text.
* @property string $info_cooking The cooking info label.
* @property string $info_consumption The consumption info label.
* @property \App\Enums\CookingModeEnum $cooking_mode The cooking mode enum for svg sticker.
* @property \App\Enums\NutriscoreEnum $nutriscore The nutriscore enum for svg sticker.
* @property object<string[]> $nutritional_values The nutritional values as a value-label list.
* @property boolean $status The publication status.
* @property integer $range_id The range associated.
*
* @package App\Models
*/
class Range extends Model implements Sitemapable
2023-02-24 15:20:58 +00:00
{
use HasFactory;
use HasFlashStatus;
/**
* The attributes that are mass assignable.
*
2024-09-06 08:30:55 +00:00
* @var array<int, string>
2023-02-24 15:20:58 +00:00
*/
protected $fillable = [
'name',
'slug',
'subtitle',
'seo_title',
'seo_description',
'picture',
'picture_alt',
'picture_title',
'banner',
'banner_alt',
'banner_title',
'sticker',
'sticker_alt',
'sticker_title',
'status'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function () {
throw new \RuntimeException('Impossible de créer une gamme');
});
static::updating(function (self $model) {
// * ASSERTIONS
2024-09-06 08:30:55 +00:00
self::assertFieldIsUnique('name', $model->name, $model->id);
self::assertAtLeastOneIsPublished($model);
2023-02-24 15:20:58 +00:00
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->banner = FileStorageHelper::storeFile($model, $model->banner, true);
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
});
static::updated(function (self $model) {
2024-09-06 08:30:55 +00:00
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'banner');
FileStorageHelper::removeOldFile($model, 'sticker');
2023-02-24 15:20:58 +00:00
static::flashStatus($model, true);
});
static::deleting(function () {
throw new \RuntimeException('Impossible de supprimer une gamme');
});
}
// * METHODS
/**
2023-08-28 08:46:47 +00:00
* Asserts using validation that the field is unique.
2023-02-24 15:20:58 +00:00
*
2023-08-28 08:46:47 +00:00
* @param string $nameField
* @param string $valueField
2023-02-24 15:20:58 +00:00
* @param integer|null $id
* @return void
*/
2023-08-28 08:46:47 +00:00
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
2023-02-24 15:20:58 +00:00
{
$table = (new self())->getTable();
2023-08-28 08:46:47 +00:00
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
2023-02-24 15:20:58 +00:00
}
/**
* Asserts using validation that the name is unique
*
* @param self $model
* @return void
* @throws \Illuminate\Validation\ValidationException If no model type is published.
*/
private static function assertAtLeastOneIsPublished(self $model)
{
if (!$model->status and self::query()->where('status', true)->count() < 2) {
throw ValidationException::withMessages([
'error' => trans('Au moins un(e) :model doit être publié, impossible de dépublier', [
'model' => trans('models.classes.' . $model->getTable())
])
]);
}
}
/**
* toSitemapTag method
*
* @return Url|string|array
*/
public function toSitemapTag(): Url | string | array
{
$model = $this->loadMissing('products');
$route = 'ranges.products';
$params = ['publishedRange' => $model->slug, 'publishedProduct' => $model];
return SitemapHelper::generateUrl($route, $params, $model);
}
2023-02-24 15:20:58 +00:00
// * RELATIONSHIPS
/**
* BelongsToMany certifications relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function products()
{
return $this->hasMany(Product::class);
}
}