208 lines
6.3 KiB
PHP
208 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\StaticPageType;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class StaticPage extends Model
|
|
{
|
|
use CastsEnums;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'date_start' => 'datetime',
|
|
'date_end' => 'datetime'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast to Enums.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'type' => StaticPageType::class,
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'value',
|
|
'banner'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
// * EVENT CLOSURES.
|
|
static::creating(function (self $model) {
|
|
if (!$model->type->equals(StaticPageType::crud())) {
|
|
throw new \RuntimeException('Impossible de créer :model de type autre que :type.', [
|
|
'model' => \trans('models.classes.static_pages'),
|
|
'type' => StaticPageType::crud()->label
|
|
]);
|
|
}
|
|
// * ASSERTIONS
|
|
static::assertTitleIsUnique($model->title);
|
|
ToolboxHelper::assertNoInternalBoUrls('value', $model->value);
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->title);
|
|
$model->value = clean($model->value);
|
|
$model->banner = FileStorageHelper::storeFile($model, $model->banner, true);
|
|
$model->value = FileStorageHelper::storeModelFilesFromHtml($model, $model->value);
|
|
});
|
|
static::created(function () {
|
|
Cache::forget('StaticPageGlobal');
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * ASSERTIONS
|
|
static::assertTitleIsUnique($model->title, $model->id);
|
|
ToolboxHelper::assertNoInternalBoUrls('value', $model->value);
|
|
// * TRANSFORMATIONS
|
|
$model->slug = Str::slug($model->title);
|
|
$model->value = clean($model->value);
|
|
$model->banner = FileStorageHelper::storeFile($model, $model->banner, true);
|
|
$model->value = FileStorageHelper::storeModelFilesFromHtml($model, $model->value);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'banner');
|
|
FileStorageHelper::removeOldModelFilesFromHtml($model->value, $model->getOriginal('value'));
|
|
Cache::forget('StaticPageGlobal');
|
|
});
|
|
static::deleting(function (self $model) {
|
|
if (!$model->type->equals(StaticPageType::crud())) {
|
|
throw new \RuntimeException('Impossible de supprimer :model de type autre que :type.', [
|
|
'model' => \trans('models.classes.static_pages'),
|
|
'type' => StaticPageType::crud()->label
|
|
]);
|
|
}
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'banner');
|
|
FileStorageHelper::removeModelFilesOfHtml($model->value);
|
|
Cache::forget('StaticPageGlobal');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the route key for the model.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getRouteKeyName()
|
|
{
|
|
return 'slug';
|
|
}
|
|
|
|
// * 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);
|
|
}
|
|
|
|
/**
|
|
* Get page of a specific type
|
|
*
|
|
* @param \App\Enums\StaticPageType $type
|
|
* @return \App\Models\StaticPage|null
|
|
* @throws \RuntimeException If type is not supported.
|
|
*/
|
|
public static function getPageOfType(StaticPageType $type)
|
|
{
|
|
if ($type->equals(StaticPageType::crud())) {
|
|
throw new \RuntimeException('Unsupported type crud');
|
|
}
|
|
return self::query()->where('type', $type)->first();
|
|
}
|
|
|
|
/**
|
|
* Get Route for a specific model type
|
|
*
|
|
* @param \App\Enums\StaticPageType $type
|
|
* @return string|null
|
|
*/
|
|
public static function getRouteForType(StaticPageType $type): ?string
|
|
{
|
|
if ($type->equals(StaticPageType::crud())) {
|
|
return null;
|
|
}
|
|
return route('static-page.view', \optional(self::getCachedPages()
|
|
->where('type', $type)->first())->slug);
|
|
}
|
|
|
|
/**
|
|
* Get Bo Route for a specific model type
|
|
*
|
|
* @param \App\Enums\StaticPageType $type
|
|
* @param string $action
|
|
* @return string|null
|
|
*/
|
|
public static function getBoRouteForType(StaticPageType $type, string $action = 'edit'): ?string
|
|
{
|
|
if (!$type->equals(StaticPageType::crud())) {
|
|
return route(
|
|
"bo.static_pages.$action",
|
|
\optional(self::getCachedPages()->where('type', $type)->first())->type
|
|
);
|
|
}
|
|
if (\in_array($action, ['index', 'create'])) {
|
|
route("bo.static_pages.$action");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get all available pages from cache.
|
|
* If cache was reset, it will fetch them from DB.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection<self>
|
|
*/
|
|
public static function getCachedPages()
|
|
{
|
|
if (!Cache::has('StaticPageGlobal')) {
|
|
self::setCachedPages();
|
|
}
|
|
return Cache::get('StaticPageGlobal');
|
|
}
|
|
|
|
/**
|
|
* Put all available pages into cache.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function setCachedPages(): void
|
|
{
|
|
$pages = self::all();
|
|
Cache::put('StaticPageGlobal', $pages);
|
|
}
|
|
}
|