src/Controller/Front/DiscountController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\MarketingRule;
  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 DiscountController extends FrontController
  10. {
  11. /**
  12. * @Route("/{_locale}/d/{id}/{url}", name="discount", 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. $discount = $em->getRepository(MarketingRule::class)->find($id);
  18. if(empty($discount)){
  19. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  20. }
  21. if($discount->getUrl()!=$url)
  22. return $this->redirectToRoute('category', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
  23. $page = $request->get('page', 1);
  24. $nbByPage = 20;
  25. $products = [];
  26. $discounts = [];
  27. foreach($discount->getProductsDiscounted() as $p){
  28. if(!$p->getProduct()->isStopped()) {
  29. $products[] = $p->getProduct();
  30. $discounts[$p->getProduct()->getId()] = $discount->getMarketingRuleDescription($request->getLocale())->getTitle();
  31. }
  32. }
  33. $productCount = count($products);
  34. $discountDescription = $discount->getMarketingRuleDescription($request->getLocale());
  35. $discountPages = $em->getRepository(\App\Entity\MarketingRule::class)->getActiveRules();
  36. $universe = $em->getRepository(\App\Entity\Category::class)->find(\App\Entity\Category::DISCOUNT_UNIVERSE);
  37. $path = [$universe];
  38. return $this->render('front/catalog/discount/view.html.twig',[
  39. 'path' => $path,
  40. 'discount' => $discount,
  41. 'category' => $universe,
  42. 'currentDiscountPage' => $discount,
  43. 'discountPages' => $discountPages,
  44. 'products' => $products,
  45. 'productCount' => $productCount,
  46. 'discounts' => $discounts,
  47. 'discountDescription' => $discountDescription
  48. ]);
  49. }
  50. }