*/ 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 self $model * @return void */ private static function manageFields(self $model) { self::setSlug($model); ToolboxHelper::assertNoInternalBoUrls('description', $model->description); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); } /** * Set the slug. * * @param self $model * * @return void */ private static function setSlug(self $model) { $model->slug = Str::slug($model->name); } /** * Set order after the last element of the list. * * @param self $model * @return void */ private static function setOrder(self $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 = '' ) { /** @var \App\Enums\Concepts\ColorsEnum */ $defaultColor = \collect(ColorsEnum::cases())->random(); $color = $color ?: $defaultColor->value; $data = []; for ($i = 1; $i <= 6; $i++) { $data = self::emptyDataBloc( \strval($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; } /** * Get the SEO object. * * @return \Uwil\Seo\Laravel\Objects\SeoItem */ public function toSeoItem(): \Uwil\Seo\Laravel\Objects\SeoItem { 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); } }