249 lines
7.5 KiB
PHP
249 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Contracts\TranslatableModel;
|
|
use App\Enums\DynamicFieldTypes;
|
|
use App\Enums\DynamicPages\PageStatusEnum;
|
|
use App\Helpers\DynamicPageHelper;
|
|
use App\Helpers\TranslationHelper;
|
|
use App\Traits\Models\HasTranslations;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
|
|
/**
|
|
* @property string $title Title of the dynamic page.
|
|
* @property string $slug Slug of the dynamic page.
|
|
* @property string $content Content of the dynamic page.
|
|
* @property integer|null $config_id Config associated to the dynamic page.
|
|
* @property \App\Enums\DynamicPages\PageStatusEnum $status Status of the dynamic page.
|
|
* @property boolean $isLocked Locked status of dynamic page.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Date on which the dynamic page was created.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Date on which the dynamic page was updated.
|
|
* @property-read \Illuminate\Support\Carbon $deleted_at Date on which the dynamic page was updated.
|
|
*/
|
|
class DynamicPage extends Model implements TranslatableModel
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'slug',
|
|
'content',
|
|
'config_id',
|
|
'status',
|
|
'isLocked',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'content' => 'array',
|
|
'status' => PageStatusEnum::class,
|
|
'isLocked' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* The translatable columns.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
public $translatable = [
|
|
'slug',
|
|
'title',
|
|
'content',
|
|
];
|
|
|
|
public function inertiaFill(array $attributes)
|
|
{
|
|
if (!empty($attributes['content'])) {
|
|
$attributes['content'] = collect($attributes['content'])
|
|
->map(function ($value, $key) {
|
|
if (
|
|
Str::startsWith($key, DynamicFieldTypes::image->name . '_') &&
|
|
$value instanceof UploadedFile
|
|
) {
|
|
return FileHelper::storeFile($this, $value);
|
|
}
|
|
return $value;
|
|
})
|
|
->all();
|
|
}
|
|
parent::fill($attributes);
|
|
}
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
self::resetCache();
|
|
DynamicPageHelper::removeImgs($model);
|
|
});
|
|
static::deleted(function () {
|
|
self::resetCache();
|
|
});
|
|
static::restoring(function (self $model) {
|
|
if ($model->config_id && $model->config()->onlyTrashed()->exists()) {
|
|
throw ValidationException::withMessages([
|
|
'error' => __('crud.messages.cannot_be_restored', [
|
|
'model' => Str::ucfirst(trans_choice('models.classes.dynamic-page', 1))
|
|
]),
|
|
]);
|
|
}
|
|
self::resetCache();
|
|
$model->status = PageStatusEnum::draft;
|
|
});
|
|
static::forceDeleted(function (self $model) {
|
|
self::resetCache();
|
|
DynamicPageHelper::removeImgs($model, true);
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
// Check unique fields.
|
|
$slug = Str::slug($model->title);
|
|
TranslationHelper::assertTranslationFieldIsUnique($model, $model->title, 'title');
|
|
TranslationHelper::assertTranslationFieldIsUnique($model, $slug, 'slug', 'title');
|
|
$model->setAttribute('slug', $slug);
|
|
// Check translations.
|
|
self::validateFallbackTranslations($model);
|
|
}
|
|
|
|
/**
|
|
* Concat all wysiwyg strings in only one.
|
|
*
|
|
* @param string|array|null $content
|
|
* @return string
|
|
*/
|
|
private static function flattenContent(string|array|null $content): string
|
|
{
|
|
if (is_string($content)) {
|
|
return $content;
|
|
}
|
|
if (is_array($content)) {
|
|
return implode("\n", array_filter($content, fn($val) => is_string($val)));
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Assert fallback locale translation is present.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException Translations missing.
|
|
*/
|
|
public static function validateFallbackTranslations(self $model): void
|
|
{
|
|
if (! $model->status->equals(PageStatusEnum::published)) {
|
|
return;
|
|
}
|
|
TranslationHelper::validateFallbackTranslations($model);
|
|
}
|
|
|
|
/**
|
|
* Get the view of a specific dynamic page.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getView(): string
|
|
{
|
|
/** @var string $view The view used for the route. */
|
|
$view = 'front.pages.dynamic-page-default';
|
|
|
|
// Check if the view in config is defined and can be used.
|
|
if (!empty($this->config->view)) {
|
|
$candidate = sprintf('front.pages.%s', $this->config->view);
|
|
if (view()->exists($candidate)) {
|
|
$view = $candidate;
|
|
}
|
|
}
|
|
|
|
// Check if a view with the slug exists.
|
|
if ($view === 'dynamic-page-default' && !empty($this->slug)) {
|
|
$candidate = sprintf('front.pages.%s', $this->slug);
|
|
if (view()->exists($candidate)) {
|
|
$view = $candidate;
|
|
}
|
|
}
|
|
return $view;
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function resetCache(): void
|
|
{
|
|
Cache::forget('publishedDynamicPages');
|
|
self::getPublishedDynamicPages();
|
|
}
|
|
|
|
/**
|
|
* Get all published dynamic pages.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection<int,self>
|
|
*/
|
|
public static function getPublishedDynamicPages(): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return Cache::remember('publishedDynamicPages', 86400, function () {
|
|
return self::query()
|
|
->where('status', PageStatusEnum::published)
|
|
->with('config')
|
|
->get();
|
|
});
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get the config for the page.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\PageConfig,$this>
|
|
*/
|
|
public function config(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(PageConfig::class);
|
|
}
|
|
}
|