2024-02-06 01:45:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Models;
|
|
|
|
|
2024-03-14 23:54:41 +01:00
|
|
|
use App\Form\Data\FieldCollection;
|
2024-03-15 01:52:27 +01:00
|
|
|
use App\Form\Data\FormConfigData;
|
2024-03-15 00:28:57 +01:00
|
|
|
use App\Form\Mails\ConfirmRegistrationMail;
|
2024-03-08 02:19:07 +01:00
|
|
|
use App\Form\Scopes\ParticipantFilterScope;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2024-02-06 01:45:25 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-02-08 21:04:00 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-03-08 02:19:07 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2024-03-15 00:28:57 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2024-02-06 01:45:25 +01:00
|
|
|
|
|
|
|
class Participant extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
public $guarded = [];
|
|
|
|
|
|
|
|
public $casts = [
|
|
|
|
'data' => 'json',
|
|
|
|
];
|
2024-02-08 21:04:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return BelongsTo<Form, self>
|
|
|
|
*/
|
|
|
|
public function form(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Form::class);
|
|
|
|
}
|
2024-03-08 02:19:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasMany<self>
|
|
|
|
*/
|
|
|
|
public function children(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Builder<self> $query
|
|
|
|
* @return Builder<self>
|
|
|
|
*/
|
|
|
|
public function scopeWithFilter(Builder $query, ParticipantFilterScope $filter): Builder
|
|
|
|
{
|
|
|
|
return $filter->apply($query);
|
|
|
|
}
|
2024-03-14 23:54:41 +01:00
|
|
|
|
|
|
|
public function getFields(): FieldCollection
|
|
|
|
{
|
|
|
|
return FieldCollection::fromRequest($this->form, $this->data);
|
|
|
|
}
|
2024-03-15 00:28:57 +01:00
|
|
|
|
2024-03-15 01:52:27 +01:00
|
|
|
public function getConfig(): FormConfigData
|
|
|
|
{
|
|
|
|
return tap($this->form->config, function ($config) {
|
|
|
|
$config->sections->each(function ($section) {
|
|
|
|
$section->fields->each(function ($field) {
|
|
|
|
$field->value = $this->getFields()->find($field)->value;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-15 00:28:57 +01:00
|
|
|
public function sendConfirmationMail(): void
|
|
|
|
{
|
|
|
|
if (!$this->getFields()->getMailRecipient()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-15 01:11:22 +01:00
|
|
|
Mail::to($this->getFields()->getMailRecipient())->queue(new ConfirmRegistrationMail($this));
|
2024-03-15 00:28:57 +01:00
|
|
|
}
|
2024-02-06 01:45:25 +01:00
|
|
|
}
|