ldc-boutique-cnc/app/Models/StaticPage.php
2022-11-08 15:41:10 +01:00

178 lines
5.1 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) {
// * 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::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
*/
public static function getPageOfType(StaticPageType $type)
{
return self::whereType($type)->first();
}
/**
* Get Route for a specific model type
*
* @param \App\Enums\StaticPageType $type
* @return string
*/
public static function getRouteForType(StaticPageType $type): string
{
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
*/
public static function getBoRouteForType(StaticPageType $type, string $action = 'edit'): string
{
return route("bo.static_page.$action", \optional(self::getCachedPages()
->where('type', $type)->first())->type);
}
/**
* 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);
}
}