ldc-boutique-cnc/app/Models/Range.php

155 lines
4.8 KiB
PHP
Raw Normal View History

2020-11-05 17:08:32 +01:00
<?php
namespace App\Models;
2022-10-03 17:13:44 +02:00
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2023-02-07 16:44:32 +01:00
use App\Traits\Models\HasFlashStatus;
2020-11-05 17:08:32 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2023-02-08 12:09:37 +01:00
use Illuminate\Support\Facades\Cache;
2022-10-03 17:13:44 +02:00
use Illuminate\Support\Str;
2020-11-05 17:08:32 +01:00
class Range extends Model
2020-11-05 17:08:32 +01:00
{
use HasFactory;
2023-02-07 16:44:32 +01:00
use HasFlashStatus;
2020-11-05 17:08:32 +01:00
2022-10-03 17:13:44 +02:00
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'status' => 'bool',
];
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name', 'description',
'picture', 'status',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
// * ASSERTIONS
static::assertNameIsUnique($model->name);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
OldSlugable::removeSameSlugs($model, $model->slug);
$model->description = clean($model->description);
$model->description = FileStorageHelper::storeModelFilesFromHtml($model, $model->description);
if ($model->picture) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
});
2023-02-07 16:44:32 +01:00
static::created(function (self $model) {
self::flashStatus($model);
2023-02-08 12:09:37 +01:00
Cache::delete('PublishedRangesGlobal');
2022-10-03 17:13:44 +02:00
});
static::updating(function (self $model) {
// * ASSERTIONS
static::assertNameIsUnique($model->name, $model->id);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
// * TRANSFORMATIONS
$model->slug = Str::slug($model->name);
if ($model->slug != $model->getOriginal('slug')) {
OldSlugable::removeSameSlugs($model, $model->getOriginal('slug'));
}
OldSlugable::insertSlug($model);
$model->description = clean($model->description);
$model->description = FileStorageHelper::storeModelFilesFromHtml($model, $model->description);
if ($model->picture) {
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
});
2023-02-07 16:44:32 +01:00
static::updated(function (self $model) {
2022-10-03 17:13:44 +02:00
FileStorageHelper::removeOldFile($model, 'picture');
FileStorageHelper::removeOldModelFilesFromHtml($model->description, $model->getOriginal('description'));
2023-02-07 16:44:32 +01:00
self::flashStatus($model, true);
2023-02-08 12:09:37 +01:00
Cache::delete('PublishedRangesGlobal');
2022-10-03 17:13:44 +02:00
});
static::deleted(function (self $model) {
$model->oldSlugs()->deleteOrFail();
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeModelFilesOfHtml($model->description);
});
}
// * RELATIONSHIPS
2022-09-03 16:56:16 +02:00
/**
* hasMany customer relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2022-08-25 12:26:43 +02:00
public function products()
{
return $this->hasMany('App\Models\Product', 'range_id');
2020-11-05 17:08:32 +01:00
}
2022-09-03 16:56:16 +02:00
/**
2022-10-03 17:13:44 +02:00
* MorphMany old_slugable relation
2022-09-03 16:56:16 +02:00
*
2022-10-03 17:13:44 +02:00
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
2022-09-03 16:56:16 +02:00
*/
public function oldSlugs()
2022-08-25 12:26:43 +02:00
{
2022-10-03 17:13:44 +02:00
return $this->morphMany(OldSlugable::class, 'old_slugable');
}
// * METHODS
/**
* Asserts using validation that the name is unique
*
* @param mixed $name
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If an actu name already exists.
*/
private static function assertNameIsUnique($name, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id);
2020-11-05 17:08:32 +01:00
}
2023-02-08 12:09:37 +01:00
// * - Cache
/**
* Get all available published ranges from cache.
* If cache was reset, it will fetch them from DB.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getCachedPublishedRanges()
{
if (!Cache::has('PublishedRangesGlobal')) {
self::setCachedPublishedRanges();
}
return Cache::get('PublishedRangesGlobal');
}
/**
* Put all published ranges into cache.
*
* @return void
*/
protected static function setCachedPublishedRanges(): void
{
Cache::put('PublishedRangesGlobal', self::where('status', true)->get());
}
2020-11-05 17:08:32 +01:00
}