ldc-groupe/app/Models/PageConfig.php
2025-11-07 19:00:09 +01:00

194 lines
5.1 KiB
PHP

<?php
namespace App\Models;
use App\Contracts\TranslatableModel;
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 Uwil\LaravelActivityLogs\Traits\ActivityLog;
/**
* @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.
*/
class PageConfig extends Model implements TranslatableModel
{
use ActivityLog;
use HasFactory;
use HasTranslations;
use SoftDeletes;
/**
* Default view of all dynamic pages.
*
* @var string $defaultView
*/
public static $defaultView = 'front.pages.dynamic-pages.default';
/**
* 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
{
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();
});
static::deleted(function (self $model) {
$model->pages()->delete();
self::resetCache();
});
static::restored(function () {
self::resetCache();
});
static::forceDeleting(function (self $model) {
$pages = $model->pages()->withTrashed()->get();
$pages->each(function ($page) {
$page->forceDelete();
});
});
static::forceDeleted(function () {
self::resetCache();
});
}
// * 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);
}
/**
* 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;
}
/**
* 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();
});
}
// * 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');
}
}