src/Controller/TaxonsController.php line 357

Open in your IDE?
  1. <?php 
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Twig\Environment;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. // use Symfony\Component\DependencyInjection\ContainerInterface;
  9. // use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. // use BitBag\SyliusCmsPlugin\Resolver\PageResourceResolverInterface;
  12. use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
  13. use Sylius\Component\Product\Repository\ProductRepositoryInterface;
  14. // use App\Entity\SiteProductOptionValueMatch;
  15. use BitBag\SyliusCmsPlugin\Entity\PageInterface;
  16. use Sylius\Component\Locale\Context\LocaleContextInterface;
  17. use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
  18. use App\Services\ElasticSearchService;
  19. use App\Entity\SiteFilterFacet;
  20. use App\Entity\SiteCollection;
  21. use Sylius\Component\Channel\Context\ChannelContextInterface;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. // use Doctrine\ORM\Query;
  24. class TaxonsController /*extends ResourceController*/
  25. {
  26.     
  27.     public function __construct(
  28.         Environment $twigEnvironment
  29.         ManagerRegistry $doctrine
  30.         ContainerInterface $container,
  31.         PageRepositoryInterface $pageRepository,
  32.         ProductRepositoryInterface $productRepository
  33.     ){
  34.         $this->twigEnvironment $twigEnvironment;
  35.         $this->doctrine $doctrine;
  36.         $this->container $container;
  37.         $this->pageRepository $pageRepository;
  38.         $this->productRepository $productRepository;
  39.     }
  40.     public function renderBlockAction(
  41.         Request $request,
  42.         LocaleContextInterface $localeContext
  43.         ProductRepositoryInterface $productRepository,
  44.         ProductVariantRepositoryInterface $productVariantRepository,
  45.         ElasticSearchService $elasticSearch,
  46.         TranslatorInterface $translator,
  47.     ): Response
  48.     {        
  49.        
  50.         $em $this->doctrine->getManager();
  51.         $taxoncode $request->query->get('code');
  52.         $typeblock $request->query->get('typeblock');
  53.         // dump( $typeblock);
  54.         // exit();
  55.         $repository $this->container->get('sylius.repository.taxon');
  56.         if(!empty($taxoncode) && !empty( $typeblock) && $typeblock != 21 && $typeblock != 25 ){            
  57.             
  58.             $taxonsChilds $repository->findChildren($taxoncode);
  59.             $taxons = array();
  60.             if(is_array($taxonsChilds) && count($taxonsChilds) > 0){
  61.                 foreach($taxonsChilds as $taxon){               
  62.                     $taxons[] =  $taxon;                       
  63.                 }
  64.             }            
  65.         }
  66.        
  67.         if($typeblock == 7){
  68.             // return new Response(
  69.             //     $this->twigEnvironment->render('@SyliusShop/Webapic/page/taxon.html.twig', [
  70.             //         'taxons' => $taxons,
  71.             //     ])
  72.             // );
  73.         // }else if($typeblock == 9){            
  74.             // return new Response(
  75.             //     $this->twigEnvironment->render('@SyliusShop/Webapic/page/encartbleu.html.twig', [
  76.             //         'taxons' => $taxons,
  77.             //     ])
  78.             // );             
  79.                        
  80.         }else if($typeblock == 17){  
  81.     
  82.             // if($request->query->has('select')){
  83.                 
  84.                 $taxon $repository->find($request->query->get('select'));
  85.                 $taxonsChilds $repository->findChildren($taxon->getCode());
  86.                 $taxons = array();
  87.                 if(is_array($taxonsChilds) && count($taxonsChilds) > 0){
  88.                     foreach($taxonsChilds as $taxon){               
  89.                         $taxons[] =  $taxon;                       
  90.                     }
  91.                 }    
  92.                 return new Response(
  93.                     $this->twigEnvironment->render('@SyliusShop/Webapic/page/select-taxon.html.twig', [
  94.                         'taxons' => $taxons,
  95.                         'name' => $request->query->get('name'),
  96.                     ])
  97.                 ); 
  98.             // }else{
  99.             //     return new Response(
  100.             //         $this->twigEnvironment->render('@SyliusShop/Webapic/page/block-cadeau.html.twig', [
  101.             //             //  'taxon' => $taxon,
  102.             //             'taxons' => $taxons,
  103.             //         ])
  104.             //     ); 
  105.             // }            
  106.            
  107.         }else if($typeblock == 23){  
  108.             $taxonsLastlevel = array();
  109.             // dump($taxons);
  110.             if(is_array($taxons) && count($taxons) > 0){
  111.                 foreach($taxons as $taxon){  
  112.                     $taxonsChilds2 $repository->findChildren($taxon->getCode());             
  113.                     if(is_array($taxonsChilds2) && count($taxonsChilds2) > 0){
  114.                         foreach($taxonsChilds2 as $taxon2){            
  115.                             $taxonsChilds3 $repository->findChildren($taxon2->getCode());             
  116.                             if(is_array($taxonsChilds3) && count($taxonsChilds3) > 0){
  117.                                 foreach($taxonsChilds3 as $taxon3){            
  118.                                     $taxonsLastlevel[$taxon3->getId()] =  $taxon3;                       
  119.                                 }
  120.                                 
  121.                             }                    
  122.                         }
  123.                         
  124.                     }                   
  125.                 }                
  126.             } 
  127.           
  128.            return new Response(
  129.                $this->twigEnvironment->render('@SyliusShop/Webapic/page/articles-last.html.twig', [
  130.                 'articles' => $taxonsLastlevel,
  131.                ])
  132.            );      
  133.           
  134.         }else if($typeblock == 25){ 
  135.             
  136.             $tstart microtime(true);
  137.             $sorting $request->query->get('sorting');
  138.             $sortBy null;
  139.             $sortOrder null;
  140.             if (is_array($sorting)) {
  141.                 $sortBy key($sorting);
  142.                 $sortOrder $sorting[key($sorting)];
  143.             }
  144.             if(!empty($request->query->get('filtreUrl'))){
  145.                 $filtre $request->query->get('filtreUrl');
  146.             }else{
  147.                 $filtre $request->query->get('filtre''');
  148.             }
  149.             $filtreUrl $filtre;
  150.             dump($filtreUrl);
  151.             dump($request);
  152.             $selectedFacets = array();
  153.             $t1 microtime(true);
  154.             // $isBadge = (array) $request->query->get('filtre', []);
  155.            
  156.             // if(count($isBadge) > 0 ){
  157.             //     foreach($isBadge as $key => $facet){
  158.             //         if (str_contains($facet, 'facet')) {
  159.             //             $value = explode("=", $facet);
  160.             //             $selectedFacets = (array) ['badge' => $value[1]];
  161.             //         }
  162.                     
  163.             //     }
  164.             // }
  165.             //  dump($isBadge);
  166.            
  167.             if(!empty($filtreUrl)){
  168.                 $values explode("/"$filtreUrl);
  169.                 if($values[1] == 'prix-bas' && $localeContext->getLocaleCode() == 'en_US' ){
  170.                     $filtreUrl 'badge/low-price';
  171.                 }                
  172.             }
  173.             dump($localeContext->getLocaleCode());
  174.             $t2 microtime(true);
  175.             // $results = $elasticSearch->search($localeContext->getLocaleCode(), '', 0, $selectedFacets, $sortBy, $sortOrder);
  176.             // $results = $elasticSearch->getElementsByTaxon($localeContext->getLocaleCode(), 1, 0, $selectedFacets, $sortBy, $sortOrder, true);
  177.           
  178.             // $variants = [];
  179.             // if (count($results['hits']['hits']) > 0) {
  180.             //     $variants = $productVariantRepository->findByIdsAndOrderByFieldId(array_map(function ($row) {
  181.             //         return str_ireplace('VARIANT_', '', $row['_id']);
  182.             //     }, $results['hits']['hits']));
  183.             // }
  184.             // dump($variants);
  185.         
  186.             // Récupère tous les filtres à facettes disponible pour 
  187.             // cette recherche
  188.             $siteFilterFacets $em->getRepository(SiteFilterFacet::class)
  189.             ->findBy([
  190.                 'enabled' => true
  191.             ], [
  192.                 'position' => 'ASC'
  193.             ]);
  194.             $t21 microtime(true);
  195.             $esAggregations $elasticSearch->getFilterFacetsSearch($localeContext->getLocaleCode(), ''$selectedFacetsarray_map(function ($row) {
  196.                 return $row->getId();
  197.             }, $siteFilterFacets));
  198.             $t3 microtime(true);
  199.     
  200.             $siteFilterShape = [];
  201.             if (count($esAggregations['shape']['buckets']) > 1) {
  202.                 $siteFilterShape $productRepository->findBy([
  203.                     'id' => array_map(function ($row) {
  204.                         return $row['key'];
  205.                     }, $esAggregations['shape']['buckets']),
  206.                     'enabled' => true
  207.                 ], [
  208.                     'code' => 'ASC'
  209.                 ]);
  210.                 usort($siteFilterShape, function ($a$b) {
  211.                     // dump($a->getTranslation()->getName());
  212.                     return strcmp$a->getTranslation()->getName() , $b->getTranslation()->getName());
  213.                 });
  214.             }
  215.             dump($esAggregations);
  216.     
  217.             $t4 microtime(true);
  218.             $siteFilterColor = [];
  219.             if (count($esAggregations['color']['buckets']) > 1) {
  220.                 $siteFilterColor $em->getRepository(SiteCollection::class)->findBy([
  221.                     'productOptionValue' => array_map(function ($row) {
  222.                         return $row['key'];
  223.                     }, $esAggregations['color']['buckets']),
  224.                 ]);
  225.             }
  226.             $siteFilterBadge = [];
  227.             $siteFilterBadgeVariants = [];
  228.             if (count($esAggregations['badge']['buckets']) > 1) {
  229.                 foreach($esAggregations['badge']['buckets'] as $badge){
  230.                     $siteFilterBadge[] = array(
  231.                         'name' => $translator->trans("app.ui.product_callout.".$badge['key']),
  232.                         'id' => $badge['key'],
  233.                     );
  234.                 }
  235.                 // $siteFilterBadgeVariants = $productVariantRepository->findBy([
  236.                 //     'badge' => array_map(function ($row) {
  237.                 //         return $row['key'];
  238.                 //     }, $esAggregations['badge']['buckets']),
  239.                 //     'enabled' => true
  240.                 // ], [
  241.                 //     'code' => 'ASC'
  242.                 // ]);
  243.                 
  244.                 // if(count($siteFilterBadgeVariants) > 0){
  245.                 //     foreach($siteFilterBadgeVariants as $siteFilterBadgeVariant){
  246.                 //         $siteFilterBadge[$siteFilterBadgeVariant->getBadge()] = array(
  247.                 //             'name' => $translator->trans("app.ui.product_callout.".$siteFilterBadgeVariant->getBadge()),
  248.                 //             'id' => $siteFilterBadgeVariant->getBadge(),
  249.                 //         );
  250.                 //     }
  251.                 // }
  252.     
  253.             }
  254.             dump($siteFilterBadge);
  255.            $t5 microtime(true);
  256.     
  257.             $siteFilterFacetsValues = [];
  258.             foreach ($esAggregations as $keyEsAggregation => $rowEsAggregation) {
  259.                 if (isset($rowEsAggregation['buckets']) && count($rowEsAggregation['buckets']) > 1) {
  260.                     $siteFilterFacetsValues[$keyEsAggregation] = array_map(function ($row) {
  261.                         return $row['key'];
  262.                     }, $rowEsAggregation['buckets']);
  263.     
  264.                     // On met dans l'ordre alphabétique
  265.                     sort($siteFilterFacetsValues[$keyEsAggregation]);
  266.                 }
  267.             }
  268.     
  269.             // Conserve que les filtres à facettes disponible pour cette recherche
  270.             $siteFilterFacets array_filter($siteFilterFacets, function ($row) use (&$siteFilterFacetsValues) {
  271.                 return in_array($row->getId(), array_keys($siteFilterFacetsValues));
  272.             });
  273.             
  274.             $t6 microtime(true);
  275.             
  276.             $response $this->twigEnvironment->render('/partials/block-listing-produits.html.twig', [
  277.                 'filtre' => $filtre,
  278.                 'filtreUrl' =>  $filtreUrl,
  279.                 'variants' => array(),
  280.                 'fromPage' => 1,
  281.                 'selectedFacets' => $selectedFacets,
  282.                 'siteFilterShape' => $siteFilterShape,
  283.                 'siteFilterColor' => $siteFilterColor,
  284.                 'siteFilterBadge' => $siteFilterBadge,
  285.                 'siteFilterFacets' => $siteFilterFacets,
  286.                 'siteFilterFacetsValues' => $siteFilterFacetsValues,
  287.                 'siteFilterMinPrice' => $esAggregations[ElasticSearchService::FACET_PRICE_MIN]['value'],
  288.                 'siteFilterMaxPrice' => $esAggregations[ElasticSearchService::FACET_PRICE_MAX]['value'],
  289.                 'maxViewResults' => ElasticSearchService::MAX_SEARCH_RESULTS,
  290.                 // 'totalResults' => $results['hits']['total']['value'],
  291.             ]);
  292.             $tend microtime(true);
  293.             $log = [];
  294.             $log[] = 'Time to retrieve taxon: ' . ($t1 $tstart) . ' seconds';
  295.             $log[] = 'Time to retrieve facets: ' . ($t2 $t1) . ' seconds';
  296.             $log[] = 'Time to retrieve facets values: ' . ($t21 $t2) . ' seconds';
  297.             $log[] = 'Time to retrieve shape: ' . ($t3 $t21) . ' seconds';
  298.             $log[] = 'Time to retrieve color: ' . ($t4 $t3) . ' seconds';
  299.             $log[] = 'Time to retrieve badge: ' . ($t5 $t4) . ' seconds';
  300.             $log[] = 'Time to retrieve facets values: ' . ($t6 $t5) . ' seconds';
  301.             $log[] = 'Time to render: ' . ($tend $t6) . ' seconds';
  302.             $log[] = 'Total time: ' . ($tend $tstart) . ' seconds';
  303.             dump($log);
  304.             return new Response($response);  
  305.         }else{
  306.             return new Response(
  307.                 $this->twigEnvironment->render('@SyliusShop/Webapic/page/caroussel.html.twig', [
  308.                     'taxons' => $taxons,
  309.                 ])
  310.             );
  311.         }
  312.     }
  313.     public function menuScrollAction(Request $request): Response
  314.     {        
  315.         
  316.         $repository $this->container->get('sylius.repository.taxon');
  317.         $taxonsChilds $repository->findChildren('Menu-deploye');
  318.         
  319.         return new Response(
  320.             $this->twigEnvironment->render('@SyliusShop/Webapic/menu-scroll.html.twig', [
  321.                 'taxons' => $taxonsChilds,
  322.             ])
  323.         );
  324.     }     
  325.     public function pageFiltreWithFiltreEngage(
  326.         Request $request,
  327.         LocaleContextInterface $localeContext
  328.         ChannelContextInterface $channelContext,
  329.     ): Response
  330.     {
  331.         
  332.         $channel $channelContext->getChannel();
  333.         $basepage $request->get('base');
  334.         dump($basepage);
  335.         $page $this->pageRepository->findOneEnabledBySlugAndChannelCode($basepage$localeContext->getLocaleCode(), $channel->getCode());
  336.         $filtreUrl '';
  337.         if(!empty($request->get('forme')) && !empty($request->get('slug'))){
  338.             $filtreUrl $request->get('forme').'/'.$request->get('slug');
  339.         }
  340.         // dump($filtreUrl);
  341.         return new Response(
  342.             $this->twigEnvironment->render('/bundles/BitBagSyliusCmsPlugin/Shop/Page/show.html.twig', [
  343.                 'typeblock' => 25,
  344.                 'page' => $page,
  345.                 'filtreUrl' => $filtreUrl
  346.             ])
  347.         );  
  348.         
  349.     }
  350. }