143 lines
4.5 KiB
PHP
143 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Breeders\OCBStatus;
|
|
use App\Enums\Breeders\GeoType;
|
|
use App\Enums\Breeders\OCBType;
|
|
use App\Enums\Breeders\PresentationType;
|
|
use App\Enums\Department;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property array $inuav_codes Array of inuav codes.
|
|
* @property string $first_name Firstname of the breeder.
|
|
* @property string $last_name Lastname of the breeder.
|
|
* @property string $picture Approach picture.
|
|
* @property string $picture_alt Picture alt attribute.
|
|
* @property string $picture_title Picture title attribute.
|
|
* @property integer $postal_code Breeder postal code.
|
|
* @property \App\Enums\Department $department Breeder department - enum.
|
|
* @property string $testimony Breeder testimony.
|
|
* @property array $approaches Array of associated approches.
|
|
* @property integer $order Breeder order.
|
|
* @property boolean $is_highlighted Is the breeder hightlighted ? (visible in carousel).
|
|
* @property boolean $published Is the breeder published ? (if false, not available in traceability).
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class Breeder extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'breeder_code',
|
|
'inuav_codes',
|
|
'first_name',
|
|
'last_name',
|
|
'picture',
|
|
'picture_alt',
|
|
'picture_title',
|
|
'postal_code',
|
|
'department',
|
|
'place',
|
|
'testimony',
|
|
'approaches',
|
|
'latitude',
|
|
'longitude',
|
|
'geo_type',
|
|
'ocbStatus',
|
|
'ocbType',
|
|
'presentation_type',
|
|
'audit',
|
|
'valid_marketing_date',
|
|
'video_link',
|
|
'order',
|
|
'is_highlighted',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'inuav_codes' => 'array',
|
|
'approaches' => 'array',
|
|
'department' => Department::class,
|
|
'is_highlighted' => 'boolean',
|
|
'published' => 'boolean',
|
|
'display_random' => 'boolean',
|
|
'presentation_type' => PresentationType::class,
|
|
'ocbStatus' => OCBStatus::class,
|
|
'ocbType' => OCBType::class,
|
|
'geo_type' => GeoType::class
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::setOrder($model);
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'picture');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'picture');
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
|
|
/** @var \App\Enums\Department|null $department */
|
|
$department = Str::of(strval($model->postal_code))->startsWith('97') ?
|
|
Department::make(Str::of(strval($model->postal_code))->take(3)) :
|
|
Department::make(Str::of(strval($model->postal_code))->take(2));
|
|
if (!is_null($department)) {
|
|
$model->department = $department;
|
|
} else {
|
|
// Set a default department if the postal code is incorrect.
|
|
$model->department = Department::ain;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
}
|