135 lines
6.9 KiB
PHP
135 lines
6.9 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' => 'version', 'value' => '1', 'type' => SettingTypesEnum::numeric],
|
|
['key' => 'ShowIsActive', 'value' => '0', 'type' => SettingTypesEnum::boolean],
|
|
['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],
|
|
['key' => 'AppStoreUUID', 'value' => 'c4ac09fd-3992-47bc-bdee-0b7f5505e873', 'type' => SettingTypesEnum::string],
|
|
['key' => 'FormSelect1', 'value' => '{"title":{"fr":"Travaille avec","en":"Work with","de":"Arbeit mit"},"values":{"pole-sable":{"fr":"P\u00f4le Sabl\u00e9","en":"P\u00f4le Sabl\u00e9","de":"P\u00f4le Sabl\u00e9"},"snv":{"fr":"SNV","en":"SNV","de":"SNV"},"pole-sud-est":{"fr":"P\u00f4le Sud-Est","en":"P\u00f4le Sud-Est","de":"P\u00f4le Sud-Est"},"aquitaine":{"fr":"Aquitaine","en":"Aquitaine","de":"Aquitaine"},"sbv":{"fr":"SBV","en":"SBV","de":"SBV"},"arrive":{"fr":"Arriv\u00e9","en":"Arriv\u00e9","de":"Arriv\u00e9"},"plusieurs-filiales":{"fr":"Plusieurs filiales","en":"Several subsidiaries","de":"Mehrere Filialen"},"autre":{"fr":"Autre","en":"Other","de":"Andere"}}}', 'type' => SettingTypesEnum::json],
|
|
['key' => 'FormSelect2', 'value' => '{"title":{"fr":"Type de commerce","en":"Type of business","de":"Art des Gesch\u00e4fts"},"values":{"commerce-de-detail":{"fr":"Commerce de d\u00e9tail","en":"Retail trade","de":"Einzelhandel"},"restauration-commerciale":{"fr":"Restauration Commerciale","en":"Commercial catering","de":"Kommerzielle Restauration"},"restauration-collective":{"fr":"Restauration Collective","en":"Collective catering","de":"Kollektiv Restauration"},"grossiste-distributeur-cash":{"fr":"Grossiste Distributeur Cash","en":"Wholesale Distributor Cash","de":"Gro\u00dfh\u00e4ndler H\u00e4ndler Cash"},"pai":{"fr":"PAI","en":"PAI","de":"PAI"},"home-freezer-center":{"fr":"Home Freezer Center","en":"Home Freezer Center","de":"Home Gefrierzentrum"},"autres":{"fr":"Autre","en":"Other","de":"Andere"}}}', 'type' => SettingTypesEnum::json],
|
|
['key' => 'FormCheck', 'value' => '[{"fr":"Frais","en":"Fresh","de":"Frisch"},{"fr":"Surgel\u00e9s","en":"Frozen food","de":"Tiefk\u00fchlkost"},{"fr":"Volailles crues","en":"Raw poultry","de":"Rohes Gefl\u00fcgel"},{"fr":"\u00c9labor\u00e9s crus","en":"Prepared raw","de":"Roh ausgearbeitet"},{"fr":"\u00c9labor\u00e9s cuits","en":"Cooked products","de":"Gekochte Elaborate"},{"fr":"Bio et Label Rouge","en":"Bio and Label Rouge","de":"Bio und Label Rouge"},{"fr":"BBC","en":"BBC","de":"BBC"},{"fr":"Halal","en":"Halal","de":"Halal"},{"fr":"Autre","en":"Other","de":"Andere"}]', 'type' => SettingTypesEnum::json],
|
|
];
|
|
// @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();
|
|
});
|
|
}
|
|
}
|