*/ protected $fillable = [ 'label', 'subtitle', 'slug', 'description', 'banner', 'banner_alt', 'banner_title', 'banner_ambiance', 'banner_ambiance_alt', 'banner_ambiance_title', 'picture', 'picture_alt', 'picture_title', 'color', 'published', 'order', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'color' => HtmlColor::class, 'published' => 'boolean', ]; /** * Perform any actions required after the model boots. */ protected static function booted(): void { static::creating(function (self $model) { self::assertLabelIsUnique($model->label); self::setOrder($model); self::manageFields($model); }); static::created(function () { self::resetCache(); }); static::updating(function (self $model) { self::assertLabelIsUnique($model->label, $model->getKey()); self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'banner'); FileStorageHelper::removeOldFile($model, 'banner_ambiance'); FileStorageHelper::removeOldFile($model, 'picture'); self::resetCache(); }); static::deleting(function (self $model) { /** @var \Illuminate\Database\Eloquent\Collection $subranges */ $subranges = $model->subranges; if ($subranges->isNotEmpty()) { session()->flash('error', trans('Des sous-gammes sont associées à cette gamme. // Veuillez supprimer les sous-gammes concernées ou les réassocier à une autre gamme.')); return false; } }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'banner'); FileStorageHelper::removeFile($model, 'banner_ambiance'); FileStorageHelper::removeFile($model, 'picture'); self::resetCache(); }); } // * METHODS /** * Manage model fields. */ private static function manageFields(self $model): void { $model->slug = Str::slug($model->label); $model->banner = FileStorageHelper::storeFile($model, $model->banner, true); $model->banner_ambiance = FileStorageHelper::storeFile($model, $model->banner_ambiance, true); $model->picture = FileStorageHelper::storeFile($model, $model->picture, true); } /** * Asserts using validation that the label is unique. * * @throws \Illuminate\Validation\ValidationException If a range label already exists. */ private static function assertLabelIsUnique(?string $label, ?int $id = null): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'label', $label, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id); } /** * Set order after the last element of the list. */ private static function setOrder(self $model): void { $model->order = \intval(self::query()->max('order')) + 1; } /** * Reset all cache elements related to this model. */ private static function resetCache(): void { Cache::forget('publishedRangesOrderedByOrder'); self::getPublishedOrdered(); Cache::forget('publishedRangesOrderedBySlug'); self::getPublishedOrderedBySlug(); Cache::forget('publishedRangesOrderedByOrderWithSubranges'); self::getPublishedOrderedWithSubranges(); } /** * Get all published Ranges ordered by order. */ public static function getPublishedOrdered(): Collection { return Cache::remember('publishedRangesOrderedByOrder', 86400, function () { return self::query()->where('published', true)->orderBy('order')->get(); }); } /** * Get all published Ranges ordered by slug. */ public static function getPublishedOrderedBySlug(): Collection { return Cache::remember('publishedRangesOrderedBySlug', 86400, function () { return self::query()->where('published', true)->orderBy('slug')->get(); }); } /** * Get all published Ranges ordered by order with subranges. */ public static function getPublishedOrderedWithSubranges(): Collection { return Cache::remember('publishedRangesOrderedByOrderWithSubranges', 86400, function () { return self::query() ->where('published', true) ->with('subranges') ->orderBy('order') ->get(); }); } /** * Create a SchemaOrg. */ public function toSchemaOrg(): DefinedTermSet { return Schema::definedTermSet() ->name($this->label) ->description(strip_tags($this->description)) ->image( Schema::imageObject() ->url(asset($this->picture)) ->headline($this->picture_title) ); } // * REDIRECTIONS. /** * All models that belongs to it. * * @return array */ protected function getRouteChildrenModels(): array { return ['App\Models\Subrange']; } /** * Get the front url of this model. * Should return the model.show route, with all binded elements if needed. */ public function getFrontUrl(string $modelKey): string { return route('fo.products.search.range', $modelKey); } // * RELATIONSHIPS /** * Get all of the products for the range. */ public function products(): HasMany { return $this->hasMany(Product::class); } /** * Get the related subranges. */ public function subranges(): HasMany { return $this->hasMany(Subrange::class); } }