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. }