ldc-legaulois/app/Traits/Models/MiniCrudModel.php

208 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2024-01-10 14:49:46 +01:00
<?php
namespace App\Traits\Models;
use App\Enums\InputType;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2026-09-25 16:25:31 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
2024-01-10 14:49:46 +01:00
trait MiniCrudModel
{
/**
* Fill the model with an array of attributes.
*
* @throws \Illuminate\Database\Eloquent\MassAssignmentException If fill prop is not allowed.
2026-09-25 16:25:31 +02:00
*
2024-01-10 14:49:46 +01:00
* @phpcs:disable Squiz.Commenting.FunctionComment.EmptyThrows
*/
2026-09-25 16:25:31 +02:00
public function fill(array $attributes): Model
2024-01-10 14:49:46 +01:00
{
// phpcs:enable
parent::fill($attributes);
if (isset($attributes['data'])) {
$this->setData($attributes['data']);
}
2026-09-25 16:25:31 +02:00
2024-02-20 10:38:38 +01:00
return $this;
2024-01-10 14:49:46 +01:00
}
/**
* Apply function on data typed text while on save event.
2024-01-10 14:49:46 +01:00
*/
public function minicrudCreating(): void
2024-01-10 14:49:46 +01:00
{
/** @var array<string,array> */
$data = $this->data;
foreach ($data as &$input) {
switch ($input['inputType']) {
case InputType::text->value:
$input['text'] = clean($input['text']);
$input['text'] = FileStorageHelper::storeModelFilesFromHtml($this, $input['text']);
break;
2026-09-25 16:25:31 +02:00
} // end switch
} // end foreach
2024-01-10 14:49:46 +01:00
$this->data = $data;
}
/**
* Apply function on data typed text while on update event.
2024-01-10 14:49:46 +01:00
*/
public function minicrudUpdating(): void
2024-01-10 14:49:46 +01:00
{
$this->minicrudCreating();
}
/**
* Apply function on data typed text while on updated event.
2024-01-10 14:49:46 +01:00
*/
public function minicrudUpdated(): void
2024-01-10 14:49:46 +01:00
{
2024-02-20 10:38:38 +01:00
/** @var array $input */
2024-01-10 14:49:46 +01:00
foreach ($this->data as $index => $input) {
switch ($input['inputType']) {
case InputType::text->value:
$original = ToolboxHelper::getOriginal($this, 'data');
FileStorageHelper::removeOldModelFilesFromHtml(
$input['text'],
$original[$index]['text'] ?? ''
);
break;
2026-09-25 16:25:31 +02:00
} // end switch
} // end foreach
2024-01-10 14:49:46 +01:00
}
/**
* Apply function on data typed text while on deleted event.
2024-01-10 14:49:46 +01:00
*/
public function minicrudDeleted(): void
2024-01-10 14:49:46 +01:00
{
/** @var array */
foreach ($this->data as $input) {
switch ($input['inputType']) {
case InputType::text->value:
FileStorageHelper::removeModelFilesOfHtml($input['text']);
break;
2026-09-25 16:25:31 +02:00
} // end switch
} // end foreach
2024-01-10 14:49:46 +01:00
}
/**
* Set a specific prop.
2024-01-10 14:49:46 +01:00
*/
private function setData(array $data): void
{
$attrs = $this->attributes;
if (isset($attrs['data'])) {
unset($attrs['data']);
}
$this->setAttribute('data', $data);
}
/**
* Set a specific prop.
2024-01-10 14:49:46 +01:00
*/
protected function setDataAttribute(array $value): void
{
$this->attributes['data'] = $this->castAttributeAsJson(
'data',
$this->serializeData($value)
);
}
/**
* Set a specific prop.
2024-01-10 14:49:46 +01:00
*/
2026-09-25 16:25:31 +02:00
protected function getDataAttribute(?string $data): array
2024-01-10 14:49:46 +01:00
{
return $this->fromJson($data ?? $this->attributes['data'] ?? '[]');
}
/**
* Serialize properties.
2024-01-10 14:49:46 +01:00
*
* @throws \ErrorException If input type is no handled.
* @throws \Illuminate\Validation\ValidationException If url is a bo url.
2026-09-25 16:25:31 +02:00
*
2024-01-10 14:49:46 +01:00
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
2026-09-25 16:25:31 +02:00
*
2024-01-10 14:49:46 +01:00
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
private function serializeData(array $data): array
{
foreach ($data as $o => $input) {
2026-09-25 16:25:31 +02:00
if (! isset($input['inputType'])) {
continue;
}
2024-01-10 14:49:46 +01:00
switch ($input['inputType']) {
case InputType::image->value:
$data[$o]['value'] = \preg_replace(
'/^\/|^/',
'',
FileStorageHelper::storeFile($this, $input['value'], true)
);
break;
case InputType::selectEnum->value:
case InputType::boolean->value:
case InputType::password->value:
case InputType::email->value:
case InputType::number->value:
case InputType::videoVimeo->value:
case InputType::videoYoutube->value:
case InputType::video->value:
case InputType::string->value:
case InputType::text->value:
case InputType::url->value:
ToolboxHelper::assertNoInternalBoUrls('content', $input['value']);
break;
2026-04-30 17:54:08 +02:00
case InputType::slider->value:
2026-09-25 16:25:31 +02:00
if (! is_array($data[$o]['value'])) {
2026-05-13 15:02:14 +02:00
break;
}
2026-04-30 17:54:08 +02:00
foreach ($data[$o]['value'] as $index => $item) {
2026-09-25 16:25:31 +02:00
if (isset($item['picture']) && $item['picture'] instanceof UploadedFile) {
2026-04-30 17:54:08 +02:00
$data[$o]['value'][$index]['picture'] = \preg_replace(
'/^\/|^/',
'',
FileStorageHelper::storeFile($this, $item['picture'], true)
);
}
}
break;
case InputType::selectImageVideo->value:
2026-09-25 16:25:31 +02:00
if (isset($data[$o]['value']['picture']) && $data[$o]['value']['picture']['path'] instanceof UploadedFile) {
$data[$o]['value']['picture']['path'] = \preg_replace(
'/^\/|^/',
'',
FileStorageHelper::storeFile($this, $data[$o]['value']['picture']['path'], true)
);
}
2026-04-30 17:54:08 +02:00
break;
2024-01-10 14:49:46 +01:00
default:
throw new \ErrorException(sprintf(
'Unknown input Type %s',
$input['inputType']
));
2026-09-25 16:25:31 +02:00
} // end switch
} // end foreach
2024-01-10 14:49:46 +01:00
return $data;
}
/**
* Get fillable properties and their values from the model.
*/
public function getFillableFields(self $model): array
{
$datas = [];
foreach ($model->toArray() as $property => $value) {
if (in_array($property, $model->getFillable())) {
$datas = array_merge($datas, [$property => $value]);
}
}
2026-09-25 16:25:31 +02:00
return $datas;
}
2024-01-10 14:49:46 +01:00
}