src/Controller/Front/ManufacturerController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Category;
  4. use App\Entity\CategoryProduct;
  5. use App\Entity\Manufacturer;
  6. use App\Entity\Product;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Doctrine\ORM\EntityManager;
  11. class ManufacturerController extends FrontController
  12. {
  13. /**
  14. * @Route("/{_locale}/nos-marques", name="manufacturers", requirements={"_locale":"fr|en"}, options={"utf8": true})
  15. */
  16. public function list(Request $request, \Symfony\Component\HttpFoundation\Session\Session $session)
  17. {
  18. $em = $this->getDoctrine()->getManager();
  19. // Ne remonte que les marques ayant au moins une référence active (produit racine, statut actif)
  20. $manufacturers = $em->createQueryBuilder()
  21. ->select('m')
  22. ->from(Manufacturer::class, 'm')
  23. ->innerJoin(Product::class, 'p', 'WITH', 'p.manufacturer = m')
  24. ->where('m.status = 1')
  25. ->andWhere('p.status = 1')
  26. ->andWhere('p.parent IS NULL')
  27. ->groupBy('m.id')
  28. ->orderBy('m.name', 'asc')
  29. ->getQuery()
  30. ->getResult();
  31. return $this->render('front/catalog/manufacturer/list.html.twig',[
  32. 'manufacturers'=>$manufacturers
  33. ]);
  34. }
  35. /**
  36. * @Route("/{_locale}/m/{id}/{url}", name="manufacturer", requirements={"id"="\d+", "_locale":"fr|en"}, options={"utf8": true})
  37. */
  38. public function view($id, $url = "", Request $request, \Symfony\Component\HttpFoundation\Session\Session $session)
  39. {
  40. $em = $this->getDoctrine()->getManager();
  41. $manufacturer = $em->getRepository(\App\Entity\Manufacturer::class)->find($id);
  42. if(empty($manufacturer)){
  43. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  44. }
  45. // $manufacturer->translate($request->getLocale());
  46. $canonicalUrl = $manufacturer->getUrl($request->getLocale());
  47. if($canonicalUrl!=$url)
  48. return $this->redirectToRoute('manufacturer', ['url'=>$canonicalUrl,'id'=>$manufacturer->getId()], 301);
  49. $status = $manufacturer->getStatus();
  50. if($status == 0){
  51. return $this->redirectToRoute('manufacturers');
  52. }
  53. $page = $request->get('page',false);
  54. if($page == 1){
  55. return $this->redirectToRoute('manufacturer', ['url'=>$canonicalUrl,'id'=>$manufacturer->getId()], 301);
  56. }
  57. $page = $page === false ? 1 : $page;
  58. $nbByPage = 20;
  59. $products = $em->getRepository('App:Product')->getProductsByManufacturer($manufacturer, true, $nbByPage, $page);
  60. $productCount = $em->getRepository('App:Product')->getProductsCountByManufacturer($manufacturer, true);
  61. $nbPages = ceil($productCount/$nbByPage);
  62. $manufacturerDescription = $manufacturer->getManufacturerDescription($request->getLocale());
  63. return $this->render('front/catalog/manufacturer/view.html.twig',[
  64. 'manufacturer'=>$manufacturer,
  65. 'manufacturerDescription'=>$manufacturerDescription,
  66. 'products'=>$products,
  67. 'productCount'=>$productCount,
  68. 'nbPages'=>$nbPages,
  69. 'page'=>$page
  70. ]);
  71. }
  72. /**
  73. * @Route("/{_locale}/aj/m/{id}", name="manufacturer_ajax", requirements={"id"="\d+", "_locale":"fr|en"}, options={"utf8": true})
  74. */
  75. public function ajax($id, Request $request)
  76. {
  77. $em = $this->getDoctrine()->getManager();
  78. $manufacturer = $em->getRepository(\App\Entity\Manufacturer::class)->find($id);
  79. if(empty($manufacturer)){
  80. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  81. }
  82. $page = $request->get('page');
  83. $page = $page ?? 1;
  84. $nbByPage = 20;
  85. $products = $em->getRepository('App:Product')->getProductsByManufacturer($manufacturer, true, $nbByPage, $page);
  86. $productCount = $em->getRepository('App:Product')->getProductsCountByManufacturer($manufacturer, true);
  87. $nbPages = ceil($productCount/$nbByPage);
  88. $output = [
  89. 'success' => true,
  90. 'data' => $request->getQueryString(),
  91. 'productCount' => $productCount,
  92. 'products' => $this->renderView('front/catalog/product/view/list.html.twig',[
  93. 'products' => $products,
  94. 'nbPages' => $nbPages,
  95. 'page' => $page,
  96. 'discounts' => $this->getDiscountList($products),
  97. 'wrapperTag' => $request->get('wrapperTag',0)
  98. ]),
  99. 'pagination' => $this->renderView('front/catalog/product/pagination.html.twig',[
  100. 'nbPages' => $nbPages,
  101. 'page' => $page,
  102. 'currentRoute' => $this->generateUrl('manufacturer', ['url'=>$manufacturer->getUrl(),'id'=>$manufacturer->getId()])
  103. ])
  104. ];
  105. return new \Symfony\Component\HttpFoundation\JsonResponse($output);
  106. }
  107. }