ldc-legaulois/app/Http/Controllers/HomeController.php

77 lines
2.7 KiB
PHP
Raw Normal View History

2020-11-04 15:07:21 +01:00
<?php
namespace App\Http\Controllers;
use App\Enums\Days;
use App\Http\Requests\SingleOptinRequest;
use App\Lib\Utils;
2020-11-10 08:57:25 +01:00
use App\Models\HomeSliderPictures;
use App\Models\NewsSliderPictures;
2021-04-08 20:06:14 +02:00
use App\Models\OCBBreeder;
2021-04-20 07:28:41 +02:00
use App\Models\PageJoinus;
use App\Models\Products;
2021-10-13 12:06:41 +02:00
use App\Models\Reduction;
use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\CrmException;
use LDC\LDCCRM\Models\SingleOptin;
2020-11-04 15:07:21 +01:00
class HomeController extends Controller
{
2021-03-21 22:51:19 +01:00
public function index()
2020-11-04 15:07:21 +01:00
{
2020-11-10 08:57:25 +01:00
$slides = HomeSliderPictures::where([
2021-03-12 10:27:19 +01:00
['days', '&', Days::make(strtolower(date('D')))->value],
2020-11-10 08:57:25 +01:00
['status', 1]
])->orderBy('order', 'ASC')->get();
$newsSlides = NewsSliderPictures::where('status', 1)->orderBy('order', 'ASC')->get();
// Images d'éleveur
2021-04-08 20:06:14 +02:00
$breeder = OCBBreeder::ocbValidated()->select('picture', 'first_name', 'last_name')
2021-03-19 10:32:45 +01:00
->inRandomOrder()->first();
// Les produits star
2021-03-21 22:51:19 +01:00
$starProducts = Products::select('picture', 'name', 'slug')->where([['star', 1], ['status', 1]])
->inRandomOrder()->limit(3)->get();
2021-03-20 14:38:15 +01:00
if ($starProducts->count() < 3) {
2021-03-21 22:51:19 +01:00
$starProducts = $starProducts->concat(Products::select('picture', 'name', 'slug')
->where([['star', 0], ['status', 1]])->inRandomOrder()->limit(3 - $starProducts->count())->get());
2021-03-20 14:38:15 +01:00
}
2021-03-12 16:02:20 +01:00
// Montant réductions
2021-10-13 12:06:41 +02:00
$montantReduction = Reduction::whereStatus(true)->sum('value');
2021-03-12 16:02:20 +01:00
2021-10-13 12:06:41 +02:00
return view('home', compact('slides', 'newsSlides', 'breeder', 'starProducts', 'montantReduction'));
2021-03-12 16:02:20 +01:00
}
/**
* Call the CRM to add a single optin
2022-02-10 16:14:58 +01:00
* @param string $email Email to add as a single optin
* @return string Message to print to the customer
2021-03-12 16:02:20 +01:00
*/
public function addOptIn(SingleOptinRequest $request)
2021-03-12 16:02:20 +01:00
{
try {
SingleOptin::setStatus($request->email, true);
2021-03-12 16:02:20 +01:00
return __("Votre inscription à la newsletter a été prise en compte");
} catch (ApiErrorException $e) {
// Il y a deja un customerInfo avec optin a true pour ce customer
if ($e->getApiError()->equals(ApiError::singleOptinCustomerExists())) {
return trans('Vous êtes déjà inscrit avec cette adresse email');
2021-03-12 16:02:20 +01:00
}
Utils::sendEmailErrorManually($e);
return trans('Une erreur est survenue, merci de réessayer ultérieurement');
} catch (CrmException $e) {
Utils::sendEmailErrorManually($e);
return trans('Une erreur est survenue, merci de réessayer ultérieurement');
2021-03-12 16:02:20 +01:00
}
2020-11-04 15:07:21 +01:00
}
2021-04-20 07:28:41 +02:00
public function joinUs()
{
return view('front.joinus', ['joinus' => PageJoinus::firstOrFail()]);
}
2020-11-04 15:07:21 +01:00
}