*/ protected $fillable = [ 'title', 'content', 'button_display', 'button_text', 'button_url', 'button_targetblank', 'picture', 'picture_alt', 'picture_title', 'picture_thumbnail', 'picture_thumbnail_alt', 'picture_thumbnail_title', 'order', 'published' ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'button_display' => 'bool', 'button_targetblank' => 'bool', 'published' => 'bool' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::manageFields($model); $model->order = \intval(self::query()->max('order')) + 1; }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'picture'); FileStorageHelper::removeOldFile($model, 'picture_thumbnail'); }); static::deleting(function (self $model) { self::assertNumberOfSlides($model, true); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'picture'); FileStorageHelper::removeFile($model, 'picture_thumbnail'); }); } // * METHODS /** * Common methods function. * * @param self $model * @return void */ private static function manageFields(self $model): void { self::assertTitleIsUnique($model->title, $model->id); self::assertNumberOfSlides($model); ToolboxHelper::assertNoInternalBoUrls($model->content, 'content'); $model->content = clean($model->content); if ($model->button_url) { ToolboxHelper::assertNoInternalBoUrls($model->button_url, 'button_url'); $model->button_url = clean($model->button_url); } $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); $model->picture_thumbnail = FileStorageHelper::storeFile($model, $model->picture_thumbnail, true); } /** * Asserts the number of slides in the slider. * * @param self $model * @param boolean $isDeleting * @return void * @throws \Illuminate\Validation\ValidationException If there is less 1 or more than 4 slides. */ private static function assertNumberOfSlides(self $model, bool $isDeleting = false): void { if ( $isDeleting && $model->published && self::query()->where('published', true)->count() <= 1 ) { throw ValidationException::withMessages( [trans('Minimum 1 slide doit être publiée, la slide n\'a pas été supprimée.')] ); } if ($model->published !== $model->getOriginal('published')) { if (self::query()->where('published', true)->count() + 1 > 4 && $model->published) { $model->published = false; \session()->flash('info', \trans('Maximum 4 slides peuvent être publiées, la slide a été dépubliée.')); } elseif (self::query()->where('published', true)->count() <= 1 && !$model->published) { $model->published = true; \session()->flash('info', \trans('Minimum 1 slide doit être publiée, la slide a été publiée.')); } } } /** * Asserts using validation that the title is unique. * * @param string $title * @param integer|null $id * @return void */ private static function assertTitleIsUnique(string $title, ?int $id = null): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id); } }