381 lines
11 KiB
PHP
381 lines
11 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\HasJsonTranslations;
|
|
use App\Traits\Models\IsCachedAsCollection;
|
|
use App\Traits\Models\MiniCrudModelJsonTranslatable;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Route;
|
|
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 HasJsonTranslations;
|
|
use IsCachedAsCollection;
|
|
use MiniCrudModelJsonTranslatable;
|
|
|
|
/**
|
|
* @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' => 'json',
|
|
];
|
|
|
|
/**
|
|
* The translatable columns.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatable = [
|
|
'seo_title',
|
|
'seo_description',
|
|
'name',
|
|
'slug',
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
'picture_alt',
|
|
'picture_title',
|
|
];
|
|
|
|
/**
|
|
* The json translatable columns.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatableJson = [
|
|
'data' => [
|
|
// * BLOC 1
|
|
'bloc_1_title',
|
|
'bloc_1_text',
|
|
'bloc_1_image',
|
|
// * BLOC 2
|
|
"bloc_2_title",
|
|
"bloc_2_text",
|
|
"bloc_2_image",
|
|
// * BLOC 3
|
|
"bloc_3_title",
|
|
"bloc_3_text",
|
|
"bloc_3_image",
|
|
// * BLOC 4
|
|
"bloc_4_title",
|
|
"bloc_4_text",
|
|
"bloc_4_image",
|
|
],
|
|
];
|
|
|
|
// * 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);
|
|
self::setDefaultData($model, 'data');
|
|
});
|
|
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);
|
|
}
|
|
|
|
// * 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);
|
|
}
|
|
|
|
/**
|
|
* Set default data
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @param mixed $field
|
|
* @return void
|
|
*/
|
|
private static function setDefaultData(Model $model, mixed $field)
|
|
{
|
|
$englishTranslation = $model->getTranslation($field, config('app.fallback_locale'));
|
|
// Check if the first line of the array is null. If it is, all the others will be too.
|
|
if ($englishTranslation['bloc_1_title'] === null) {
|
|
$model->setJsonTranslation($field, $model->$field, config('app.fallback_locale'));
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 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 = ''
|
|
) {
|
|
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,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the model for a bound value.
|
|
*
|
|
* @param mixed $value
|
|
* @param string|null $field
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
*/
|
|
public function resolveRouteBinding(mixed $value, $field = null)
|
|
{
|
|
if (Route::current()->action['prefix'] !== '/bo') {
|
|
$field = 'slug';
|
|
}
|
|
// Default field to query if no parameter field is specified.
|
|
$field = $field ?: $this->getRouteKeyName();
|
|
// Perform the query to find the parameter value in the database.
|
|
if (count($this->where($field, $value)->get())) {
|
|
return $this->where($field, $value)->firstOrFail();
|
|
} else {
|
|
foreach (config('app.locales') as $locale) {
|
|
if (count($this->where($field . '->' . $locale, $value)->get())) {
|
|
return $this->where($field . '->' . $locale, $value)->firstOrFail();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|