<?php
namespace App\Controller\Front;
use App\Entity\Category;
use App\Entity\CategoryProduct;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManager;
class ManufacturerController extends FrontController
{
/**
* @Route("/{_locale}/nos-marques", name="manufacturers", requirements={"_locale":"fr|en"}, options={"utf8": true})
*/
public function list(Request $request, \Symfony\Component\HttpFoundation\Session\Session $session)
{
$em = $this->getDoctrine()->getManager();
$manufacturers = $em->getRepository('App:Manufacturer')->findBy(['status'=>1], ['name'=>'asc']);
return $this->render('front/catalog/manufacturer/list.html.twig',[
'manufacturers'=>$manufacturers
]);
}
/**
* @Route("/{_locale}/m/{id}/{url}", name="manufacturer", requirements={"id"="\d+", "_locale":"fr|en"}, options={"utf8": true})
*/
public function view($id, $url = "", Request $request, \Symfony\Component\HttpFoundation\Session\Session $session)
{
$em = $this->getDoctrine()->getManager();
$manufacturer = $em->getRepository(\App\Entity\Manufacturer::class)->find($id);
if(empty($manufacturer)){
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
// $manufacturer->translate($request->getLocale());
$canonicalUrl = $manufacturer->getUrl($request->getLocale());
if($canonicalUrl!=$url)
return $this->redirectToRoute('manufacturer', ['url'=>$canonicalUrl,'id'=>$manufacturer->getId()], 301);
$status = $manufacturer->getStatus();
if($status == 0){
return $this->redirectToRoute('manufacturers');
}
$page = $request->get('page',false);
if($page == 1){
return $this->redirectToRoute('manufacturer', ['url'=>$canonicalUrl,'id'=>$manufacturer->getId()], 301);
}
$page = $page === false ? 1 : $page;
$nbByPage = 20;
$products = $em->getRepository('App:Product')->getProductsByManufacturer($manufacturer, true, $nbByPage, $page);
$productCount = $em->getRepository('App:Product')->getProductsCountByManufacturer($manufacturer, true);
$nbPages = ceil($productCount/$nbByPage);
$manufacturerDescription = $manufacturer->getManufacturerDescription($request->getLocale());
return $this->render('front/catalog/manufacturer/view.html.twig',[
'manufacturer'=>$manufacturer,
'manufacturerDescription'=>$manufacturerDescription,
'products'=>$products,
'productCount'=>$productCount,
'nbPages'=>$nbPages,
'page'=>$page
]);
}
}