314 lines
8.8 KiB
PHP
314 lines
8.8 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',
|
|
'title_background_color',
|
|
'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',
|
|
'title_background_color' => ColorsEnum::class
|
|
];
|
|
|
|
/**
|
|
* 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->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 <= 6; $i++) {
|
|
$data = self::emptyDataBloc(
|
|
$i,
|
|
$data,
|
|
$title,
|
|
$color,
|
|
$text,
|
|
$img,
|
|
$imgAlt,
|
|
$imgTitle
|
|
);
|
|
} //end for
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Empty data bloc
|
|
*
|
|
* @param string $index
|
|
* @param array $data
|
|
* @param string $title
|
|
* @param integer $color
|
|
* @param string $text
|
|
* @param string $img
|
|
* @param string $imgAlt
|
|
* @param string $imgTitle
|
|
* @return array
|
|
*/
|
|
public static function emptyDataBloc(
|
|
string $index,
|
|
array $data,
|
|
string $title = '',
|
|
int $color = 0,
|
|
string $text = '',
|
|
string $img = '',
|
|
string $imgAlt = '',
|
|
string $imgTitle = ''
|
|
): array {
|
|
if ($index > 4) {
|
|
$data["bloc_{$index}_use_bloc"] = [
|
|
'label' => "Bloc {$index} - Display bloc",
|
|
'inputType' => InputType::boolean->value,
|
|
'value' => false,
|
|
];
|
|
}
|
|
$data["bloc_{$index}_title"] = [
|
|
'label' => "Bloc {$index} - Title",
|
|
'inputType' => InputType::string->value,
|
|
'value' => $title,
|
|
'validate' => ($index > 4 ? "required_if:bloc_{$index}_use_bloc,true" : 'required') . '|min:4|max:100'
|
|
];
|
|
$data["bloc_{$index}_color"] = [
|
|
'label' => "Bloc {$index} - Color",
|
|
'inputType' => InputType::selectEnum->value,
|
|
'value' => $color,
|
|
'validate' => [
|
|
($index > 4 ? "required_if:bloc_{$index}_use_bloc,true" : 'required'),
|
|
'integer',
|
|
new Enum(ColorsEnum::class)
|
|
]
|
|
];
|
|
$data["bloc_{$index}_text"] = [
|
|
'label' => "Bloc {$index} - Text",
|
|
'inputType' => InputType::text->value,
|
|
'value' => $text,
|
|
'validate' => 'string|min:10'
|
|
];
|
|
$data["bloc_{$index}_image"] = [
|
|
'label' => "Bloc {$index} - Image",
|
|
'dimensions' => ['width' => 1140, 'height' => 795],
|
|
'inputType' => InputType::image->value,
|
|
'alt' => $imgAlt,
|
|
'title' => $imgTitle,
|
|
'value' => $img,
|
|
'validate' => ($index > 4 ? "required_if:bloc_{$index}_use_bloc,true" : '') . '',
|
|
];
|
|
|
|
$data["bloc_{$index}_video"] = [
|
|
'label' => "Bloc {$index} - Video (Youtube or Vimeo)",
|
|
'inputType' => InputType::video->value,
|
|
'value' => '',
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
'validate' => [
|
|
"required_if:data.bloc_{$index}_has_video.value,true",
|
|
'nullable',
|
|
'regex:%([0-9]{8,10}|[^\"&?\/\s]{11})%i'
|
|
],
|
|
'required' => false
|
|
];
|
|
|
|
$data["bloc_{$index}_has_video"] = [
|
|
'label' => "Bloc {$index} - Display video",
|
|
'inputType' => InputType::boolean->value,
|
|
'value' => false,
|
|
];
|
|
|
|
if ($index > 4) {
|
|
$data["bloc_{$index}_title"]['required'] = false;
|
|
$data["bloc_{$index}_title"]['validate'] .= '|nullable';
|
|
$data["bloc_{$index}_color"]['required'] = false;
|
|
$data["bloc_{$index}_text"]['required'] = false;
|
|
$data["bloc_{$index}_text"]['validate'] .= '|nullable';
|
|
$data["bloc_{$index}_image"]['required'] = false;
|
|
$data["bloc_{$index}_image"]['validate'] .= '|nullable';
|
|
$data["bloc_{$index}_video"]['required'] = false;
|
|
$data["bloc_{$index}_has_video"]['required'] = false;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|