191 lines
5.4 KiB
PHP
191 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\Models\ActivityLog;
|
|
use App\Traits\Models\HasTranslations;
|
|
use App\Traits\Models\IsCachedAsCollection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
use App\Helpers\ShowroomFileHelper;
|
|
|
|
/**
|
|
* @property string $sub_title Sous-titre de l'application'.
|
|
* @property string $footer_phrase Texte en bas de page.
|
|
* @property string $color HTML Color string.
|
|
* @property string $logo Logo path for the app.
|
|
* @property integer $home_timeout Timeout time in seconds before returning to homepage.
|
|
* @property string $tenant_id Id of the related tenant.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*
|
|
*/
|
|
class Settings extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
use IsCachedAsCollection;
|
|
use BelongsToTenant;
|
|
use ActivityLog;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'sub_title',
|
|
'footer_phrase',
|
|
'color',
|
|
'logo',
|
|
'home_timeout',
|
|
'tenant_id',
|
|
];
|
|
|
|
/**
|
|
* Translatable fields array
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
public $translatable = [
|
|
'sub_title',
|
|
'footer_phrase',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
// * TRANSFORMATIONS
|
|
/** @phpstan-ignore-next-line */
|
|
if ($model->logo instanceof UploadedFile) {
|
|
$model->logo = ShowroomFileHelper::storeFileWithUUIDName($model->logo, $model);
|
|
}
|
|
});
|
|
static::created(function (self $model) {
|
|
self::resetCache($model->tenant_id);
|
|
});
|
|
static::updating(function (self $model) {
|
|
// * TRANSFORMATIONS
|
|
/** @phpstan-ignore-next-line */
|
|
if ($model->logo instanceof UploadedFile) {
|
|
$model->logo = ShowroomFileHelper::storeFileWithUUIDName($model->logo, $model);
|
|
}
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileHelper::removeOldFile($model, 'logo');
|
|
self::resetCache($model->tenant_id);
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileHelper::removeFile($model, 'logo');
|
|
self::removeFromCache($model->tenant_id);
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Get from cache a Setting model with a key like this : {tenant}_settings and for remember it 24 hours.
|
|
*
|
|
* @param string|null $tenant_id
|
|
*
|
|
* @return self|null
|
|
*/
|
|
public static function getFromCache(?string $tenant_id): self|null
|
|
{
|
|
if (!$tenant_id) {
|
|
return null;
|
|
};
|
|
|
|
return Cache::remember(
|
|
self::getCacheKey($tenant_id),
|
|
86400,
|
|
function () use ($tenant_id) {
|
|
return self::query()->where('tenant_id', $tenant_id)->first();
|
|
},
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to a given tenant id.
|
|
*
|
|
* @param string $tenant_id
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(string $tenant_id): void
|
|
{
|
|
Cache::forget(self::getCacheKey($tenant_id));
|
|
self::getFromCache($tenant_id);
|
|
}
|
|
|
|
/**
|
|
* Remove from cache a Setting model.
|
|
*
|
|
* @param string $tenant_id
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function removeFromCache(string $tenant_id): void
|
|
{
|
|
Cache::forget(self::getCacheKey($tenant_id));
|
|
}
|
|
|
|
/**
|
|
* Get the cache key for a setting related to its tenant.
|
|
*
|
|
* @param string $tenant_id
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function getCacheKey(string $tenant_id): string
|
|
{
|
|
return \sprintf('%s_settings', $tenant_id);
|
|
}
|
|
|
|
/**
|
|
* Create a new Setting with fake datas.
|
|
*
|
|
* @param string $tenantId The ID of the related tenant.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function createNewData(string $tenantId)
|
|
{
|
|
$settings = new Settings();
|
|
$settings->logo = FileHelper::storeFile(
|
|
new Settings(),
|
|
new \SplFileInfo(
|
|
\resource_path('../database/factories/assets/settings/default-picture-200x200.jpg')
|
|
),
|
|
prefix: $tenantId
|
|
);
|
|
$settings->setTranslations('sub_title', [
|
|
'fr' => 'FR',
|
|
'nl' => 'NL',
|
|
'en' => 'EN',
|
|
'es' => 'ES',
|
|
'de' => 'DE',
|
|
]);
|
|
|
|
$settings->setTranslations('footer_phrase', [
|
|
'fr' => 'FR',
|
|
'nl' => 'NL',
|
|
'en' => 'EN',
|
|
'es' => 'ES',
|
|
'de' => 'DE',
|
|
]);
|
|
$settings->color = '#000000';
|
|
$settings->home_timeout = 60;
|
|
$settings->tenant_id = $tenantId;
|
|
$settings->saveOrFail();
|
|
}
|
|
}
|