2023-07-17 16:39:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Enums\Formularies\InputStatusEnum;
|
|
|
|
|
use App\Enums\Formularies\InputTypeEnum;
|
2025-06-17 12:29:15 +02:00
|
|
|
use App\Traits\Models\IsRelatedToTenant;
|
2023-07-17 16:39:18 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Support\Str;
|
2025-09-02 12:44:53 +02:00
|
|
|
use Stancl\Tenancy\Contracts\Tenant as BaseTenant;
|
2025-05-20 16:20:23 +02:00
|
|
|
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
|
2025-06-17 12:29:15 +02:00
|
|
|
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
|
2023-07-17 16:39:18 +02:00
|
|
|
|
|
|
|
|
/**
|
2025-05-26 11:33:47 +02:00
|
|
|
* @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.
|
2023-07-17 16:39:18 +02:00
|
|
|
*/
|
|
|
|
|
class Formulary extends Model
|
|
|
|
|
{
|
2025-05-20 16:20:23 +02:00
|
|
|
use BelongsToTenant;
|
2025-06-17 12:29:15 +02:00
|
|
|
use IsRelatedToTenant;
|
2023-07-17 16:39:18 +02:00
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-20 10:16:12 +02:00
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
2023-07-17 16:39:18 +02:00
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'type',
|
|
|
|
|
'name',
|
|
|
|
|
'slug',
|
|
|
|
|
'placeholder',
|
|
|
|
|
'details',
|
2023-08-23 09:44:51 +02:00
|
|
|
'is_visible',
|
2023-07-17 16:39:18 +02:00
|
|
|
'required',
|
|
|
|
|
'status',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
2025-05-20 10:16:12 +02:00
|
|
|
* @var array<string, string>
|
2023-07-17 16:39:18 +02:00
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
protected $casts = [
|
|
|
|
|
'required' => 'bool',
|
|
|
|
|
'is_visible' => 'bool',
|
2025-05-28 15:16:35 +02:00
|
|
|
// FIXME: update the json cast if api crash.
|
2025-05-20 10:16:12 +02:00
|
|
|
'placeholder' => 'json',
|
|
|
|
|
'details' => 'json',
|
|
|
|
|
'type' => InputTypeEnum::class,
|
|
|
|
|
'status' => InputStatusEnum::class,
|
2023-07-17 16:39:18 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The "booted" method of the model.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
2025-06-17 12:29:15 +02:00
|
|
|
// * ASSERTIONS
|
2025-06-18 11:52:45 +02:00
|
|
|
self::assertNameIsUnique($model->name, tenant: $model->tenant);
|
2025-06-17 12:29:15 +02:00
|
|
|
|
|
|
|
|
// * TRANSFORMATIONS
|
2025-05-20 10:16:12 +02:00
|
|
|
self::setSlug($model);
|
|
|
|
|
self::setStatusField($model);
|
|
|
|
|
self::setOrder($model);
|
|
|
|
|
self::makeRequiredSense($model);
|
2023-07-17 16:39:18 +02:00
|
|
|
});
|
|
|
|
|
static::updating(function (self $model) {
|
2025-06-17 12:29:15 +02:00
|
|
|
// * ASSERTIONS
|
2025-06-18 11:52:45 +02:00
|
|
|
self::assertNameIsUnique($model->name, $model->id, $model->tenant);
|
2025-06-17 12:29:15 +02:00
|
|
|
|
|
|
|
|
// * TRANSFORMATIONS
|
2025-05-20 10:16:12 +02:00
|
|
|
self::setSlug($model);
|
|
|
|
|
self::makeRequiredSense($model);
|
2023-07-17 16:39:18 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
2025-06-17 12:29:15 +02:00
|
|
|
/**
|
|
|
|
|
* Asserts using validation that the name is unique
|
|
|
|
|
*
|
2025-06-18 11:52:45 +02:00
|
|
|
* @param string|null $name
|
|
|
|
|
* @param integer|null $id
|
|
|
|
|
* @param \App\Models\Tenant|\Stancl\Tenancy\Contracts\Tenant|null $tenant
|
2025-06-17 12:29:15 +02:00
|
|
|
* @return void
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException If name already exists.
|
|
|
|
|
*/
|
2025-06-18 11:52:45 +02:00
|
|
|
private static function assertNameIsUnique(
|
|
|
|
|
string|null $name,
|
|
|
|
|
?int $id = null,
|
2025-09-02 12:44:53 +02:00
|
|
|
Tenant|BaseTenant|null $tenant = null
|
2025-06-18 11:52:45 +02:00
|
|
|
) {
|
2025-06-17 12:29:15 +02:00
|
|
|
$table = (new self())->getTable();
|
2025-06-18 11:52:45 +02:00
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id, self::whereTenantQuery($tenant));
|
|
|
|
|
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name), $id, self::whereTenantQuery($tenant));
|
2025-06-17 12:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-17 16:39:18 +02:00
|
|
|
/**
|
|
|
|
|
* Set the slug.
|
|
|
|
|
*
|
|
|
|
|
* @param self $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
private static function setSlug(self $model): void
|
2023-07-17 16:39:18 +02:00
|
|
|
{
|
|
|
|
|
$model->slug = Str::slug($model->name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 09:44:51 +02:00
|
|
|
/**
|
2025-05-20 10:16:12 +02:00
|
|
|
* Set required logical.
|
2023-08-23 09:44:51 +02:00
|
|
|
*
|
|
|
|
|
* @param self $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
private static function makeRequiredSense(self $model): void
|
2023-08-23 09:44:51 +02:00
|
|
|
{
|
|
|
|
|
if ($model->is_visible == false) {
|
|
|
|
|
$model->required = false;
|
|
|
|
|
\session()->flash(trans('Le champ ne peut être obligatoire si il n\'est pas visible'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-17 16:39:18 +02:00
|
|
|
/**
|
|
|
|
|
* Set the status field.
|
|
|
|
|
*
|
|
|
|
|
* @param self $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-05-20 10:16:12 +02:00
|
|
|
private static function setStatusField(self $model): void
|
2023-07-17 16:39:18 +02:00
|
|
|
{
|
|
|
|
|
if (!isset($model->status)) {
|
2025-05-20 10:16:12 +02:00
|
|
|
$model->status = InputStatusEnum::default;
|
2023-07-17 16:39:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set order after the last element of the list.
|
|
|
|
|
*
|
|
|
|
|
* @param self $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private static function setOrder(self $model): void
|
|
|
|
|
{
|
2025-05-26 11:33:47 +02:00
|
|
|
$model->order = \intval(
|
2025-06-17 12:29:15 +02:00
|
|
|
self::query()->where(self::whereTenantQuery())->max('order')
|
2025-05-26 11:33:47 +02:00
|
|
|
) + 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 = [
|
|
|
|
|
[
|
2025-08-28 18:34:10 +02:00
|
|
|
'name' => 'email',
|
2025-05-26 11:33:47 +02:00
|
|
|
'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();
|
|
|
|
|
});
|
2023-07-17 16:39:18 +02:00
|
|
|
}
|
|
|
|
|
}
|