248 lines
8 KiB
PHP
248 lines
8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Contracts\TranslatableModel;
|
|
use App\Enums\DynamicFieldTypes;
|
|
use App\Enums\DynamicPages\PageStatusEnum;
|
|
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\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
|
|
|
|
/**
|
|
* @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 string $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',
|
|
];
|
|
|
|
/**
|
|
* 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();
|
|
self::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();
|
|
self::removeImgs($model);
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'title', $model->title);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($model->title), $model->getKey());
|
|
$model->setAttribute('slug', Str::slug($model->title));
|
|
self::validateFallbackTranslations($model);
|
|
}
|
|
|
|
/**
|
|
* Remove imgs from wysiwyg OR dynamic wysiwyg.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
public static function removeImgs(self $model): void
|
|
{
|
|
$model = $model->load('config');
|
|
if (!is_null($model->config)) {
|
|
collect($model->config->config)
|
|
->filter(fn(array $field) => $field['type'] === DynamicFieldTypes::text->value())
|
|
->map(function (array $field) use ($model) {
|
|
collect($model->getOriginal("content"))
|
|
->map(function (string|array $content, string $locale) use ($model, $field) {
|
|
// Need json decoding because $model->getAttributes() is an array.
|
|
$modelContent = json_decode($model->getAttributes()['content'], true);
|
|
if (isset($modelContent[$locale])) {
|
|
$modelContent = $modelContent[$locale];
|
|
$modelContent = $modelContent[$field["key"]] ?? "";
|
|
}
|
|
if (is_array($content)) {
|
|
$content = $content[$field["key"]] ?? "";
|
|
}
|
|
FileHelper::removeOldModelFilesFromHtml($modelContent, $content);
|
|
});
|
|
});
|
|
} else {
|
|
if (isset($model->getOriginal("content")[config('app.locale')])) {
|
|
FileHelper::removeOldModelFilesFromHtml(
|
|
$model->content,
|
|
$model->getOriginal("content")[config('app.locale')]
|
|
);
|
|
}
|
|
} //end if
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|