'array', 'status' => 'bool', 'show_ingredients' => 'bool', 'show_preparation_advices' => 'bool', 'show_sales_pitch' => 'bool', 'show_allergen_list' => 'bool', 'show_nutrionnal_values' => 'bool', 'publish_at' => 'datetime', 'unpublish_at' => 'datetime' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { static::assertTitleIsUnique($model->title); self::validateLabels($model->labels); $model->slug = Str::slug($model->title); ToolboxHelper::assertNoInternalBoUrls('description', $model->description); ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients); ToolboxHelper::assertNoInternalBoUrls('preparation_advices', $model->preparation_advices); ToolboxHelper::assertNoInternalBoUrls('sales_pitch', $model->sales_pitch); ToolboxHelper::assertNoInternalBoUrls('allergen_list', $model->allergen_list); $model->slug = Str::slug($model->title); $model->description = clean($model->description); $model->ingredients = clean($model->ingredients); $model->preparation_advices = clean($model->preparation_advices); $model->sales_pitch = clean($model->sales_pitch); $model->allergen_list = clean($model->allergen_list); $model->picture = ToolboxHelper::storeImage($model, $model->picture); $model->picture_thumb = ToolboxHelper::storeImage($model, $model->picture_thumb); if (!$model->status) { $model->publish_at = null; } else { $model->unpublish_at = null; } }); static::updating(function (self $model) { static::assertTitleIsUnique($model->title, $model->id); self::validateLabels($model->labels); $model->slug = Str::slug($model->title); ToolboxHelper::assertNoInternalBoUrls('description', $model->description); ToolboxHelper::assertNoInternalBoUrls('ingredients', $model->ingredients); ToolboxHelper::assertNoInternalBoUrls('preparation_advices', $model->preparation_advices); ToolboxHelper::assertNoInternalBoUrls('sales_pitch', $model->sales_pitch); ToolboxHelper::assertNoInternalBoUrls('allergen_list', $model->allergen_list); $model->slug = Str::slug($model->title); $model->description = clean($model->description); $model->ingredients = clean($model->ingredients); $model->preparation_advices = clean($model->preparation_advices); $model->sales_pitch = clean($model->sales_pitch); $model->allergen_list = clean($model->allergen_list); // * Store new picture if changed if ($model->picture != $model->getOriginal('picture')) { $model->picture = ToolboxHelper::storeImage($model, $model->picture); } // * Store new picture_thumb if changed if ($model->picture_thumb != $model->getOriginal('picture_thumb')) { $model->picture_thumb = ToolboxHelper::storeImage($model, $model->picture_thumb); } }); static::updated(function (self $model) { ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'description'); ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'ingredients'); ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'preparation_advices'); ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'sales_pitch'); ToolboxHelper::removeUnusedImagesOnCkEditor($model, 'allergen_list'); if ($model->picture != $model->getOriginal('picture')) { $model->picture = ToolboxHelper::removeOldImage($model, 'picture'); } }); static::deleting(function (self $model) { ToolboxHelper::removeImage($model, 'picture'); ToolboxHelper::removeImage($model, 'picture_thumb'); ToolboxHelper::removeImagesOnCkEditor($model, 'description'); ToolboxHelper::removeImagesOnCkEditor($model, 'ingredients'); ToolboxHelper::removeImagesOnCkEditor($model, 'preparation_advices'); ToolboxHelper::removeImagesOnCkEditor($model, 'sales_pitch'); ToolboxHelper::removeImagesOnCkEditor($model, 'allergen_list'); }); } // * RELATIONSHIPS /** * HasOne incoNutritionalValue relation * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function incoNutritionalValues() { return $this->hasOne(IncoNutritionalValue::class); } /** * BelongsTo range relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function range() { return $this->belongsTo(Range::class); } // * METHODS /** * Asserts using validation that the title is unique * * @param mixed $title * @param integer|null $id * @return void * @throws \Illuminate\Validation\ValidationException If an actu title already exists. */ private static function assertTitleIsUnique($title, ?int $id = null) { $table = (new self())->getTable(); ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id); ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id); } /** * * @param array $labels * @return void * @throws \Illuminate\Validation\ValidationException If a label is invalid. */ private static function validateLabels(array $labels) { collect($labels)->map(function ($label) { if (!\is_object($label) or !($label instanceof ProductLabelEnum)) { if ( !\is_array($label) or !\array_key_exists('value', $label) or !\in_array($label['value'], ProductLabelEnum::toValues()) ) { throw ValidationException::withMessages([ 'labels' => 'Invalid label given' ]); } } }); } }