145 lines
3.9 KiB
PHP
145 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\EleveurOcbStatus;
|
|
use App\Enums\GeoType;
|
|
use App\Enums\OCBType;
|
|
use App\Enums\PresentationType;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
// old Breeders
|
|
class OCBBreeder extends BreederForGmap
|
|
{
|
|
use CastsEnums;
|
|
|
|
protected $casts = [
|
|
'display' => 'boolean',
|
|
'inuavs' => 'array'
|
|
];
|
|
|
|
protected $enumCasts = [
|
|
'presentation_type' => PresentationType::class,
|
|
'ocbStatus' => EleveurOcbStatus::class,
|
|
'ocbType' => OCBType::class,
|
|
'geo_type' => GeoType::class
|
|
];
|
|
|
|
protected $table = 'ocb_breeders';
|
|
|
|
protected $fillable = [
|
|
'breeder_code',
|
|
'first_name',
|
|
'last_name',
|
|
'postal_code',
|
|
'place',
|
|
'testimony',
|
|
'latitude',
|
|
'longitude',
|
|
'geo_type',
|
|
'ocbStatus',
|
|
'ocbType',
|
|
'presentation_type',
|
|
'audit',
|
|
'valid_marketing_date',
|
|
'video_link',
|
|
'status',
|
|
'display_random',
|
|
'inuavs',
|
|
];
|
|
|
|
protected static function booted()
|
|
{
|
|
static::creating(function ($breeder) {
|
|
$slug = Str::slug(sprintf(
|
|
'%s %s %s',
|
|
$breeder->breeder_code,
|
|
$breeder->last_name,
|
|
$breeder->first_name
|
|
));
|
|
if ($breeder->slug !== $slug and static::where('slug', $slug)->count()) {
|
|
$slug .= sprintf('_%d', rand(100, 9000));
|
|
}
|
|
$breeder->slug = $slug;
|
|
OCBWaitingList::whereIn('inuav_code', $breeder->inuavs)->delete();
|
|
});
|
|
static::updating(function ($breeder) {
|
|
$slug = Str::slug(sprintf(
|
|
'%s %s %s',
|
|
$breeder->breeder_code,
|
|
$breeder->last_name,
|
|
$breeder->first_name
|
|
));
|
|
if ($breeder->slug !== $slug and static::where('slug', $slug)->count()) {
|
|
$slug .= sprintf('_%d', rand(100, 9000));
|
|
}
|
|
$breeder->slug = $slug;
|
|
OCBWaitingList::whereIn('inuav_code', $breeder->inuavs)->delete();
|
|
});
|
|
static::deleted(function ($breeder) {
|
|
$removePicture = function ($picture) {
|
|
if ($picture) {
|
|
if (strpos($picture, '/uploads') === 0) {
|
|
File::delete(public_path($picture));
|
|
} else {
|
|
Storage::disk('public')->delete($picture);
|
|
}
|
|
}
|
|
};
|
|
$removePicture($breeder->getRawOriginal('picture'));
|
|
$removePicture($breeder->getRawOriginal('picture1'));
|
|
$removePicture($breeder->getRawOriginal('picture2'));
|
|
$removePicture($breeder->getRawOriginal('picture3'));
|
|
});
|
|
}
|
|
|
|
public function getRouteKeyName()
|
|
{
|
|
return "slug";
|
|
}
|
|
|
|
/**
|
|
* Get ocb validated breeders query
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public static function ocbValidated()
|
|
{
|
|
return static::where('ocbStatus', EleveurOcbStatus::yes());
|
|
}
|
|
|
|
protected function getPictureAttribute()
|
|
{
|
|
return $this->pictureAttribute('picture');
|
|
}
|
|
|
|
protected function getPicture1Attribute()
|
|
{
|
|
return $this->pictureAttribute('picture1');
|
|
}
|
|
protected function getPicture2Attribute()
|
|
{
|
|
return $this->pictureAttribute('picture2');
|
|
}
|
|
protected function getPicture3Attribute()
|
|
{
|
|
return $this->pictureAttribute('picture3');
|
|
}
|
|
|
|
private function pictureAttribute(string $prop)
|
|
{
|
|
$p = $this->attributes[$prop];
|
|
// For migrated urls from 2016
|
|
if (
|
|
is_null($p) or
|
|
!strlen($p) or
|
|
strpos($p, '/uploads') === 0
|
|
) {
|
|
return $p;
|
|
}
|
|
return Storage::url($p);
|
|
}
|
|
}
|