ldc-wenn-dann-mit-respekt/app/Models/Concept.php

253 lines
7 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Concepts\ColorsEnum;
use App\Enums\InputType;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasTranslations;
use App\Traits\Models\IsCachedAsCollection;
use App\Traits\Models\MiniCrudModel;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
/**
* @property string $seo_title
* @property string $seo_description
* @property string $name
* @property string $slug
* @property string $title
* @property string $subtitle
* @property string $description
* @property object $data
* @property string $picture
* @property string $picture_alt
* @property string $picture_title
* @property string $created_at
* @property string $updated_at
*/
class Concept extends Model
{
use HasFactory;
use HasTranslations;
use IsCachedAsCollection;
use MiniCrudModel;
/**
* @var array
*/
protected $fillable = [
'seo_title',
'seo_description',
'name',
'slug',
'title',
'subtitle',
'description',
'data',
'picture',
'picture_alt',
'picture_title',
'order',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'data' => 'object',
];
/**
* The translatable columns.
*
* @var array
*/
public $translatable = [
'data',
'seo_title',
'seo_description',
'name',
'slug',
'title',
'subtitle',
'description',
'picture',
'picture_alt',
'picture_title',
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::assertFieldIsUnique('name', $model->name);
self::manageFields($model);
self::setOrder($model);
});
static::created(function () {
self::removeCached();
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
self::removeCached();
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
// * - Cache
/**
* Change query for cached elements
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected static function queryCached(Builder $query): Builder
{
return $query->where('id', '<>', 1)->orderBy('order');
}
// * METHODS
/**
* Common methods function.
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
self::setSlug($model);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
$model->description = clean($model->description);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
}
/**
* Set the slug.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
private static function setSlug(Model $model)
{
$model->slug = Str::slug($model->name);
}
/**
* Set order after the last element of the list.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
private static function setOrder(Model $model): void
{
$model->order = \intval(self::query()->max('order')) + 1;
}
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $valueField
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
/**
* Return the structure necessary of data.
*
* @param string $title
* @param integer $color
* @param string $text
* @param string $img
* @param string $imgAlt
* @param string $imgTitle
* @return ((string|\App\Enums\InputType)[]|(string|int[]|\App\Enums\InputType)[]|(string|\App\Enums\InputType|int|(string|\Illuminate\Validation\Rules\Enum)[])[])[]
*/
public static function getEmptyData(
string $title = '',
int $color = 0,
string $text = '',
string $img = '',
string $imgAlt = '',
string $imgTitle = ''
) {
$color = $color ?: \collect(ColorsEnum::cases())->random()->value;
$data = [];
for ($i = 1; $i <= 4; $i++) {
$data["bloc_{$i}_title"] = [
'label' => "Bloc {$i} - Title",
'inputType' => InputType::string->value,
'value' => $title,
'validate' => 'required|min:4|max:100'
];
$data["bloc_{$i}_color"] = [
'label' => "Bloc {$i} - Color",
'inputType' => InputType::selectEnum->value,
'value' => $color,
'validate' => ['required', 'integer', new Enum(ColorsEnum::class)]
];
$data["bloc_{$i}_text"] = [
'label' => "Bloc {$i} - Text",
'inputType' => InputType::text->value,
'value' => $text,
'validate' => 'string|min:10'
];
$data["bloc_{$i}_image"] = [
'label' => "Bloc {$i} - Image",
'dimensions' => ['width' => 1140, 'height' => 795],
'inputType' => InputType::image->value,
'alt' => $imgAlt,
'title' => $imgTitle,
'value' => $img,
'validate' => "required_if:data.bloc_{$i}_has_video.value,false",
];
$data["bloc_{$i}_video"] = [
'label' => "Bloc {$i} - Video (Youtube or Vimeo)",
'inputType' => InputType::video->value,
'value' => '',
// phpcs:ignore Generic.Files.LineLength.TooLong
'validate' => [
'required_if:data.bloc_{$i}_has_video.value,true',
'nullable',
'regex:%([0-9]{8,10}|[^\"&?\/\s]{11})%i'
],
'required' => false
];
$data["bloc_{$i}_has_video"] = [
'label' => "Bloc {$i} - Display video",
'inputType' => InputType::boolean->value,
'value' => false,
];
} //end for
return $data;
}
}