src/Controller/Front/CategoryController.php line 113

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