src/Controller/ProductController.php line 211

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  5. use Sylius\Component\Resource\ResourceActions;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use App\Repository\ProductRepository;
  9. use App\Entity\ProductAttribute;
  10. use App\Repository\ProductAttributeRepository;
  11. use App\Form\SearchProductFormType;
  12. use Twig\Environment;
  13. class ProductController extends ResourceController
  14. {
  15.     public function filters (Environment $twigRequest $requestProductRepository $productRepository): Response
  16.      {
  17.          
  18.         $originalRequest $this->get('request_stack')->getMasterRequest();
  19.         $locale $originalRequest->getLocale();
  20.         //Liste des attributs
  21.         $productAttributeRepository $this->container->get('sylius.repository.product_attribute');
  22.         $attrs $productAttributeRepository->findAll();
  23.         $taxonRepository $this->container->get('sylius.repository.taxon');
  24.         $availableTaxons = array();        
  25.         $taxons $taxonRepository->findAll();
  26.         foreach ($taxons as $taxon) {
  27.             $availableTaxons[$taxon->getName()] = $taxon;
  28.         }
  29.         $typelait_choices $this->getChoicesValues ($productAttributeRepository"typelait"$locale);
  30.         $traitement_choices $this->getChoicesValues ($productAttributeRepository"traitement"$locale);
  31.         //$poids_choices = $this->getChoicesValues ($productAttributeRepository, "poids", $locale);
  32.         $pays_choices $this->getChoicesTaxonValuesSameLevel2 ($taxonRepository"pays"$locale);
  33.         $typepate_choices $this->getChoicesValues ($productAttributeRepository"typepate"$locale);
  34.         $typeproduction_choices $this->getChoicesValues ($productAttributeRepository"typeproduction"$locale);
  35.         
  36.         //$bio_choices = $this->getChoicesValues ($productAttributeRepository, "bio", $locale);
  37.         /*$productAttributes = $productAttributeRepository->findAll();
  38.         foreach ($productAttributes as $productAttribute) {
  39.             $configuration = $productAttribute->getConfiguration();
  40.         }*/
  41.   
  42.         //Formulaire de recherche
  43.         $view_form null;
  44.         /*
  45.         $form = $this->createForm(SearchProductFormType::class,  [
  46.             'typelait_choices' => $typelait_choices,
  47.             'traitement_choices' => $traitement_choices,
  48.             'poids_choices' => $poids_choices,
  49.             'pays_choices' => $pays_choices,
  50.             'typepate_choices' => $typepate_choices,
  51.             'typeproduction_choices' => $typeproduction_choices,
  52.         ]);
  53.         $form->handleRequest($request);  //important !
  54.         if ($form->isSubmitted() && $form->isValid()) {
  55.              //Recherche
  56.         }
  57.         $view_form = $form->createView();*/
  58.         //Rendu
  59.         $render $this->render('@SyliusShop/Product/_filters.html.twig', [
  60.             'search_form' => $view_form,
  61.             'typelait_choices' => $typelait_choices,
  62.             'traitement_choices' => $traitement_choices,
  63.             'pays_choices' => $pays_choices,
  64.             'typepate_choices' => $typepate_choices,   
  65.             'typeproduction_choices' => $typeproduction_choices
  66.             'originalRequest' => $originalRequest,          
  67.         ]);
  68.         return $render;
  69.             
  70.      }
  71.      private function getChoicesTaxonValuesSameLevel2 $taxonRepositorystring $codestring $locale$inverse false)
  72.      {
  73.          $res = array();
  74.         
  75.         //TODO faire une fonction recursive
  76.         // Find the parent taxon
  77.         $taxons $taxonRepository->findChildren($code);
  78.         foreach ($taxons  as $key => $taxon) {
  79.                 $res[$taxon->getId()]["name"] = $taxon->getName();
  80.                 $taxons_child $taxonRepository->findChildren($taxon->getCode());
  81.                 $mychild = array();
  82.                 foreach ($taxons_child  as $keyc => $taxonc) {
  83.                     $mychild[$taxonc->getId()] = $taxonc->getName();
  84.                 }
  85.                 asort ($mychild);
  86.                 $res[$taxon->getId()]["childs"] = $mychild;          
  87.         }        
  88.         return $res;
  89.      }
  90.      private function getChoicesTaxonValuesSameLevel $taxonRepositorystring $codestring $locale$inverse false)
  91.      {
  92.          $res = array();
  93.         
  94.         //TODO faire une fonction recursive
  95.         // Find the parent taxon
  96.         $taxons $taxonRepository->findChildren($code);
  97.         foreach ($taxons  as $key => $taxon) {
  98.             if ($inverse)
  99.                 $res$taxon->getName()] = $taxon->getId();
  100.             else
  101.                 $res$taxon->getId()] = $taxon->getName();
  102.             $taxons_child $taxonRepository->findChildren($taxon->getCode());
  103.             foreach ($taxons_child  as $keyc => $taxonc) {
  104.                 if ($inverse)
  105.                     $res$taxonc->getName()] = $taxonc->getId();
  106.                 else
  107.                     $res$taxonc->getId()] = $taxonc->getName();
  108.             }
  109.         }
  110.         
  111.          return sort($res);
  112.      }
  113.      
  114.      
  115.      private function getChoicesTaxonValues $taxonRepositorystring $codestring $locale)
  116.      {
  117.          $res = array();
  118.         
  119.         //TODO faire une fonction recursive
  120.         // Find the parent taxon
  121.         $taxons $taxonRepository->findChildren($code);
  122.         foreach ($taxons  as $key => $taxon) {
  123.             $res$taxon->getId()] = array();
  124.             $res$taxon->getId()]["name"] = $taxon->getName();
  125.             $res$taxon->getId()]["code"] = $taxon->getCode();
  126.             $res$taxon->getId()]["id"] = $taxon->getId();               
  127.             $res$taxon->getId()]["children"] = array();
  128.             $taxons_child $taxonRepository->findChildren($taxon->getCode());
  129.             foreach ($taxons_child  as $keyc => $taxonc) {
  130.                 $res$taxon->getId()]["children"][ $taxonc->getId()] = array();
  131.                 $res$taxon->getId()]["children"][ $taxonc->getId()]["name"] = $taxonc->getName();
  132.                 $res$taxon->getId()]["children"][ $taxonc->getId()]["code"] = $taxonc->getCode();
  133.                 $res$taxon->getId()]["children"][ $taxonc->getId()]["id"] = $taxonc->getId();               
  134.                 $res$taxon->getId()]["children"][ $taxonc->getId()]["children"] = false;                
  135.             }
  136.         }
  137.  
  138.          return $res;
  139.      }
  140.     private function getChoicesValues $productAttributeRepositorystring $codestring $locale$inverse false)
  141.     {
  142.         $res = array();
  143.         $attribute $productAttributeRepository->findOneBy(array('code' => $code));
  144.         
  145.         $configuration $attribute->getConfiguration();
  146.         $choices $configuration["choices"];
  147.         //$locale = str_replace ("fr", "fr_FR", $locale);
  148.         //$locale = str_replace ("en", "en_US" , $locale);
  149.         foreach ($choices  as $key => $choice) {
  150.             if (array_key_exists($locale$choice))
  151.             {
  152.                 if ($inverse)
  153.                     $res[$choice[$locale]] =  $key;
  154.                 else
  155.                     $res[$key] =  $choice[$locale];
  156.             }
  157.         }        
  158.         return $res;
  159.     }
  160.     public function showAction(Request $request): Response
  161.     {
  162.         /*$this->get('sylius.email_sender')->send(
  163.             'giftcard',
  164.              ['john.doe.mon@yopmail.com'],
  165.              [
  166.                 'message' => "",
  167.                  'code' => "xxxxx",
  168.                  'user' => $this->getUser(),
  169.                  'channel' => $channel,
  170.                  'localeCode' => $locale
  171.             ]);*/
  172.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  173.         $this->isGrantedOr403($configurationResourceActions::SHOW);
  174.         $product $this->findOr404($configuration);
  175.         $locale $request->getLocale();
  176.         $defaultlocale $request->getDefaultLocale();
  177.         $mode =   'product';
  178.         // some custom provider service to retrieve recommended products
  179.         //$recommendationService = $this->get('app.provider.product');
  180.         //$recommendedProducts = $recommendationService->getRecommendedProducts($product);
  181.         $this->eventDispatcher->dispatch(ResourceActions::SHOW$configuration$product);
  182.         $AttributesByCode[] = array();
  183.         $taxons $product->getTaxons();
  184.         $regions = array ();
  185.         $pays "";
  186.         foreach ($taxons as $key => $taxonValue) {
  187.             //On restructure
  188.             $root $taxonValue->getRoot();
  189.             if ($root->getCode() == "pays" && $taxonValue->getLevel() == 2)
  190.             {   
  191.                 $regions[] = $taxonValue->getName() ;
  192.             }  
  193.             if ($root->getCode() == "categorie" && $taxonValue->getCode() == "cadeau")
  194.             {   
  195.                 $mode =   'gift';
  196.             }  
  197.             if ($root->getCode() == "pays" && $taxonValue->getLevel() == 1)
  198.             {   
  199.                 $pays $taxonValue->getName() ;
  200.             } 
  201.         }
  202.         $regions_str implode (" / "$regions);
  203.         
  204.         /*
  205.         {% for ancestor in ancestors %}
  206.             {% if ancestor.isRoot()  or not ancestor.enabled %}
  207.                 <div class="section">{{ ancestor.name }}</div>
  208.             {% else %}
  209.                 <a href="{{ path('sylius_shop_product_index', {'slug': ancestor.slug, '_locale': ancestor.translation.locale}) }}" class="section">{{ ancestor.name }}</a>
  210.             {% endif %}
  211.             <div class="divider"> / </div>
  212.         {% endfor %}*/
  213.         $attributes $product->getAttributesByLocale($locale$defaultlocale);
  214.         foreach ($attributes as $key => $attributevalue){
  215.             $attribute $attributevalue->getAttribute();
  216.             $typeAttribute $attribute->getType();
  217.             if ($typeAttribute == "select")
  218.             {
  219.                 $valuechoice = array();
  220.                 $configurationA $attribute->getConfiguration();
  221.                 $choices $configurationA["choices"];
  222.                 foreach ($attributevalue->getValue() as $keyc => $value){
  223.                     if (array_key_exists($locale$choices[$value]))
  224.                         $valuechoice[] = $choices[$value][$locale];
  225.                     else
  226.                         $valuechoice[] = $choices[$value]["fr"];
  227.                 }
  228.                 $value implode ", " ,  $valuechoice);
  229.                 $AttributesByCode[$attribute->getCode()] = array("value" => $value"name" => $attributevalue->getName(), "attribute" => $attributevalue);  
  230.             }
  231.             else
  232.             {
  233.                 $AttributesByCode[$attribute->getCode()] = array("value" => $attributevalue->getValue(), "name" => $attributevalue->getName(), "attribute" => $attributevalue);  
  234.             }
  235.             
  236.         }
  237.         
  238.        // if ($mode=="gift")
  239.        //     $template = "@SyliusShop/Product/gift.html.twig";
  240.        // else
  241.             $template $configuration->getTemplate(ResourceActions::SHOW '.html');
  242.         if ($configuration->isHtmlRequest()) {
  243.             return $this->render($template, [
  244.                 'configuration' => $configuration,
  245.                 'metadata' => $this->metadata,
  246.                 'resource' => $product,
  247.                 'mode' => $mode,
  248.                 'SimpleAttributes' => $AttributesByCode,
  249.                 'regions' => $regions_str,                
  250.                 'pays_origine' => $pays,                
  251.                 $this->metadata->getName() => $product,
  252.             ]);
  253.         }
  254.         return $this->createRestView($configuration$product);
  255.     }    
  256. }