src/Controller/Bikers/ListProductIndexController.php line 248

Open in your IDE?
  1. <?php
  2. namespace BitBag\OpenMarketplace\Controller\Bikers;
  3. use BitBag\OpenMarketplace\Entity\ProductListing\ProductDraft;
  4. use BitBag\OpenMarketplace\Entity\ProductListing\ProductListingInterface;
  5. use BitBag\OpenMarketplace\Repository\ProductListing\ProductDraftRepository;
  6. use BitBag\OpenMarketplace\Repository\ProductListing\ProductListingRepositoryInterface;
  7. use BitBag\OpenMarketplace\Repository\ProductRepositoryInterface;
  8. use BitBag\OpenMarketplace\Updater\ProductAttributeUpdaterInterface;
  9. use Doctrine\Persistence\ObjectManager;
  10. use Sylius\Bundle\ResourceBundle\Controller\AuthorizationCheckerInterface;
  11. use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
  12. use Sylius\Bundle\ResourceBundle\Controller\FlashHelperInterface;
  13. use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
  14. use Sylius\Bundle\ResourceBundle\Controller\RedirectHandlerInterface;
  15. use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
  16. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  17. use Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandlerInterface;
  18. use Sylius\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface;
  19. use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
  20. use Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandlerInterface;
  21. use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface;
  22. use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
  23. use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
  24. use Sylius\Component\Resource\Factory\FactoryInterface;
  25. use Sylius\Component\Resource\Metadata\MetadataInterface;
  26. use Sylius\Component\Resource\Repository\RepositoryInterface;
  27. use Sylius\Component\Resource\ResourceActions;
  28. use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\Response;
  31. use Symfony\Component\HttpKernel\Exception\HttpException;
  32. use Symfony\Component\HttpFoundation\RedirectResponse;
  33. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  34. final class ListProductIndexController extends ResourceController
  35. {
  36.     private $taxonRepository;
  37.     private $productRepository;
  38.     private $productDraftRepository;
  39.     private $productListingRepository;
  40.     public function __construct(
  41.         MetadataInterface $metadata,
  42.         RequestConfigurationFactoryInterface $requestConfigurationFactory,
  43.         ?ViewHandlerInterface $viewHandler,
  44.         RepositoryInterface $repository,
  45.         FactoryInterface $factory,
  46.         NewResourceFactoryInterface $newResourceFactory,
  47.         ObjectManager $manager,
  48.         SingleResourceProviderInterface $singleResourceProvider,
  49.         ResourcesCollectionProviderInterface $resourcesFinder,
  50.         ResourceFormFactoryInterface $resourceFormFactory,
  51.         RedirectHandlerInterface $redirectHandler,
  52.         FlashHelperInterface $flashHelper,
  53.         AuthorizationCheckerInterface $authorizationChecker,
  54.         EventDispatcherInterface $eventDispatcher,
  55.         ?StateMachineInterface $stateMachine,
  56.         ResourceUpdateHandlerInterface $resourceUpdateHandler,
  57.         ResourceDeleteHandlerInterface $resourceDeleteHandler,
  58.         TaxonRepositoryInterface $taxonRepository,
  59.         ProductRepositoryInterface $productRepository,
  60.         ProductDraftRepository $productDraftRepository,
  61.         ProductListingRepositoryInterface $productListingRepository
  62.     ) {
  63.         parent::__construct(
  64.             $metadata,
  65.             $requestConfigurationFactory,
  66.             $viewHandler,
  67.             $repository,
  68.             $factory,
  69.             $newResourceFactory,
  70.             $manager,
  71.             $singleResourceProvider,
  72.             $resourcesFinder,
  73.             $resourceFormFactory,
  74.             $redirectHandler,
  75.             $flashHelper,
  76.             $authorizationChecker,
  77.             $eventDispatcher,
  78.             $stateMachine,
  79.             $resourceUpdateHandler,
  80.             $resourceDeleteHandler,
  81.         );
  82.         $this->taxonRepository $taxonRepository;
  83.         $this->productRepository $productRepository;
  84.         $this->productDraftRepository $productDraftRepository;
  85.         $this->productListingRepository $productListingRepository;
  86.     }
  87.     public function indexAction(Request $request): Response
  88.     {
  89.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  90.         $this->isGrantedOr403($configurationResourceActions::INDEX);
  91.         $resources $this->resourcesCollectionProvider->get($configuration$this->repository);
  92.         $taxons $this->taxonRepository->findBy(['parent' => null]);
  93.         $taxons $taxons[0]->getChildren()->toArray();
  94.         $url $request->getUri();
  95.         $url explode('/'$url);
  96.         $url end($url);
  97. //
  98.         $category $this->taxonRepository->findOneBy(['code' => $url]);
  99.         $url $category;
  100.         // Date d'aujourd'hui
  101.         // Date d'aujourd'hui
  102.         $aujourdHui = new \DateTime();
  103.         $nombreJours = [];
  104.         $product $this->productRepository->findAll();
  105.         foreach ($product as $resource) {
  106.             // Date de création de la ressource
  107.             $createdAt $resource->getCreatedAt();
  108.             $id $resource->getId();
  109.             // Calculer la différence entre les deux dates
  110.             $difference $aujourdHui->diff($createdAt);
  111. //            dd($resource);
  112.             // Obtenir le nombre de jours à partir de la différence
  113.             $nombreJours [$id] = $difference->days;
  114.         }
  115. //        dd($nombreJours);
  116.         $event $this->eventDispatcher->dispatchMultiple(ResourceActions::INDEX$configuration$resources);
  117.         $eventResponse $event->getResponse();
  118.         if (null !== $eventResponse) {
  119.             return $eventResponse;
  120.         }
  121.         if ($request->isMethod('POST')) {
  122.             $products $this->productRepository->findAll();
  123.             // Récupérer le paramètre 'category-product' sous forme de tableau
  124.             $categories $request->get('category-product', []);
  125.             // Récupérer les paramètres 'color-product' sous forme de tableau
  126.             $colors $request->get('color-product', []);
  127.             $colorseri = [];
  128.             foreach ($colors as $color) {
  129.                 $colorseri [] = serialize([$color]);
  130.             }
  131.             // Récupérer les paramètres 'size-product' sous forme de tableau
  132.             $sizes $request->get('size-product', []);
  133.             $sizeseri = [];
  134.             foreach ($sizes as $size) {
  135.                 $sizeseri [] = serialize([$size]);
  136.             }
  137.             // Récupérer le paramètre 'min-price-product'
  138.             $minPrice $request->get('min-price-product');
  139.             $minPrice =  $minPrice 100;
  140.             // Récupérer le paramètre 'max-price-product'
  141.             $maxPrice $request->get('max-price-product');
  142.             $maxPrice =  $maxPrice 100;
  143.             // Récupérer le paramètre 'state-product'
  144.             $state $request->get('state-product', []);
  145.             // Récupérer le paramètre 'brand-product'
  146.             $brand $request->get('brand-product', []);
  147.             // Récupérer le paramètre 'vendor-type-product'
  148.             $vendorType $request->get('vendor-type-product', []);
  149.             // Récupérer le paramètre 'location-product'
  150.             $location $request->get('location-product', []);
  151.             //find first 50 products
  152.             $e $this->productListingRepository->findBy([], null50);
  153.             $d = [];
  154.             foreach ($e as $product) {
  155.                 $id =  $this->productDraftRepository->findLatestDraft($product);
  156.                 $d [] = $id->getId();
  157.             }
  158.             $a $this->productDraftRepository->findAll();
  159.             $qb $this->productDraftRepository->createQueryBuilder('p')
  160.                 ->leftJoin('p.productListingPrice''plp')
  161.                 ->andWhere('plp.price BETWEEN :minPrice AND :maxPrice')
  162.                 ->setParameter('minPrice'$minPrice)
  163.                 ->setParameter('maxPrice'$maxPrice);
  164.             $qb->andWhere(
  165.                 $qb->expr()->in(
  166.                     'p.id',
  167.                     $d
  168.                 )
  169.             );
  170.             if($categories){
  171.                 $qb->leftJoin('p.productDraftTaxons''pt')
  172.                     ->leftJoin('pt.taxon''t')
  173.                     ->leftJoin('t.parent''tp'// Ajout d'une jointure avec le parent du taxon
  174.                     ->andWhere(
  175.                         $qb->expr()->orX(
  176.                             $qb->expr()->in('t.code'':categories'),
  177.                             $qb->expr()->in('tp.code'':categories'// Ajout d'une condition pour le code du parent
  178.                         )
  179.                     )
  180.                     ->setParameter('categories'$categories);
  181.             }
  182.             $results $qb->getQuery()->getResult();
  183.             $matching_products = array();
  184.             $matching_products_2 = array();
  185.             if($colorseri){
  186.                 foreach ($results as $product){
  187.                     $color_product $product->getColor();
  188.                     if (count(array_intersect($color_product$colors)) > 0) {
  189.                         $matching_products[] = $product;
  190.                     }
  191.                 }
  192.             }else{
  193.                 $matching_products $results;
  194.             }
  195.             if($sizeseri){
  196.                 foreach($matching_products as $product){
  197.                     $size_product $product->getSize();
  198.                     if (count(array_intersect($size_product$sizes)) > 0) {
  199.                         $matching_products_2[] = $product;
  200.                     }
  201.                 }
  202.             }
  203.             if(!$colorseri && !$sizeseri){
  204.                 $matching_products_2 $results;
  205.             }
  206.             $products = [];
  207.             foreach ($matching_products_2 as $draft){
  208.                 $products [] = $draft->getProductListing()->getProduct();
  209.             }
  210.             return $this->render($configuration->getTemplate(ResourceActions::INDEX '.html'), [
  211.                 'configuration' => $configuration,
  212.                 'metadata' => $this->metadata,
  213.                 'resources' => $resources,
  214.                 'taxons' => $taxons,
  215.                 'product_filter' => $products,
  216.                 'slug' => $url,
  217.                 'nombreJours' => $nombreJours,
  218.                 $this->metadata->getPluralName() => $resources,
  219.             ]);
  220.         }
  221.         if ($configuration->isHtmlRequest()) {
  222.             return $this->render($configuration->getTemplate(ResourceActions::INDEX '.html'), [
  223.                 'configuration' => $configuration,
  224.                 'metadata' => $this->metadata,
  225.                 'resources' => $resources,
  226.                 'taxons' => $taxons,
  227.                 'nombreJours' => $nombreJours,
  228.                 'product_filter' => null,
  229.                 'slug' => $url,
  230.                 $this->metadata->getPluralName() => $resources,
  231.             ]);
  232.         }
  233.         return $this->createRestView($configuration$resources);
  234.     }
  235. }