128 lines
3 KiB
PHP
128 lines
3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use App\Enums\BreederCivilty;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
use Illuminate\Validation\ValidationException;
|
||
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
||
|
|
|
||
|
|
class CafelPoultryBreeder extends Model
|
||
|
|
{
|
||
|
|
use CastsEnums;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The primary key for the model.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $primaryKey = 'cafel_code';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicates if the IDs are auto-incrementing.
|
||
|
|
*
|
||
|
|
* @var boolean
|
||
|
|
*/
|
||
|
|
public $incrementing = false;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicates if the model should be timestamped.
|
||
|
|
*
|
||
|
|
* @var boolean
|
||
|
|
*/
|
||
|
|
public $timestamps = false;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The attributes that are mass assignable.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $fillable = [
|
||
|
|
'cafel_code',
|
||
|
|
'civility',
|
||
|
|
'lastname',
|
||
|
|
'firstname',
|
||
|
|
'birth_date',
|
||
|
|
'postcode',
|
||
|
|
'city',
|
||
|
|
'date_start_operation',
|
||
|
|
'inuavs',
|
||
|
|
'issue_1',
|
||
|
|
'issue_2',
|
||
|
|
'path_picture_1',
|
||
|
|
'path_picture_2',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if inuav codes are unique
|
||
|
|
*
|
||
|
|
* @param self $breeder
|
||
|
|
* @return boolean
|
||
|
|
* @throws \Illuminate\Validation\ValidationException If inuav code is already used.
|
||
|
|
*/
|
||
|
|
protected static function areInuavCodesUniques(self $breeder): bool
|
||
|
|
{
|
||
|
|
$q = self::query();
|
||
|
|
// * Update condition
|
||
|
|
$q->where(function ($q) use ($breeder) {
|
||
|
|
foreach ($breeder->inuavs as $inuav) {
|
||
|
|
$q->orWhere('inuavs', 'LIKE', "%$inuav%");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
if ($breeder->cafel_code) {
|
||
|
|
$q->where('cafel_code', '!=', $breeder->cafel_code);
|
||
|
|
}
|
||
|
|
if ($q->count()) {
|
||
|
|
throw ValidationException::withMessages([
|
||
|
|
'inuavs' => 'Les codes INUAVS doinvent être uniques'
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|