92 lines
3.5 KiB
PHP
92 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\SingleOptinRequest;
|
|
use App\Models\DemarcheSlide;
|
|
use App\Models\MainSlide;
|
|
use App\Models\Product;
|
|
use App\Models\GeneralSetting;
|
|
use App\Models\Recipe;
|
|
use App\Models\Video;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Exceptions\CrmException;
|
|
use LDC\LDCCRM\Models\SingleOptin;
|
|
|
|
class HomeController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function index(): \Illuminate\Contracts\View\View
|
|
{
|
|
$mainSlideModels = MainSlide::query()->where('published', true)->orderBy('order', 'ASC')
|
|
->with('approach')->get();
|
|
$demarcheSlideModels = DemarcheSlide::query()->where('published', true)->orderBy('order', 'ASC')->get();
|
|
$productViews = Product::getProductsViewsByRange();
|
|
$hotNewsSettings = GeneralSetting::query()->where('type', SettingsType::hotNews)->first();
|
|
// FIXME: replace 'for loop' by eloquent orm on news publicated.
|
|
for ($i = 0; $i < 5; $i++) {
|
|
$newsModels[] = (object)['title' => "lorem ipsum"];
|
|
}
|
|
$newsModels = collect($newsModels);
|
|
$recipeModels = Recipe::query()->where('published', true)->with([
|
|
'products',
|
|
'products.submeats',
|
|
'consumptionMoment'
|
|
])->limit(9)->get();
|
|
$videoModels = Video::query()->where('published', true)->where('is_on_homepage', true)->orderBy('order', 'ASC')->with('product')->limit(9)->get();
|
|
return view('fo.pages.home', compact(
|
|
'mainSlideModels',
|
|
'demarcheSlideModels',
|
|
'productViews',
|
|
'newsModels',
|
|
'recipeModels',
|
|
'hotNewsSettings',
|
|
'videoModels'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
if ($e->getApiError()->equals(ApiError::singleOptinCustomerExists())) {
|
|
return trans('Vous êtes déjà inscrit avec cette adresse email');
|
|
}
|
|
\report($e);
|
|
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
|
|
} catch (CrmException $e) {
|
|
\report($e);
|
|
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function updateHotNewsSatus(): \Illuminate\Http\JsonResponse
|
|
{
|
|
Cache::put('hotNewsIsDisplayed', true, now()->addHours(24));
|
|
return response()->json();
|
|
}
|
|
}
|