src/Controller/Front/ProductController.php line 55

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($id,$url,\Doctrine\ORM\EntityManagerInterface $em, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session, \App\Manager\SeenProductManager $seenProductMgr, \App\Manager\PriceManager $priceMgr, \App\Manager\CategoryManager $categoryMgr, \App\Manager\CustomerManager $customerMgr, \App\Manager\CartManager $cartMgr, \App\Manager\RedirectionManager $redirectionMgr)
  16. {
  17. $product = $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 = $em->getRepository('App:Redirection')->getByProduct($product);
  28. if($redirection){
  29. $url = $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($redirectionMgr->getRedirectUrl($redirection), 301);
  34. }
  35. }
  36. $seenProductMgr->addToSeenProducts($id);
  37. $path = [];
  38. $defaultCategory = $product->getDefaultCategory();
  39. $inCategory = [];
  40. if($defaultCategory){
  41. $path = $em->getRepository(Category::class)->getPath($defaultCategory);
  42. $inCategory = $categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $request->getLocale());
  43. // $inCategory = $em->getRepository('App:CategoryProduct')->getProductsByCategory($defaultCategory, true, 10, 1, [], [$product]);
  44. }
  45. $associates = $em->getRepository('App:ProductAssociated')->getAssociatedProduct($product);
  46. $priceGrids = [];
  47. $discountsList = [];
  48. if($product->hasChildren()){
  49. foreach($product->getChildren() as $child){
  50. $priceGrids[$child->getId()] = $priceMgr->getPriceGrid($child);
  51. $discountsList[$child->getId()] = $priceMgr->getBestDiscountByProduct($child);
  52. }
  53. }else{
  54. $priceGrids[$product->getId()] = $priceMgr->getPriceGrid($product);
  55. $discountsList[$product->getId()] = $priceMgr->getBestDiscountByProduct($product);
  56. }
  57. $productDescription = $product->getProductDescription($request->getLocale());
  58. $tags = $productDescription->getTags();
  59. if(empty($tags) && $defaultCategory){
  60. $tags = $categoryMgr->getPathTags($defaultCategory, $request->getLocale());
  61. }
  62. return $this->render('front/catalog/product/view.html.twig',[
  63. 'product'=>$product,
  64. 'associates'=>$associates,
  65. 'inCategory'=>$inCategory,
  66. 'path'=>$path,
  67. 'priceGrids'=>$priceGrids,
  68. 'discountsList'=>$discountsList,
  69. // 'discounts'=>[],
  70. 'pathTags'=>$tags,
  71. 'productDescription'=>$productDescription,
  72. 'customer'=>$customerMgr->getCustomer(),
  73. 'cart' => $cartMgr->getCart(),
  74. 'priceGroup' => $customerMgr->getPriceContext(),
  75. 'hasWelcomeDiscount'=>$priceMgr->hasWelcomeDiscount() && !$priceMgr->isLockedPrice($product)
  76. ]);
  77. }
  78. }