src/Controller/Front/ProductController.php line 48

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. $redirection = $this->em->getRepository('App:Redirection')->getByProduct($product);
  22. if($redirection){
  23. $redirectUrl = $this->redirectionMgr->getRedirectUrl($redirection);
  24. if($this->getUser()) {
  25. $request->getSession()->getFlashBag()->add('notice',"Ce produit fait l'object d'une redirection vers <a href=\"".$redirectUrl."\">".$redirectUrl."</a>.");
  26. }else{
  27. return $this->redirect($redirectUrl, 301);
  28. }
  29. }
  30. // un ancien produit parent est remplacé par sa première déclinaison
  31. $target = $product->hasChildren() ? $product->getChildren()[0] : $product;
  32. // une seule URL par fiche : les déclinaisons pointent directement vers la fiche modèle
  33. // (quel que soit le slug demandé), l'ancre ignorée par les moteurs présélectionne la déclinaison
  34. $productModel = $target->getTopProductModel();
  35. if($productModel) {
  36. $productModel->translate($request->getLocale());
  37. return $this->redirect($this->generateUrl('product_model', ['id'=>$productModel->getId(), 'url'=>$productModel->getUrl()]).'#p='.$target->getId(), 301);
  38. }
  39. if($target !== $product || $product->getUrl()!=$url) {
  40. return $this->redirectToRoute('product', ['url'=>$target->getUrl(),'id'=>$target->getId()], 301);
  41. }
  42. $this->seenProductMgr->addToSeenProducts($id);
  43. return $this->renderProduct($product, $request->getLocale());
  44. }
  45. /**
  46. * @Route("/{_locale}/pm/m-{id}/{url}", name="product_model", requirements={"id"="\d+","url"=".*", "_locale":"fr|en"}, options={"utf8": true})
  47. */
  48. public function productModel(Request $request, $id,$url)
  49. {
  50. $productModel = $this->em->getRepository(\App\Entity\ProductModel::class)->find($id);
  51. if(empty($productModel)){
  52. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  53. }
  54. // la fiche est toujours celle du modèle racine
  55. if($productModel->hasParent()) {
  56. $productModel = $productModel->getParent();
  57. while($productModel->hasParent()) {
  58. $productModel = $productModel->getParent();
  59. }
  60. $productModel->translate($request->getLocale());
  61. return $this->redirectToRoute('product_model', ['id'=>$productModel->getId(), 'url'=>$productModel->getUrl()], 301);
  62. }
  63. $productModel->translate($request->getLocale());
  64. if($productModel->getUrl()!=$url) {
  65. return $this->redirectToRoute('product_model', ['id'=>$productModel->getId(), 'url'=>$productModel->getUrl()], 301);
  66. }
  67. $product = $productModel->getDefaultProduct();
  68. if(empty($product)){
  69. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  70. }
  71. $this->seenProductMgr->addToSeenProducts($product->getId());
  72. return $this->renderProductModel($productModel, $product, $request->getLocale());
  73. }
  74. protected function renderProduct(Product $product, $locale) {
  75. $defaultCategory = $this->productMgr->getDefaultCategory($product);
  76. $path = [];
  77. $inCategory = [];
  78. if($defaultCategory){
  79. $path = $this->em->getRepository(Category::class)->getPath($defaultCategory);
  80. $inCategory = $this->categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $locale);
  81. }
  82. $associates = $this->em->getRepository('App:ProductAssociated')->getAssociatedProduct($product);
  83. $priceGrids = [];
  84. $discountsList = [];
  85. $priceGrids[$product->getId()] = $this->priceMgr->getPriceGrid($product);
  86. $discountsList[$product->getId()] = $this->priceMgr->getBestDiscountByProduct($product);
  87. $productDescription = $product->getProductDescription($locale);
  88. $tags = $productDescription->getTags();
  89. if(empty($tags) && $defaultCategory){
  90. $tags = $this->categoryMgr->getPathTags($defaultCategory, $locale);
  91. }
  92. return $this->render('front/catalog/product/view.html.twig',[
  93. 'product'=>$product,
  94. 'associates'=>$associates,
  95. 'inCategory'=>$inCategory,
  96. 'path'=>$path,
  97. 'priceGrids'=>$priceGrids,
  98. 'discountsList'=>$discountsList,
  99. 'discounts'=>[],
  100. 'pathTags'=>$tags,
  101. 'productDescription'=>$productDescription,
  102. 'customer'=>$this->customerMgr->getCustomer(),
  103. 'cart' => $this->cartMgr->getCart(),
  104. 'priceGroup' => $this->customerMgr->getPriceContext(),
  105. 'hasWelcomeDiscount'=>$this->priceMgr->hasWelcomeDiscount() && !$this->priceMgr->isLockedPrice($product)
  106. ]);
  107. }
  108. protected function renderProductModel(\App\Entity\ProductModel $productModel, Product $product, $locale) {
  109. $defaultCategory = $this->productMgr->getDefaultCategory($product);
  110. $path = [];
  111. $inCategory = [];
  112. if($defaultCategory){
  113. $path = $this->em->getRepository(Category::class)->getPath($defaultCategory);
  114. $inCategory = $this->categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $locale);
  115. }
  116. $productModelDescription = $productModel->getProductModelDescription($locale);
  117. $productDescription = $product->getProductDescription($locale);
  118. $tags = $productDescription->getTags();
  119. if(empty($tags) && $defaultCategory){
  120. $tags = $this->categoryMgr->getPathTags($defaultCategory, $locale);
  121. }
  122. $products = $productModel->getAllProducts();
  123. $defaultProduct = $productModel->getDefaultProduct();
  124. $priceGrids = [];
  125. $discountsList = [];
  126. foreach($productModel->getAllProducts() as $child){
  127. $priceGrids[$child->getId()] = $this->priceMgr->getPriceGrid($child);
  128. $discountsList[$child->getId()] = $this->priceMgr->getBestDiscountByProduct($child);
  129. }
  130. return $this->render('front/catalog/model/view.html.twig',[
  131. 'attributes'=>$this->attributeMgr->getAttributesByProductModel($productModel),
  132. 'defaultProduct'=>$defaultProduct,
  133. 'product'=>$product,
  134. 'products'=>$products,
  135. 'assets'=>$productModel->getAssets(),
  136. 'productModel'=>$productModel,
  137. 'associates'=>$this->em->getRepository('App:ProductAssociated')->getAssociatedProduct($product),
  138. 'inCategory'=>$inCategory,
  139. 'path'=>$path,
  140. 'pathTags'=>$tags,
  141. 'discountsList'=>[],
  142. 'discounts'=>[],
  143. 'productDescription'=>$productDescription,
  144. 'productModelDescription'=>$productModelDescription,
  145. 'customer'=>$this->customerMgr->getCustomer(),
  146. 'cart' => $this->cartMgr->getCart(),
  147. 'priceGroup' => $this->customerMgr->getPriceContext(),
  148. 'priceGrids'=>$priceGrids,
  149. 'hasWelcomeDiscount'=>$this->priceMgr->hasWelcomeDiscount() && !$this->priceMgr->isLockedPrice($product)
  150. ]);
  151. }
  152. }