ldc-nature-and-respect/app/Models/Concept.php

382 lines
11 KiB
PHP
Raw Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
2023-03-24 15:39:11 +00:00
use App\Enums\Concepts\ColorsEnum;
use App\Enums\InputType;
2023-03-15 13:51:01 +01:00
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2023-04-14 08:06:43 +00:00
use App\Traits\Models\HasJsonTranslations;
2023-05-04 16:00:15 +02:00
use App\Traits\Models\IsCachedAsCollection;
2023-04-14 08:06:43 +00:00
use App\Traits\Models\MiniCrudModelJsonTranslatable;
2023-05-04 16:00:15 +02:00
use Illuminate\Contracts\Database\Eloquent\Builder;
2023-03-16 09:47:16 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-15 13:51:01 +01:00
use Illuminate\Database\Eloquent\Model;
2023-04-21 10:22:34 +00:00
use Illuminate\Support\Facades\Route;
2023-03-24 15:39:11 +00:00
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
2023-03-15 13:51:01 +01:00
/**
* @property integer $id
* @property string $seo_title
* @property string $seo_description
* @property string $name
* @property string $slug
* @property string $title
2023-03-24 15:39:11 +00:00
* @property string $subtitle
2023-03-15 13:51:01 +01:00
* @property string $description
* @property string $content
* @property string $picture
2023-03-24 15:39:11 +00:00
* @property string $picture_alt
* @property string $picture_title
2023-03-15 13:51:01 +01:00
* @property string $created_at
* @property string $updated_at
*/
class Concept extends Model
{
2023-03-16 09:47:16 +01:00
use HasFactory;
2023-04-14 08:06:43 +00:00
use HasJsonTranslations;
2023-05-04 16:00:15 +02:00
use IsCachedAsCollection;
2023-04-14 08:06:43 +00:00
use MiniCrudModelJsonTranslatable;
2023-03-16 09:47:16 +01:00
2023-03-15 13:51:01 +01:00
/**
* @var array
*/
protected $fillable = [
'seo_title',
'seo_description',
'name',
'slug',
'title',
2023-03-24 15:39:11 +00:00
'subtitle',
2023-03-15 13:51:01 +01:00
'description',
2023-03-24 15:39:11 +00:00
'data',
2023-03-15 13:51:01 +01:00
'picture',
2023-03-24 15:39:11 +00:00
'picture_alt',
'picture_title',
'order',
2023-03-15 13:51:01 +01:00
'created_at',
'updated_at'
];
2023-03-24 15:39:11 +00:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
2023-04-14 08:06:43 +00:00
'data' => 'json',
];
/**
* The translatable columns.
2023-04-18 16:05:34 +02:00
*
* @var array
2023-04-14 08:06:43 +00:00
*/
public $translatable = [
'seo_title',
'seo_description',
'name',
'slug',
'title',
'subtitle',
'description',
'picture_alt',
'picture_title',
];
/**
* The json translatable columns.
2023-04-18 16:05:34 +02:00
*
* @var array
2023-04-14 08:06:43 +00:00
*/
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",
],
2023-03-24 15:39:11 +00:00
];
2023-03-15 13:51:01 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
2023-03-24 15:39:11 +00:00
self::assertFieldIsUnique('name', $model->name);
2023-03-15 13:51:01 +01:00
self::manageFields($model);
2023-03-24 15:39:11 +00:00
self::setOrder($model);
self::setDefaultData($model, 'data');
2023-03-15 13:51:01 +01:00
});
2023-05-04 16:00:15 +02:00
static::created(function () {
self::removeCached();
});
2023-03-15 13:51:01 +01:00
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture');
2023-05-04 16:00:15 +02:00
self::removeCached();
2023-03-15 13:51:01 +01:00
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'picture');
});
}
2023-05-04 16:00:15 +02:00
// * - 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);
}
2023-03-15 13:51:01 +01:00
// * METHODS
/**
2023-03-23 09:08:28 +01:00
* Common methods function.
2023-03-24 15:39:11 +00:00
*
2023-03-23 09:08:28 +01:00
* @param Model $model
2023-03-24 15:39:11 +00:00
* @return void
*/
2023-03-23 09:08:28 +01:00
private static function manageFields(Model $model)
2023-03-24 15:39:11 +00:00
{
2023-03-23 09:08:28 +01:00
self::setSlug($model);
ToolboxHelper::assertNoInternalBoUrls('description', $model->description);
$model->description = clean($model->description);
2023-04-18 10:49:39 +02:00
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
2023-03-24 15:39:11 +00:00
}
/**
2023-03-23 09:08:28 +01:00
* Set the slug.
*
* @param \Illuminate\Database\Eloquent\Model $model
2023-03-15 13:51:01 +01:00
*
* @return void
*/
2023-03-23 09:08:28 +01:00
private static function setSlug(Model $model)
2023-03-15 13:51:01 +01:00
{
2023-03-23 09:08:28 +01:00
$model->slug = Str::slug($model->name);
2023-03-15 13:51:01 +01:00
}
2023-03-24 15:39:11 +00:00
/**
* 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;
}
2023-03-15 13:51:01 +01:00
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
2023-05-04 14:52:14 +02:00
* @param string $valueField
2023-03-15 13:51:01 +01:00
* @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);
}
2023-03-24 15:39:11 +00:00
2023-05-04 14:52:14 +02:00
/**
* 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'));
};
}
2023-03-24 15:39:11 +00:00
/**
* Return the structure necessary of data.
*
2023-05-04 14:52:14 +02:00
* @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)[])[])[]
2023-03-24 15:39:11 +00:00
*/
public static function getEmptyData(
2023-05-04 14:52:14 +02:00
string $title = '',
2023-05-04 16:00:15 +02:00
int $color = 0,
2023-05-04 14:52:14 +02:00
string $text = '',
string $img = '',
string $imgAlt = '',
string $imgTitle = ''
2023-03-24 15:39:11 +00:00
) {
return [
// * BLOC 1
"bloc_1_title" => [
"label" => "Bloc 1 - Title",
"inputType" => InputType::string,
2023-05-04 14:52:14 +02:00
"value" => $title,
2023-03-24 15:39:11 +00:00
"validate" => "required|min:4|max:100"
],
"bloc_1_color" => [
"label" => "Bloc 1 - Color",
"inputType" => InputType::selectEnum,
2023-05-04 14:52:14 +02:00
"value" => $color,
2023-03-24 15:39:11 +00:00
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
],
"bloc_1_text" => [
"label" => "Bloc 1 - Text",
"inputType" => InputType::text,
2023-05-04 14:52:14 +02:00
"value" => $text,
2023-03-24 15:39:11 +00:00
"validate" => "string|min:10"
],
"bloc_1_image" => [
"label" => "Bloc 1 - Image",
"dimensions" => ["width" => 1140, "height" => 795],
"inputType" => InputType::image,
2023-05-04 14:52:14 +02:00
"alt" => $imgAlt,
"title" => $imgTitle,
"value" => $img,
2023-03-24 15:39:11 +00:00
],
// * BLOC 2
"bloc_2_title" => [
"label" => "Bloc 2 - Title",
"inputType" => InputType::string,
2023-05-04 14:52:14 +02:00
"value" => $title,
2023-03-24 15:39:11 +00:00
"validate" => "required|min:4|max:100"
],
"bloc_2_color" => [
"label" => "Bloc 2 - Color",
"inputType" => InputType::selectEnum,
2023-05-04 14:52:14 +02:00
"value" => $color,
2023-03-24 15:39:11 +00:00
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
],
"bloc_2_text" => [
"label" => "Bloc 2 - Text",
"inputType" => InputType::text,
2023-05-04 14:52:14 +02:00
"value" => $text,
2023-03-24 15:39:11 +00:00
"validate" => "string|min:10"
],
"bloc_2_image" => [
"label" => "Bloc 2 - Image",
"dimensions" => ["width" => 1140, "height" => 795],
"inputType" => InputType::image,
2023-05-04 14:52:14 +02:00
"alt" => $imgAlt,
"title" => $imgTitle,
"value" => $img,
2023-03-24 15:39:11 +00:00
],
// * BLOC 3
"bloc_3_title" => [
"label" => "Bloc 3 - Title",
"inputType" => InputType::string,
2023-05-04 14:52:14 +02:00
"value" => $title,
2023-03-24 15:39:11 +00:00
"validate" => "required|min:4|max:100"
],
"bloc_3_color" => [
"label" => "Bloc 3 - Color",
"inputType" => InputType::selectEnum,
2023-05-04 14:52:14 +02:00
"value" => $color,
2023-03-24 15:39:11 +00:00
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
],
"bloc_3_text" => [
"label" => "Bloc 3 - Text",
"inputType" => InputType::text,
2023-05-04 14:52:14 +02:00
"value" => $text,
2023-03-24 15:39:11 +00:00
"validate" => "string|min:10"
],
"bloc_3_image" => [
"label" => "Bloc 3 - Image",
"dimensions" => ["width" => 1140, "height" => 795],
"inputType" => InputType::image,
2023-05-04 14:52:14 +02:00
"alt" => $imgAlt,
"title" => $imgTitle,
"value" => $img,
2023-03-24 15:39:11 +00:00
],
// * BLOC 4
"bloc_4_title" => [
"label" => "Bloc 4 - Title",
"inputType" => InputType::string,
2023-05-04 14:52:14 +02:00
"value" => $title,
2023-03-24 15:39:11 +00:00
"validate" => "required|min:4|max:100"
],
"bloc_4_color" => [
"label" => "Bloc 4 - Color",
"inputType" => InputType::selectEnum,
2023-05-04 14:52:14 +02:00
"value" => $color,
2023-03-24 15:39:11 +00:00
"validate" => ["required", "integer", new Enum(ColorsEnum::class)]
],
"bloc_4_text" => [
"label" => "Bloc 4 - Text",
"inputType" => InputType::text,
2023-05-04 14:52:14 +02:00
"value" => $text,
2023-03-24 15:39:11 +00:00
"validate" => "string|min:10"
],
"bloc_4_image" => [
"label" => "Bloc 4 - Image",
"dimensions" => ["width" => 1140, "height" => 795],
"inputType" => InputType::image,
2023-05-04 14:52:14 +02:00
"alt" => $imgAlt,
"title" => $imgTitle,
"value" => $img,
2023-03-24 15:39:11 +00:00
],
];
}
2023-04-21 10:22:34 +00:00
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Model|null
*/
2023-05-04 14:52:14 +02:00
public function resolveRouteBinding(mixed $value, $field = null)
2023-04-21 10:22:34 +00:00
{
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();
}
}
}
2023-04-21 10:22:34 +00:00
}
2023-03-15 13:51:01 +01:00
}