2024-01-10 14:49:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
|
|
2024-04-19 17:03:25 +02:00
|
|
|
use App\Enums\Settings\SettingsType;
|
2024-07-10 17:10:33 +02:00
|
|
|
use App\Enums\StaticPageType;
|
2024-01-10 14:49:46 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2024-04-10 15:18:22 +02:00
|
|
|
use App\Http\Requests\Fo\SingleOptinRequest;
|
2024-04-12 12:15:18 +02:00
|
|
|
use App\Models\DemarcheSlide;
|
2024-09-19 09:21:21 +02:00
|
|
|
use App\Models\Discount;
|
2024-04-04 13:00:12 +02:00
|
|
|
use App\Models\MainSlide;
|
2024-04-19 10:08:36 +02:00
|
|
|
use App\Models\Product;
|
2024-04-19 17:03:25 +02:00
|
|
|
use App\Models\GeneralSetting;
|
2024-08-30 16:09:01 +02:00
|
|
|
use App\Models\News;
|
2024-06-17 16:50:56 +02:00
|
|
|
use App\Models\Range;
|
2024-05-16 14:06:45 +02:00
|
|
|
use App\Models\Recipe;
|
2024-07-10 17:10:33 +02:00
|
|
|
use App\Models\StaticPage;
|
2024-05-27 17:29:42 +02:00
|
|
|
use App\Models\Video;
|
2024-04-19 17:03:25 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-10-10 16:14:29 +02:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
use Illuminate\Validation\ValidationException as IlluminateValidationException;
|
2024-04-10 16:31:20 +02:00
|
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
|
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
2024-10-10 16:14:29 +02:00
|
|
|
use LDC\LDCCRM\Exceptions\ValidationException;
|
2024-04-10 16:31:20 +02:00
|
|
|
use LDC\LDCCRM\Models\SingleOptin;
|
2024-10-10 16:14:29 +02:00
|
|
|
use LDC\LDCCRM\Models\Customer;
|
2024-01-10 14:49:46 +01:00
|
|
|
|
|
|
|
|
class HomeController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function index(): \Illuminate\Contracts\View\View
|
|
|
|
|
{
|
2024-09-26 12:18:18 +02:00
|
|
|
$staticPageModel = StaticPage::getCachedStaticPage(StaticPageType::home);
|
|
|
|
|
$mainSlideModels = MainSlide::getPublishedOrderedWithApproaches();
|
|
|
|
|
$demarcheSlideModels = DemarcheSlide::getPublishedOrdered();
|
|
|
|
|
$rangeModels = Range::getPublishedOrderedBySlug();
|
2024-04-19 10:08:36 +02:00
|
|
|
$productViews = Product::getProductsViewsByRange();
|
2024-09-26 12:18:18 +02:00
|
|
|
$hotNewsSettings = GeneralSetting::getCachedGeneralSetting(SettingsType::hotNews);
|
|
|
|
|
$newsModels = News::getPublishedWithNewsCategory();
|
|
|
|
|
$recipeModels = Recipe::get9PublishedWithProducts();
|
|
|
|
|
$videoModels = Video::get9PublishedHomeVideosOrderedWithProducts();
|
2024-09-19 09:21:21 +02:00
|
|
|
$discountsTotal = Discount::query()->get()->sum('value');
|
2024-04-11 14:17:06 +02:00
|
|
|
return view('fo.pages.home', compact(
|
2024-07-10 17:10:33 +02:00
|
|
|
'staticPageModel',
|
2024-04-11 14:17:06 +02:00
|
|
|
'mainSlideModels',
|
2024-06-17 16:50:56 +02:00
|
|
|
'rangeModels',
|
2024-04-12 12:15:18 +02:00
|
|
|
'demarcheSlideModels',
|
2024-04-11 14:17:06 +02:00
|
|
|
'productViews',
|
|
|
|
|
'newsModels',
|
2024-04-19 17:03:25 +02:00
|
|
|
'recipeModels',
|
2024-05-27 17:29:42 +02:00
|
|
|
'hotNewsSettings',
|
2024-09-19 09:21:21 +02:00
|
|
|
'videoModels',
|
|
|
|
|
'discountsTotal',
|
2024-04-11 14:17:06 +02:00
|
|
|
));
|
2024-04-10 15:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-10 16:31:20 +02:00
|
|
|
/**
|
|
|
|
|
* Call the CRM to add a single optin
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Http\Requests\Fo\SingleOptinRequest $request
|
|
|
|
|
* @return string|array|null|\Illuminate\Contracts\Translation\Translator
|
|
|
|
|
* @throws \LDC\LDCCRM\Exceptions\ValidationException ValidationException.
|
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
|
|
|
|
|
*/
|
|
|
|
|
public function addOptIn(SingleOptinRequest $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
SingleOptin::setStatus($request->email, true);
|
|
|
|
|
return \response(trans("Votre inscription à la newsletter a été prise en compte"), 200);
|
|
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
// Il y a deja un customerInfo avec optin a true pour ce customer.
|
2024-07-30 16:05:27 +02:00
|
|
|
if ($e->getApiError()->equals(ApiError::singleOptinCustomerExists)) {
|
2024-04-10 16:31:20 +02:00
|
|
|
return trans('Vous êtes déjà inscrit avec cette adresse email');
|
|
|
|
|
}
|
2024-04-18 15:35:30 +02:00
|
|
|
\report($e);
|
2024-04-10 16:31:20 +02:00
|
|
|
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
|
|
|
|
|
} catch (CrmException $e) {
|
2024-04-18 15:35:30 +02:00
|
|
|
\report($e);
|
2024-04-10 16:31:20 +02:00
|
|
|
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-19 17:03:25 +02:00
|
|
|
|
2024-10-10 16:14:29 +02:00
|
|
|
/**
|
|
|
|
|
* Unsubscribe from newsletter
|
|
|
|
|
*
|
|
|
|
|
* @param App\Http\Requests\Fo\SingleOptinRequest $request
|
|
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
|
|
|
*/
|
|
|
|
|
public function unscribeNewsletter(SingleOptinRequest $request)
|
|
|
|
|
{
|
|
|
|
|
// TODO: handle single optin avec hashcode.
|
|
|
|
|
$message = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// ? Is hashcode valid ?
|
|
|
|
|
Validator::validate([
|
|
|
|
|
'hashcode' => $request->hashcode
|
|
|
|
|
], [
|
|
|
|
|
'hashcode' => 'required|uuid'
|
|
|
|
|
]);
|
|
|
|
|
if ($customer = Customer::find($request->hashcode)) {
|
|
|
|
|
$customer->opt_in = false;
|
|
|
|
|
// ! Les données peuvent être parsés en retour de la crm et inconsitantes (Héritage vielle crm).
|
|
|
|
|
try {
|
|
|
|
|
$customer->save();
|
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
$message = null;
|
|
|
|
|
}
|
|
|
|
|
$message = trans('Votre désinscription à la newsletter a été prise en compte');
|
|
|
|
|
}
|
|
|
|
|
} catch (IlluminateValidationException $e) {
|
|
|
|
|
// Les données peuvent être parsés en retour de la crm et sont inconsitantes.
|
|
|
|
|
$message = null;
|
|
|
|
|
} catch (ApiErrorException $e) {
|
|
|
|
|
if (!$e->getApiError()->equals(ApiError::singleOptinCustomerExists)) {
|
|
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
if ($request->hashcode) {
|
|
|
|
|
SingleOptin::setStatus($request->email, true);
|
|
|
|
|
$message = trans('Votre désinscription à la newsletter a été prise en compte');
|
|
|
|
|
} else {
|
|
|
|
|
$message = trans('Une erreur est survenue, merci de réessayer ultérieurement');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (CrmException $e) {
|
|
|
|
|
if (app()->bound('sentry')) {
|
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
|
}
|
|
|
|
|
$message = trans('Une erreur est survenue, merci de réessayer ultérieurement');
|
|
|
|
|
}//end try
|
|
|
|
|
|
|
|
|
|
if (!$message) {
|
|
|
|
|
$message = trans('Le lien de désinscription est invalide');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('front.singleoptin-unsubscribe', compact('message'));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 17:03:25 +02:00
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
|
*/
|
|
|
|
|
public function updateHotNewsSatus(): \Illuminate\Http\JsonResponse
|
|
|
|
|
{
|
2024-04-26 09:29:39 +02:00
|
|
|
Cache::put('hotNewsIsDisplayed', true, now()->addHours(24));
|
2024-04-19 17:03:25 +02:00
|
|
|
return response()->json();
|
|
|
|
|
}
|
2024-01-10 14:49:46 +01:00
|
|
|
}
|