ldc-nature-and-respect/app/Models/Recipe.php

204 lines
5.2 KiB
PHP
Raw Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
2023-05-09 09:14:21 +02:00
use App\Enums\Recipes\DifficultyEnum;
2023-03-15 13:51:01 +01:00
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2023-05-10 10:39:24 +02:00
use App\Traits\Models\HasTranslations;
2023-03-16 09:47:16 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-15 13:51:01 +01:00
use Illuminate\Database\Eloquent\Model;
2023-03-23 09:08:28 +01:00
use Illuminate\Support\Str;
2023-03-15 13:51:01 +01:00
/**
* @property integer $range_id
* @property string $seo_title
* @property string $seo_description
* @property string $name
* @property string $slug
* @property string $picture
* @property string $picture_thumbnail
* @property string $ingredients
* @property string $teaser
* @property string $steps
2023-05-09 15:21:00 +02:00
* @property \App\Enums\Recipes\DifficultyEnum $difficulty
* @property string $duration
2023-03-23 09:08:28 +01:00
* @property string $tips
2023-03-15 13:51:01 +01:00
* @property boolean $status
* @property integer $order
* @property string $created_at
* @property string $updated_at
*/
class Recipe extends Model
{
2023-03-16 09:47:16 +01:00
use HasFactory;
use HasTranslations;
2023-03-16 09:47:16 +01:00
2023-03-15 13:51:01 +01:00
/**
* @var array
*/
protected $fillable = [
'range_id',
'seo_title',
'seo_description',
'name',
'slug',
'picture',
2023-03-23 09:08:28 +01:00
'picture_alt',
'picture_title',
2023-03-15 13:51:01 +01:00
'picture_thumbnail',
2023-03-23 09:08:28 +01:00
'picture_thumbnail_alt',
'picture_thumbnail_title',
2023-03-15 13:51:01 +01:00
'ingredients',
'teaser',
'steps',
'difficulty',
'duration',
2023-03-23 09:08:28 +01:00
'tips',
2023-03-15 13:51:01 +01:00
'status',
'order',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'ingredients' => 'json',
2023-05-09 15:21:00 +02:00
'difficulty' => DifficultyEnum::class
2023-03-15 13:51:01 +01:00
];
/**
* The translatable columns.
2023-04-18 16:05:34 +02:00
*
* @var array
*/
public $translatable = [
'seo_title',
'seo_description',
'name',
'slug',
2023-05-09 19:24:09 +02:00
'picture',
'picture_alt',
'picture_title',
2023-05-09 19:24:09 +02:00
'picture_thumbnail',
'picture_thumbnail_alt',
'picture_thumbnail_title',
'ingredients',
2023-05-09 15:21:00 +02:00
'duration',
2023-04-03 09:05:55 +00:00
'teaser',
'steps',
'tips',
];
2023-03-15 13:51:01 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
2023-03-23 09:08:28 +01:00
self::assertFieldIsUnique('name', $model->name);
self::setSlug($model);
2023-03-15 13:51:01 +01:00
self::manageFields($model);
2023-03-23 09:08:28 +01:00
self::setOrder($model);
2023-03-15 13:51:01 +01:00
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldFile($model, 'picture_thumbnail');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'picture_thumbnail');
});
}
// * METHODS
/**
2023-03-23 09:08:28 +01:00
* Common methods function.
2023-03-15 13:51:01 +01:00
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
if (is_array($model->ingredients)) {
foreach ($model->ingredients as $itemIngredients) {
2023-04-18 10:49:39 +02:00
if (isset($itemIngredients['header'])) {
ToolboxHelper::assertNoInternalBoUrls('ingredients', $itemIngredients['header']);
}
2023-05-09 09:14:21 +02:00
ToolboxHelper::assertNoInternalBoUrls('ingredients', \strval($itemIngredients['value']));
}
2023-03-23 09:08:28 +01:00
}
2023-03-15 13:51:01 +01:00
ToolboxHelper::assertNoInternalBoUrls('steps', $model->steps);
2023-03-23 09:08:28 +01:00
ToolboxHelper::assertNoInternalBoUrls('tips', $model->tips);
2023-03-15 13:51:01 +01:00
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
}
2023-03-23 09:08:28 +01:00
/**
* Set the slug.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
private static function setSlug(Model $model)
{
$model->slug = Str::slug($model->name);
}
/**
* Set order after the last element of the list.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
private static function setOrder(Model $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
2023-03-15 13:51:01 +01:00
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
2023-05-09 09:14:21 +02:00
* @param string $valueField
2023-03-15 13:51:01 +01:00
* @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);
}
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
2023-05-09 09:14:21 +02:00
return $this->belongsToMany(Product::class, 'recipes_products');
2023-03-15 13:51:01 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function range()
{
2023-05-09 09:14:21 +02:00
return $this->belongsTo(Range::class);
2023-03-15 13:51:01 +01:00
}
}