<?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 CategoryController extends FrontController
{
/**
* @Route("/{_locale}/c/{id}/{url}", name="category", requirements={"id"="\d+","url"=".*", "_locale":"fr|en"}, options={"utf8": true})
*/
public function view($id, $url, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session, \App\Manager\CategoryManager $categoryMgr, \App\Manager\RedirectionManager $redirectMgr)
{
$em = $this->getDoctrine()->getManager();
$category = $em->getRepository(Category::class)->find($id);
if(empty($category)){
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
$redirect = $em->getRepository('App:Redirection')->findOneByFromCategory($category);
if($redirect){
return $this->redirect($redirectMgr->getRedirectUrl($redirect), 301);
}
if(is_null($category->getParent())){
return $this->redirectToRoute('universe',['id'=>$category->getId(),'url'=>$category->getUrl()], 301);
}
if($category->getUrl()!=$url)
return $this->redirectToRoute('category', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
$page = $request->get('page',null);
if($page == 1)
return $this->redirectToRoute('category', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
$page = empty($page)?1:$page;
if($category->isRoot()){
$nbByPage = 20;
}else{
$nbByPage = 21;
}
// $products = $em->getRepository(CategoryProduct::class)->getProductsByCategory($category, true, $nbByPage, $page);
// $productCount = $em->getRepository(CategoryProduct::class)->getProductCountByCategory($category, true);
$products = $categoryMgr->getProducts($category, [], null, 1, $nbByPage);
$productCount = $categoryMgr->getProductsCount($category);
$nbPages = ceil($productCount/$nbByPage);
$path = $em->getRepository(Category::class)->getPath($category->getParent());
$filters = $categoryMgr->getFilters($category, $request->getLocale());
$tpl = 'front/catalog/category/view.html.twig';
$categoryDescription = $category->getCategoryDescription($request->getLocale());
return $this->render('front/catalog/category/view.html.twig',[
'filters' => $filters,
'category' => $category,
'products' => $products,
'productCount' => $productCount,
'path' => $path,
'nbPages' => $nbPages,
'filterUrl' => $this->generateUrl('ajax_category',['id'=>$id]),
'page' => $page,
'categoryDescription' => $categoryDescription,
'discounts' => $this->getDiscountList($products)
]);
}
/**
* @Route("/{_locale}/aj/c/{id}", name="ajax_category", requirements={"id"="\d+","_locale":"fr|en"})
*/
public function ajax($id, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session, \App\Manager\CategoryManager $categoryMgr, \App\Manager\RedirectionManager $redirectMgr)
{
$em = $this->getDoctrine()->getManager();
$category = $em->getRepository(Category::class)->find($id);
if(empty($category)){
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
$page = (int) $request->get('page',1);
$sort = $request->get('sort','discount-desc');
$filters = $request->get('filter',[]);
if($category->hasChildren()){
$nbByPage = 21;
}else{
$nbByPage = 20;
}
$products = $categoryMgr->getProducts($category,$filters,$sort,$page,$nbByPage);
$productCount = $categoryMgr->getProductsCount($category,$filters);
$nbPages = ceil($productCount/$nbByPage);
$output = [
'success' => true,
'data' => $request->getQueryString(),
'productCount' => $productCount,
'products' => $this->renderView('front/catalog/product/view/list.html.twig',[
'products' => $products,
'nbPages' => $nbPages,
'page' => $page,
'discounts' => $this->getDiscountList($products),
'wrapperTag' => $request->get('wrapperTag',0)
]),
'pagination' => $this->renderView('front/catalog/product/pagination.html.twig',[
'nbPages' => $nbPages,
'page' => $page,
'currentRoute' => $this->generateUrl('category', ['url'=>$category->getUrl(),'id'=>$category->getId()])
])
];
return new \Symfony\Component\HttpFoundation\JsonResponse($output);
}
/**
* @Route("/{_locale}/u/{id}/{url}", name="universe", requirements={"id"="\d+","url":"[a-zA-Z\-]*", "_locale":"fr|en"}, options={"utf8": true})
*/
public function universe($id, $url, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session, \App\Manager\RedirectionManager $redirectMgr, \App\Manager\CategoryManager $categoryMgr, \App\Manager\DiscountManager $discountMgr)
{
$em = $this->getDoctrine()->getManager();
$category = $em->getRepository(Category::class)->find($id);
if(empty($category)){
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
$redirect = $em->getRepository('App:Redirection')->findOneByFromCategory($category);
if($redirect){
return $this->redirect($redirectMgr->getRedirectUrl($redirect), 301);
}
if(!is_null($category->getParent())){
return $this->redirectToRoute('category',['id'=>$category->getId(),'url'=>$category->getUrl()]);
}
if($category->getUrl()!=$url)
return $this->redirectToRoute('universe', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
if($id == Category::DISCOUNT_UNIVERSE){
// $filters = $categoryMgr->getFilters($category, $request->getLocale());
$filters = [];
$path = $em->getRepository(Category::class)->getPath($category);
// $products = $em->getRepository(\App\Entity\SpecialPrice::class)->getDiscountedProducts();
// $products2 = $em->getRepository(\App\Entity\MarketingRuleProductDiscounted::class)->getDiscountedProducts();
// $products = array_merge($products, $products2['products']);
$page = $request->get('page', 1);
$sortGet = $request->get('sort', false);
if($sortGet !== false){
$session->set('sort', $sortGet);
}
$sort = $session->get('sort', null);
$nbByPage = 20;
$products = $discountMgr->getDiscountedProducts($sort, $page, 21, $request->getLocale());
$productCount = $discountMgr->getDiscountedProductsCount();
$categoryDescription = $category->getCategoryDescription($request->getLocale());
$discountPages = $em->getRepository(\App\Entity\MarketingRule::class)->getActiveRules();
return $this->render('front/catalog/discount/universe.html.twig',[
'filters' => $filters,
'category' => $category,
'products' => $products['products'],
'discounts' => $products['discounts'],
'discountPages' => $discountPages,
'productCount' => $productCount,
'path' => [],
'sort' => $sort,
'nbPages' => ceil($productCount / $nbByPage),
'page' => $page,
'filterUrl' => $this->generateUrl('ajax_category',['id'=>$id]),
'categoryDescription' => $categoryDescription
]);
}
$categoryDescription = $category->getCategoryDescription($request->getLocale());
return $this->render('front/catalog/category/universe.html.twig',[
'category'=>$category,
'path'=>[],
'categoryDescription' => $categoryDescription
]);
}
/**
* @Route("/{_locale}/aj/c/{id}", name="ajax_load", requirements={"id"="\d+","_locale":"fr|en"}, options={"utf8": true})
*/
public function load($id, $url, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session)
{
$em = $this->getDoctrine()->getManager();
$nbByPage = 20;
$page = $request->get('page',1);
$category = $em->getRepository(Category::class)->find($id);
if(empty($category)){
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
// $category->translate($request->getLocale());
$products = $em->getRepository(CategoryProduct::class)->getProductsByCategory($category, true, $nbByPage, $page);
return new \Symfony\Component\HttpFoundation\JsonResponse([
'products' => $products
]);
}
public function subcategories(Request $request, Category $category, \App\Manager\CustomerManager $customerMgr, \App\Manager\CategoryManager $categoryMgr, \App\Manager\CacheManager $cacheMgr)
{
$customer = $customerMgr->getCustomer();
$cachedData = $cacheMgr->getItem($request->getLocale(), 'universe-'.$category->getId());
if(empty($customer) && $cachedData->isHit()){
return new Response($cachedData->get());
}
$subcategories = [];
foreach($category->getChildren() as $child){
$subcategories[$child->getId()] = $categoryMgr->getProducts($child, [], null, 1, 10, $request->getLocale());
}
$html = $this->renderView('front/catalog/category/subcategories.html.twig', [
'children' => $category->getChildren(),
'subcategories' => $subcategories
]);
if(empty($customer)){
$cacheMgr->setItem($cachedData, $html, \App\Manager\CacheManager::TAG_UNIVERSE);
}
return new Response($html);
}
}