161 lines
4.1 KiB
PHP
161 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class Network extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'name',
|
|
'slug',
|
|
'title',
|
|
'subtitle',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'order',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'published' => 'bool',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::setOrder($model);
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
});
|
|
static::deleting(function (self $model) {
|
|
self::assertLimitPublishedModels($model, true);
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
// * ASSERTIONS
|
|
self::assertLimitPublishedModels($model);
|
|
self::assertFieldIsUnique('name', $model->name, $model->id);
|
|
self::assertFieldIsUnique('slug', Str::slug($model->name), $model->id);
|
|
|
|
// * TRANSFORMATIONS
|
|
self::setSlug($model, $model->name);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
/**
|
|
* Set the slug.
|
|
*
|
|
* @param self $model
|
|
* @param string $original
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function setSlug(self $model, string $original): void
|
|
{
|
|
$model->slug = Str::slug($original);
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
/**
|
|
* 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): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
|
|
}
|
|
|
|
/**
|
|
* Check if the limit of published models is reached.
|
|
*
|
|
* @param self $model
|
|
* @param boolean $isDeletion
|
|
* @return void
|
|
*/
|
|
private static function assertLimitPublishedModels(self $model, $isDeletion = false)
|
|
{
|
|
$count = self::query()->where('published', true)->count();
|
|
if (
|
|
(!$model->published && $count <= 1) ||
|
|
($model->published && $count >= 5) ||
|
|
($isDeletion && $count === 1)
|
|
) {
|
|
throw ValidationException::withMessages([
|
|
'Il doit y avoir entre 1 et 5 éléments publiés simultanément.'
|
|
]);
|
|
}
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* The aisles that belong to the logo.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function aisles(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Aisle::class);
|
|
}
|
|
}
|