269 lines
8.1 KiB
PHP
269 lines
8.1 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\MiniCrudModel;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rules\Enum;
|
|
|
|
/**
|
|
* @property integer $id
|
|
* @property string $seo_title
|
|
* @property string $seo_description
|
|
* @property string $name
|
|
* @property string $slug
|
|
* @property string $title
|
|
* @property string $subtitle
|
|
* @property string $description
|
|
* @property string $content
|
|
* @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 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' => 'array',
|
|
];
|
|
|
|
// * EVENTS
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::setSlug($model);
|
|
self::assertFieldIsUnique('name', $model->name);
|
|
self::manageFields($model);
|
|
self::setOrder($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::setSlug($model);
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Set the slug.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function setSlug(Model $model)
|
|
{
|
|
$model->slug = Str::slug($model->name);
|
|
}
|
|
|
|
/**
|
|
* Common methods function.
|
|
*
|
|
* @param Model $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(Model $model)
|
|
{
|
|
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
|
|
$model->description = clean($model->description);
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
}
|
|
|
|
/**
|
|
* 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 mixed $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 Model $model
|
|
* @return void
|
|
*/
|
|
public static function getEmptyData(
|
|
string $title = null,
|
|
int $color = null,
|
|
string $text = null,
|
|
string $img = null,
|
|
string $imgAlt = null,
|
|
string $imgTitle = null
|
|
) {
|
|
return [
|
|
// * BLOC 1
|
|
"bloc_1_title" => [
|
|
"label" => "Bloc 1 - Title",
|
|
"inputType" => InputType::string,
|
|
"value" => $title ?? "",
|
|
"validate" => "required|min:4|max:100"
|
|
],
|
|
"bloc_1_color" => [
|
|
"label" => "Bloc 1 - Color",
|
|
"inputType" => InputType::selectEnum,
|
|
"value" => $color ?? "",
|
|
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
|
|
],
|
|
"bloc_1_text" => [
|
|
"label" => "Bloc 1 - Text",
|
|
"inputType" => InputType::text,
|
|
"value" => $text ?? "",
|
|
"validate" => "string|min:10"
|
|
],
|
|
"bloc_1_image" => [
|
|
"label" => "Bloc 1 - Image",
|
|
"dimensions" => ["width" => 1140, "height" => 795],
|
|
"inputType" => InputType::image,
|
|
"alt" => $imgAlt ?? "",
|
|
"title" => $imgTitle ?? "",
|
|
"value" => $img ?? "",
|
|
],
|
|
// * BLOC 2
|
|
"bloc_2_title" => [
|
|
"label" => "Bloc 2 - Title",
|
|
"inputType" => InputType::string,
|
|
"value" => $title ?? "",
|
|
"validate" => "required|min:4|max:100"
|
|
],
|
|
"bloc_2_color" => [
|
|
"label" => "Bloc 2 - Color",
|
|
"inputType" => InputType::selectEnum,
|
|
"value" => $color ?? "",
|
|
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
|
|
],
|
|
"bloc_2_text" => [
|
|
"label" => "Bloc 2 - Text",
|
|
"inputType" => InputType::text,
|
|
"value" => $text ?? "",
|
|
"validate" => "string|min:10"
|
|
],
|
|
"bloc_2_image" => [
|
|
"label" => "Bloc 2 - Image",
|
|
"dimensions" => ["width" => 1140, "height" => 795],
|
|
"inputType" => InputType::image,
|
|
"alt" => $imgAlt ?? "",
|
|
"title" => $imgTitle ?? "",
|
|
"value" => $img ?? "",
|
|
],
|
|
// * BLOC 3
|
|
"bloc_3_title" => [
|
|
"label" => "Bloc 3 - Title",
|
|
"inputType" => InputType::string,
|
|
"value" => $title ?? "",
|
|
"validate" => "required|min:4|max:100"
|
|
],
|
|
"bloc_3_color" => [
|
|
"label" => "Bloc 3 - Color",
|
|
"inputType" => InputType::selectEnum,
|
|
"value" => $color ?? "",
|
|
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
|
|
],
|
|
"bloc_3_text" => [
|
|
"label" => "Bloc 3 - Text",
|
|
"inputType" => InputType::text,
|
|
"value" => $text ?? "",
|
|
"validate" => "string|min:10"
|
|
],
|
|
"bloc_3_image" => [
|
|
"label" => "Bloc 3 - Image",
|
|
"dimensions" => ["width" => 1140, "height" => 795],
|
|
"inputType" => InputType::image,
|
|
"alt" => $imgAlt ?? "",
|
|
"title" => $imgTitle ?? "",
|
|
"value" => $img ?? "",
|
|
],
|
|
// * BLOC 4
|
|
"bloc_4_title" => [
|
|
"label" => "Bloc 4 - Title",
|
|
"inputType" => InputType::string,
|
|
"value" => $title ?? "",
|
|
"validate" => "required|min:4|max:100"
|
|
],
|
|
"bloc_4_color" => [
|
|
"label" => "Bloc 4 - Color",
|
|
"inputType" => InputType::selectEnum,
|
|
"value" => $color ?? "",
|
|
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
|
|
],
|
|
"bloc_4_text" => [
|
|
"label" => "Bloc 4 - Text",
|
|
"inputType" => InputType::text,
|
|
"value" => $text ?? "",
|
|
"validate" => "string|min:10"
|
|
],
|
|
"bloc_4_image" => [
|
|
"label" => "Bloc 4 - Image",
|
|
"dimensions" => ["width" => 1140, "height" => 795],
|
|
"inputType" => InputType::image,
|
|
"alt" => $imgAlt ?? "",
|
|
"title" => $imgTitle ?? "",
|
|
"value" => $img ?? "",
|
|
],
|
|
];
|
|
}
|
|
}
|