2023-12-27 22:54:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Models;
|
|
|
|
|
2024-02-06 01:45:25 +01:00
|
|
|
use App\Form\Fields\Field;
|
2024-01-12 22:52:42 +01:00
|
|
|
use Cviebrock\EloquentSluggable\Sluggable;
|
2023-12-27 22:54:58 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-02-06 01:45:25 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2024-03-01 03:00:33 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2024-02-06 01:45:25 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2024-01-29 22:07:33 +01:00
|
|
|
use Laravel\Scout\Searchable;
|
2024-01-19 18:16:57 +01:00
|
|
|
use Spatie\Image\Manipulations;
|
2024-01-12 23:29:18 +01:00
|
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
2024-01-19 18:16:57 +01:00
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
2024-01-12 23:29:18 +01:00
|
|
|
use Zoomyboy\MedialibraryHelper\DefersUploads;
|
2023-12-27 22:54:58 +01:00
|
|
|
|
2024-01-12 23:29:18 +01:00
|
|
|
class Form extends Model implements HasMedia
|
2023-12-27 22:54:58 +01:00
|
|
|
{
|
|
|
|
use HasFactory;
|
2024-01-12 22:52:42 +01:00
|
|
|
use Sluggable;
|
2024-01-12 23:29:18 +01:00
|
|
|
use InteractsWithMedia;
|
|
|
|
use DefersUploads;
|
2024-01-29 22:07:33 +01:00
|
|
|
use Searchable;
|
2023-12-27 22:54:58 +01:00
|
|
|
|
|
|
|
public $guarded = [];
|
|
|
|
|
|
|
|
public $casts = [
|
|
|
|
'config' => 'json',
|
2024-02-21 22:44:52 +01:00
|
|
|
'meta' => 'json',
|
2024-02-02 01:05:45 +01:00
|
|
|
'description' => 'json',
|
2023-12-27 22:54:58 +01:00
|
|
|
];
|
2023-12-31 21:46:52 +01:00
|
|
|
|
2024-02-06 01:45:25 +01:00
|
|
|
/** @var array<int, string> */
|
|
|
|
public $dates = ['from', 'to', 'registration_from', 'registration_until'];
|
|
|
|
|
2024-01-14 13:51:48 +01:00
|
|
|
/**
|
|
|
|
* @return SluggableConfig
|
|
|
|
*/
|
2024-01-12 22:52:42 +01:00
|
|
|
public function sluggable(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'slug' => ['source' => ['name']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-06 01:45:25 +01:00
|
|
|
/**
|
2024-02-08 23:13:59 +01:00
|
|
|
* @return HasMany<Participant>
|
2024-02-06 01:45:25 +01:00
|
|
|
*/
|
|
|
|
public function participants(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Participant::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-12 23:29:18 +01:00
|
|
|
public function registerMediaCollections(): void
|
|
|
|
{
|
|
|
|
$this->addMediaCollection('headerImage')
|
|
|
|
->singleFile()
|
|
|
|
->maxWidth(fn () => 500)
|
2024-01-19 18:16:57 +01:00
|
|
|
->forceFileName(fn (Form $model, string $name) => $model->slug)
|
|
|
|
->registerMediaConversions(function (Media $media) {
|
|
|
|
$this->addMediaConversion('square')->fit(Manipulations::FIT_CROP, 400, 400);
|
|
|
|
});
|
2024-01-12 23:29:18 +01:00
|
|
|
}
|
|
|
|
|
2024-02-06 01:45:25 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
|
|
|
public function getRegistrationRules(): array
|
|
|
|
{
|
|
|
|
return $this->getFields()->reduce(function ($carry, $current) {
|
|
|
|
$field = Field::fromConfig($current);
|
|
|
|
|
|
|
|
return [
|
|
|
|
...$carry,
|
2024-02-19 02:10:58 +01:00
|
|
|
...$field->getRegistrationRules($this),
|
2024-02-06 01:45:25 +01:00
|
|
|
];
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
|
|
|
public function getRegistrationMessages(): array
|
|
|
|
{
|
|
|
|
return $this->getFields()->reduce(function ($carry, $current) {
|
|
|
|
$field = Field::fromConfig($current);
|
|
|
|
|
|
|
|
return [
|
|
|
|
...$carry,
|
2024-02-19 02:10:58 +01:00
|
|
|
...$field->getRegistrationMessages($this),
|
2024-02-06 01:45:25 +01:00
|
|
|
];
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
|
|
|
public function getRegistrationAttributes(): array
|
|
|
|
{
|
|
|
|
return $this->getFields()->reduce(function ($carry, $current) {
|
|
|
|
$field = Field::fromConfig($current);
|
|
|
|
|
|
|
|
return [
|
|
|
|
...$carry,
|
2024-02-19 02:10:58 +01:00
|
|
|
...$field->getRegistrationAttributes($this),
|
2024-02-06 01:45:25 +01:00
|
|
|
];
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Collection<string, mixed>
|
|
|
|
*/
|
|
|
|
public function getFields(): Collection
|
|
|
|
{
|
|
|
|
return collect($this->config['sections'])->reduce(fn ($carry, $current) => $carry->merge($current['fields']), collect([]));
|
|
|
|
}
|
|
|
|
|
2024-01-12 22:52:42 +01:00
|
|
|
|
2024-01-29 22:07:33 +01:00
|
|
|
// --------------------------------- Searching ---------------------------------
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2024-01-12 22:52:42 +01:00
|
|
|
/**
|
2024-01-29 22:07:33 +01:00
|
|
|
* Get the indexable data array for the model.
|
2024-01-12 22:52:42 +01:00
|
|
|
*
|
2024-01-29 22:07:33 +01:00
|
|
|
* @return array<string, mixed>
|
2024-01-12 22:52:42 +01:00
|
|
|
*/
|
2024-01-29 22:07:33 +01:00
|
|
|
public function toSearchableArray()
|
2024-01-12 22:52:42 +01:00
|
|
|
{
|
2024-01-29 22:07:33 +01:00
|
|
|
return [
|
|
|
|
'from' => $this->from->timestamp,
|
|
|
|
'to' => $this->to->timestamp,
|
|
|
|
'name' => $this->name,
|
|
|
|
];
|
2024-01-12 22:52:42 +01:00
|
|
|
}
|
2024-02-08 23:09:51 +01:00
|
|
|
|
|
|
|
public static function booted(): void
|
|
|
|
{
|
|
|
|
static::saving(function (self $model) {
|
2024-02-21 22:44:52 +01:00
|
|
|
if (is_null($model->meta)) {
|
|
|
|
$model->setAttribute('meta', [
|
2024-02-22 00:06:16 +01:00
|
|
|
'active_columns' => $model->getFields()->count() ? $model->getFields()->take(4)->pluck('key')->toArray() : null,
|
|
|
|
'sorting' => $model->getFields()->count() ? [$model->getFields()->first()['key'], 'asc'] : null,
|
2024-02-21 22:44:52 +01:00
|
|
|
]);
|
2024-02-08 23:09:51 +01:00
|
|
|
}
|
2024-03-01 03:00:33 +01:00
|
|
|
|
|
|
|
if (is_array(data_get($model->meta, 'active_columns'))) {
|
|
|
|
$model->setAttribute('meta', [
|
|
|
|
...$model->meta,
|
|
|
|
'active_columns' => array_diff($model->getFields()->pluck('key')->toArray(), $model->meta['active_columns']),
|
|
|
|
]);
|
|
|
|
}
|
2024-02-08 23:09:51 +01:00
|
|
|
});
|
|
|
|
}
|
2023-12-27 22:54:58 +01:00
|
|
|
}
|