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

196 lines
6.1 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Formularies\InputStatusEnum;
use App\Enums\Formularies\InputTypeEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
/**
* @property integer $id Formulary id.
* @property \App\Enums\Formularies\InputTypeEnum $type Formulary type.
* @property string $name Formulary name.
* @property string $slug Formulary slug.
* @property array<string,string> $placeholder Formulary placeholders.
* @property array<string,array<string,string>>|null $details Formuary details.
* @property string|null $tenant_id Associated tenant.
* @property boolean $is_visible Formulary visibility.
* @property boolean $required Formulary required status.
* @property \App\Enums\Formularies\InputStatusEnum $status Formulary status.
* @property integer $order Formulary order.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class Formulary extends Model
{
use BelongsToTenant;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'type',
'name',
'slug',
'placeholder',
'details',
'is_visible',
'required',
'status',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'required' => 'bool',
'is_visible' => 'bool',
'placeholder' => 'json',
'details' => 'json',
'type' => InputTypeEnum::class,
'status' => InputStatusEnum::class,
];
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $model) {
self::setSlug($model);
self::setStatusField($model);
self::setOrder($model);
self::makeRequiredSense($model);
});
static::updating(function (self $model) {
self::setSlug($model);
self::makeRequiredSense($model);
});
}
// * METHODS
/**
* Set the slug.
*
* @param self $model
* @return void
*/
private static function setSlug(self $model): void
{
$model->slug = Str::slug($model->name);
}
/**
* Set required logical.
*
* @param self $model
* @return void
*/
private static function makeRequiredSense(self $model): void
{
if ($model->is_visible == false) {
$model->required = false;
\session()->flash(trans('Le champ ne peut être obligatoire si il n\'est pas visible'));
}
}
/**
* Set the status field.
*
* @param self $model
* @return void
*/
private static function setStatusField(self $model): void
{
if (!isset($model->status)) {
$model->status = InputStatusEnum::default;
}
}
/**
* Set order after the last element of the list.
*
* @param self $model
* @return void
*/
private static function setOrder(self $model): void
{
$model->order = \intval(
self::query()->where('tenant_id', \tenant() ?? Tenant::first()->getKey())->max('order')
) + 1;
}
/**
* Set default settings for a specific tenant.
*
* @param \App\Models\Tenant $tenant
* @return void
*/
public function setDefaultFormularies(Tenant $tenant): void
{
// @phpcs:disable Generic.Files.LineLength.TooLong
$array = [
[
'name' => 'e-mail',
'placeholder' => ['fr' => 'E-mail'],
'details' => null,
'type' => InputTypeEnum::email,
'status' => InputStatusEnum::system,
'is_visible' => true,
'required' => true,
],
[
'name' => 'firstname',
'placeholder' => ['fr' => 'Prénom'],
'details' => null,
'type' => InputTypeEnum::text,
'status' => InputStatusEnum::locked,
'is_visible' => true,
'required' => true,
],
[
'name' => 'lastname',
'placeholder' => ['fr' => 'Nom'],
'details' => null,
'type' => InputTypeEnum::text,
'status' => InputStatusEnum::locked,
'is_visible' => true,
'required' => true,
],
[
'name' => 'phone',
'placeholder' => ['fr' => 'Téléphone'],
'details' => null,
'type' => InputTypeEnum::phone,
'status' => InputStatusEnum::locked,
'is_visible' => true,
'required' => true,
],
];
// @phpcs:enable
collect($array)->map(function (array $item) use ($tenant) {
$setting = new self();
$setting->name = $item['name'];
$setting->slug = Str::of($item['name'])->slug();
$setting->placeholder = $item['placeholder'];
$setting->details = $item['details'];
$setting->type = $item['type'];
$setting->status = $item['status'];
$setting->is_visible = $item['is_visible'];
$setting->required = $item['required'];
$setting->tenant_id = $tenant->getKey();
$setting->saveOrFail();
});
}
}