259 lines
7.3 KiB
PHP
259 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Recipes\DifficultyEnum;
|
|
use App\Enums\Recipes\CookingEnum;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
/**
|
|
* @property integer $id
|
|
* @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
|
|
* @property boolean $difficulty
|
|
* @property boolean $preparation
|
|
* @property boolean $duration
|
|
* @property string $tips
|
|
* @property boolean $cooking
|
|
* @property boolean $status
|
|
* @property integer $order
|
|
* @property string $created_at
|
|
* @property string $updated_at
|
|
* @property Product[] $products
|
|
* @property Range $range
|
|
*/
|
|
class Recipe extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'range_id',
|
|
'seo_title',
|
|
'seo_description',
|
|
'name',
|
|
'slug',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'picture_thumbnail',
|
|
'picture_thumbnail_alt',
|
|
'picture_thumbnail_title',
|
|
'ingredients',
|
|
'teaser',
|
|
'steps',
|
|
'difficulty',
|
|
'preparation',
|
|
'duration',
|
|
'tips',
|
|
'cooking',
|
|
'status',
|
|
'order',
|
|
'created_at',
|
|
'updated_at'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'ingredients' => 'json',
|
|
'difficulty' => DifficultyEnum::class,
|
|
'cooking' => CookingEnum::class,
|
|
];
|
|
|
|
/**
|
|
* The translatable columns.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'name',
|
|
'slug',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'picture_thumbnail_alt',
|
|
'picture_thumbnail_title',
|
|
'ingredients',
|
|
'teaser',
|
|
'steps',
|
|
'tips',
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::assertFieldIsUnique('name', $model->name);
|
|
self::setSlug($model);
|
|
self::manageFields($model);
|
|
self::cleanTranslationJsonFields($model, 'ingredients');
|
|
self::setOrder($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
self::cleanTranslationJsonFields($model, 'ingredients');
|
|
});
|
|
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
|
|
|
|
/**
|
|
* Common methods function.
|
|
*
|
|
* @param Model $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(Model $model)
|
|
{
|
|
if (is_array($model->ingredients)) {
|
|
foreach ($model->ingredients as $itemIngredients) {
|
|
if (isset($itemIngredients['header'])) {
|
|
ToolboxHelper::assertNoInternalBoUrls('ingredients', $itemIngredients['header']);
|
|
}
|
|
ToolboxHelper::assertNoInternalBoUrls('ingredients', $itemIngredients['value']);
|
|
}
|
|
}
|
|
ToolboxHelper::assertNoInternalBoUrls('steps', $model->steps);
|
|
ToolboxHelper::assertNoInternalBoUrls('tips', $model->tips);
|
|
$model->ingredients = clean($model->ingredients);
|
|
$model->steps = clean($model->steps);
|
|
$model->tips = clean($model->tips);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
$model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true);
|
|
}
|
|
|
|
/**
|
|
* Remove the original array from json field
|
|
* @param Model $model
|
|
* @param string $fields The names of the json fields
|
|
*/
|
|
private static function cleanTranslationJsonFields(Model $model, ...$fields)
|
|
{
|
|
foreach ($fields as $field) {
|
|
$translations = $model->getTranslations($field);
|
|
foreach ($translations as $key => $trans) {
|
|
if (!in_array($key, config('app.locales'))) {
|
|
$model->forgetTranslation($field, $key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Asserts using validation that the field is unique.
|
|
*
|
|
* @param string $nameField
|
|
* @param mixed $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);
|
|
}
|
|
|
|
/**
|
|
* Retrieve the model for a bound value.
|
|
*
|
|
* @param mixed $value
|
|
* @param string|null $field
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
*/
|
|
public function resolveRouteBinding($value, $field = null)
|
|
{
|
|
if (Route::current()->action['prefix'] !== '/bo') {
|
|
$field = 'slug';
|
|
}
|
|
// Default field to query if no parameter field is specified.
|
|
$field = $field ?: $this->getRouteKeyName();
|
|
// Perform the query to find the parameter value in the database.
|
|
if (count($this->where($field, $value)->get())) {
|
|
return $this->where($field, $value)->firstOrFail();
|
|
} else {
|
|
foreach (config('app.locales') as $locale) {
|
|
if (count($this->where($field . '->' . $locale, $value)->get())) {
|
|
return $this->where($field . '->' . $locale, $value)->firstOrFail();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function products()
|
|
{
|
|
return $this->belongsToMany('App\Models\Product', 'recipes_products');
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function range()
|
|
{
|
|
return $this->belongsTo('App\Models\Range');
|
|
}
|
|
}
|