ldc-legaulois/app/Models/OCBBreeder.php

146 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
2021-03-19 10:32:45 +01:00
use App\Enums\EleveurOcbStatus;
2021-04-08 15:20:48 +02:00
use App\Enums\GeoType;
use App\Enums\OCBType;
use App\Enums\PresentationType;
2021-04-09 10:49:38 +02:00
use Illuminate\Support\Facades\File;
2021-04-08 20:06:14 +02:00
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
2021-03-19 10:32:45 +01:00
use Kwaadpepper\Enum\Traits\CastsEnums;
2021-04-08 15:20:48 +02:00
// old Breeders
2021-04-09 12:52:54 +02:00
class OCBBreeder extends BreederForGmap
{
2021-03-19 10:32:45 +01:00
use CastsEnums;
protected $casts = [
2021-04-08 20:06:14 +02:00
'display' => 'boolean',
'inuavs' => 'array'
];
2021-04-08 15:20:48 +02:00
protected $enumCasts = [
'presentation_type' => PresentationType::class,
'ocbStatus' => EleveurOcbStatus::class,
'ocbType' => OCBType::class,
'geo_type' => GeoType::class
];
2021-04-08 15:20:48 +02:00
protected $table = 'ocb_breeders';
2021-04-08 20:06:14 +02:00
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) {
2021-05-19 11:23:11 +02:00
$slug = Str::slug(sprintf(
2021-04-08 20:06:14 +02:00
'%s %s %s',
2021-05-19 11:23:11 +02:00
$breeder->breeder_code,
2021-04-08 20:06:14 +02:00
$breeder->last_name,
2021-05-19 11:23:11 +02:00
$breeder->first_name
2021-04-08 20:06:14 +02:00
));
if ($breeder->slug !== $slug and static::where('slug', $slug)->count()) {
2021-05-19 11:23:11 +02:00
$slug .= sprintf('_%d', rand(100, 9000));
}
$breeder->slug = $slug;
OCBWaitingList::whereIn('inuav_code', $breeder->inuavs)->delete();
2021-04-08 20:06:14 +02:00
});
static::updating(function ($breeder) {
2021-05-19 11:23:11 +02:00
$slug = Str::slug(sprintf(
2021-04-08 20:06:14 +02:00
'%s %s %s',
2021-05-19 11:23:11 +02:00
$breeder->breeder_code,
2021-04-08 20:06:14 +02:00
$breeder->last_name,
2021-05-19 11:23:11 +02:00
$breeder->first_name
2021-04-08 20:06:14 +02:00
));
if ($breeder->slug !== $slug and static::where('slug', $slug)->count()) {
2021-05-19 11:23:11 +02:00
$slug .= sprintf('_%d', rand(100, 9000));
}
$breeder->slug = $slug;
OCBWaitingList::whereIn('inuav_code', $breeder->inuavs)->delete();
2021-04-08 20:06:14 +02:00
});
2021-04-09 10:49:38 +02:00
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'));
2021-04-08 20:06:14 +02:00
});
}
2021-04-08 15:20:48 +02:00
public function getRouteKeyName()
{
2021-04-08 15:20:48 +02:00
return "slug";
}
2021-03-19 10:32:45 +01:00
/**
* Get ocb validated breeders query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function ocbValidated()
{
2021-04-09 17:26:26 +02:00
return static::where('ocbStatus', EleveurOcbStatus::yes());
2021-03-19 10:32:45 +01:00
}
2021-04-08 20:06:14 +02:00
2021-04-09 10:49:38 +02:00
protected function getPictureAttribute()
2021-04-08 20:06:14 +02:00
{
2021-04-09 10:49:38 +02:00
return $this->pictureAttribute('picture');
}
protected function getPicture1Attribute()
2021-04-09 10:49:38 +02:00
{
return $this->pictureAttribute('picture1');
}
protected function getPicture2Attribute()
2021-04-09 10:49:38 +02:00
{
return $this->pictureAttribute('picture2');
}
protected function getPicture3Attribute()
2021-04-09 10:49:38 +02:00
{
return $this->pictureAttribute('picture3');
2021-04-08 20:06:14 +02:00
}
2021-04-09 10:49:38 +02:00
private function pictureAttribute(string $prop)
2021-04-08 20:06:14 +02:00
{
2021-04-09 10:49:38 +02:00
$p = $this->attributes[$prop];
2021-04-08 20:06:14 +02:00
// For migrated urls from 2016
if (
is_null($p) or
!strlen($p) or
strpos($p, '/uploads') === 0
) {
2021-04-09 10:49:38 +02:00
return $p;
2021-04-08 20:06:14 +02:00
}
2021-04-09 10:49:38 +02:00
return Storage::url($p);
2021-04-08 20:06:14 +02:00
}
}