src/Controller/Front/CategoryController.php line 54

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 CategoryController extends FrontController
  10. {
  11. /**
  12. * @Route("/{_locale}/c/{id}/{url}", name="category", requirements={"id"="\d+","url"=".*", "_locale":"fr|en"}, options={"utf8": true})
  13. */
  14. public function view($id, $url, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session, \App\Manager\CategoryManager $categoryMgr, \App\Manager\RedirectionManager $redirectMgr)
  15. {
  16. $em = $this->getDoctrine()->getManager();
  17. $category = $em->getRepository(Category::class)->find($id);
  18. if(empty($category)){
  19. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  20. }
  21. $redirect = $em->getRepository('App:Redirection')->findOneByFromCategory($category);
  22. if($redirect){
  23. return $this->redirect($redirectMgr->getRedirectUrl($redirect), 301);
  24. }
  25. if(is_null($category->getParent())){
  26. return $this->redirectToRoute('universe',['id'=>$category->getId(),'url'=>$category->getUrl()], 301);
  27. }
  28. if($category->getUrl()!=$url)
  29. return $this->redirectToRoute('category', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
  30. $page = $request->get('page',null);
  31. if($page == 1)
  32. return $this->redirectToRoute('category', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
  33. $page = empty($page)?1:$page;
  34. if($category->isRoot()){
  35. $nbByPage = 20;
  36. }else{
  37. $nbByPage = 21;
  38. }
  39. $products = $categoryMgr->getProducts($category,[],[],1,$nbByPage);
  40. $productCount = $categoryMgr->getProductsCount($category);
  41. $nbPages = ceil($productCount/$nbByPage);
  42. $path = $em->getRepository(Category::class)->getPath($category->getParent());
  43. $filters = $categoryMgr->getFilters($category, $request->getLocale());
  44. $tpl = 'front/catalog/category/view.html.twig';
  45. $categoryDescription = $category->getCategoryDescription($request->getLocale());
  46. return $this->render('front/catalog/category/view.html.twig',[
  47. 'filters' => $filters,
  48. 'category' => $category,
  49. 'products' => $products,
  50. 'productCount' => $productCount,
  51. 'path' => $path,
  52. 'nbPages' => $nbPages,
  53. 'filterUrl' => $this->generateUrl('ajax_category',['id'=>$id]),
  54. 'page' => $page,
  55. 'categoryDescription' => $categoryDescription,
  56. 'discounts' => $this->getDiscountList($products)
  57. ]);
  58. }
  59. /**
  60. * @Route("/{_locale}/aj/c/{id}", name="ajax_category", requirements={"id"="\d+","_locale":"fr|en"})
  61. */
  62. public function ajax($id, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session, \App\Manager\CategoryManager $categoryMgr, \App\Manager\RedirectionManager $redirectMgr)
  63. {
  64. $em = $this->getDoctrine()->getManager();
  65. $category = $em->getRepository(Category::class)->find($id);
  66. if(empty($category)){
  67. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  68. }
  69. $page = (int) $request->get('page',1);
  70. $sort = $request->get('sort','discount-desc');
  71. $filters = $request->get('filter',[]);
  72. // if($category->hasChildren()){
  73. $nbByPage = 21;
  74. // }else{
  75. // $nbByPage = 20;
  76. // }
  77. $products = $categoryMgr->getProducts($category,$filters,$sort,$page,$nbByPage);
  78. $productCount = $categoryMgr->getProductsCount($category,$filters);
  79. $nbPages = ceil($productCount/$nbByPage);
  80. $output = [
  81. 'success' => true,
  82. 'data' => $request->getQueryString(),
  83. 'productCount' => $productCount,
  84. 'products' => $this->renderView('front/catalog/product/view/list.html.twig',[
  85. 'products' => $products,
  86. 'nbPages' => $nbPages,
  87. 'page' => $page,
  88. 'discounts' => $this->getDiscountList($products),
  89. 'wrapperTag' => $request->get('wrapperTag',0)
  90. ]),
  91. 'pagination' => $this->renderView('front/catalog/product/pagination.html.twig',[
  92. 'nbPages' => $nbPages,
  93. 'page' => $page,
  94. 'currentRoute' => $this->generateUrl('category', ['url'=>$category->getUrl(),'id'=>$category->getId()])
  95. ])
  96. ];
  97. return new \Symfony\Component\HttpFoundation\JsonResponse($output);
  98. }
  99. /**
  100. * @Route("/{_locale}/u/{id}/{url}", name="universe", requirements={"id"="\d+","url":"[a-zA-Z\-]*", "_locale":"fr|en"}, options={"utf8": true})
  101. */
  102. 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)
  103. {
  104. $em = $this->getDoctrine()->getManager();
  105. $category = $em->getRepository(Category::class)->find($id);
  106. if(empty($category)){
  107. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  108. }
  109. $redirect = $em->getRepository('App:Redirection')->findOneByFromCategory($category);
  110. if($redirect){
  111. return $this->redirect($redirectMgr->getRedirectUrl($redirect), 301);
  112. }
  113. if(!is_null($category->getParent())){
  114. return $this->redirectToRoute('category',['id'=>$category->getId(),'url'=>$category->getUrl()]);
  115. }
  116. if($category->getUrl()!=$url)
  117. return $this->redirectToRoute('universe', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
  118. if($id == Category::DISCOUNT_UNIVERSE){
  119. $filters = [];
  120. $path = $em->getRepository(Category::class)->getPath($category);
  121. $page = $request->get('page', 1);
  122. $sortGet = $request->get('sort', false);
  123. if($sortGet !== false){
  124. $session->set('sort', $sortGet);
  125. }
  126. $sort = $session->get('sort', null);
  127. $nbByPage = 20;
  128. $products = $discountMgr->getDiscountedProducts($sort, $page, 21, $request->getLocale());
  129. $productCount = $discountMgr->getDiscountedProductsCount();
  130. $categoryDescription = $category->getCategoryDescription($request->getLocale());
  131. $discountPages = $em->getRepository(\App\Entity\MarketingRule::class)->getActiveRules();
  132. return $this->render('front/catalog/discount/universe.html.twig',[
  133. 'filters' => $filters,
  134. 'category' => $category,
  135. 'products' => $products['products'],
  136. 'discounts' => $products['discounts'],
  137. 'discountPages' => $discountPages,
  138. 'productCount' => $productCount,
  139. 'path' => [],
  140. 'sort' => $sort,
  141. 'nbPages' => ceil($productCount / $nbByPage),
  142. 'page' => $page,
  143. 'filterUrl' => $this->generateUrl('ajax_category',['id'=>$id]),
  144. 'categoryDescription' => $categoryDescription
  145. ]);
  146. }
  147. $categoryDescription = $category->getCategoryDescription($request->getLocale());
  148. return $this->render('front/catalog/category/universe.html.twig',[
  149. 'category'=>$category,
  150. 'path'=>[],
  151. 'categoryDescription' => $categoryDescription
  152. ]);
  153. }
  154. /**
  155. * @Route("/{_locale}/aj/c/{id}", name="ajax_load", requirements={"id"="\d+","_locale":"fr|en"}, options={"utf8": true})
  156. */
  157. public function load($id, $url, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session)
  158. {
  159. $em = $this->getDoctrine()->getManager();
  160. $nbByPage = 20;
  161. $page = $request->get('page',1);
  162. $category = $em->getRepository(Category::class)->find($id);
  163. if(empty($category)){
  164. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  165. }
  166. // $category->translate($request->getLocale());
  167. $products = $em->getRepository(CategoryProduct::class)->getProductsByCategory($category, true, $nbByPage, $page);
  168. return new \Symfony\Component\HttpFoundation\JsonResponse([
  169. 'products' => $products
  170. ]);
  171. }
  172. public function subcategories(Request $request, Category $category, \App\Manager\CustomerManager $customerMgr, \App\Manager\CategoryManager $categoryMgr, \App\Manager\CacheManager $cacheMgr)
  173. {
  174. $customer = $customerMgr->getCustomer();
  175. $cachedData = $cacheMgr->getItem($request->getLocale(), 'universe-'.$category->getId());
  176. if(empty($customer) && $cachedData->isHit()){
  177. return new Response($cachedData->get());
  178. }
  179. $subcategories = [];
  180. foreach($category->getChildren() as $child){
  181. $subcategories[$child->getId()] = $categoryMgr->getProducts($child, [], null, 1, 10, $request->getLocale());
  182. }
  183. $html = $this->renderView('front/catalog/category/subcategories.html.twig', [
  184. 'children' => $category->getChildren(),
  185. 'subcategories' => $subcategories
  186. ]);
  187. if(empty($customer)){
  188. $cacheMgr->setItem($cachedData, $html, \App\Manager\CacheManager::TAG_UNIVERSE);
  189. }
  190. return new Response($html);
  191. }
  192. }