<?php
declare(strict_types=1);
namespace App\Controller\Shop;
use App\Entity\Customer\Customer;
use App\Repository\Parameter\ParameterRepository;
use App\Repository\Product\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\CustomerRepository;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
final class ProductsVisitedController extends AbstractController
{
public function __construct(private CustomerRepository $customerRepository,
private ProductRepository $productRepository,
private ParameterRepository $parameterRepository,
private EntityManagerInterface $em,
private ChannelContextInterface $channelContext){}
public function addProductAjaxAction(Request $request): JsonResponse {
$customerId = $request->request->get('customerId');
$productId = $request->request->get('productId');
if (!$productId)
return new JsonResponse(null, Response::HTTP_BAD_REQUEST);
if ($customerId && $customerId !== 0) {
/** @var Customer $customer */
$customer = $this->customerRepository->find($customerId);
if (!$customer)
return new JsonResponse(null, Response::HTTP_BAD_REQUEST);
$productsVisited = $this->updateProductsVisited($customer->getProductsVisited(), $productId);
$customer->setProductsVisited($productsVisited);
$this->em->persist($customer);
$this->em->flush();
}
else {
//mettre simplement le produit en session
$productsVisited = $request->getSession()->get('productsVisited');
if (!$productsVisited || count($productsVisited) < 1) {
$productsVisited = [];
}
$productsVisited = $this->updateProductsVisited($productsVisited, $productId);
}
//mettre le tableau en session
$request->getSession()->set('productsVisited', $productsVisited);
return new JsonResponse(null, Response::HTTP_OK);
}
public function getCustomerProductsVisitedAction(Request $request) {
$customer = null;
$customerId = $request->query->get('customerId');
if ($customerId) {
/** @var Customer $customer */
$customer = $this->customerRepository->find($customerId);
}
// récupérer le nombre de produits à mettre dans le tableau
$nbProduct = $this->parameterRepository->findByCodeAndChannelCode('nb_product_visited', $this->channelContext->getChannel()->getCode());
if (!$nbProduct)
$nbProduct = 8;
// récupérer les produits visités, et les trier dans l'ordre où ils ont été consultés
$productsIdVisited = $request->getSession()->get('productsVisited');
$productsVisitedSorted = [];
if ($productsIdVisited && count($productsIdVisited) > 0) {
$productsVisited = $this->productRepository->findProductsVisited($productsIdVisited, (int)$nbProduct);
foreach ($productsIdVisited as $productid) {
foreach ($productsVisited as $product) {
if ($product->getId() === (int)$productid) {
$productsVisitedSorted[] = $product;
}
}
}
}
return $this->render('@SyliusShop/Product/_horizontalListWithHeader.html.twig', ['products' => $productsVisitedSorted, 'header' => 'app.product.products_visited', 'trans' => true]);
}
private function updateProductsVisited($productsVisited, $productId) {
//supprimer d'abord l'id du tableau correspondant s'il existe, pour le mettre ensuite en dernière position
if (in_array($productId, $productsVisited)) {
$index = array_search($productId, $productsVisited);
unset($productsVisited[$index]);
}
// on garde que les 15 derniers produits consultés
if (count($productsVisited) > 15)
array_shift($productsVisited);
$productsVisited[] = $productId;
return $productsVisited;
}
}