ldc-doux/app/Models/Gamme.php

176 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2021-12-20 17:21:09 +01:00
<?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;
2021-12-20 17:21:09 +01:00
class Gamme extends Model
{
use HasFactory;
use HasTranslations;
2021-12-20 17:21:09 +01:00
2022-08-29 17:23:10 +02:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
2026-10-27 09:12:51 +01:00
#[\Override]
2021-12-20 17:21:09 +01:00
protected $fillable = [
2021-12-21 17:30:39 +01:00
'parent_gamme_id',
2021-12-20 17:21:09 +01:00
'title',
2021-12-21 17:30:39 +01:00
'slug',
2021-12-20 17:21:09 +01:00
'text',
'color',
'picture',
'alt_text_picture',
'banner',
'alt_text_banner',
];
2022-08-29 17:23:10 +02:00
/**
* The attributes that are translatable.
*
* @var array
*/
public $translatable = [
'title',
'slug',
'text',
2022-01-23 15:03:05 +01:00
'picture',
'alt_text_picture',
2022-01-23 15:03:05 +01:00
'banner',
'alt_text_banner',
];
2022-08-29 17:23:10 +02:00
/**
* Perform any actions required after the model boots.
*
* @return void
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
2021-12-20 17:21:09 +01:00
protected static function boot()
{
2026-10-27 09:12:51 +01:00
static::creating(function (Gamme $gamme): void {
2022-03-01 18:40:52 +01:00
if (is_a($gamme->picture, "SplFileInfo")) {
2021-12-20 17:21:09 +01:00
$gamme->picture = Utils::storeImage($gamme->picture, $gamme->getTable());
}
2022-03-01 18:40:52 +01:00
if (is_a($gamme->banner, "SplFileInfo")) {
2021-12-20 17:21:09 +01:00
$gamme->banner = Utils::storeImage($gamme->banner, $gamme->getTable());
}
2021-12-21 17:30:39 +01:00
if ($gamme->parent_gamme_id != null) {
2022-08-29 17:23:10 +02:00
// Si Creation gamme enfant sans enfant, alors il faut update sa couleur selon celle du parent.
2021-12-21 17:30:39 +01:00
$gamme->color = Gamme::whereId($gamme->parent_gamme_id)->select(['color'])->first()->color;
}
2021-12-20 17:21:09 +01:00
});
2026-10-27 09:12:51 +01:00
static::updating(function (Gamme $gamme): void {
2022-03-01 18:40:52 +01:00
if (is_a($gamme->picture, "SplFileInfo")) {
2021-12-20 17:21:09 +01:00
$gamme->picture = Utils::storeImage($gamme->picture, $gamme->getTable());
}
2022-03-01 18:40:52 +01:00
if (is_a($gamme->banner, "SplFileInfo")) {
2021-12-20 17:21:09 +01:00
$gamme->banner = Utils::storeImage($gamme->banner, $gamme->getTable());
}
2021-12-21 17:30:39 +01:00
if ($gamme->parent_gamme_id === null) {
2022-08-29 17:23:10 +02:00
// Si Update gamme parente, alors il faut update la couleur des enfants.
2021-12-21 17:30:39 +01:00
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 {
2022-08-29 17:23:10 +02:00
// Si Update gamme enfant sans enfant, alors il faut update sa couleur selon celle du parent.
2021-12-21 17:30:39 +01:00
$gamme->color = Gamme::whereId($gamme->parent_gamme_id)->select(['color'])->first()->color;
}
2021-12-20 17:21:09 +01:00
});
2026-10-27 09:12:51 +01:00
static::deleting(function (Gamme $gamme): void {
if ($gamme->childGammes()->exists()) {
Gamme::whereParentGammeId($gamme->id)
->update(['parent_gamme_id' => null]);
}
});
2026-10-27 09:12:51 +01:00
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);
}
}
}
});
2021-12-20 17:21:09 +01:00
parent::boot();
}
2022-08-29 17:23:10 +02:00
/**
* map hex color notation to rgba() syntax
*
* @return string
*/
2026-10-27 09:12:51 +01:00
protected function getRgbaColorAttribute()
2021-12-21 17:30:39 +01:00
{
$color = $this->color;
if (!$color) {
return '';
}
if ($color[0] == '#') {
$color = substr($color, 1);
}
2026-10-27 09:12:51 +01:00
[$r, $g, $b] = array_map(hexdec(...), str_split($color, (strlen($color) / 3)));
2021-12-21 17:30:39 +01:00
2022-03-01 18:40:52 +01:00
return "rgba($r,$g,$b,1)";
2021-12-21 17:30:39 +01:00
}
2022-08-29 17:23:10 +02:00
/**
* HasMany childGammes relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2021-12-20 17:21:09 +01:00
public function childGammes()
{
return $this->hasMany(Gamme::class, 'parent_gamme_id');
}
2022-08-29 17:23:10 +02:00
/**
* BelongsTo parentGamme relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2021-12-20 17:21:09 +01:00
public function parentGamme()
{
return $this->belongsTo(Gamme::class);
}
2021-12-24 15:58:59 +01:00
/**
* Get Products for the Range.
2022-08-29 17:23:10 +02:00
* HasMany products relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2021-12-24 15:58:59 +01:00
*/
public function products()
{
return $this->hasMany(Product::class);
}
2021-12-20 17:21:09 +01:00
}