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

208 lines
5.1 KiB
PHP
Raw Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
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-22 14:04: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 $label
* @property string $slug
* @property string $description
2023-05-30 17:04:24 +02:00
* @property string|null $marinade
2023-03-15 13:51:01 +01:00
* @property string $picture
2023-05-09 19:24:09 +02:00
* @property array $certifications
* @property array $nutritionnal_values
* @property array $list
2023-03-15 13:51:01 +01:00
* @property boolean $status
* @property integer $order
* @property string $created_at
* @property string $updated_at
*/
class Product 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',
'label',
'slug',
'description',
2023-05-30 17:04:24 +02:00
'marinade',
2023-03-15 13:51:01 +01:00
'picture',
2023-03-22 14:04:28 +01:00
'picture_alt',
'picture_title',
2023-03-15 13:51:01 +01:00
'certifications',
'nutritionnal_values',
'list',
'status',
'order',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
2023-03-16 09:47:16 +01:00
'certifications' => 'array',
2023-03-22 14:04:28 +01:00
'nutritionnal_values' => 'array',
'list' => 'array',
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',
'label',
'slug',
'description',
2023-05-30 17:04:24 +02:00
'marinade',
2023-05-09 19:24:09 +02:00
'picture',
'picture_alt',
'picture_title',
'nutritionnal_values',
'list',
];
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-22 14:04:28 +01:00
self::assertFieldIsUnique('label', $model->label);
2023-03-15 13:51:01 +01:00
self::manageFields($model);
2023-03-22 14:04: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');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
// * METHODS
/**
* Undocumented function
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
2023-03-22 14:04:28 +01:00
self::setSlug($model);
if (is_array($model->list)) {
foreach ($model->list as $itemList) {
2023-05-04 14:52:14 +02:00
ToolboxHelper::assertNoInternalBoUrls('list', $itemList['value'] ?? '');
}
2023-03-22 14:04:28 +01:00
}
if (is_array($model->nutritionnal_values)) {
foreach ($model->nutritionnal_values as $itemNutritionnalValues) {
2023-05-04 14:52:14 +02:00
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $itemNutritionnalValues['label'] ?? '');
ToolboxHelper::assertNoInternalBoUrls('nutritionnal_values', $itemNutritionnalValues['value'] ?? '');
}
2023-03-22 14:04:28 +01:00
}
2023-05-30 20:26:00 +02:00
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
2023-03-15 13:51:01 +01:00
}
2023-03-22 14:04: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->label);
}
2023-03-15 13:51:01 +01:00
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
2023-05-04 14:52:14 +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);
}
2023-03-22 14:04:28 +01:00
/**
* 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
// * 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');
}
}