src/Controller/ProductController.php line 231

  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\Traits\HreflangsControllerTrait;
  4. use App\Controller\Traits\MetatagsControllerTrait;
  5. use App\Controller\Traits\StaticPageHelperTrait;
  6. use App\Entity\OpenPosition;
  7. use App\Entity\Product;
  8. use App\Entity\ProductCategory;
  9. use App\Entity\ProductLine;
  10. use App\Entity\ProductType;
  11. use App\Entity\RecipeType;
  12. use App\Form\CustomerServiceType;
  13. use App\Form\WorkWithUsType;
  14. use App\Repository\ProductCategoryRepository;
  15. use App\Repository\ProductRepository;
  16. use App\Repository\ProductLineRepository;
  17. use App\Repository\OpenPositionRepository;
  18. use App\Repository\ProductTypeRepository;
  19. use App\Repository\RecipeRepository;
  20. use App\Repository\RecipeTypeRepository;
  21. use App\Service\ProductCategoryUrlChainGenerator;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use Exception;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\RequestStack;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. use Symfony\Component\HttpFoundation\RedirectResponse;
  30. use Symfony\Contracts\Translation\TranslatorInterface;
  31. use App\Entity\Recipe;
  32. class ProductController extends AbstractController
  33. {
  34.     use MetatagsControllerTraitHreflangsControllerTraitStaticPageHelperTrait;
  35.     private $em;
  36.     private $transalor;
  37.     private $request;
  38.     public function __construct(EntityManagerInterface $emTranslatorInterface $translatorRequestStack $requestStack){
  39.         $this->em $em;
  40.         $this->transalor $translator;
  41.         $this->request $requestStack->getCurrentRequest();
  42.     }
  43.     /**
  44.      * PRODUCT_CATEGORY
  45.      */
  46.     #[Routepath: [
  47.         'it' => '/categorie/{urlChain}',
  48. //        'en' => 'en/categories/{urlChain}',
  49. //        'es' => 'es/categorias/{urlChain}'
  50.     ], name'app_product_category_show'requirements: ['urlChain' => '.+''_locale' => 'it'])]
  51.     public function productCategory(
  52.         Request $request,
  53.         ProductCategoryUrlChainGenerator $productCategoryUrlChainGenerator,
  54.         string|null $urlChain
  55.     )
  56.     {
  57.         $productCategories $productCategoryUrlChainGenerator->getProductCategoryResources($urlChain$request->getLocale());
  58.         if(!is_array($productCategories)) {
  59.             throw $this->createNotFoundException("Unable to find {$urlChain} path");
  60.         }
  61.         /** @var ProductCategory $productCategory */
  62.         $productCategory null;
  63.         if (count($productCategories)) { // ricava l'ultima categoria
  64.             $productCategory end($productCategories);
  65.         }
  66.         if (!$productCategory) { // controlla che la categoria esista
  67.             return $this->redirectToRoute('homepage', [], 301);
  68.         }
  69.         // eventuali sottocategorie
  70.         /** @var ProductCategoryRepository $repoProductCategory */
  71.         $repoProductCategory $this->em->getRepository(ProductCategory::class);
  72.         $productSubCategories $repoProductCategory->findBy([
  73.             'parent' => $productCategory,
  74.             'locale' => $request->getLocale(),
  75.             'enabled' => true,
  76.         ],
  77.             [
  78.                 'sortablePosition' => 'ASC'
  79.             ]);
  80.         // eventuali prodotti associati
  81.         /** @var ProductRepository $repoProduct */
  82.         $repoProduct $this->em->getRepository(Product::class);
  83.         $products $repoProduct->findBy([
  84.             'productCategory' => $productCategory,
  85.             'locale' => $this->request->getLocale(),
  86.             'isEnabled' => true,
  87.         ],
  88.             [
  89.                 'sortablePosition' => 'ASC'
  90.             ]);
  91.         $metatags $this->getMetatags($productCategory'name''name');
  92.         $listBreadcrumbs = [];
  93.         /** @var ProductCategory $productCategoryItem */
  94.         foreach ($productCategories as $productCategoryItem) {
  95.             $listBreadcrumbs[] = [
  96.                 $this->generateUrl('app_product_category_show', [
  97.                     'urlChain' => $productCategoryUrlChainGenerator->buildPath(($productCategoryItem->getParent() ? $productCategories : [$productCategoryItem]))
  98.                 ]),
  99.                 $productCategoryItem->getName(),
  100.             ];
  101.         }
  102.         $template 'prodotti/productCategory.html.twig';
  103.         if ($productCategory->getIdentifier() == ProductCategory::IDENTIFIER_CAMPIAMATI) {
  104.             $template 'prodotti/campiamati.html.twig';
  105.         }
  106.         if ($productCategory->getIdentifier() == ProductCategory::IDENTIFIER_KROCCANTI) {
  107.             $template 'prodotti/kroccanti.html.twig';
  108.         }
  109.         return $this->render($template, [
  110.             'listBreadcrumbs' => $listBreadcrumbs,
  111.             'products' => $products,
  112.             'productCategory' => $productCategory,
  113.             'productSubCategories' => $productSubCategories,
  114.             'metatags' => $metatags,
  115.         ]);
  116.     }
  117.     /**
  118.      * RENDER ALL THE PRODUCTS FOR THE PRODUCT_TYPE
  119.      */
  120.     #[Routepath: [
  121.         'it' => null,
  122.         'en' => 'en/products/{category_slug}',
  123.         'es' => 'es/productos/{category_slug}'
  124.     ], name'app_show_category'requirements: ['_locale' => 'en|es'])]
  125.     public function showCategory(string|null $category_slug)
  126.     {
  127.         $arrayRedirect = [
  128.             'ortofrutta',
  129.             'quienes-somos'
  130.         ];
  131.         if (in_array($category_slug$arrayRedirect)){
  132.             return $this->redirectToRoute('app_show_category_ortofrutta');
  133.         }
  134.         if($category_slug == 'spalmabile'){
  135.             return $this->redirectToRoute('spalmabile_vegetale');
  136.         }
  137.         if ($category_slug == 'kroccanti') {
  138.             return $this->redirectToRoute('kroccanti');
  139.         }
  140.         /** @var ProductTypeRepository $productTypeRepo */
  141.         $productTypeRepo $this->em->getRepository(ProductType::class);
  142.         $routeName $this->request->attributes->get('_route');
  143.         if($routeName == 'app_show_category_ortofrutta'){
  144.             switch ($this->request->getLocale()) {
  145.                 case 'es':
  146.                     $category_slug 'quienes-somos';
  147.                     break;
  148.                 default:
  149.                     $category_slug 'ortofrutta';
  150.                     break;
  151.             }
  152.         }
  153.         $productType $productTypeRepo->findOneBy([
  154.             'slug' => $category_slug,
  155.             'locale' => $this->request->getLocale(),
  156.         ]);
  157.         if (!$productType) { // controlla che la linea prodotti esista
  158. //            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  159.             return $this->redirectToRoute('homepage');
  160.         }
  161.         /** @var ProductLineRepository $productLineRepo */
  162.         $productLineRepo $this->em->getRepository(ProductLine::class);
  163.         $productLine $productLineRepo->findBy([
  164.             'productType' => $productType,
  165.             'locale' => $this->request->getLocale(),
  166.             'isEnabled' => true,
  167.         ]);
  168.         if (!$productLine) { // controlla che la linea prodotti esista
  169. //            throw $this->createNotFoundException($this->transalor->trans('exception.product_line_not_found'));
  170.             return $this->redirectToRoute('homepage');
  171.         }
  172.         /** @var ProductRepository $productRepo */
  173.         $productRepo $this->em->getRepository(Product::class);
  174.         $products $productRepo->findBy([
  175.             'type' => $productLine,
  176.             'locale' => $this->request->getLocale(),
  177.             'isEnabled' => true,
  178.         ],
  179.         [
  180.             'sortablePosition' => 'ASC'
  181.         ]);
  182.         $metatags $this->getMetatags($productType'name''name');
  183.         return $this->render('prodotti/productIndex.html.twig', [
  184.             'products' => $products,
  185.             'productLine' => $productLine,
  186.             'productType' => $productType,
  187.             'metatags' => $metatags,
  188.         ]);
  189.     }
  190.     /**
  191.      * RENDER THE PRODUCT
  192.      */
  193.     #[Route(path: [
  194.         'it' => '/prodotti/{product_slug}',
  195.         'en' => null,
  196.         'es' => null
  197.     ]  , name'app_product_show')]
  198.     #[Route(path: [
  199.         'it' => null,
  200.         'en' => 'en/products/{category_slug}/{product_slug}',
  201.         'es' => 'es/productos/{category_slug}/{product_slug}'
  202.     ]  , name'app_show_product')]
  203.     public function showProduct(
  204.         ProductCategoryUrlChainGenerator $productCategoryUrlChainGenerator,
  205.         Request $requeststring|null
  206.         $category_slug,
  207.         string $product_slug
  208.     )
  209.     {
  210.         if ($request->get('_route') == 'app_show_product') {
  211.             if($category_slug == 'spalmabile'){
  212.                 return $this->redirectToRoute('spalmabile_vegetale');
  213.             }
  214.             $arrayRedirect = [
  215.                 'ortofrutta',
  216.                 'quienes-somos'
  217.             ];
  218.             if (in_array($category_slug$arrayRedirect)){
  219.                 return $this->redirectToRoute('app_show_product_ortofrutta', [
  220.                     'category_slug' => null,
  221.                     'product_slug' => $product_slug
  222.                 ]);
  223.             }
  224.             /** @var ProductRepository $productRepo */
  225.             $productRepo $this->em->getRepository(Product::class);
  226.             $routeName $this->request->attributes->get('_route');
  227.             if($routeName == 'app_show_product_ortofrutta'){
  228.                 switch ($this->request->getLocale()) {
  229.                     case 'es':
  230.                         $category_slug 'quienes-somos';
  231.                         break;
  232.                     default:
  233.                         $category_slug 'ortofrutta';
  234.                         break;
  235.                 }
  236.             }
  237.             $product $productRepo->findOneBy([
  238.                 'slug' => $product_slug,
  239.                 'isEnabled' => true,
  240.                 'locale' => $this->request->getLocale(),
  241.             ]);
  242.             if (!$product) { // controlla che la linea prodotti esista
  243.                 return $this->redirectToRoute('homepage');
  244. //            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  245.             }
  246.             /** @var ProductTypeRepository $productTypeRepo */
  247.             $productTypeRepo $this->em->getRepository(ProductType::class);
  248.             $productType $productTypeRepo->findOneBy([
  249.                 'slug' => $category_slug,
  250.                 'locale' => $this->request->getLocale(),
  251.             ]);
  252.             // dd($productType, $category_slug, $this->request->getLocale());
  253.             if (!$productType) { // controlla che la linea prodotti esista
  254.                 return $this->redirectToRoute('homepage');
  255. //            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  256.             }
  257.             /** @var ProductLineRepository $productLineRepo */
  258.             $productLineRepo $this->em->getRepository(ProductLine::class);
  259.             $productLine $productLineRepo->findOneBy([
  260.                 'productType' => $productType,
  261.                 'id' => $product->getType()->getId(),
  262.                 'locale' => $this->request->getLocale(),
  263.                 'isEnabled' => true,
  264.             ]); // if the category exists
  265.             if ($product->getType() != $productLine) { // if product doesn't belong to the category
  266.                 return $this->redirectToRoute('homepage');
  267. //            throw $this->createNotFoundException($this->transalor->trans('exception.product_not_found'));
  268.             }
  269.             $correlatedProducts $productRepo->findCorrelatedProducts($product->getId(), $product->getType()->getId(), $this->request->getLocale());
  270.             $metatags $this->getMetatags($product'name''description');
  271.             return $this->render('prodotti/productDetailOld.html.twig', [
  272.                 'product' => $product,
  273.                 'metatags' => $metatags,
  274.                 'correlatedProducts' => $correlatedProducts
  275.             ]);
  276.         }
  277. //        if($category_slug == 'spalmabile'){
  278. //            return $this->redirectToRoute('spalmabile_vegetale');
  279. //        }
  280. //        $arrayRedirect = [
  281. //            'ortofrutta',
  282. //            'quienes-somos'
  283. //        ];
  284. //        if (in_array($category_slug, $arrayRedirect)){
  285. //            return $this->redirectToRoute('app_show_product_ortofrutta', [
  286. //                'category_slug' => null,
  287. //                'product_slug' => $product_slug
  288. //            ]);
  289. //        }
  290.         /** @var ProductRepository $productRepo */
  291.         $productRepo $this->em->getRepository(Product::class);
  292. //        $routeName = $this->request->attributes->get('_route');
  293. //        if($routeName == 'app_show_product_ortofrutta'){
  294. //            switch ($this->request->getLocale()) {
  295. //                case 'es':
  296. //                    $category_slug = 'quienes-somos';
  297. //                    break;
  298. //                default:
  299. //                    $category_slug = 'ortofrutta';
  300. //                    break;
  301. //            }
  302. //        }
  303.         /** @var Product $product */
  304.         $product $productRepo->findOneBy([
  305.             'slug' => $product_slug,
  306.             'isEnabled' => true,
  307.             'locale' => $this->request->getLocale(),
  308.         ]);
  309.         if (!$product) { // controlla che la linea prodotti esista
  310.             return $this->redirectToRoute('homepage', [], 301);
  311. //            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  312.         }
  313.         $listBreadcrumbs = [];
  314.         /** @var ProductCategory $productCategoryItem */
  315.         $productCategoryItem $product->getProductCategory();
  316.         if ($productCategoryItem instanceof ProductCategory) {
  317.             do {
  318.                 $listBreadcrumbs[] = [
  319.                     $this->generateUrl('app_product_category_show', [
  320.                         'urlChain' => $productCategoryItem->getSlugComposite(),
  321.                     ]),
  322.                     $productCategoryItem->getName(),
  323.                 ];
  324.             } while ($productCategoryItem $productCategoryItem->getParent());
  325.         }
  326.         krsort($listBreadcrumbs);
  327.         $listBreadcrumbs[] = [
  328.             $this->generateUrl('app_product_show', [
  329.                 'product_slug' => $product->getSlug()
  330.             ]),
  331.             $product->getName(),
  332.         ];
  333.         if ($product->getIdentifier() == Product::IDENTIFIER_SPALMABILE) {
  334.             $metatags $this->getMetatags($product'name''description');
  335.             return $this->render('prodotti/spalmabile.html.twig', [
  336.                 'product' => $product,
  337.                 'metatags' => $metatags,
  338.                 'listBreadcrumbs' => $listBreadcrumbs
  339.             ]);
  340.         }
  341.         if (
  342.             $request->get('_route') == 'app_show_product_ortofrutta'
  343.             or $request->get('_route') == 'app_show_product'
  344.         ) {
  345.             return $this->redirectToRoute('app_product_show', [
  346.                 'product_slug' => $product->getSlug()
  347.             ], 301);
  348.         }
  349. //        /** @var ProductTypeRepository $productTypeRepo */
  350. //        $productTypeRepo = $this->em->getRepository(ProductType::class);
  351. //        $productType = $productTypeRepo->findOneBy([
  352. //            'slug' => $category_slug,
  353. //            'locale' => $this->request->getLocale(),
  354. //        ]);
  355. //        // dd($productType, $category_slug, $this->request->getLocale());
  356. //        if (!$productType) { // controlla che la linea prodotti esista
  357. //            return $this->redirectToRoute('homepage');
  358. ////            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  359. //        }
  360. //        /** @var ProductLineRepository $productLineRepo */
  361. //        $productLineRepo = $this->em->getRepository(ProductLine::class);
  362. //        $productLine = $productLineRepo->findOneBy([
  363. //            'productType' => $productType,
  364. //            'id' => $product->getType()->getId(),
  365. //            'locale' => $this->request->getLocale(),
  366. //            'isEnabled' => true,
  367. //        ]); // if the category exists
  368. //        if ($product->getType() != $productLine) { // if product doesn't belong to the category
  369. //            return $this->redirectToRoute('homepage');
  370. ////            throw $this->createNotFoundException($this->transalor->trans('exception.product_not_found'));
  371. //        }
  372.         $correlatedProducts $productRepo->findCorrelatedProducts($product->getId(), $product->getType()->getId(), $this->request->getLocale());
  373.         $metatags $this->getMetatags($product'name''description');
  374.         return $this->render('prodotti/productDetail.html.twig', [
  375.             'product' => $product,
  376.             'metatags' => $metatags,
  377.             'correlatedProducts' => $correlatedProducts,
  378.             'listBreadcrumbs' => $listBreadcrumbs
  379.         ]);
  380.     }
  381. //    #[Route(['/prodotti/spalmabile-vegetale',
  382. //    //            'en' => '/en',
  383. //    //            'es' => '/es',
  384. //            ], name: 'spalmabile_vegetale', priority:2)]
  385. //    public function landingSpalmabileVegetale(Request $request, EntityManagerInterface $em)
  386. //    {
  387. //        list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $em, $this->getParameter('locales'));
  388. //
  389. //        return $this->render('landing/spalmabileVegetale.html.twig', [
  390. //            'metatags' => $metatags,
  391. //            'hreflangs' => $hreflangs
  392. //        ]);
  393. //    }
  394. }