149 lines
5.8 KiB
PHP
149 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\SingleOptinRequest;
|
|
use App\Models\ApproachSlide;
|
|
use App\Models\MainSlide;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Vite;
|
|
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')->get();
|
|
$approachSlideModels = ApproachSlide::query()->where('published', true)->orderBy('order', 'ASC')->get();
|
|
$productViews = $this->getProductsViewsByRange();
|
|
$newsModels = $this->getNewsModels();
|
|
|
|
return view('fo.pages.home', compact('mainSlideModels', 'approachSlideModels', 'productViews', 'newsModels'));
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param string|null $slug
|
|
* @return array<string>
|
|
*/
|
|
public function getProductsViewsByRange(string|null $slug = null): array
|
|
{
|
|
/** FIXME: replace 'first-range' by the first Range slug publicated with order by ASC. */
|
|
$rangeSlug = !is_null($slug) ? $slug : "first-range";
|
|
/** FIXME: replace this part of 'productModels' values by products associated to the range (limit 6, random). */
|
|
$productModels = [];
|
|
for ($i = 0; $i < 5; $i++) {
|
|
$productModels[] = (object)[
|
|
"name" => "Produit $i de la gamme $rangeSlug",
|
|
'picture' => Vite::asset('resources/assets/images/tmp/packaging.png'),
|
|
'product' => Vite::asset('resources/assets/images/tmp/product-picture.png'),
|
|
'tag' => Vite::asset('resources/assets/images/tmp/product-tag.png'),
|
|
'alt' => 'Alt attribute',
|
|
'title' => 'Title attribute',
|
|
'isFavorite' => (bool)random_int(0, 1),
|
|
'isNew' => (bool)random_int(0, 1),
|
|
];
|
|
}
|
|
$productViews = [];
|
|
foreach ($productModels as $productModel) {
|
|
/** FIXME: change fields of the product if necessary. */
|
|
$productViews[] = view('fo.partials.product-card', [
|
|
'name' => $productModel->name,
|
|
'packagePicture' => [
|
|
'src' => $productModel->picture,
|
|
'alt' => $productModel->alt,
|
|
'title' => $productModel->title,
|
|
],
|
|
'productPicture' => [
|
|
'src' => $productModel->product,
|
|
'alt' => $productModel->alt,
|
|
'title' => $productModel->title,
|
|
],
|
|
'tag' => [
|
|
'src' => $productModel->tag,
|
|
'alt' => $productModel->alt,
|
|
'title' => $productModel->title,
|
|
],
|
|
'isFavorite' => $productModel->isFavorite,
|
|
'isNew' => $productModel->isNew,
|
|
])->render();
|
|
} //end foreach
|
|
return $productViews;
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param \Illuminate\Http\Request|null $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function jsonGetProductsViewsByRange(Request $request): \Illuminate\Http\JsonResponse
|
|
{
|
|
return response()->json($this->getProductsViewsByRange($request->RANGESLUG));
|
|
}
|
|
|
|
/**
|
|
* Get news models in an array to make the split in front easier.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getNewsModels(): array
|
|
{
|
|
// TODO: Remplacer cette boucle par un appel à la base quand le crud aura été fait.
|
|
$newsModelsCollection = [];
|
|
for ($i = 0; $i < 3; $i++) {
|
|
$newsModelsCollection[] = [
|
|
'title' => \fake()->text(100),
|
|
];
|
|
}
|
|
$newsModelsCollection = \collect($newsModelsCollection);
|
|
// END TODO
|
|
|
|
// Split news in 3 parts.
|
|
return [
|
|
'first' => $newsModelsCollection->shift(),
|
|
'three_following' => $newsModelsCollection->shift(3),
|
|
'latest' => $newsModelsCollection,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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');
|
|
}
|
|
if (app()->bound('sentry')) {
|
|
app('sentry')->captureException($e);
|
|
}
|
|
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
|
|
} catch (CrmException $e) {
|
|
if (app()->bound('sentry')) {
|
|
app('sentry')->captureException($e);
|
|
}
|
|
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
|
|
}
|
|
}
|
|
}
|