src/Controller/Front/CategoryController.php line 186

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. 'productTitleTag' => 'h2'
  91. ]),
  92. 'pagination' => $this->renderView('front/catalog/product/pagination.html.twig',[
  93. 'nbPages' => $nbPages,
  94. 'page' => $page,
  95. 'currentRoute' => $this->generateUrl('category', ['url'=>$category->getUrl(),'id'=>$category->getId()])
  96. ])
  97. ];
  98. return new \Symfony\Component\HttpFoundation\JsonResponse($output);
  99. }
  100. /**
  101. * @Route("/{_locale}/u/{id}/{url}", name="universe", requirements={"id"="\d+","url":"[a-zA-Z\-]*", "_locale":"fr|en"}, options={"utf8": true})
  102. */
  103. 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)
  104. {
  105. $em = $this->getDoctrine()->getManager();
  106. $category = $em->getRepository(Category::class)->find($id);
  107. if(empty($category)){
  108. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  109. }
  110. $redirect = $em->getRepository('App:Redirection')->findOneByFromCategory($category);
  111. if($redirect){
  112. return $this->redirect($redirectMgr->getRedirectUrl($redirect), 301);
  113. }
  114. if(!is_null($category->getParent())){
  115. return $this->redirectToRoute('category',['id'=>$category->getId(),'url'=>$category->getUrl()]);
  116. }
  117. if($category->getUrl()!=$url)
  118. return $this->redirectToRoute('universe', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
  119. if($id == Category::DISCOUNT_UNIVERSE){
  120. $filters = [];
  121. $path = $em->getRepository(Category::class)->getPath($category);
  122. $page = $request->get('page', 1);
  123. $sortGet = $request->get('sort', false);
  124. if($sortGet !== false){
  125. $session->set('sort', $sortGet);
  126. }
  127. $sort = $session->get('sort', null);
  128. $nbByPage = 20;
  129. $products = $discountMgr->getDiscountedProducts($sort, $page, 21, $request->getLocale());
  130. $productCount = $discountMgr->getDiscountedProductsCount();
  131. $categoryDescription = $category->getCategoryDescription($request->getLocale());
  132. $discountPages = $em->getRepository(\App\Entity\MarketingRule::class)->getActiveRules();
  133. return $this->render('front/catalog/discount/universe.html.twig',[
  134. 'filters' => $filters,
  135. 'category' => $category,
  136. 'products' => $products['products'],
  137. 'discounts' => $products['discounts'],
  138. 'discountPages' => $discountPages,
  139. 'productCount' => $productCount,
  140. 'path' => [],
  141. 'sort' => $sort,
  142. 'nbPages' => ceil($productCount / $nbByPage),
  143. 'page' => $page,
  144. 'filterUrl' => $this->generateUrl('ajax_category',['id'=>$id]),
  145. 'categoryDescription' => $categoryDescription
  146. ]);
  147. }
  148. $categoryDescription = $category->getCategoryDescription($request->getLocale());
  149. return $this->render('front/catalog/category/universe.html.twig',[
  150. 'category'=>$category,
  151. 'path'=>[],
  152. 'categoryDescription' => $categoryDescription
  153. ]);
  154. }
  155. /**
  156. * @Route("/{_locale}/aj/c/{id}", name="ajax_load", requirements={"id"="\d+","_locale":"fr|en"}, options={"utf8": true})
  157. */
  158. public function load($id, $url, Request $request, \Symfony\Component\HttpFoundation\Session\Session $session)
  159. {
  160. $em = $this->getDoctrine()->getManager();
  161. $nbByPage = 20;
  162. $page = $request->get('page',1);
  163. $category = $em->getRepository(Category::class)->find($id);
  164. if(empty($category)){
  165. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  166. }
  167. // $category->translate($request->getLocale());
  168. $products = $em->getRepository(CategoryProduct::class)->getProductsByCategory($category, true, $nbByPage, $page);
  169. return new \Symfony\Component\HttpFoundation\JsonResponse([
  170. 'products' => $products
  171. ]);
  172. }
  173. public function subcategories(Request $request, Category $category, \App\Manager\CustomerManager $customerMgr, \App\Manager\CategoryManager $categoryMgr, \App\Manager\CacheManager $cacheMgr)
  174. {
  175. $customer = $customerMgr->getCustomer();
  176. $cachedData = $cacheMgr->getItem($request->getLocale(), 'universe-'.$category->getId());
  177. // if(empty($customer) && $cachedData->isHit()){
  178. // return new Response($cachedData->get());
  179. // }
  180. $subcategories = [];
  181. foreach($category->getChildren() as $child){
  182. $subcategories[$child->getId()] = $categoryMgr->getProducts($child, [], null, 1, 10, $request->getLocale());
  183. }
  184. $html = $this->renderView('front/catalog/category/subcategories.html.twig', [
  185. 'children' => $category->getChildren(),
  186. 'subcategories' => $subcategories
  187. ]);
  188. if(empty($customer)){
  189. $cacheMgr->setItem($cachedData, $html, \App\Manager\CacheManager::TAG_UNIVERSE);
  190. }
  191. return new Response($html);
  192. }
  193. }