src/Controller/Front/ManufacturerController.php line 33

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