ldc-com-app-bo/app/Models/Setting.php

130 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2023-04-17 17:46:20 +02:00
<?php
namespace App\Models;
use App\Enums\SettingTypesEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2025-05-20 16:20:23 +02:00
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
use Uwil\LaravelCommon\Helpers\FileHelper;
2023-04-17 17:46:20 +02:00
/**
* @property integer $id Settings id.
* @property string $key Settings key.
2026-10-20 16:03:44 +02:00
* @property string|null $value Settings value.
* @property string|null $tenant_id Associated tenant.
* @property \App\Enums\SettingTypesEnum $type Settings type.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
2023-04-17 17:46:20 +02:00
*/
class Setting extends Model
{
2025-05-20 16:20:23 +02:00
use BelongsToTenant;
2023-04-17 17:46:20 +02:00
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
2023-04-17 17:46:20 +02:00
*/
protected $fillable = [
'key',
'value',
'type',
'created_at',
'updated_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
2023-04-17 17:46:20 +02:00
*/
protected $casts = [
2023-04-17 17:46:20 +02:00
'type' => SettingTypesEnum::class
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
2023-04-17 17:46:20 +02:00
{
static::creating(function (self $model) {
if (
$model->key === "SquaredLogo" ||
$model->key === "RectangleLogo" ||
$model->key === "BackgroundImage" ||
$model->key === "HomeButtonImage"
) {
$model->value = FileHelper::storeFile(
model: $model,
file: $model->value,
slugify: true,
prefix: (\tenant() ?? Tenant::first())->getKey(),
);
}
});
2023-04-17 17:46:20 +02:00
static::updating(function (self $model) {
if (
$model->key === "SquaredLogo" ||
$model->key === "RectangleLogo" ||
$model->key === "BackgroundImage" ||
$model->key === "HomeButtonImage"
) {
$model->value = FileHelper::storeFile(
model: $model,
file: $model->value,
slugify: true,
prefix: (\tenant() ?? Tenant::first())->getKey(),
);
2023-04-17 17:46:20 +02:00
}
});
static::deleted(function (self $model) {
if (
$model->key === "SquaredLogo" ||
$model->key === "RectangleLogo" ||
$model->key === "BackgroundImage" ||
$model->key === "HomeButtonImage"
) {
FileHelper::removeFile($model, 'value');
2023-04-17 17:46:20 +02:00
}
});
}
// * METHODS
/**
* Set default settings for a specific tenant.
*
* @param \App\Models\Tenant $tenant
* @return void
*/
public function setDefaultSettings(Tenant $tenant): void
{
// @phpcs:disable Generic.Files.LineLength.TooLong
$array = [
['key' => 'MainColor', 'value' => '#FFF', 'type' => SettingTypesEnum::color],
['key' => 'SquaredLogo', 'value' => new \SplFileInfo(\resource_path('assets/settings/SquaredLogo.png')), 'type' => SettingTypesEnum::picture],
['key' => 'RectangleLogo', 'value' => new \SplFileInfo(\resource_path('assets/settings/RectangleLogo.png')), 'type' => SettingTypesEnum::picture],
['key' => 'BackgroundImage', 'value' => new \SplFileInfo(\resource_path('assets/settings/BackgroundImage.png')), 'type' => SettingTypesEnum::picture],
['key' => 'HomeButtonImage', 'value' => new \SplFileInfo(\resource_path('assets/settings/HomeButtonImage.png')), 'type' => SettingTypesEnum::picture],
['key' => 'Button1', 'value' => null, 'type' => SettingTypesEnum::directory],
['key' => 'Button2', 'value' => null, 'type' => SettingTypesEnum::directory],
['key' => 'Button3', 'value' => null, 'type' => SettingTypesEnum::directory],
];
// @phpcs:enable
collect($array)->map(function (array $item) use ($tenant) {
$setting = new self();
$setting->key = $item['key'];
$setting->value = $item['value'];
$setting->type = $item['type'];
$setting->tenant_id = $tenant->getKey();
$setting->saveOrFail();
});
}
2023-04-17 17:46:20 +02:00
}