<?php
namespace App\Controller\Front;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityManager;
use App\Entity\Product;
use App\Entity\Category;
class ProductController extends FrontController
{
/**
* @Route("/{_locale}/p/{id}/{url}", name="product", requirements={"id"="\d+","url"=".*", "_locale":"fr|en"}, options={"utf8": true})
*/
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)
{
$product = $em->getRepository(Product::class)->find($id);
if(empty($product)){
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
if($product->getUrl()!=$url)
return $this->redirectToRoute('product', ['url'=>$product->getUrl(),'id'=>$product->getId()], 301);
$parent = $product->getParent();
if($product->getParent()){
return $this->redirectToRoute('product',['url'=>$parent->getUrl(),'id'=>$parent->getId()],302);
}
$redirection = $em->getRepository('App:Redirection')->getByProduct($product);
if($redirection){
$url = $redirectionMgr->getRedirectUrl($redirection);
if($this->getUser()) {
$request->getSession()->getFlashBag()->add('notice',"Ce produit fait l'object d'une redirection vers <a href=\"".$url."\">".$url."</a>.");
}else{
return $this->redirect($redirectionMgr->getRedirectUrl($redirection), 301);
}
}
$seenProductMgr->addToSeenProducts($id);
$path = [];
$defaultCategory = $product->getDefaultCategory();
$inCategory = [];
if($defaultCategory){
$path = $em->getRepository(Category::class)->getPath($defaultCategory);
$inCategory = $categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $request->getLocale());
// $inCategory = $em->getRepository('App:CategoryProduct')->getProductsByCategory($defaultCategory, true, 10, 1, [], [$product]);
}
$associates = $em->getRepository('App:ProductAssociated')->getAssociatedProduct($product);
$priceGrids = [];
$discountsList = [];
if($product->hasChildren()){
foreach($product->getChildren() as $child){
$priceGrids[$child->getId()] = $priceMgr->getPriceGrid($child);
$discountsList[$child->getId()] = $priceMgr->getBestDiscountByProduct($child);
}
}else{
$priceGrids[$product->getId()] = $priceMgr->getPriceGrid($product);
$discountsList[$product->getId()] = $priceMgr->getBestDiscountByProduct($product);
}
$productDescription = $product->getProductDescription($request->getLocale());
$tags = $productDescription->getTags();
if(empty($tags) && $defaultCategory){
$tags = $categoryMgr->getPathTags($defaultCategory, $request->getLocale());
}
return $this->render('front/catalog/product/view.html.twig',[
'product'=>$product,
'associates'=>$associates,
'inCategory'=>$inCategory,
'path'=>$path,
'priceGrids'=>$priceGrids,
'discountsList'=>$discountsList,
// 'discounts'=>[],
'pathTags'=>$tags,
'productDescription'=>$productDescription,
'customer'=>$customerMgr->getCustomer(),
'cart' => $cartMgr->getCart(),
'priceGroup' => $customerMgr->getPriceContext(),
'hasWelcomeDiscount'=>$priceMgr->hasWelcomeDiscount() && !$priceMgr->isLockedPrice($product)
]);
}
}