<?php
/**
* Created by PhpStorm.
* User: Slim Sayari
* Date: 10/07/2019
* Time: 13:43
*/
namespace App\Controller\Front;
use App\Entity\Accessoire;
use App\Entity\CategoryPdf;
use App\Entity\Product;
use App\Entity\ProductAttribute;
use App\Entity\ProductPdf;
use App\Services\CartService;
use App\Services\CheckoutService;
use App\Services\Liseuse;
use App\Services\Notifications;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Knp\Component\Pager\PaginatorInterface;
/**
* @Route("/top-100")
*/
class TopPdfProductController extends AbstractController
{
private $em;
private $session;
private $trans;
private $cart_service;
private $authenticationUtils;
private $checkout_service;
private $notifications;
private $router;
private $paginator;
private $liseuse_service;
private $params;
public function __construct(
Notifications $notifications,
CheckoutService $checkout_service,
CartService $cart_service,
SessionInterface $session,
TranslatorInterface $trans,
EntityManagerInterface $em,
AuthenticationUtils $authenticationUtils,
UrlGeneratorInterface $router,
PaginatorInterface $paginator,
Liseuse $liseuse_service,
ParameterBagInterface $params
) {
$this->em = $em;
$this->trans = $trans;
$this->session = $session;
$this->cart_service = $cart_service;
$this->checkout_service = $checkout_service;
$this->authenticationUtils = $authenticationUtils;
$this->notifications = $notifications;
$this->router = $router;
$this->paginator = $paginator;
$this->liseuse_service = $liseuse_service;
$this->params = $params;
}
/**
* @Route("/", name="index_top_100")
*/
public function index(Request $request): Response
{
$categories = $this->em->getRepository(CategoryPdf::class)->getActive();
$queryBuilder = $this->em->getRepository(ProductPdf::class)->getActiveQueryBuilder();
$products = $this->paginator->paginate(
$queryBuilder, /* query NOT result */
$request->query->getInt('page', 1)/*page number*/,
12/*limit per page*/
);
return $this->render('front/top-100-product/index.html.twig', ['products' => $products,'categories'=>$categories]);
}
/**
* @Route("/pagination-product-top-100", name="getProductByPagination",options={"expose"=true})
*/
public function getProductByPagination(Request $request) : Response
{
$category = $request->query->get('category');
$string = $request->query->get('string');
$queryBuilder = $this->em->getRepository(ProductPdf::class)->getActiveQueryBuilder($category,$string);
$products = $this->paginator->paginate(
$queryBuilder, /* query NOT result */
$request->query->getInt('page', 1)/*page number*/,
12/*limit per page*/
);
$html = $this->render('front/top-100-product/results.html.twig', ['products' => $products]);
return $html;
}
/**
* @Route("/add-to-cart/{id}", name="add_to_cart_top_100")
*/
public function addToCart(Request $request,$id): Response
{
$productPdf = $this->em->getRepository(ProductPdf::class)->find($id);
if($productPdf){
$idProduct = $request->query->get('id_product',3);
$cart = $this->cart_service->createCart(true); // create cart and token
$product = $this->em->getRepository(Product::class)->find($idProduct);
// get productAtribute default
$productAttributeDefault = $this->em->getRepository(ProductAttribute::class)->findOneBy(array('product'=>$product,'isDefault' => true,'isEnabled'=>true));
if(!$productAttributeDefault){
$productAttributeDefault = $this->em->getRepository(ProductAttribute::class)->findOneBy(array('product'=>$product,'isEnabled'=>true));
}
// nbr page
$token = $this->liseuse_service->generateUniqueFileName();
// tableau qty
$tabQty = array(
'qtyColor'=> (int)$productPdf->getQtyColor(),
'qtyNoir'=> (int)$productPdf->getQtyNoir(),
'qty'=> (int)$productPdf->getQtyColor()+ (int)$productPdf->getQtyNoir(),
);
// tableau file
$tabFile = array(
'file'=> $productPdf->getFile(),
'name'=> $productPdf->getTitre()
);
if((int)$productPdf->getQtyColor() > 0){
$accesoireColor = $this->em->getRepository(Accessoire::class)->findOneBy(array('typeAccessoire' => Accessoire::COULEUR,'typeCouleur'=>Accessoire::TYPE_COULEUR));
}else{
$accesoireColor = $this->em->getRepository(Accessoire::class)->findOneBy(array('typeAccessoire' => Accessoire::COULEUR,'typeCouleur'=>Accessoire::TYPE_NOIR));
}
// add to cartDetail
$user = $this->getUser();
$this->cart_service->addToCartPapier($user,false,$token,$cart,$product,$productAttributeDefault,$tabQty,1,$tabFile,$accesoireColor,false,$productPdf->getImage(),true,true);
return $this->redirectToRoute('liseuse',array('token'=>$token));
}else{
die('erreur!');
}
}
/**
* @Route("/change-to-cart/{id}/{token}", name="change_to_cart_top_100")
*/
public function changePdfToCart(Request $request,$id,$token): Response
{
$data = $this->cart_service->changePdfToCartPapier($this->getUser(),$token,$id);
return $this->redirectToRoute('liseuse',array('token'=>$token));
}
/**
* @Route("/download-product/{id}", name="download_product_top_100")
*/
public function download( Request $request,$id){
$productPdf = $this->em->getRepository(ProductPdf::class)->find($id);
$response = new Response();
if($productPdf){
$response->setContent(file_get_contents($this->params->get('productPdf_directory').'/'.$productPdf->getFile()));
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-disposition', 'filename= '.$productPdf->getFile());
$response->setStatusCode(200);
return $response;
}
$response->setContent('ok');
return $response;
}
}