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

352 lines
10 KiB
PHP
Raw Permalink 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-05-09 09:14:21 +02:00
use App\Traits\Models\HasTranslations;
2023-05-04 16:00:15 +02:00
use App\Traits\Models\IsCachedAsCollection;
2023-05-09 09:14:21 +02:00
use App\Traits\Models\MiniCrudModel;
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-03-24 15:39:11 +00:00
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
use Spatie\Sitemap\Contracts\Sitemapable;
2023-07-11 09:57:55 +00:00
use Uwil\Seo\Laravel\Interfaces\SeoInterface;
2023-08-14 10:07:57 +02:00
use Uwil\Seo\Laravel\Objects\SeoItem;
2023-07-11 09:57:55 +00:00
use Uwil\Seo\Laravel\Traits\SeoTrait;
use Uwil\SitemapGenerator\Lib\SitemapHelper;
2023-03-15 13:51:01 +01:00
/**
* @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
2023-05-09 19:24:09 +02:00
* @property object $data
2023-03-15 13:51:01 +01:00
* @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 implements SeoInterface, Sitemapable
2023-03-15 13:51:01 +01:00
{
2023-03-16 09:47:16 +01:00
use HasFactory;
2023-05-09 09:14:21 +02:00
use HasTranslations;
2023-05-04 16:00:15 +02:00
use IsCachedAsCollection;
2023-05-09 09:14:21 +02:00
use MiniCrudModel;
2023-07-11 09:57:55 +00:00
use SeoTrait;
2023-03-16 09:47:16 +01:00
2023-03-15 13:51:01 +01:00
/**
2024-07-16 18:21:29 +02:00
* The attributes that are mass assignable.
*
* @var array<int, string>
2023-03-15 13:51:01 +01:00
*/
protected $fillable = [
'seo_title',
'seo_description',
'name',
'slug',
'title',
'title_background_color',
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.
*
2024-01-29 16:02:03 +01:00
* @var array<string, string>
2023-03-24 15:39:11 +00:00
*/
protected $casts = [
2023-05-09 19:24:09 +02:00
'data' => 'object',
'title_background_color' => ColorsEnum::class
2023-04-14 08:06:43 +00:00
];
/**
* The translatable columns.
2023-04-18 16:05:34 +02:00
*
2024-01-29 16:02:03 +01:00
* @var array<string>
2023-04-14 08:06:43 +00:00
*/
public $translatable = [
2023-05-09 09:14:21 +02:00
'data',
2023-04-14 08:06:43 +00:00
'seo_title',
'seo_description',
'name',
'slug',
'title',
'subtitle',
'description',
2023-05-09 19:24:09 +02:00
'picture',
2023-04-14 08:06:43 +00:00
'picture_alt',
'picture_title',
];
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);
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
{
2023-05-05 07:52:58 +02:00
return $query->where('id', '<>', 1)->orderBy('order');
2023-05-04 16:00:15 +02:00
}
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
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-24 15:39:11 +00:00
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function manageFields(self $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);
2023-05-30 20:26:00 +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.
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-15 13:51:01 +01:00
*
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function setSlug(self $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.
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-24 15:39:11 +00:00
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function setOrder(self $model): void
2023-03-24 15:39:11 +00:00
{
$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
/**
* 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
) {
2024-01-29 16:02:03 +01:00
/** @var \App\Enums\Concepts\ColorsEnum */
$defaultColor = \collect(ColorsEnum::cases())->random();
$color = $color ?: $defaultColor->value;
$data = [];
2023-05-31 09:59:41 +02:00
for ($i = 1; $i <= 6; $i++) {
$data = self::emptyDataBloc(
2024-01-29 16:02:03 +01:00
\strval($i),
2023-05-31 09:59:41 +02:00
$data,
$title,
$color,
$text,
$img,
$imgAlt,
$imgTitle
);
} //end for
return $data;
}
2023-05-31 09:59:41 +02:00
/**
* 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,
];
2023-05-31 09:59:41 +02:00
}
$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'
2023-05-31 09:59:41 +02:00
];
$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)
]
2023-05-31 09:59:41 +02:00
];
$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" : '') . '',
2023-05-31 09:59:41 +02:00
];
$data["bloc_{$index}_video"] = [
'label' => "Bloc {$index} - Video (Youtube or Vimeo)",
'inputType' => InputType::video->value,
'value' => '',
// phpcs:ignore Generic.Files.LineLength.TooLong
'validate' => [
2023-06-13 13:11:30 +02:00
"required_if:data.bloc_{$index}_has_video.value,true",
2023-05-31 09:59:41 +02:00
'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,
];
2023-06-13 13:11:30 +02:00
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;
2023-06-13 13:11:30 +02:00
$data["bloc_{$index}_has_video"]['required'] = false;
}
return $data;
2023-03-24 15:39:11 +00:00
}
2023-08-14 10:07:57 +02:00
/**
* Get the SEO object.
2023-08-14 10:07:57 +02:00
*
2024-01-29 16:02:03 +01:00
* @return \Uwil\Seo\Laravel\Objects\SeoItem
2023-08-14 10:07:57 +02:00
*/
public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem
2023-08-14 10:07:57 +02:00
{
return new SeoItem(
\trans($this->getTable()),
$this->seo_title,
$this->seo_description,
true,
\route(\config('seo.bo_route_prefix') . $this->getTable() . '.edit', $this),
\route(Str::singular($this->getTable()) . '.show', $this)
);
}
/**
* To sitemap tag .
*
* @return \Spatie\Sitemap\Tags\Url|string|array
*/
public function toSitemapTag(): \Spatie\Sitemap\Tags\Url|string|array
{
return SitemapHelper::generateUrl('concept.show', ['concept' => $this], $this);
}
2023-03-15 13:51:01 +01:00
}