129 lines
4.5 KiB
PHP
129 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\SettingTypesEnum;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
|
|
/**
|
|
* @property integer $id Settings id.
|
|
* @property string $key Settings key.
|
|
* @property string $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.
|
|
*/
|
|
class Setting extends Model
|
|
{
|
|
use BelongsToTenant;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'key',
|
|
'value',
|
|
'type',
|
|
'created_at',
|
|
'updated_at',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'type' => SettingTypesEnum::class
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
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(),
|
|
);
|
|
}
|
|
});
|
|
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(),
|
|
);
|
|
}
|
|
});
|
|
static::deleted(function (self $model) {
|
|
if (
|
|
$model->key === "SquaredLogo" ||
|
|
$model->key === "RectangleLogo" ||
|
|
$model->key === "BackgroundImage" ||
|
|
$model->key === "HomeButtonImage"
|
|
) {
|
|
FileHelper::removeFile($model, 'value');
|
|
}
|
|
});
|
|
}
|
|
|
|
// * 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();
|
|
});
|
|
}
|
|
}
|