<?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(Request $request, $id,$url)
{
$product = $this->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 = $this->em->getRepository('App:Redirection')->getByProduct($product);
if($redirection){
$url = $this->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($this->redirectionMgr->getRedirectUrl($redirection), 301);
}
}
$this->seenProductMgr->addToSeenProducts($id);
if($product->hasProductModel()) {
return $this->renderProductModel($product->getTopProductModel(), $product, $request->getLocale());
}else{
return $this->renderProduct($product, $request->getLocale());
}
}
protected function renderProduct(Product $product, $locale) {
$defaultCategory = $this->productMgr->getDefaultCategory($product);
$path = [];
$inCategory = [];
if($defaultCategory){
$path = $this->em->getRepository(Category::class)->getPath($defaultCategory);
$inCategory = $this->categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $locale);
}
$associates = $this->em->getRepository('App:ProductAssociated')->getAssociatedProduct($product);
$priceGrids = [];
$discountsList = [];
if($product->hasChildren()){
foreach($product->getChildren() as $child){
$priceGrids[$child->getId()] = $this->priceMgr->getPriceGrid($child);
$discountsList[$child->getId()] = $this->priceMgr->getBestDiscountByProduct($child);
}
}else{
$priceGrids[$product->getId()] = $this->priceMgr->getPriceGrid($product);
$discountsList[$product->getId()] = $this->priceMgr->getBestDiscountByProduct($product);
}
$productDescription = $product->getProductDescription($locale);
$tags = $productDescription->getTags();
if(empty($tags) && $defaultCategory){
$tags = $this->categoryMgr->getPathTags($defaultCategory, $locale);
}
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'=>$this->customerMgr->getCustomer(),
'cart' => $this->cartMgr->getCart(),
'priceGroup' => $this->customerMgr->getPriceContext(),
'hasWelcomeDiscount'=>$this->priceMgr->hasWelcomeDiscount() && !$priceMgr->isLockedPrice($product)
]);
}
protected function renderProductModel(\App\Entity\ProductModel $productModel, Product $product, $locale) {
$defaultCategory = $this->productMgr->getDefaultCategory($product);
$path = [];
$inCategory = [];
if($defaultCategory){
$path = $this->em->getRepository(Category::class)->getPath($defaultCategory);
$inCategory = $this->categoryMgr->getProducts($defaultCategory, [], null, 1, 10, $locale);
}
$productModelDescription = $productModel->getProductModelDescription($locale);
$productDescription = $product->getProductDescription($locale);
$tags = $productDescription->getTags();
if(empty($tags) && $defaultCategory){
$tags = $this->categoryMgr->getPathTags($defaultCategory, $locale);
}
$products = $productModel->getAllProducts();
$defaultProduct = $products[0];
return $this->render('front/catalog/model/view.html.twig',[
'attributes'=>$this->attributeMgr->getAttributesByProductModel($productModel),
'defaultProduct'=>$defaultProduct,
'product'=>$product,
'products'=>$products,
'assets'=>$productModel->getAssets(),
'productModel'=>$productModel,
'associates'=>$this->em->getRepository('App:ProductAssociated')->getAssociatedProduct($product),
'inCategory'=>$inCategory,
'path'=>$path,
'pathTags'=>$tags,
'discountsList'=>[],
'discounts'=>[],
'productDescription'=>$productDescription,
'productModelDescription'=>$productModelDescription,
'customer'=>$this->customerMgr->getCustomer(),
'cart' => $this->cartMgr->getCart(),
'priceGroup' => $this->customerMgr->getPriceContext(),
'hasWelcomeDiscount'=>$this->priceMgr->hasWelcomeDiscount() && !$this->priceMgr->isLockedPrice($product)
]);
}
}