2025-09-16 16:44:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2025-09-23 13:20:23 +02:00
|
|
|
use App\Contracts\TranslatableModel;
|
2025-09-16 16:44:49 +02:00
|
|
|
use App\Enums\DynamicPages\PageStatusEnum;
|
2025-09-23 13:20:23 +02:00
|
|
|
use App\Helpers\TranslationHelper;
|
2025-09-16 16:44:49 +02:00
|
|
|
use App\Traits\Models\HasTranslations;
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2025-10-29 10:29:57 +01:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2025-09-24 12:33:25 +02:00
|
|
|
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
2025-09-16 16:44:49 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @property string $label Label of the page configuration.
|
|
|
|
|
* @property string|null $view View of the page configuration.
|
|
|
|
|
* @property array $config Config of the page.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $created_at Date on which the page configuration was created.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Date on which the page configuration was updated.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $deleted_at Date on which the page configuration was updated.
|
|
|
|
|
*/
|
2025-09-23 13:20:23 +02:00
|
|
|
class PageConfig extends Model implements TranslatableModel
|
2025-09-16 16:44:49 +02:00
|
|
|
{
|
2025-09-24 12:33:25 +02:00
|
|
|
use ActivityLog;
|
2025-09-16 16:44:49 +02:00
|
|
|
use HasFactory;
|
|
|
|
|
use HasTranslations;
|
|
|
|
|
use SoftDeletes;
|
|
|
|
|
|
2025-10-29 10:29:57 +01:00
|
|
|
/**
|
|
|
|
|
* Default view of all dynamic pages.
|
|
|
|
|
*
|
|
|
|
|
* @var string $defaultView
|
|
|
|
|
*/
|
2025-11-07 19:00:09 +01:00
|
|
|
public static $defaultView = 'front.pages.dynamic-pages.default';
|
2025-10-29 10:29:57 +01:00
|
|
|
|
2025-09-16 16:44:49 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'label',
|
|
|
|
|
'view',
|
|
|
|
|
'config',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'config' => 'array',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The translatable columns.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
public $translatable = [
|
|
|
|
|
'label',
|
|
|
|
|
'config',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
2025-09-23 13:20:23 +02:00
|
|
|
static::creating(function (self $model) {
|
2025-09-30 15:51:54 +02:00
|
|
|
self::manageFields($model);
|
2025-09-23 13:20:23 +02:00
|
|
|
});
|
2025-10-29 10:29:57 +01:00
|
|
|
static::created(function () {
|
|
|
|
|
self::resetCache();
|
|
|
|
|
});
|
2025-09-23 13:20:23 +02:00
|
|
|
static::updating(function (self $model) {
|
2025-09-30 15:51:54 +02:00
|
|
|
self::manageFields($model);
|
2025-09-23 13:20:23 +02:00
|
|
|
});
|
2025-09-16 16:44:49 +02:00
|
|
|
static::updated(function (self $model) {
|
2025-10-29 10:29:57 +01:00
|
|
|
self::resetCache();
|
2025-09-16 16:44:49 +02:00
|
|
|
});
|
2025-09-23 13:20:23 +02:00
|
|
|
static::deleted(function (self $model) {
|
2025-09-16 16:44:49 +02:00
|
|
|
$model->pages()->delete();
|
2025-10-29 10:29:57 +01:00
|
|
|
self::resetCache();
|
|
|
|
|
});
|
|
|
|
|
static::restored(function () {
|
|
|
|
|
self::resetCache();
|
2025-09-16 16:44:49 +02:00
|
|
|
});
|
2025-09-26 15:08:37 +02:00
|
|
|
static::forceDeleting(function (self $model) {
|
|
|
|
|
$pages = $model->pages()->withTrashed()->get();
|
|
|
|
|
$pages->each(function ($page) {
|
|
|
|
|
$page->forceDelete();
|
|
|
|
|
});
|
2025-09-16 16:44:49 +02:00
|
|
|
});
|
2025-10-29 10:29:57 +01:00
|
|
|
static::forceDeleted(function () {
|
|
|
|
|
self::resetCache();
|
|
|
|
|
});
|
2025-09-16 16:44:49 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 15:51:54 +02:00
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manage model fields.
|
|
|
|
|
*
|
|
|
|
|
* @param self $model
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException Label already used.
|
|
|
|
|
*/
|
|
|
|
|
private static function manageFields(self $model): void
|
|
|
|
|
{
|
|
|
|
|
// Check unique fields.
|
|
|
|
|
$label = $model->label;
|
|
|
|
|
TranslationHelper::assertTranslationFieldIsUnique(
|
|
|
|
|
model: $model,
|
|
|
|
|
value: $label,
|
|
|
|
|
dbAttribute: 'label',
|
|
|
|
|
errorTranslation: trans('validation.custom.label')
|
|
|
|
|
);
|
|
|
|
|
$model->setAttribute('label', $label);
|
|
|
|
|
// Check translations.
|
|
|
|
|
TranslationHelper::validateFallbackTranslations($model);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 16:44:49 +02:00
|
|
|
/**
|
|
|
|
|
* Check if the config is updated.
|
|
|
|
|
*
|
|
|
|
|
* @param array $newArray
|
|
|
|
|
* @param array $originalArray
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
private static function isConfigUpdated(array $newArray, array $originalArray): bool
|
|
|
|
|
{
|
|
|
|
|
if (count($newArray) !== count($originalArray)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($newArray as $i => $subA) {
|
|
|
|
|
$subB = $originalArray[$i] ?? null;
|
|
|
|
|
if ($subB === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ksort($subA);
|
|
|
|
|
ksort($subB);
|
|
|
|
|
|
|
|
|
|
if ($subA != $subB) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 10:29:57 +01:00
|
|
|
/**
|
|
|
|
|
* Reset all cache elements related to this model.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function resetCache(): void
|
|
|
|
|
{
|
|
|
|
|
Cache::forget('publishedPageConfigs');
|
|
|
|
|
self::getPublishedPageConfigs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all published dynamic pages.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection<int,self>
|
|
|
|
|
*/
|
|
|
|
|
public static function getPublishedPageConfigs(): \Illuminate\Database\Eloquent\Collection
|
|
|
|
|
{
|
|
|
|
|
return Cache::remember('publishedPageConfigs', 86400, function () {
|
|
|
|
|
return self::query()
|
|
|
|
|
->with('pages')
|
|
|
|
|
->get();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 16:44:49 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get pages for the config.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\DynamicPage,$this>
|
|
|
|
|
*/
|
|
|
|
|
public function pages(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(DynamicPage::class, 'config_id');
|
|
|
|
|
}
|
|
|
|
|
}
|