You've already forked Atomcms-edit
130 lines
4.1 KiB
PHP
Executable File
130 lines
4.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Miscellaneous;
|
|
|
|
use App\Services\CacheService;
|
|
use App\Services\SettingsService;
|
|
use Database\Factories\Miscellaneous\WebsiteSettingFactory;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting where($column, $operator = null, $value = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting whereIn(string $column, array $values)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting whereNotIn(string $column, array $values)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting orWhere($column, $operator = null, $value = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting count($columns = '*')
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting get($columns = ['*'])
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting first()
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting updateOrCreate(array $attributes, array $values = [])
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting pluck($column, $key = null)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|WebsiteSetting exists()
|
|
*
|
|
* @property string $key
|
|
* @property string|null $value
|
|
*/
|
|
class WebsiteSetting extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected static function newFactory()
|
|
{
|
|
return WebsiteSettingFactory::new();
|
|
}
|
|
|
|
#[\Override]
|
|
protected $guarded = ['id'];
|
|
|
|
public static function updateOrCreate(array $attributes, array $values = []): Builder|Model
|
|
{
|
|
$instance = static::firstOrNew($attributes);
|
|
|
|
if (! $instance->getKey()) {
|
|
$instance->forceFill(array_merge($attributes, $values))->save();
|
|
} else {
|
|
$instance->fill($values)->save();
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
|
|
public static function firstOrCreate(array $attributes, array $values = []): Model|static
|
|
{
|
|
$instance = static::firstOrNew($attributes);
|
|
|
|
if (! $instance->getKey()) {
|
|
$instance->forceFill(array_merge($attributes, $values))->save();
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
|
|
#[\Override]
|
|
public $timestamps = false;
|
|
|
|
#[\Override]
|
|
protected static function booted(): void
|
|
{
|
|
static::saved(function (WebsiteSetting $setting) {
|
|
SettingsService::clearCache();
|
|
self::clearRelatedCache($setting->key);
|
|
});
|
|
static::deleted(function (WebsiteSetting $setting) {
|
|
SettingsService::clearCache();
|
|
self::clearRelatedCache($setting->key);
|
|
});
|
|
}
|
|
|
|
private static function clearRelatedCache(?string $key): void
|
|
{
|
|
if ($key === null) {
|
|
CacheService::clearAllAppCache();
|
|
|
|
return;
|
|
}
|
|
|
|
if (str_starts_with($key, 'points_')) {
|
|
CacheService::clearPointsSettings();
|
|
CacheService::clearLeaderboards();
|
|
CacheService::clearApiCache();
|
|
|
|
return;
|
|
}
|
|
|
|
if (str_starts_with($key, 'ai_filter_') || str_starts_with($key, 'filter_word_')) {
|
|
CacheService::clearContentModeration();
|
|
|
|
return;
|
|
}
|
|
|
|
if (str_starts_with($key, 'radio_')) {
|
|
CacheService::clearRadioSettings();
|
|
CacheService::clearLeaderboards();
|
|
CacheService::clearApiCache();
|
|
CacheService::clearLeaderboard();
|
|
|
|
return;
|
|
}
|
|
|
|
if (str_starts_with($key, 'rare_value_')) {
|
|
CacheService::clearRareValueCategories();
|
|
|
|
return;
|
|
}
|
|
|
|
if (str_starts_with($key, 'staff_')) {
|
|
CacheService::clearStaff();
|
|
CacheService::clearLeaderboard();
|
|
|
|
return;
|
|
}
|
|
|
|
if (str_starts_with($key, 'help_center') || str_starts_with($key, 'website_rules')) {
|
|
CacheService::clearHelpCenter();
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|