src/Controller/Front/TopPdfProductController.php line 115

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Slim Sayari
  5.  * Date: 10/07/2019
  6.  * Time: 13:43
  7.  */
  8. namespace App\Controller\Front;
  9. use App\Entity\Accessoire;
  10. use App\Entity\CategoryPdf;
  11. use App\Entity\Product;
  12. use App\Entity\ProductAttribute;
  13. use App\Entity\ProductPdf;
  14. use App\Services\CartService;
  15. use App\Services\CheckoutService;
  16. use App\Services\Liseuse;
  17. use App\Services\Notifications;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Twig\Environment;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  26. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  27. use Doctrine\ORM\EntityManagerInterface;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  30. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  31. use Knp\Component\Pager\PaginatorInterface;
  32. /**
  33.  * @Route("/top-100")
  34.  */
  35. class TopPdfProductController extends AbstractController
  36. {
  37.     private $em;
  38.     private $session;
  39.     private $trans;
  40.     private $cart_service;
  41.     private $authenticationUtils;
  42.     private $checkout_service;
  43.     private $notifications;
  44.     private $router;
  45.     private $paginator;
  46.     private $liseuse_service;
  47.     private $params;
  48.     public function __construct(
  49.         Notifications $notifications,
  50.         CheckoutService $checkout_service,
  51.         CartService $cart_service,
  52.         SessionInterface $session,
  53.         TranslatorInterface $trans,
  54.         EntityManagerInterface $em,
  55.         AuthenticationUtils $authenticationUtils,
  56.         UrlGeneratorInterface $router,
  57.         PaginatorInterface $paginator,
  58.         Liseuse $liseuse_service,
  59.         ParameterBagInterface $params
  60.     ) {
  61.         $this->em $em;
  62.         $this->trans $trans;
  63.         $this->session $session;
  64.         $this->cart_service $cart_service;
  65.         $this->checkout_service $checkout_service;
  66.         $this->authenticationUtils $authenticationUtils;
  67.         $this->notifications $notifications;
  68.         $this->router $router;
  69.         $this->paginator $paginator;
  70.         $this->liseuse_service $liseuse_service;
  71.         $this->params $params;
  72.     }
  73.     /**
  74.      * @Route("/", name="index_top_100")
  75.      */
  76.     public function index(Request $request): Response
  77.     {
  78.         $categories $this->em->getRepository(CategoryPdf::class)->getActive();
  79.         $queryBuilder $this->em->getRepository(ProductPdf::class)->getActiveQueryBuilder();
  80.         $products $this->paginator->paginate(
  81.             $queryBuilder/* query NOT result */
  82.             $request->query->getInt('page'1)/*page number*/,
  83.             12/*limit per page*/
  84.         );
  85.         return $this->render('front/top-100-product/index.html.twig', ['products' => $products,'categories'=>$categories]);
  86.     }
  87.     /**
  88.      * @Route("/pagination-product-top-100", name="getProductByPagination",options={"expose"=true})
  89.      */
  90.     public function getProductByPagination(Request $request) : Response
  91.     {
  92.         $category =  $request->query->get('category');
  93.         $string =  $request->query->get('string');
  94.         $queryBuilder $this->em->getRepository(ProductPdf::class)->getActiveQueryBuilder($category,$string);
  95.         $products $this->paginator->paginate(
  96.             $queryBuilder/* query NOT result */
  97.             $request->query->getInt('page'1)/*page number*/,
  98.             12/*limit per page*/
  99.         );
  100.         $html $this->render('front/top-100-product/results.html.twig', ['products' => $products]);
  101.         return $html;
  102.     }
  103.     /**
  104.      * @Route("/add-to-cart/{id}", name="add_to_cart_top_100")
  105.      */
  106.     public function addToCart(Request $request,$id): Response
  107.     {
  108.         $productPdf $this->em->getRepository(ProductPdf::class)->find($id);
  109.         if($productPdf){
  110.             $idProduct $request->query->get('id_product',3);
  111.             $cart $this->cart_service->createCart(true); // create cart and token
  112.             $product $this->em->getRepository(Product::class)->find($idProduct);
  113.             // get productAtribute default
  114.             $productAttributeDefault $this->em->getRepository(ProductAttribute::class)->findOneBy(array('product'=>$product,'isDefault' => true,'isEnabled'=>true));
  115.             if(!$productAttributeDefault){
  116.                 $productAttributeDefault $this->em->getRepository(ProductAttribute::class)->findOneBy(array('product'=>$product,'isEnabled'=>true));
  117.             }
  118.             // nbr page
  119.             $token $this->liseuse_service->generateUniqueFileName();
  120.             // tableau qty
  121.             $tabQty = array(
  122.                 'qtyColor'=> (int)$productPdf->getQtyColor(),
  123.                 'qtyNoir'=>  (int)$productPdf->getQtyNoir(),
  124.                 'qty'=> (int)$productPdf->getQtyColor()+  (int)$productPdf->getQtyNoir(),
  125.             );
  126.             // tableau file
  127.             $tabFile = array(
  128.                 'file'=> $productPdf->getFile(),
  129.                 'name'=> $productPdf->getTitre()
  130.             );
  131.             if((int)$productPdf->getQtyColor() > 0){
  132.                 $accesoireColor $this->em->getRepository(Accessoire::class)->findOneBy(array('typeAccessoire' => Accessoire::COULEUR,'typeCouleur'=>Accessoire::TYPE_COULEUR));
  133.             }else{
  134.                 $accesoireColor $this->em->getRepository(Accessoire::class)->findOneBy(array('typeAccessoire' => Accessoire::COULEUR,'typeCouleur'=>Accessoire::TYPE_NOIR));
  135.             }
  136.             // add to cartDetail
  137.             $user $this->getUser();
  138.             $this->cart_service->addToCartPapier($user,false,$token,$cart,$product,$productAttributeDefault,$tabQty,1,$tabFile,$accesoireColor,false,$productPdf->getImage(),true,true);
  139.             return $this->redirectToRoute('liseuse',array('token'=>$token));
  140.         }else{
  141.             die('erreur!');
  142.         }
  143.     }
  144.     /**
  145.      * @Route("/change-to-cart/{id}/{token}", name="change_to_cart_top_100")
  146.      */
  147.     public function changePdfToCart(Request $request,$id,$token): Response
  148.     {
  149.         $data =  $this->cart_service->changePdfToCartPapier($this->getUser(),$token,$id);
  150.         return $this->redirectToRoute('liseuse',array('token'=>$token));
  151.     }
  152.     /**
  153.      * @Route("/download-product/{id}", name="download_product_top_100")
  154.      */
  155.     public function downloadRequest $request,$id){
  156.         $productPdf $this->em->getRepository(ProductPdf::class)->find($id);
  157.         $response = new Response();
  158.         if($productPdf){
  159.             $response->setContent(file_get_contents($this->params->get('productPdf_directory').'/'.$productPdf->getFile()));
  160.             $response->headers->set('Content-Type''application/force-download');
  161.             $response->headers->set('Content-disposition''filename= '.$productPdf->getFile());
  162.             $response->setStatusCode(200);
  163.             return $response;
  164.         }
  165.         $response->setContent('ok');
  166.         return $response;
  167.     }
  168. }