*/ use HasFactory; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'seo_title', 'seo_description', 'name', 'slug', 'description', 'description_2', 'employees', 'pictos', 'address', 'phone_number', 'access_line', 'director_name', 'director_function', 'director_speach', 'invert_director_block', 'employee_name', 'employee_function', 'employee_speach', 'highlights', 'latitude', 'longitude', 'sticky_title', 'sticky_link', 'logo', 'logo_alt', 'logo_title', 'picture_1', 'picture_1_alt', 'picture_1_title', 'picture_2', 'picture_2_alt', 'picture_2_title', 'picture_3', 'picture_3_alt', 'picture_3_title', 'order', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'pictos' => 'array', 'access_line' => 'array', 'invert_director_block' => 'boolean' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted(): void { static::creating(function (self $model) { self::setOrder($model); self::manageFields($model); }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { FileStorageHelper::removeOldFile($model, 'logo'); FileStorageHelper::removeOldFile($model, 'picture_1'); FileStorageHelper::removeOldFile($model, 'picture_2'); FileStorageHelper::removeOldFile($model, 'picture_3'); }); static::deleted(function (self $model) { FileStorageHelper::removeFile($model, 'logo'); FileStorageHelper::removeFile($model, 'picture_1'); FileStorageHelper::removeFile($model, 'picture_2'); FileStorageHelper::removeFile($model, 'picture_3'); }); } // * METHODS. /** * Manage model fields. * * @param self $model * @return void */ private static function manageFields(self $model): void { // * ASSERTIONS self::assertFieldIsUnique('name', $model->name, $model->id); self::assertFieldIsUnique('slug', Str::slug($model->name), $model->id); // * TRANSFORMATIONS self::setSlug($model, $model->name); $model->logo = FileStorageHelper::storeFile($model, $model->logo, true); $model->picture_1 = FileStorageHelper::storeFile($model, $model->picture_1, true); $model->picture_2 = FileStorageHelper::storeFile($model, $model->picture_2, true); $model->picture_3 = FileStorageHelper::storeFile($model, $model->picture_3, true); } /** * Set the slug. * * @param self $model * @param string $original * * @return void */ private static function setSlug(self $model, string $original): void { $model->slug = Str::slug($original); } /** * 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): void { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id); } /** * To sitemap tag method. * * @return \Spatie\Sitemap\Tags\Url|string|array */ public function toSitemapTag(): \Spatie\Sitemap\Tags\Url | string | array { $model = $this; $route = 'fo.sites.show'; $params = ['site' => $model]; return SitemapHelper::generateUrl($route, $params, $model); } // * RELATIONS. /** * The logos that belong to the site. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\App\Models\Logo,$this> */ public function logos(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(Logo::class); } }