ldc-showroom-bo/app/Models/Settings.php
2025-05-27 09:11:43 +02:00

116 lines
3.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 Stancl\Tenancy\Database\Concerns\BelongsToTenant;
use Uwil\LaravelCommon\Helpers\FileHelper;
/**
* @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
$model->logo = FileHelper::storeFile($model, $model->logo, true, prefix: $model->tenant_id);
});
static::updating(function (self $model) {
// * TRANSFORMATIONS
$model->logo = FileHelper::storeFile($model, $model->logo, true, prefix: $model->tenant_id);
});
static::updated(function (self $model) {
FileHelper::removeOldFile($model, 'logo');
});
static::deleted(function (self $model) {
FileHelper::removeFile($model, 'logo');
});
}
/**
* 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();
}
}