2020-09-30 16:17:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
namespace App\Http\Controllers\Bo;
|
2020-09-30 16:17:55 +02:00
|
|
|
|
2021-04-07 09:45:24 +02:00
|
|
|
use App\Enums\OCBType;
|
2020-09-30 16:17:55 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use App\Models\Breeders;
|
|
|
|
|
use App\Models\BreedersSites;
|
|
|
|
|
use App\Models\BreedersInuav;
|
|
|
|
|
|
|
|
|
|
class BreedersController extends Controller
|
|
|
|
|
{
|
2021-04-07 09:45:24 +02:00
|
|
|
public function index(OCBType $type)
|
2020-09-30 16:17:55 +02:00
|
|
|
{
|
2021-04-07 09:45:24 +02:00
|
|
|
$b = Breeders::whereHas('site', function ($q) use ($type) {
|
|
|
|
|
$q->where('ocbType', $type->value);
|
|
|
|
|
})->paginate();
|
|
|
|
|
return view('bo.breeders.index', compact('b', 'type'));
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
|
|
|
|
return view('bo.breeders.create')->with('types', $this->types);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
// Partie éleveur
|
|
|
|
|
$display = $request->display === null ? 0 : 1;
|
|
|
|
|
$breederCode = $request->breeder_code;
|
|
|
|
|
$firstName = $request->first_name;
|
|
|
|
|
$lastName = $request->last_name;
|
2021-01-29 12:17:47 +01:00
|
|
|
$slug = strtolower($lastName . '-' . $firstName . '-' . time());
|
2020-09-30 16:17:55 +02:00
|
|
|
$picture = $request->picture;
|
|
|
|
|
$postalCode = $request->postal_code;
|
|
|
|
|
$town = $request->town;
|
|
|
|
|
$testimony = $request->testimony;
|
|
|
|
|
$type = $request->type;
|
|
|
|
|
$status = 0;
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'breeder_code' => 'required',
|
|
|
|
|
'first_name' => 'required|max:150',
|
|
|
|
|
'last_name' => 'required|max:150',
|
|
|
|
|
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
'postal_code' => 'required|max:20',
|
|
|
|
|
'testimony' => 'required|max:1000',
|
|
|
|
|
]);
|
|
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
$imageName = Str::slug($lastName) . '-' . Str::slug($firstName) . '-le-gaulois.' . $picture->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture->move(public_path('uploads/images/eleveurs/' . date('Y') . '/' . date('m')), $imageName);
|
2020-09-30 16:17:55 +02:00
|
|
|
|
|
|
|
|
// Partie site de l'éleveur
|
|
|
|
|
$latitude = $request->latitude;
|
|
|
|
|
$longitude = $request->longitude;
|
|
|
|
|
$geoType = $request->geo_type;
|
|
|
|
|
$audit = $request->audit;
|
|
|
|
|
$validMarketingDate = $request->valid_marketing_date;
|
|
|
|
|
$presentationType = $request->presentation_type;
|
|
|
|
|
$videoLink = $request->video_link;
|
|
|
|
|
$picture1 = $request->picture1;
|
|
|
|
|
$picture2 = $request->picture2;
|
|
|
|
|
$picture3 = $request->picture3;
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'latitude' => 'required|max:150',
|
|
|
|
|
'longitude' => 'required|max:150',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($presentationType === "1") {
|
|
|
|
|
$request->validate([
|
2020-10-07 16:46:58 +02:00
|
|
|
'video_link' => 'required|max:255'
|
2020-09-30 16:17:55 +02:00
|
|
|
]);
|
|
|
|
|
} elseif ($presentationType === "2") {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'picture1' => 'image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
'picture2' => 'image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
'picture3' => 'image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($picture1 !== null) {
|
2021-03-20 14:38:15 +01:00
|
|
|
$imageNameSite1 = Str::slug($lastName) . '-' . Str::slug($firstName) . '-site1-elevage-le-gaulois.' . $picture1->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture1->move(public_path('uploads/images/elevages/' . date('Y') . '/' . date('m')), $imageNameSite1);
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
if ($picture2 !== null) {
|
2021-03-20 14:38:15 +01:00
|
|
|
$imageNameSite2 = Str::slug($lastName) . '-' . Str::slug($firstName) . '-site2-elevage-le-gaulois.' . $picture2->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture2->move(public_path('uploads/images/elevages/' . date('Y') . '/' . date('m')), $imageNameSite2);
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
if ($picture3 !== null) {
|
2021-03-20 14:38:15 +01:00
|
|
|
$imageNameSite3 = Str::slug($lastName) . '-' . Str::slug($firstName) . '-site3-elevage-le-gaulois.' . $picture3->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture3->move(public_path('uploads/images/elevages/' . date('Y') . '/' . date('m')), $imageNameSite3);
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Partie code INUAV
|
|
|
|
|
$inuavs = $request->inuavs;
|
|
|
|
|
|
|
|
|
|
// Ajout des différents objets en base
|
|
|
|
|
// Eleveur
|
|
|
|
|
$b = new Breeders();
|
|
|
|
|
$b->display = $display;
|
|
|
|
|
$b->breeder_code = $breederCode;
|
|
|
|
|
$b->first_name = $firstName;
|
|
|
|
|
$b->last_name = $lastName;
|
2021-01-14 13:44:59 +01:00
|
|
|
$b->slug = $slug;
|
2021-02-16 17:32:51 +01:00
|
|
|
$b->picture = 'uploads/images/eleveurs/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
2020-09-30 16:17:55 +02:00
|
|
|
$b->postal_code = $postalCode;
|
|
|
|
|
$b->town = $town;
|
|
|
|
|
$b->testimony = $testimony;
|
|
|
|
|
$b->type = $type;
|
|
|
|
|
$b->status = $status;
|
|
|
|
|
|
|
|
|
|
// Site d'élevage
|
|
|
|
|
$s = new BreedersSites();
|
|
|
|
|
$s->latitude = $latitude;
|
|
|
|
|
$s->longitude = $longitude;
|
|
|
|
|
$s->geo_type = $geoType;
|
|
|
|
|
$s->audit = $audit;
|
|
|
|
|
$s->valid_marketing_date = $validMarketingDate;
|
|
|
|
|
$s->presentation_type = $presentationType;
|
|
|
|
|
if ($presentationType === "1") {
|
|
|
|
|
$s->video_link = $videoLink;
|
|
|
|
|
} elseif ($presentationType === "2") {
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($picture1 !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$s->picture1 = 'uploads/images/elevages/' . date('Y') . '/' . date('m') . '/' . $imageNameSite1;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($picture2 !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$s->picture2 = 'uploads/images/elevages/' . date('Y') . '/' . date('m') . '/' . $imageNameSite2;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($picture3 !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$s->picture3 = 'uploads/images/elevages/' . date('Y') . '/' . date('m') . '/' . $imageNameSite3;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ajout en BDD
|
|
|
|
|
$b->save();
|
|
|
|
|
$s->breeder_id = $b->id;
|
|
|
|
|
$s->save();
|
|
|
|
|
if ($inuavs !== null) {
|
|
|
|
|
array_filter($inuavs);
|
2021-03-20 14:38:15 +01:00
|
|
|
foreach ($inuavs as $inuav) {
|
2020-09-30 16:17:55 +02:00
|
|
|
$i = new BreedersInuav();
|
|
|
|
|
$i->inuav = $inuav;
|
|
|
|
|
$i->breeder_id = $b->id;
|
|
|
|
|
$i->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.breeders.content', $type);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-07 09:45:24 +02:00
|
|
|
public function edit(OCBType $type, Breeders $breeder)
|
2020-09-30 16:17:55 +02:00
|
|
|
{
|
2021-04-07 09:45:24 +02:00
|
|
|
return view('bo.breeders.edit', compact('breeder', 'type'));
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-07 09:45:24 +02:00
|
|
|
public function update(Request $request, Breeders $breeder)
|
2020-09-30 16:17:55 +02:00
|
|
|
{
|
|
|
|
|
// Partie éleveur
|
|
|
|
|
$display = $request->display === null ? 0 : 1;
|
|
|
|
|
$breederCode = $request->breeder_code;
|
|
|
|
|
$firstName = $request->first_name;
|
|
|
|
|
$lastName = $request->last_name;
|
2021-01-29 12:17:47 +01:00
|
|
|
$slug = strtolower($lastName . '-' . $firstName . '-' . time());
|
2020-09-30 16:17:55 +02:00
|
|
|
$picture = $request->picture;
|
|
|
|
|
$postalCode = $request->postal_code;
|
|
|
|
|
$town = $request->town;
|
|
|
|
|
$testimony = $request->testimony;
|
|
|
|
|
$type = $request->type;
|
|
|
|
|
$status = $request->status === null ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'breeder_code' => 'required',
|
|
|
|
|
'first_name' => 'required|max:150',
|
|
|
|
|
'last_name' => 'required|max:150',
|
|
|
|
|
'postal_code' => 'required|max:20',
|
|
|
|
|
'testimony' => 'required|max:1000',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($picture !== null) {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Partie site de l'éleveur
|
|
|
|
|
$latitude = $request->latitude;
|
|
|
|
|
$longitude = $request->longitude;
|
|
|
|
|
$geoType = $request->geo_type;
|
|
|
|
|
$audit = $request->audit;
|
|
|
|
|
$validMarketingDate = $request->valid_marketing_date;
|
|
|
|
|
$presentationType = $request->presentation_type;
|
|
|
|
|
$videoLink = $request->video_link;
|
|
|
|
|
$picture1 = $request->picture1;
|
|
|
|
|
$picture2 = $request->picture2;
|
|
|
|
|
$picture3 = $request->picture3;
|
|
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
'latitude' => 'required|max:150',
|
|
|
|
|
'longitude' => 'required|max:150',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($type === 1) {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'videoLink' => 'required|max:255'
|
|
|
|
|
]);
|
|
|
|
|
} elseif ($type === 2) {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'picture1' => 'image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
'picture2' => 'image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
'picture3' => 'image|mimes:jpeg,png,jpg|max:2048',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Partie code INUAV
|
|
|
|
|
$inuavs = $request->inuavs;
|
|
|
|
|
|
2021-04-07 09:45:24 +02:00
|
|
|
$b = $breeder;
|
2020-09-30 16:17:55 +02:00
|
|
|
|
|
|
|
|
// Gestion des images
|
|
|
|
|
if ($picture !== null) {
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($b->picture !== null && file_exists(public_path() . '/' . $b->picture)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->picture);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
$imageName = Str::slug($lastName) . '-' . Str::slug($firstName) . '-le-gaulois.' . $picture->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture->move(public_path('uploads/images/eleveurs/' . date('Y') . '/' . date('m')), $imageName);
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($presentationType === "2") {
|
|
|
|
|
// Ajout des images seulement si le type de présentation est "Photos"
|
|
|
|
|
if ($picture1 !== null) {
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($b->site->picture1 !== null && file_exists(public_path() . '/' . $b->site->picture1)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture1);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
$imageNameSite1 = Str::slug($lastName) . '-' . Str::slug($firstName) . '-site1-elevage-le-gaulois.' . $picture1->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture1->move(public_path('uploads/images/elevages/' . date('Y') . '/' . date('m')), $imageNameSite1);
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
if ($picture2 !== null) {
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($b->site->picture2 !== null && file_exists(public_path() . '/' . $b->site->picture2)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture2);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
$imageNameSite2 = Str::slug($lastName) . '-' . Str::slug($firstName) . '-site2-elevage-le-gaulois.' . $picture2->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture2->move(public_path('uploads/images/elevages/' . date('Y') . '/' . date('m')), $imageNameSite2);
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
if ($picture3 !== null) {
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($b->site->picture3 !== null && file_exists(public_path() . '/' . $b->site->picture3)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture3);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
$imageNameSite3 = Str::slug($lastName) . '-' . Str::slug($firstName) . '-site3-elevage-le-gaulois.' . $picture3->extension();
|
2021-02-16 17:32:51 +01:00
|
|
|
$picture3->move(public_path('uploads/images/elevages/' . date('Y') . '/' . date('m')), $imageNameSite3);
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Sinon, suppression des photos puisqu'elles ne seront plus utilisées
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($b->site->picture1 !== null && file_exists(public_path() . '/' . $b->site->picture1)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture1);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($b->site->picture2 !== null && file_exists(public_path() . '/' . $b->site->picture2)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture2);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($b->site->picture3 !== null && file_exists(public_path() . '/' . $b->site->picture3)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture3);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ajout des différents objets en base
|
|
|
|
|
// Eleveur
|
|
|
|
|
$b->display = $display;
|
|
|
|
|
$b->breeder_code = $breederCode;
|
|
|
|
|
$b->first_name = $firstName;
|
|
|
|
|
$b->last_name = $lastName;
|
2021-01-14 13:44:59 +01:00
|
|
|
$b->slug = $slug;
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($picture !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$b->picture = 'uploads/images/eleveurs/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-30 16:17:55 +02:00
|
|
|
$b->postal_code = $postalCode;
|
|
|
|
|
$b->town = $town;
|
|
|
|
|
$b->testimony = $testimony;
|
|
|
|
|
$b->type = $type;
|
|
|
|
|
$b->status = $status;
|
|
|
|
|
|
|
|
|
|
// Site d'élevage
|
2021-01-08 14:00:22 +01:00
|
|
|
$s = $b->site;
|
2020-09-30 16:17:55 +02:00
|
|
|
$s->latitude = $latitude;
|
|
|
|
|
$s->longitude = $longitude;
|
|
|
|
|
$s->geo_type = $geoType;
|
|
|
|
|
$s->audit = $audit;
|
|
|
|
|
$s->valid_marketing_date = $validMarketingDate;
|
|
|
|
|
$s->presentation_type = $presentationType;
|
|
|
|
|
if ($presentationType === "1") {
|
|
|
|
|
$s->video_link = $videoLink;
|
|
|
|
|
$s->picture1 = null;
|
|
|
|
|
$s->picture2 = null;
|
|
|
|
|
$s->picture3 = null;
|
|
|
|
|
} elseif ($presentationType === "2") {
|
|
|
|
|
$s->video_link = null;
|
2021-03-20 14:38:15 +01:00
|
|
|
if ($picture1 !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$s->picture1 = 'uploads/images/elevages/' . date('Y') . '/' . date('m') . '/' . $imageNameSite1;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($picture2 !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$s->picture2 = 'uploads/images/elevages/' . date('Y') . '/' . date('m') . '/' . $imageNameSite2;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($picture3 !== null) {
|
2021-02-16 17:32:51 +01:00
|
|
|
$s->picture3 = 'uploads/images/elevages/' . date('Y') . '/' . date('m') . '/' . $imageNameSite3;
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ajout en BDD
|
|
|
|
|
$b->inuavs()->delete();
|
2021-01-08 14:00:22 +01:00
|
|
|
|
|
|
|
|
if ($inuavs !== null) {
|
|
|
|
|
array_filter($inuavs);
|
|
|
|
|
foreach ($inuavs as $inuav) {
|
|
|
|
|
$b->inuavs()->create([
|
|
|
|
|
'inuav' => $inuav,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-09-30 16:17:55 +02:00
|
|
|
}
|
2021-01-08 14:00:22 +01:00
|
|
|
|
|
|
|
|
$s->save();
|
2020-09-30 16:17:55 +02:00
|
|
|
$b->save();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.breeders.edit', $b->id)->with('success', 'Enregistré !');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
|
|
|
|
$b = Breeders::findOrFail($id);
|
|
|
|
|
|
2021-03-20 14:38:15 +01:00
|
|
|
if (file_exists(public_path() . '/' . $b->picture)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->picture);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($b->site->picture1 !== null && file_exists(public_path() . '/' . $b->site->picture1)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture1);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($b->site->picture2 !== null && file_exists(public_path() . '/' . $b->site->picture2)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture2);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
|
|
|
|
if ($b->site->picture3 !== null && file_exists(public_path() . '/' . $b->site->picture3)) {
|
2021-01-19 12:28:54 +01:00
|
|
|
unlink(public_path() . '/' . $b->site->picture3);
|
2021-03-20 14:38:15 +01:00
|
|
|
}
|
2020-09-30 16:17:55 +02:00
|
|
|
|
2020-10-07 16:46:58 +02:00
|
|
|
$i = BreedersInuav::where('breeder_id', $id);
|
|
|
|
|
$i->delete();
|
|
|
|
|
$s = BreedersSites::where('breeder_id', $id);
|
|
|
|
|
$s->delete();
|
|
|
|
|
|
2020-09-30 16:17:55 +02:00
|
|
|
$b->delete();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.breeders.content', 'volaille');
|
|
|
|
|
}
|
|
|
|
|
}
|