ldc-wenn-dann-mit-respekt/app/Models/Product.php
2023-05-30 20:26:54 +02:00

230 lines
6 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
/**
* @property integer $range_id
* @property string $seo_title
* @property string $seo_description
* @property string $label
* @property string $slug
* @property string $description
* @property string|null $marinade
* @property string $picture
* @property array $certifications
* @property array $nutritionnal_values
* @property array $list
* @property boolean $status
* @property integer $order
* @property string $created_at
* @property string $updated_at
*/
class Product extends Model
{
use HasFactory;
use HasTranslations;
/**
* @var array
*/
protected $fillable = [
'range_id',
'seo_title',
'seo_description',
'label',
'slug',
'description',
'marinade',
'picture',
'picture_alt',
'picture_title',
'certifications',
'nutritionnal_values',
'list',
'status',
'order',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'certifications' => 'array',
'nutritionnal_values' => 'array',
'list' => 'array',
];
/**
* The translatable columns.
*
* @var array
*/
public $translatable = [
'seo_title',
'seo_description',
'label',
'slug',
'description',
'marinade',
'picture',
'picture_alt',
'picture_title',
'nutritionnal_values',
'list',
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::assertFieldIsUnique('label', $model->label);
self::manageFields($model);
self::cleanTranslationJsonFields($model, 'nutritionnal_values', 'list');
self::setOrder($model);
});
static::updating(function (self $model) {
self::manageFields($model);
self::cleanTranslationJsonFields($model, 'nutritionnal_values', 'list');
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS
/**
* Undocumented function
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
self::setSlug($model);
if (is_array($model->list)) {
foreach ($model->list as $itemList) {
ToolboxHelper::assertNoInternalBoUrls('list', $itemList['value'] ?? '');
}
}
if (is_array($model->nutritionnal_values)) {
foreach ($model->nutritionnal_values as $itemNutritionnalValues) {
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $itemNutritionnalValues['label'] ?? '');
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $itemNutritionnalValues['value'] ?? '');
}
}
$model->nutritionnal_values = clean($model->nutritionnal_values);
$model->list = clean($model->list);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
/**
* Remove the original array from json field
*
* @param Model $model
* @param string ...$fields The names of the json fields.
* @return void
*/
private static function cleanTranslationJsonFields(Model $model, string ...$fields): void
{
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->label);
}
/**
* 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);
}
/**
* 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;
}
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function recipes()
{
return $this->belongsToMany('App\Models\Recipe', 'recipes_products');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function range()
{
return $this->belongsTo('App\Models\Range');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function homes1()
{
return $this->hasMany('App\Models\Home', 'part2_product1_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function homes2()
{
return $this->hasMany('App\Models\Home', 'part2_product2_id');
}
}