ldc-bogt/app/Models/Eleveur.php
Jérémy Munsch 529c99a055
- Add bootstrap paginator
- add Civilité enum to Eleveur
- add type enum to eleveur
- add search bar on eleveurs
- Rewrite eleveur CRUD
- Add flashMessages module
2022-03-09 16:01:33 +01:00

153 lines
3.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\EleveurCivilite;
use App\Enums\EleveurType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Validation\ValidationException;
use Kwaadpepper\Enum\Traits\CastsEnums;
class Eleveur extends Model
{
use CastsEnums;
use SoftDeletes;
/**
* Indicates if the model should be timestamped.
*
* @var boolean
*/
public $timestamps = false;
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'code_inuav' => 'array'
];
/**
* Map attribute names to enum classes.
*
* @var array
*/
protected $enumCasts = [
'civilite' => EleveurCivilite::class,
'type' => EleveurType::class
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'type',
'code_cafel',
'code_ede',
'civilite',
'nom',
'prenom',
'naissance',
'cp',
'ville',
'elv_date_debut',
'code_inuav',
'exploitation',
'aboutyou',
'vocation',
'likeinjob',
'loue_means',
'path_photo1',
'path_photo2',
'marquage_oeuf',
'marquage_b1',
'marquage_b2'
];
/**
* @var string
*/
protected $table = 'eleveurs';
/**
* The accessors to append to the model's array form.
*
* @var array
*/
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) {
return self::areInuavCodesUniques($model);
});
}
/**
* Questions attribute accessor
*
* @return boolean
*/
public function getQuestionsAttribute(): bool
{
if ($this->aboutyou != null && $this->aboutyou != '') {
return true;
}
if ($this->vocation != null && $this->vocation != '') {
return true;
}
if ($this->likeinjob != null && $this->likeinjob != '') {
return true;
}
if ($this->loue_means != null && $this->loue_means != '') {
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
{
if (count($eleveur->code_inuav) > count(array_unique($eleveur->code_inuav))) {
throw ValidationException::withMessages([
'code_inuav' => 'Les codes INUAVS doinvent être uniques'
]);
}
$q = self::query();
// * Update condition
$q->where(function ($q) use ($eleveur) {
foreach ($eleveur->code_inuav as $inuav) {
$q->orWhere('code_inuav', 'LIKE', "%$inuav%");
}
});
if ($eleveur->id) {
$q->where('id', '!=', $eleveur->id);
}
if ($q->count()) {
throw ValidationException::withMessages([
'code_inuav' => 'Les codes INUAVS doinvent être uniques'
]);
}
return true;
}
}