ldc-bogt/app/Models/CafelEggBreeder.php

163 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2019-12-04 17:30:46 +01:00
<?php
2022-03-08 11:40:52 +01:00
namespace App\Models;
2019-12-04 17:30:46 +01:00
use App\Enums\BreederCivilty;
2019-12-04 17:30:46 +01:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use Kwaadpepper\Enum\Traits\CastsEnums;
2022-03-08 09:42:26 +01:00
class CafelEggBreeder extends Model
2019-12-04 17:30:46 +01:00
{
use CastsEnums;
2020-07-20 11:20:46 +02:00
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'cafel_code';
/**
* Indicates if the IDs are auto-incrementing.
*
* @var boolean
*/
public $incrementing = false;
2022-03-08 11:40:52 +01:00
/**
* Indicates if the model should be timestamped.
*
* @var boolean
*/
2019-12-04 17:30:46 +01:00
public $timestamps = false;
2022-03-08 09:42:26 +01:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'birth_date' => 'date',
'date_start_operation' => 'date',
'inuavs' => 'array'
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'civility' => BreederCivilty::class
];
2019-12-04 17:30:46 +01:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'cafel_code',
'ede_code',
'civility',
'lastname',
'firstname',
'birth_date',
'postcode',
'city',
'date_start_operation',
'inuavs',
2020-07-20 11:20:46 +02:00
'exploitation',
'aboutyou',
'vocation',
'likeinjob',
2019-12-04 17:30:46 +01:00
'loue_means',
'path_picture_1',
'path_picture_2',
'marking_egg',
'marking_b1',
'marking_b2'
2019-12-04 17:30:46 +01:00
];
2022-03-08 09:42:26 +01:00
2022-03-08 11:40:52 +01:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
2020-01-30 17:01:44 +01:00
protected $appends = ['questions'];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
return self::areInuavCodesUniques($model);
});
static::updating(function (self $model) {
if ($model->getRawOriginal('path_picture_1') !== $model->path_picture_1) {
Storage::delete(\public_path($model->getRawOriginal('path_picture_1')));
}
if ($model->getRawOriginal('path_picture_2') !== $model->path_picture_2) {
Storage::delete(\public_path($model->getRawOriginal('path_picture_2')));
}
return self::areInuavCodesUniques($model);
});
}
// * METHODS
2022-03-08 11:40:52 +01:00
/**
* Questions attribute accessor
*
* @return boolean
*/
public function getQuestionsAttribute(): bool
2022-03-08 09:42:26 +01:00
{
if ($this->aboutyou != null && $this->aboutyou != '') {
2020-01-30 17:01:44 +01:00
return true;
}
2022-03-08 09:42:26 +01:00
if ($this->vocation != null && $this->vocation != '') {
2020-01-30 17:01:44 +01:00
return true;
}
2022-03-08 09:42:26 +01:00
if ($this->likeinjob != null && $this->likeinjob != '') {
2020-01-30 17:01:44 +01:00
return true;
}
2022-03-08 09:42:26 +01:00
if ($this->loue_means != null && $this->loue_means != '') {
2020-01-30 17:01:44 +01:00
return true;
}
return false;
}
/**
* Check if inuav codes are unique
*
* @param self $eleveur
* @return boolean
* @throws \Illuminate\Validation\ValidationException If inuav code is already used.
*/
protected static function areInuavCodesUniques(self $eleveur): bool
{
$q = self::query();
// * Update condition
$q->where(function ($q) use ($eleveur) {
foreach ($eleveur->inuavs as $inuav) {
$q->orWhere('inuavs', 'LIKE', "%$inuav%");
}
});
if ($eleveur->cafel_code) {
$q->where('cafel_code', '!=', $eleveur->cafel_code);
}
if ($q->count()) {
throw ValidationException::withMessages(['inuavs' => 'Les codes INUAVS doinvent être uniques'
]);
}
return true;
}
2019-12-04 17:30:46 +01:00
}