ldc-loue/app/Models/Product.php

220 lines
8.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Products\ProductLabelEnum;
use App\Lib\Helpers\ToolboxHelper;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Str;
class Product extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'slug',
'seo_title',
'seo_description',
'description',
'picture',
'picture_title',
'picture_alt',
'picture_thumb',
'picture_thumb_title',
'picture_thumb_alt',
'labels',
'ref_article',
'ingredients',
'preparation_advices',
'sales_pitch',
'allergen_list',
'number_portion',
'status',
'publish_at',
'unpublish_at',
'range_id',
'show_ingredients',
'show_preparation_advices',
'show_sales_pitch',
'show_allergen_list',
'show_nutrionnal_values',
'show_portions',
'link1',
'link2'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'labels' => 'array',
'status' => 'bool',
'show_ingredients' => 'bool',
'show_preparation_advices' => 'bool',
'show_sales_pitch' => 'bool',
'show_allergen_list' => 'bool',
'show_nutrionnal_values' => 'bool',
'publish_at' => 'datetime',
'unpublish_at' => 'datetime'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
static::assertTitleIsUnique($model->title);
self::validateLabels($model->labels);
$model->slug = Str::slug($model->title);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients);
ToolboxHelper::assertNoInternalBoUrls('preparation_advices', $model->preparation_advices);
ToolboxHelper::assertNoInternalBoUrls('sales_pitch', $model->sales_pitch);
ToolboxHelper::assertNoInternalBoUrls('allergen_list', $model->allergen_list);
$model->slug = Str::slug($model->title);
$model->description = clean($model->description);
$model->ingredients = clean($model->ingredients);
$model->preparation_advices = clean($model->preparation_advices);
$model->sales_pitch = clean($model->sales_pitch);
$model->allergen_list = clean($model->allergen_list);
$model->picture = FileStorageHelper::storeFile($model, $model->picture);
$model->picture_thumb = FileStorageHelper::storeFile($model, $model->picture_thumb);
if (!$model->status) {
$model->publish_at = null;
} else {
$model->unpublish_at = null;
}
if ($model->link1) {
self::validateLinkAttribute('link1', $model->link1);
}
if ($model->link2) {
self::validateLinkAttribute('link2', $model->link2);
}
});
static::updating(function (self $model) {
static::assertTitleIsUnique($model->title, $model->id);
self::validateLabels($model->labels);
$model->slug = Str::slug($model->title);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients);
ToolboxHelper::assertNoInternalBoUrls('preparation_advices', $model->preparation_advices);
ToolboxHelper::assertNoInternalBoUrls('sales_pitch', $model->sales_pitch);
ToolboxHelper::assertNoInternalBoUrls('allergen_list', $model->allergen_list);
$model->slug = Str::slug($model->title);
$model->description = clean($model->description);
$model->ingredients = clean($model->ingredients);
$model->preparation_advices = clean($model->preparation_advices);
$model->sales_pitch = clean($model->sales_pitch);
$model->allergen_list = clean($model->allergen_list);
// * Store new picture if changed
if ($model->picture != $model->getOriginal('picture')) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture);
}
// * Store new picture_thumb if changed
if ($model->picture_thumb != $model->getOriginal('picture_thumb')) {
$model->picture_thumb = FileStorageHelper::storeFile($model, $model->picture_thumb);
}
if ($model->link1) {
self::validateLinkAttribute('link1', $model->link1);
}
if ($model->link2) {
self::validateLinkAttribute('link2', $model->link2);
}
});
static::updated(function (self $model) {
ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'description');
ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'ingredients');
ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'preparation_advices');
ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'sales_pitch');
ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'allergen_list');
if ($model->picture != $model->getOriginal('picture')) {
$model->picture = FileStorageHelper::removeOldFile($model, 'picture');
}
});
static::deleting(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'picture_thumb');
ToolboxHelper::removeImagesOnCkEditor($model, 'description');
ToolboxHelper::removeImagesOnCkEditor($model, 'ingredients');
ToolboxHelper::removeImagesOnCkEditor($model, 'preparation_advices');
ToolboxHelper::removeImagesOnCkEditor($model, 'sales_pitch');
ToolboxHelper::removeImagesOnCkEditor($model, 'allergen_list');
});
}
// * RELATIONSHIPS
/**
* HasOne incoNutritionalValue relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function incoNutritionalValues()
{
return $this->hasOne(IncoNutritionalValue::class);
}
/**
* BelongsTo range relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function range()
{
return $this->belongsTo(Range::class);
}
// * METHODS
/**
* Asserts using validation that the title is unique
*
* @param mixed $title
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If an actu title already exists.
*/
private static function assertTitleIsUnique($title, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
}
/**
*
* @param array $labels
* @return void
* @throws \Illuminate\Validation\ValidationException If a label is invalid.
*/
private static function validateLabels(array $labels)
{
collect($labels)->map(function ($label) {
if (!\is_object($label) or !($label instanceof ProductLabelEnum)) {
if (
!\is_array($label) or
!\array_key_exists('value', $label) or
!\in_array($label['value'], ProductLabelEnum::toValues())
) {
throw ValidationException::withMessages([
'labels' => 'Invalid label given'
]);
}
}
});
}
}