201 lines
5.5 KiB
PHP
201 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\CookingModeEnum;
|
|
use App\Enums\DifficultyEnum;
|
|
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 Spatie\Sitemap\Contracts\Sitemapable;
|
|
use Spatie\Sitemap\Tags\Url;
|
|
use Uwil\SitemapGenerator\Lib\SitemapHelper;
|
|
|
|
class Recipe extends Model implements Sitemapable
|
|
{
|
|
use HasFactory;
|
|
use HasFlashStatus;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'seo_title',
|
|
'seo_description',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'sticker',
|
|
'sticker_alt',
|
|
'sticker_title',
|
|
'youtube_video_id',
|
|
'description',
|
|
'preparation',
|
|
'ingredients',
|
|
'portions',
|
|
'difficulty',
|
|
'cooking_mode',
|
|
'cooking_time',
|
|
'preparation_time',
|
|
'status',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'ingredients' => 'array',
|
|
'status' => 'bool',
|
|
'cooking_mode' => CookingModeEnum::class,
|
|
'difficulty' => DifficultyEnum::class
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
// * EVENT CLOSURES.
|
|
static::creating(function (self $model) {
|
|
self::assertFieldIsUnique('name', $model->name);
|
|
self::manageFields($model);
|
|
});
|
|
static::created(function (self $model) {
|
|
self::flashStatus($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::assertFieldIsUnique('name', $model->name, $model->id);
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
FileStorageHelper::removeOldFile($model, 'sticker');
|
|
self::flashStatus($model, true);
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
FileStorageHelper::removeFile($model, 'sticker');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage fields on this model event.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model)
|
|
{
|
|
// * ASSERTIONS
|
|
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
|
|
ToolboxHelper::assertNoInternalBoUrls('preparation', $model->preparation ?? '');
|
|
|
|
if ($model->youtube_video_id) {
|
|
ToolboxHelper::assertNoInternalBoUrls('youtube_video_id', $model->youtube_video_id);
|
|
$model->youtube_video_id = clean($model->youtube_video_id);
|
|
}
|
|
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->name);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
|
|
/**
|
|
* Convert database value (in minutes) to string "nnnH nnnM".
|
|
|
|
* @return string Text formated "nnnH nnnM".
|
|
*/
|
|
public function getCookingTimeLabelAttribute(): string
|
|
{
|
|
if ($this->cooking_time) {
|
|
return self::convertMinutesToHourMinutes($this->cooking_time);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Convert database value (in minutes) to string "nnnH nnnM".
|
|
*
|
|
* @return string Text formated "nnnH nnnM".
|
|
*/
|
|
public function getPreparationTimeLabelAttribute(): string
|
|
{
|
|
return self::convertMinutesToHourMinutes($this->preparation_time);
|
|
}
|
|
|
|
/**
|
|
* Convert minutes to hour minutes string
|
|
*
|
|
* @param integer $minutes
|
|
* @return string
|
|
*/
|
|
private static function convertMinutesToHourMinutes(int $minutes): string
|
|
{
|
|
$nbHours = \intval($minutes / 60);
|
|
$nbMinutes = $minutes % 60;
|
|
return "{$nbHours}H {$nbMinutes}M";
|
|
}
|
|
|
|
/**
|
|
* toSitemapTag method
|
|
*
|
|
* @return Url|string|array
|
|
*/
|
|
public function toSitemapTag(): Url | string | array
|
|
{
|
|
$model = $this->loadMissing('recipes');
|
|
$route = 'recipes.show';
|
|
$params = ['publishedRecipe' => $model->slug, 'publishedProduct' => $model];
|
|
return SitemapHelper::generateUrl($route, $params, $model);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsToMany recipes
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function recipes()
|
|
{
|
|
return $this->belongsToMany(Recipe::class, 'recipe_recipe', 'recipe_id', 'associated_recipe_id');
|
|
}
|
|
|
|
/**
|
|
* Get products for the recipe.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function products()
|
|
{
|
|
return $this->belongsToMany(Product::class, 'recipe_product');
|
|
}
|
|
}
|