src/Controller/ProductController.php line 66

  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' => '.+'])]
  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. //    public function showCategory(string|null $category_slug)
  121. //    {
  122. //        $arrayRedirect = [
  123. //            'ortofrutta',
  124. //            'quienes-somos'
  125. //        ];
  126. //        if (in_array($category_slug, $arrayRedirect)){
  127. //            return $this->redirectToRoute('app_show_category_ortofrutta');
  128. //        }
  129. //
  130. //        if($category_slug == 'spalmabile'){
  131. //            return $this->redirectToRoute('spalmabile_vegetale');
  132. //        }
  133. //        if ($category_slug == 'kroccanti') {
  134. //            return $this->redirectToRoute('kroccanti');
  135. //        }
  136. //        /** @var ProductTypeRepository $productTypeRepo */
  137. //        $productTypeRepo = $this->em->getRepository(ProductType::class);
  138. //        $routeName = $this->request->attributes->get('_route');
  139. //        if($routeName == 'app_show_category_ortofrutta'){
  140. //            switch ($this->request->getLocale()) {
  141. //                case 'es':
  142. //                    $category_slug = 'quienes-somos';
  143. //                    break;
  144. //                default:
  145. //                    $category_slug = 'ortofrutta';
  146. //                    break;
  147. //            }
  148. //        }
  149. //
  150. //        $productType = $productTypeRepo->findOneBy([
  151. //            'slug' => $category_slug,
  152. //            'locale' => $this->request->getLocale(),
  153. //        ]);
  154. //        if (!$productType) { // controlla che la linea prodotti esista
  155. ////            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  156. //            return $this->redirectToRoute('homepage');
  157. //        }
  158. //        /** @var ProductLineRepository $productLineRepo */
  159. //        $productLineRepo = $this->em->getRepository(ProductLine::class);
  160. //        $productLine = $productLineRepo->findBy([
  161. //            'productType' => $productType,
  162. //            'locale' => $this->request->getLocale(),
  163. //            'isEnabled' => true,
  164. //        ]);
  165. //        if (!$productLine) { // controlla che la linea prodotti esista
  166. ////            throw $this->createNotFoundException($this->transalor->trans('exception.product_line_not_found'));
  167. //            return $this->redirectToRoute('homepage');
  168. //        }
  169. //
  170. //        /** @var ProductRepository $productRepo */
  171. //        $productRepo = $this->em->getRepository(Product::class);
  172. //        $products = $productRepo->findBy([
  173. //            'type' => $productLine,
  174. //            'locale' => $this->request->getLocale(),
  175. //            'isEnabled' => true,
  176. //        ],
  177. //        [
  178. //            'sortablePosition' => 'ASC'
  179. //        ]);
  180. //
  181. //
  182. //        $metatags = $this->getMetatags($productType, 'name', 'name');
  183. //
  184. //        return $this->render('prodotti/productIndex.html.twig', [
  185. //            'products' => $products,
  186. //            'productLine' => $productLine,
  187. //            'productType' => $productType,
  188. //            'metatags' => $metatags,
  189. //        ]);
  190. //    }
  191.     /**
  192.      * RENDER THE PRODUCT
  193.      */
  194.     #[Route(path: [
  195.         'it' => '/prodotti/{product_slug}',
  196.         'en' => 'en/products/{product_slug}',
  197.         'es' => 'es/productos/{product_slug}'
  198.     ]  , name'app_product_show')]
  199.     public function showProduct(
  200.         ProductCategoryUrlChainGenerator $productCategoryUrlChainGenerator,
  201.         Request $requeststring|null
  202.         $category_slug,
  203.         string $product_slug
  204.     )
  205.     {
  206. //        if($category_slug == 'spalmabile'){
  207. //            return $this->redirectToRoute('spalmabile_vegetale');
  208. //        }
  209. //        $arrayRedirect = [
  210. //            'ortofrutta',
  211. //            'quienes-somos'
  212. //        ];
  213. //        if (in_array($category_slug, $arrayRedirect)){
  214. //            return $this->redirectToRoute('app_show_product_ortofrutta', [
  215. //                'category_slug' => null,
  216. //                'product_slug' => $product_slug
  217. //            ]);
  218. //        }
  219.         /** @var ProductRepository $productRepo */
  220.         $productRepo $this->em->getRepository(Product::class);
  221. //        $routeName = $this->request->attributes->get('_route');
  222. //        if($routeName == 'app_show_product_ortofrutta'){
  223. //            switch ($this->request->getLocale()) {
  224. //                case 'es':
  225. //                    $category_slug = 'quienes-somos';
  226. //                    break;
  227. //                default:
  228. //                    $category_slug = 'ortofrutta';
  229. //                    break;
  230. //            }
  231. //        }
  232.         /** @var Product $product */
  233.         $product $productRepo->findOneBy([
  234.             'slug' => $product_slug,
  235.             'isEnabled' => true,
  236.             'locale' => $this->request->getLocale(),
  237.         ]);
  238.         if (!$product) { // controlla che la linea prodotti esista
  239.             return $this->redirectToRoute('homepage', [], 301);
  240. //            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  241.         }
  242.         $listBreadcrumbs = [];
  243.         /** @var ProductCategory $productCategoryItem */
  244.         $productCategoryItem $product->getProductCategory();
  245.         if ($productCategoryItem instanceof ProductCategory) {
  246.             do {
  247.                 $listBreadcrumbs[] = [
  248.                     $this->generateUrl('app_product_category_show', [
  249.                         'urlChain' => $productCategoryItem->getSlugComposite(),
  250.                     ]),
  251.                     $productCategoryItem->getName(),
  252.                 ];
  253.             } while ($productCategoryItem $productCategoryItem->getParent());
  254.         }
  255.         krsort($listBreadcrumbs);
  256.         $listBreadcrumbs[] = [
  257.             $this->generateUrl('app_product_show', [
  258.                 'product_slug' => $product->getSlug()
  259.             ]),
  260.             $product->getName(),
  261.         ];
  262.         if ($product->getIdentifier() == Product::IDENTIFIER_SPALMABILE) {
  263.             $metatags $this->getMetatags($product'name''description');
  264.             return $this->render('prodotti/spalmabile.html.twig', [
  265.                 'metatags' => $metatags,
  266.                 'listBreadcrumbs' => $listBreadcrumbs
  267.             ]);
  268.         }
  269.         if (
  270.             $request->get('_route') == 'app_show_product_ortofrutta'
  271.             or $request->get('_route') == 'app_show_product'
  272.         ) {
  273.             return $this->redirectToRoute('app_product_show', [
  274.                 'slug' => $product->getSlug()
  275.             ], 301);
  276.         }
  277. //        /** @var ProductTypeRepository $productTypeRepo */
  278. //        $productTypeRepo = $this->em->getRepository(ProductType::class);
  279. //        $productType = $productTypeRepo->findOneBy([
  280. //            'slug' => $category_slug,
  281. //            'locale' => $this->request->getLocale(),
  282. //        ]);
  283. //        // dd($productType, $category_slug, $this->request->getLocale());
  284. //        if (!$productType) { // controlla che la linea prodotti esista
  285. //            return $this->redirectToRoute('homepage');
  286. ////            throw $this->createNotFoundException($this->transalor->trans('exception.product_type_not_found'));
  287. //        }
  288. //        /** @var ProductLineRepository $productLineRepo */
  289. //        $productLineRepo = $this->em->getRepository(ProductLine::class);
  290. //        $productLine = $productLineRepo->findOneBy([
  291. //            'productType' => $productType,
  292. //            'id' => $product->getType()->getId(),
  293. //            'locale' => $this->request->getLocale(),
  294. //            'isEnabled' => true,
  295. //        ]); // if the category exists
  296. //        if ($product->getType() != $productLine) { // if product doesn't belong to the category
  297. //            return $this->redirectToRoute('homepage');
  298. ////            throw $this->createNotFoundException($this->transalor->trans('exception.product_not_found'));
  299. //        }
  300.         $correlatedProducts $productRepo->findCorrelatedProducts($product->getId(), $product->getType()->getId(), $this->request->getLocale());
  301.         $metatags $this->getMetatags($product'name''description');
  302.         return $this->render('prodotti/productDetail.html.twig', [
  303.             'product' => $product,
  304.             'metatags' => $metatags,
  305.             'correlatedProducts' => $correlatedProducts,
  306.             'listBreadcrumbs' => $listBreadcrumbs
  307.         ]);
  308.     }
  309. //    #[Route(['/prodotti/spalmabile-vegetale',
  310. //    //            'en' => '/en',
  311. //    //            'es' => '/es',
  312. //            ], name: 'spalmabile_vegetale', priority:2)]
  313. //    public function landingSpalmabileVegetale(Request $request, EntityManagerInterface $em)
  314. //    {
  315. //        list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $em, $this->getParameter('locales'));
  316. //
  317. //        return $this->render('landing/spalmabileVegetale.html.twig', [
  318. //            'metatags' => $metatags,
  319. //            'hreflangs' => $hreflangs
  320. //        ]);
  321. //    }
  322. }