src/Controller/Front/ProductController.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\EntityManager;
  8. use App\Entity\Product;
  9. use App\Entity\Category;
  10. class ProductController extends FrontController
  11. {
  12. /**
  13. * @Route("/{_locale}/p/{id}/{url}", name="product", requirements={"id"="\d+","url"=".*", "_locale":"fr|en"}, options={"utf8": true})
  14. */
  15. public function product(Request $request, $id,$url)
  16. {
  17. $product = $this->em->getRepository(Product::class)->find($id);
  18. if(empty($product)){
  19. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  20. }
  21. if($product->getUrl()!=$url)
  22. return $this->redirectToRoute('product', ['url'=>$product->getUrl(),'id'=>$product->getId()], 301);
  23. $parent = $product->getParent();
  24. if($product->getParent()){
  25. return $this->redirectToRoute('product',['url'=>$parent->getUrl(),'id'=>$parent->getId()],302);
  26. }
  27. $redirection = $this->em->getRepository('App:Redirection')->getByProduct($product);
  28. if($redirection){
  29. $url = $this->redirectionMgr->getRedirectUrl($redirection);
  30. if($this->getUser()) {
  31. $request->getSession()->getFlashBag()->add('notice',"Ce produit fait l'object d'une redirection vers <a href=\"".$url."\">".$url."</a>.");
  32. }else{
  33. return $this->redirect($this->redirectionMgr->getRedirectUrl($redirection), 301);
  34. }
  35. }
  36. $this->seenProductMgr->addToSeenProducts($id);
  37. if($product->hasProductModel()) {
  38. return $this->renderProductModel($product->getTopProductModel(), $product, $request->getLocale());
  39. }else{
  40. return $this->renderProduct($product, $request->getLocale());
  41. }
  42. }
  43. protected function renderProduct(Product $product, $locale) {
  44. $defaultCategory = $this->productMgr->getDefaultCategory($product);
  45. $path = [];
  46. $inCategory = [];
  47. if($defaultCategory){
  48. $path = $this->em->getRepository(Category::class)->getPath($defaultCategory);
  49. $inCategory = $this->categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $locale);
  50. }
  51. $associates = $this->em->getRepository('App:ProductAssociated')->getAssociatedProduct($product);
  52. $priceGrids = [];
  53. $discountsList = [];
  54. if($product->hasChildren()){
  55. foreach($product->getChildren() as $child){
  56. $priceGrids[$child->getId()] = $this->priceMgr->getPriceGrid($child);
  57. $discountsList[$child->getId()] = $this->priceMgr->getBestDiscountByProduct($child);
  58. }
  59. }else{
  60. $priceGrids[$product->getId()] = $this->priceMgr->getPriceGrid($product);
  61. $discountsList[$product->getId()] = $this->priceMgr->getBestDiscountByProduct($product);
  62. }
  63. $productDescription = $product->getProductDescription($locale);
  64. $tags = $productDescription->getTags();
  65. if(empty($tags) && $defaultCategory){
  66. $tags = $this->categoryMgr->getPathTags($defaultCategory, $locale);
  67. }
  68. return $this->render('front/catalog/product/view.html.twig',[
  69. 'product'=>$product,
  70. 'associates'=>$associates,
  71. 'inCategory'=>$inCategory,
  72. 'path'=>$path,
  73. 'priceGrids'=>$priceGrids,
  74. 'discountsList'=>$discountsList,
  75. 'discounts'=>[],
  76. 'pathTags'=>$tags,
  77. 'productDescription'=>$productDescription,
  78. 'customer'=>$this->customerMgr->getCustomer(),
  79. 'cart' => $this->cartMgr->getCart(),
  80. 'priceGroup' => $this->customerMgr->getPriceContext(),
  81. 'hasWelcomeDiscount'=>$this->priceMgr->hasWelcomeDiscount() && !$priceMgr->isLockedPrice($product)
  82. ]);
  83. }
  84. protected function renderProductModel(\App\Entity\ProductModel $productModel, Product $product, $locale) {
  85. $defaultCategory = $this->productMgr->getDefaultCategory($product);
  86. $path = [];
  87. $inCategory = [];
  88. if($defaultCategory){
  89. $path = $this->em->getRepository(Category::class)->getPath($defaultCategory);
  90. $inCategory = $this->categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $locale);
  91. }
  92. $productModelDescription = $productModel->getProductModelDescription($locale);
  93. $productDescription = $product->getProductDescription($locale);
  94. $tags = $productDescription->getTags();
  95. if(empty($tags) && $defaultCategory){
  96. $tags = $this->categoryMgr->getPathTags($defaultCategory, $locale);
  97. }
  98. $products = $productModel->getAllProducts();
  99. $defaultProduct = $products[0];
  100. return $this->render('front/catalog/model/view.html.twig',[
  101. 'attributes'=>$this->attributeMgr->getAttributesByProductModel($productModel),
  102. 'defaultProduct'=>$defaultProduct,
  103. 'product'=>$product,
  104. 'products'=>$products,
  105. 'assets'=>$productModel->getAssets(),
  106. 'productModel'=>$productModel,
  107. 'associates'=>$this->em->getRepository('App:ProductAssociated')->getAssociatedProduct($product),
  108. 'inCategory'=>$inCategory,
  109. 'path'=>$path,
  110. 'pathTags'=>$tags,
  111. 'discountsList'=>[],
  112. 'discounts'=>[],
  113. 'productDescription'=>$productDescription,
  114. 'productModelDescription'=>$productModelDescription,
  115. 'customer'=>$this->customerMgr->getCustomer(),
  116. 'cart' => $this->cartMgr->getCart(),
  117. 'priceGroup' => $this->customerMgr->getPriceContext(),
  118. 'hasWelcomeDiscount'=>$this->priceMgr->hasWelcomeDiscount() && !$this->priceMgr->isLockedPrice($product)
  119. ]);
  120. }
  121. }