'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); }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); }); } // * 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 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 ?? "", ], ]; } }