217 lines
7 KiB
PHP
217 lines
7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\DynamicFieldTypes;
|
|
use App\Enums\DynamicPages\PageStatusEnum;
|
|
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\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
|
|
{
|
|
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 = [
|
|
'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);
|
|
self::validateTranslations($model);
|
|
});
|
|
static::created(function () {
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
self::validateTranslations($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
self::resetCache();
|
|
self::removeImgs($model);
|
|
});
|
|
static::deleted(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->slug = Str::slug($model->title);
|
|
}
|
|
|
|
/**
|
|
* Validate all translations required are defined.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException Translations missing.
|
|
*/
|
|
public static function validateTranslations(self $model): void
|
|
{
|
|
if (! $model->status->equals(PageStatusEnum::published)) {
|
|
return;
|
|
}
|
|
|
|
$locales = collect(config('app.locales'));
|
|
$modelLocales = collect(array_keys($model->config->getTranslations('config')));
|
|
$allowedLocales = $modelLocales->intersect($locales);
|
|
|
|
$missingLocales = $allowedLocales->reject(
|
|
fn($locale) => $model->hasTranslation('content', $locale)
|
|
);
|
|
|
|
if ($missingLocales->isNotEmpty()) {
|
|
throw ValidationException::withMessages([
|
|
'content' => trans_choice(
|
|
'back.missing_translations',
|
|
$missingLocales->count() > 2 ? \INF : 1,
|
|
['locales' => Str::upper($missingLocales->implode(', '))]
|
|
),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
FileHelper::removeOldModelFilesFromHtml(
|
|
$model->content,
|
|
$model->getOriginal("content")[config('app.locale')]
|
|
);
|
|
} //end if
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|