175 lines
5.1 KiB
PHP
175 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Utils;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\File;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
class Gamme extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
#[\Override]
|
|
protected $fillable = [
|
|
'parent_gamme_id',
|
|
'title',
|
|
'slug',
|
|
'text',
|
|
'color',
|
|
'picture',
|
|
'alt_text_picture',
|
|
'banner',
|
|
'alt_text_banner',
|
|
];
|
|
|
|
/**
|
|
* The attributes that are translatable.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $translatable = [
|
|
'title',
|
|
'slug',
|
|
'text',
|
|
'picture',
|
|
'alt_text_picture',
|
|
'banner',
|
|
'alt_text_banner',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
protected static function boot()
|
|
{
|
|
static::creating(function (Gamme $gamme): void {
|
|
if (is_a($gamme->picture, "SplFileInfo")) {
|
|
$gamme->picture = Utils::storeImage($gamme->picture, $gamme->getTable());
|
|
}
|
|
if (is_a($gamme->banner, "SplFileInfo")) {
|
|
$gamme->banner = Utils::storeImage($gamme->banner, $gamme->getTable());
|
|
}
|
|
|
|
if ($gamme->parent_gamme_id != null) {
|
|
// Si Creation gamme enfant sans enfant, alors il faut update sa couleur selon celle du parent.
|
|
$gamme->color = Gamme::whereId($gamme->parent_gamme_id)->select(['color'])->first()->color;
|
|
}
|
|
});
|
|
|
|
static::updating(function (Gamme $gamme): void {
|
|
if (is_a($gamme->picture, "SplFileInfo")) {
|
|
$gamme->picture = Utils::storeImage($gamme->picture, $gamme->getTable());
|
|
}
|
|
if (is_a($gamme->banner, "SplFileInfo")) {
|
|
$gamme->banner = Utils::storeImage($gamme->banner, $gamme->getTable());
|
|
}
|
|
if ($gamme->parent_gamme_id === null) {
|
|
// Si Update gamme parente, alors il faut update la couleur des enfants.
|
|
Gamme::whereParentGammeId($gamme->id)
|
|
->update(['color' => $gamme->color]);
|
|
} elseif ($gamme->childGammes()->exists()) {
|
|
/**
|
|
* Si Update gamme enfant qui a des enfants,
|
|
* alors il faut update sa couleur selon celle du parent
|
|
* et vider l'association parent/enfant des petits enfants
|
|
*/
|
|
$gamme->color = Gamme::whereId($gamme->parent_gamme_id)->select(['color'])->first()->color;
|
|
Gamme::whereParentGammeId($gamme->id)
|
|
->update(['parent_gamme_id' => null]);
|
|
} else {
|
|
// Si Update gamme enfant sans enfant, alors il faut update sa couleur selon celle du parent.
|
|
$gamme->color = Gamme::whereId($gamme->parent_gamme_id)->select(['color'])->first()->color;
|
|
}
|
|
});
|
|
|
|
static::deleting(function (Gamme $gamme): void {
|
|
if ($gamme->childGammes()->exists()) {
|
|
Gamme::whereParentGammeId($gamme->id)
|
|
->update(['parent_gamme_id' => null]);
|
|
}
|
|
});
|
|
|
|
static::deleted(function (Gamme $gamme): void {
|
|
if ($gamme->picture) {
|
|
$urlPicture = public_path() . $gamme->picture;
|
|
if ($urlPicture) {
|
|
if (File::exists($urlPicture)) {
|
|
File::delete($urlPicture);
|
|
}
|
|
}
|
|
}
|
|
if ($gamme->banner) {
|
|
$urlPicture = public_path() . $gamme->banner;
|
|
if ($urlPicture) {
|
|
if (File::exists($urlPicture)) {
|
|
File::delete($urlPicture);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
parent::boot();
|
|
}
|
|
|
|
/**
|
|
* map hex color notation to rgba() syntax
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function getRgbaColorAttribute()
|
|
{
|
|
$color = $this->color;
|
|
|
|
if (!$color) {
|
|
return '';
|
|
}
|
|
if ($color[0] == '#') {
|
|
$color = substr($color, 1);
|
|
}
|
|
[$r, $g, $b] = array_map(hexdec(...), str_split($color, (strlen($color) / 3)));
|
|
|
|
return "rgba($r,$g,$b,1)";
|
|
}
|
|
|
|
/**
|
|
* HasMany childGammes relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function childGammes()
|
|
{
|
|
return $this->hasMany(Gamme::class, 'parent_gamme_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsTo parentGamme relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function parentGamme()
|
|
{
|
|
return $this->belongsTo(Gamme::class);
|
|
}
|
|
|
|
/**
|
|
* Get Products for the Range.
|
|
* HasMany products relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function products()
|
|
{
|
|
return $this->hasMany(Product::class);
|
|
}
|
|
}
|