'boolean' ]; /** * Perform any actions required after the model boots. * * @return void */ protected static function booted() { static::creating(function (self $model) { self::setStarLogic($model); $model->slug = Str::slug($model->name); $model->preparation_advice = clean($model->preparation_advice); $model->ingredients = clean($model->ingredients); $model->tips = clean($model->tips); OldSlugable::removeSameSlugs($model, $model->slug); if (!$model->incoNutritionalValues) { $inc = new IncoNutritionalValues(); $inc->product()->associate($inc); } }); static::updating(function (self $model) { self::setStarLogic($model); $model->slug = Str::slug($model->name); $model->preparation_advice = clean($model->preparation_advice); $model->ingredients = clean($model->ingredients); $model->tips = clean($model->tips); if ($model->slug != $model->getOriginal('slug')) { OldSlugable::removeSameSlugs($model, $model->getOriginal('slug')); } if ($model->status !== $model->getOriginal('status')) { Session::flash('info', trans('Le produit :product à été :status', [ 'product' => $model->name, 'status' => $model->status ? trans('publié') : trans('dépublié'), ])); } OldSlugable::insertSlug($model); }); } // * RELATIONSHIPS /** * belongsToMany ProductPictograms * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function pictograms() { return $this->belongsToMany( ProductPictograms::class, "products_pictograms", "product_id", "pictogram_id" ); } /** * BelongsTo Range * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function range() { return $this->belongsTo(Range::class); } /** * BelongsTo WidSubRangeget * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function subrange() { return $this->belongsTo(SubRange::class, 'sub_range_id'); } /** * BelongsTo MeatTypes * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function meatType() { return $this->belongsTo(MeatTypes::class, 'meat_type_id'); } /** * BelongsTo Label * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function label() { return $this->belongsTo(Label::class, 'label_id'); } /** * BelongsTo ConsumptionTimes * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function consumptionTimes() { return $this->belongsTo(ConsumptionTimes::class, 'consumption_times_id'); } /** * BelongsTo Recipes * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function recipe() { return $this->belongsTo(Recipes::class); } /** * HasMany IncoNutritionalValues * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function incoNutritionalValues() { return $this->hasOne(IncoNutritionalValues::class, 'product_id'); } /** * BelongsToMany Recipes * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function recipes() { return $this->belongsToMany(Recipes::class, 'recipes_products', 'product_id', 'recipe_id'); } /** * BelongsToMany Customers * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function customers() { return $this->belongsToMany(Customers::class, 'cart', 'product_id', 'customer_id'); } /** * BelongsToMany Reduction * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function reductions() { return $this->belongsToMany(Reduction::class, 'reduction_config', 'product_id', 'reduction_id'); } /** * BelongsTo XmlInco * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function xmlInco() { return $this->belongsTo(XmlInco::class, 'xml_inco_id'); } // * METHODS /** * Is start attribution logic * * @param self $product * @return void */ protected static function setStarLogic(self $product) { // * Prevent a product beeing star if not published if (!$product->getOriginal('star') and $product->star and !$product->status) { Session::flash('info', trans( 'Le produit :product ne peut être mis en avant car il est non publié.', ['product' => $product->name] )); $product->star = false; return; } if (!$product->status and $product->star) { $product->star = false; } // * Set Star logic for range $rangeStarProductQuery = Products::whereStar(true)->whereHas('range', function ($q) use ($product) { $q->whereId($product->range_id); }); $removedAllStar = false; // * Make sur only one product is star if ($product->star and $product->range_id) { // * Ensure only one product will be star for this range $updated = $rangeStarProductQuery ->where('id', '!=', $product->id) ->update(['star' => false]); if ($updated) { Session::flash('warning', trans('Le produit mis en avant de la gamme :range à été changé.', [ 'range' => optional(Range::whereId($product->range_id)->first())->name ])); } $removedAllStar = true; } if ($product->getOriginal('star') !== null && $product->getOriginal('star') !== $product->star) { Session::flash('warning', trans('Le produit mis en avant de la gamme :range à été changé.', [ 'range' => optional(Range::whereId($product->range_id)->first())->name ])); } // * If this product is not star then no product will be star if ( $product->getOriginal('star') and !$product->star and ($removedAllStar or $rangeStarProductQuery->count()) ) { Session::flash('warning', trans('La gamme :range n\'a plus de produits mis en avant.', [ 'range' => optional(Range::whereId($product->range_id)->first())->name ])); } } }