<?php
namespace App\Controller\Front;
use App\Entity\MarketingRule;
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 DiscountController extends FrontController
{
/**
* @Route("/{_locale}/d/{id}/{url}", name="discount", 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();
$discount = $em->getRepository(MarketingRule::class)->find($id);
if(empty($discount)){
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
if($discount->getUrl()!=$url)
return $this->redirectToRoute('category', ['url'=>$category->getUrl(),'id'=>$category->getId()], 301);
$page = $request->get('page', 1);
$nbByPage = 20;
$products = [];
$discounts = [];
foreach($discount->getProductsDiscounted() as $p){
if(!$p->getProduct()->isStopped()) {
$products[] = $p->getProduct();
$discounts[$p->getProduct()->getId()] = $discount->getMarketingRuleDescription($request->getLocale())->getTitle();
}
}
$productCount = count($products);
$discountDescription = $discount->getMarketingRuleDescription($request->getLocale());
$discountPages = $em->getRepository(\App\Entity\MarketingRule::class)->getActiveRules();
$universe = $em->getRepository(\App\Entity\Category::class)->find(\App\Entity\Category::DISCOUNT_UNIVERSE);
$path = [$universe];
return $this->render('front/catalog/discount/view.html.twig',[
'path' => $path,
'discount' => $discount,
'category' => $universe,
'currentDiscountPage' => $discount,
'discountPages' => $discountPages,
'products' => $products,
'productCount' => $productCount,
'discounts' => $discounts,
'discountDescription' => $discountDescription
]);
}
}