src/Controller/SiteCollectionController.php line 124

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 Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Sylius\Component\Locale\Context\LocaleContextInterface;
  9. use Sylius\Component\Channel\Context\ChannelContextInterface;
  10. use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
  11. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  12. use App\Entity\SiteCollection;
  13. use App\Entity\SiteCollectionTranslation;
  14. use App\Entity\SiteProductOptionValueMatch;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use App\Services\PPMCErpSynchroService;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. /**
  19.  * Page Collection
  20.  */
  21. class SiteCollectionController extends AbstractController
  22. {
  23.     public const CATEGORIES = [
  24.         'fr_FR' => [
  25.             'children' => 'enfant',
  26.             'classic' => 'classique',
  27.             'temporary' => 'temporaire'
  28.         ],
  29.         'en_US' => [
  30.             'children' => 'children',
  31.             'classic' => 'classic',
  32.             'temporary' => 'temporary'
  33.         ]
  34.     ];
  35.     public const CATEGORIESREVERT = [
  36.         'fr_FR' => [
  37.             'enfant' => 'children',
  38.             'classique' => 'classic',
  39.             'temporaire' => 'temporary'
  40.         ],
  41.         'en_US' => [
  42.             'children' => 'children',
  43.             'classic' => 'classic',
  44.             'temporary' => 'temporary'
  45.         ]
  46.     ];
  47.     public function indexAction(
  48.         Breadcrumbs $breadcrumbs,
  49.         LocaleContextInterface $localeContext,
  50.     )
  51.     {
  52.         $em $this->get('doctrine')->getManager();
  53.         $collectionsTemporary =  $em->getRepository(SiteCollection::class)->findBy(
  54.             ['category' => 'temporary''enabled' => true'visible' => false], 
  55.             ['dateRelease' => 'DESC'],
  56.             4
  57.         );
  58.         $collectionsClassic =  $em->getRepository(SiteCollection::class)->findBy(
  59.             ['category' => 'classic''enabled' => true'visible' => false], 
  60.             ['dateRelease' => 'DESC'],
  61.             4
  62.         );
  63.         $collectionsChildren =  $em->getRepository(SiteCollection::class)->findBy(
  64.             ['category' => 'children''enabled' => true'visible' => false], 
  65.             ['dateRelease' => 'DESC'],
  66.             4
  67.         );
  68.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  69.         $breadcrumbs->addItem("Collections");
  70.         return $this->render('site_collection_index.html.twig', [
  71.             'collectionsTemporary' => $collectionsTemporary,
  72.             'collectionsClassic' => $collectionsClassic,
  73.             'collectionsChildren' => $collectionsChildren,
  74.             'linkscat' => $this::CATEGORIES[$localeContext->getLocaleCode()]
  75.         ]);
  76.     }
  77.     public function categoryAction(
  78.         Breadcrumbs $breadcrumbs,
  79.         $slugCategory,
  80.         TranslatorInterface $translator,
  81.         LocaleContextInterface $localeContext,
  82.     )
  83.     {
  84.         
  85.         if(isset($this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory])){
  86.             $slugBase $this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory];
  87.         }else{            
  88.             $slugBase $this::CATEGORIES[$localeContext->getLocaleCode()][$slugCategory];
  89.             return $this->redirectToRoute('app_collection_category', ['slugCategory' => $slugBase]);
  90.         }
  91.        
  92.         // dump($slugBase);
  93.         $em $this->get('doctrine')->getManager();
  94.         $collectionsCategory =  $em->getRepository(SiteCollection::class)->findBy(
  95.             ['category' => $slugBase'enabled' => true'visible' => false], 
  96.             ['dateRelease' => 'DESC']
  97.         );
  98.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  99.         $breadcrumbs->addRouteItem("Collections""app_collection_index");
  100.         $breadcrumbs->addItem("app.form.site_collection_category."$slugBase);
  101.         return $this->render('site_collection_category.html.twig', [
  102.             'collectionsCategory' => $collectionsCategory,
  103.             'title' => $translator->trans("app.form.site_collection_category."$slugBase).'s',
  104.             'slug' =>  $slugCategory,
  105.             // 'linkscat' => $slugCategory
  106.         ]);
  107.     }
  108.     public function pageCollectionAction(
  109.         Request $request,
  110.         Breadcrumbs $breadcrumbs,
  111.         ChannelContextInterface $channelContext,
  112.         LocaleContextInterface $localeContext,
  113.         ProductVariantRepositoryInterface $productVariantRepository,
  114.         TranslatorInterface $translator,
  115.         $slugCategory,
  116.         $slug
  117.     ) {
  118.         $em $this->get('doctrine')->getManager();
  119.         $collectionTranslationRepository $em->getRepository(SiteCollectionTranslation::class);
  120.         $collectionTranslation $collectionTranslationRepository->findOneBySlug($slug);
  121.         $collection $collectionTranslation->getTranslatable();
  122.         
  123.         if (empty($collectionTranslation)) {
  124.             throw new NotFoundHttpException("Cette collection n'existe pas.");
  125.         }
  126.         $isAdmin intval($request->query->get('admin'));
  127.         if ($collection->getEnabled() == false && $isAdmin == 0) {
  128.             throw new NotFoundHttpException("Cette collection est désactivée");
  129.         }
  130.         
  131.         if(isset($this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory])){
  132.             $slugBase $this::CATEGORIESREVERT[$localeContext->getLocaleCode()][$slugCategory];
  133.         }else{            
  134.             $slugBase $this::CATEGORIES[$localeContext->getLocaleCode()][$slugCategory];
  135.             return $this->redirectToRoute('app_collection_show', ['slugCategory' => $slugBase'slug' => $slug]);
  136.         }
  137.         // dump( $this::CATEGORIESREVERT[$localeContext->getLocaleCode()]);
  138.         // dump($slugCategory);
  139.         $breadcrumbs->addRouteItem("sylius.ui.home""sylius_shop_homepage");
  140.         $breadcrumbs->addRouteItem("Nos collections""app_collection_index");
  141.         $breadcrumbs->addRouteItem($translator->trans("app.form.site_collection_category."$slugBase), "app_collection_category", [
  142.             'slugCategory' => $slugCategory
  143.         ]);
  144.         $breadcrumbs->addItem($collectionTranslation->getTitle());
  145.         $channel $channelContext->getChannel();
  146.         $locale $localeContext->getLocaleCode();
  147.         // Récupère les déclinaisons de cette collection
  148.         $variants $productVariantRepository->findVariantsByColors(
  149.             $channel,
  150.             $locale,
  151.             [$collection->getProductOptionValue()]
  152.         );
  153.         $complementaryFabric null;
  154.         if (count($variants) > 0) {
  155.             // Récupère un produit tissu s'il existe avec la même couleur 
  156.             // pour l'afficher en bas de page
  157.             $complementaryFabric $productVariantRepository->findOneFabricForThisProductVariant(
  158.                 $channel,
  159.                 $variants[0],
  160.                 $locale
  161.             );
  162.         }
  163.         // Récupère les collections qui matchent (5 maximum)
  164.         $em $this->get('doctrine')->getManager();
  165.         $matchingColors $em->getRepository(SiteProductOptionValueMatch::class)
  166.         ->findByProductOptionValue($collection->getProductOptionValue());
  167.         $collectionMatchs $em->getRepository(SiteCollection::class)->findBy([
  168.             'productOptionValue' => array_map(function ($row) {
  169.                 return $row->getMatchedProductOptionValue()->getId();
  170.             }, $matchingColors)
  171.         ], [], 5);
  172.         // Pour chaque collection qui matchent, on récupère 5 produits finaux
  173.         $variantsMatch = [];
  174.         foreach ($collectionMatchs as $row) {
  175.             $rowVariants $productVariantRepository->findVariantsByColors(
  176.                 $channel,
  177.                 $locale,
  178.                 [$row->getProductOptionValue()],
  179.                 5
  180.             );
  181.             $variantsMatch[$row->getId()] = $rowVariants;
  182.         }
  183.         return $this->render('site_collection_show.html.twig', [
  184.             'collection' => $collection,
  185.             'collectionTranslation' => $collectionTranslation,
  186.             'complementaryFabric' => $complementaryFabric,
  187.             'variants' => $variants,
  188.             'collectionMatchs' => $collectionMatchs,
  189.             'variantsMatch' => $variantsMatch,
  190.             'type'  => $translator->trans("app.form.site_collection_category."$slugBase)
  191.         ]);
  192.     }
  193.     public function syncImageAction(
  194.         ContainerInterface $container,
  195.         PPMCErpSynchroService $ppmcErpSynchroService,
  196.         $idCollection
  197.     ) {
  198.         $em $this->get('doctrine')->getManager();
  199.         $collectionRepository $em->getRepository(SiteCollection::class);
  200.         $collection $collectionRepository->findOneById($idCollection);
  201.             
  202.         $dones $ppmcErpSynchroService->syncCollectionImages($container$collection->getProductOptionValue()->getCode());
  203.         $this->get('session')->getFlashBag()->add('success'$dones['variantsDone'].' variants synchronisés');
  204.         $this->get('session')->getFlashBag()->add('success'$dones['imagesDone'].' images synchronisées');
  205.         // dump($collection->getProductOptionValue()->getCode());
  206.         return $this->redirectToRoute('app_admin_site_collection_index');
  207.     }
  208. }