src/Controller/Shop/ProductsVisitedController.php line 74

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Shop;
  4. use App\Entity\Customer\Customer;
  5. use App\Repository\Parameter\ParameterRepository;
  6. use App\Repository\Product\ProductRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Sylius\Bundle\CoreBundle\Doctrine\ORM\CustomerRepository;
  9. use Sylius\Component\Channel\Context\ChannelContextInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. final class ProductsVisitedController extends AbstractController
  15. {
  16.     public function __construct(private CustomerRepository $customerRepository,
  17.                                 private ProductRepository $productRepository,
  18.                                 private ParameterRepository $parameterRepository,
  19.                                 private EntityManagerInterface $em,
  20.                                 private ChannelContextInterface $channelContext){}
  21.     public function addProductAjaxAction(Request $request): JsonResponse {
  22.         $customerId $request->request->get('customerId');
  23.         $productId $request->request->get('productId');
  24.         if (!$productId)
  25.             return new JsonResponse(nullResponse::HTTP_BAD_REQUEST);
  26.         if ($customerId && $customerId !== 0) {
  27.             /** @var Customer $customer */
  28.             $customer $this->customerRepository->find($customerId);
  29.             if (!$customer)
  30.                 return new JsonResponse(nullResponse::HTTP_BAD_REQUEST);
  31.             $productsVisited $this->updateProductsVisited($customer->getProductsVisited(), $productId);
  32.             $customer->setProductsVisited($productsVisited);
  33.             $this->em->persist($customer);
  34.             $this->em->flush();
  35.         }
  36.         else {
  37.             //mettre simplement le produit en session
  38.             $productsVisited $request->getSession()->get('productsVisited');
  39.             if (!$productsVisited || count($productsVisited) < 1) {
  40.                 $productsVisited = [];
  41.             }
  42.             $productsVisited $this->updateProductsVisited($productsVisited$productId);
  43.         }
  44.         //mettre le tableau en session
  45.         $request->getSession()->set('productsVisited'$productsVisited);
  46.         return new JsonResponse(nullResponse::HTTP_OK);
  47.     }
  48.     public function getCustomerProductsVisitedAction(Request $request) {
  49.         $customer null;
  50.         $customerId $request->query->get('customerId');
  51.         if ($customerId) {
  52.             /** @var Customer $customer */
  53.             $customer $this->customerRepository->find($customerId);
  54.         }
  55.         // récupérer le nombre de produits à mettre dans le tableau
  56.         $nbProduct $this->parameterRepository->findByCodeAndChannelCode('nb_product_visited'$this->channelContext->getChannel()->getCode());
  57.         if (!$nbProduct)
  58.             $nbProduct 8;
  59.         // récupérer les produits visités, et les trier dans l'ordre où ils ont été consultés
  60.         $productsIdVisited $request->getSession()->get('productsVisited');
  61.         $productsVisitedSorted = [];
  62.         if ($productsIdVisited && count($productsIdVisited) > 0) {
  63.             $productsVisited $this->productRepository->findProductsVisited($productsIdVisited, (int)$nbProduct);
  64.             foreach ($productsIdVisited as $productid) {
  65.                 foreach ($productsVisited as $product) {
  66.                     if ($product->getId() === (int)$productid) {
  67.                         $productsVisitedSorted[] = $product;
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.         return $this->render('@SyliusShop/Product/_horizontalListWithHeader.html.twig', ['products' => $productsVisitedSorted'header' => 'app.product.products_visited''trans' => true]);
  73.     }
  74.     private function updateProductsVisited($productsVisited$productId) {
  75.         //supprimer d'abord l'id du tableau correspondant s'il existe, pour le mettre ensuite en dernière position
  76.         if (in_array($productId$productsVisited)) {
  77.             $index array_search($productId$productsVisited);
  78.             unset($productsVisited[$index]);
  79.         }
  80.         // on garde que les 15 derniers produits consultés
  81.         if (count($productsVisited) > 15)
  82.             array_shift($productsVisited);
  83.         $productsVisited[] = $productId;
  84.         return $productsVisited;
  85.     }
  86. }