src/Controller/AuctionsController.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Auctions;
  4. use App\Entity\AuctionVehicles;
  5. use App\Entity\MetaData;
  6. use App\Entity\PageViews;
  7. use App\Entity\Vehicle;
  8. use App\Entity\VehicleOption;
  9. use App\Services\PaginationManager;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Dompdf\Dompdf;
  12. use Dompdf\Options;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class AuctionsController extends AbstractController
  20. {
  21.     const ITEMS_PER_PAGE 12;
  22.     private $session;
  23.     private $params;
  24.     public function __construct(PaginationManager $pageManagerParameterBagInterface $params) {
  25.         $this->pageManager $pageManager;
  26.         $this->session = new Session();
  27.         //$this->session->start();
  28.         $this->params $params;
  29.     }
  30.     /**
  31.      * @Route("/auctions", name="auctions")
  32.      */
  33.     public function index(Request $request): Response
  34.     {
  35.         $repo_meta $this->getDoctrine()->getRepository(MetaData::class)
  36.             ->findOneBy(['id' => 4]);
  37.         $auction_vehicles $this->getDoctrine()->getRepository(AuctionVehicles::class)
  38.             ->findByAuction();
  39.         $auction $this->getDoctrine()->getRepository(Auctions::class)
  40.             ->findOneBy(['status' => 1]);
  41.         $results $this->pageManager->paginate($auction_vehicles$requestself::ITEMS_PER_PAGE);
  42.         return $this->render('frontend/auctions.html.twig',
  43.             [
  44.                 'meta' => $repo_meta,
  45.                 'auctions' => $auction,
  46.                 'auction_vehicles' => $results,
  47.                 'lastPage' => $this->pageManager->lastPage($results),
  48.             ]
  49.         );
  50.     }
  51.     /**
  52.      * @Route("/auction-vehicle/{year}/{make}/{model}/{id}", name="vehicle")
  53.      */
  54.     public function getVehicleAction(Request $request): Response
  55.     {
  56.         $auction $this->getDoctrine()->getRepository(Auctions::class)->findOneBy(['status' => 2]);
  57.         $vehicle $this->getDoctrine()->getRepository(Vehicle::class)->findOneBy(['id' => $request->get('id')]);
  58.         $features $this->getDoctrine()->getRepository(VehicleOption::class)
  59.             ->findByCategory($request->get('id'),'Feature');
  60.         $accessories  $this->getDoctrine()->getRepository(VehicleOption::class)
  61.             ->findByCategory($request->get('id'),'Accessory');
  62.         $min $vehicle->getVehicleMotusData()->getMgv() - '25000';
  63.         $max $vehicle->getVehicleMotusData()->getMgv() + '25000';
  64.         $similar_vehicles $this->getDoctrine()->getRepository(AuctionVehicles::class)
  65.             ->findBySimilarPrice($auction->getId(),$min,$max);
  66.         return $this->render('frontend/vehicle.html.twig',
  67.             [
  68.                 'vehicle' => $vehicle,
  69.                 'auction' => $auction,
  70.                 'similar_vehicle' => $similar_vehicles,
  71.                 'features' => $features,
  72.                 'accessories' => $accessories
  73.             ]
  74.         );
  75.     }
  76.     /**
  77.      * @Route("/page-views", name="page_views")
  78.      */
  79.     public function pageViewsAction(Request $requestEntityManagerInterface $entityManager): Response
  80.     {
  81.         $page_view = new PageViews();
  82.         $page_view->setPage($request->get('page'));
  83.         $page_view->setUserIp($request->get('remote_ip'));
  84.         $entityManager->persist($page_view);
  85.         $entityManager->flush();
  86.         $page_count $this->getDoctrine()->getRepository(PageViews::class)->findByPage($request->get('page'));
  87.         return new Response($page_count[0][1]);
  88.     }
  89.     /**
  90.      * @Route("/auction-pdf", name="auction_pdf")
  91.      */
  92.     public function generateAuctionPdf(Request $request)
  93.     {
  94.         $vehicles $this->getDoctrine()->getRepository(AuctionVehicles::class)->findAll();
  95.         $auction $this->getDoctrine()->getRepository(Auctions::class)->findOneBy(['status' => 1]);
  96.         if($vehicles != null) {
  97.             $vehicles_arr = [];
  98.             $host $request->getSchemeAndHttpHost();
  99.             $path $host '/images/vehicles/';
  100.             foreach($vehicles as $vehicle){
  101.                 $vehicles_arr[] = [
  102.                     'id' => $vehicle->getVehicle()->getId(),
  103.                     'year' => $vehicle->getVehicle()->getYear(),
  104.                     'make' => $vehicle->getVehicle()->getMake(),
  105.                     'model' => $vehicle->getVehicle()->getModel(),
  106.                     'reg_no' => $vehicle->getVehicle()->getRegNo(),
  107.                     'mileage' => $vehicle->getVehicle()->getMileage(),
  108.                     'price' => $vehicle->getVehicle()->getVehicleMotusData()->getMgv(),
  109.                     'lot_no' => $vehicle->getVehicle()->getVehicleMotusData()->getLotNo(),
  110.                     'image' => $this->display($path $vehicle->getVehicle()->getVehicleImages()->first()->getThumbName()),
  111.                 ];
  112.             }
  113.             $css file_get_contents(__DIR__ '/../../public/css/bootstrap3.min.css');
  114.             $image $this->display($host '/images/img-5.jpeg');
  115.             // Configure Dompdf according to your needs
  116.             $pdfOptions = new Options();
  117.             $pdfOptions->set('isRemoteEnabled'true);
  118.             $pdfOptions->set('defaultFont''Arial');
  119.             // Instantiate Dompdf with our options
  120.             $dompdf = new Dompdf($pdfOptions);
  121.             $dompdf->setBasePath("/www/public/css/");
  122.             $html $this->renderView('frontend/auctions-pdf.html.twig', [
  123.                 'title' => "Welcome to our PDF Test",
  124.                 'auction' => $auction,
  125.                 'vehicles' => $vehicles_arr,
  126.                 'css' => $css,
  127.                 'image' => $image,
  128.                 'auction_date' => date('D d M Y'strtotime('next monday'))
  129.             ]);
  130.             // Load HTML to Dompdf
  131.             $dompdf->loadHtml($html);
  132.             // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
  133.             $dompdf->setPaper('A4''portrait');
  134.             // Render the HTML as PDF
  135.             $dompdf->render();
  136.             // Output the generated PDF to Browser (force download)
  137.             $dompdf->stream('maa-auction-#' $auction->getId() . '.pdf', [
  138.                 "Attachment" => true
  139.             ]);
  140.             return new Response(''200, [
  141.                 'Content-Type' => 'application/pdf',
  142.             ]);
  143.         }
  144.         return new Response('No vehicles found.');
  145.     }
  146.     /**
  147.      * @Route("/vehicle-pdf/{year}/{make}/{model}/{id}", name="vehicle_pdf")
  148.      */
  149.     public function generateVehiclePdf(Request $request)
  150.     {
  151.         $auction $this->getDoctrine()->getRepository(Auctions::class)->findOneBy(['status' => 2]);
  152.         $vehicle $this->getDoctrine()->getRepository(Vehicle::class)->findOneBy(['id' => $request->get('id')]);
  153.         $features $this->getDoctrine()->getRepository(VehicleOption::class)
  154.             ->findByCategory($request->get('id'),'Feature');
  155.         $accessories  $this->getDoctrine()->getRepository(VehicleOption::class)
  156.             ->findByCategory($request->get('id'),'Accessory');
  157.         $min $vehicle->getVehicleMotusData()->getMgv() - '25000';
  158.         $max $vehicle->getVehicleMotusData()->getMgv() + '25000';
  159.         $similar_vehicles $this->getDoctrine()->getRepository(AuctionVehicles::class)
  160.             ->findBySimilarPrice($auction->getId(),$min,$max);
  161.         if($vehicle != null) {
  162.             $vehicles_arr = [];
  163.             $host $request->getSchemeAndHttpHost();
  164.             $path $host '/images/vehicles/';
  165.             $css file_get_contents(__DIR__ '/../../public/css/bootstrap3.min.css');
  166.             $image $this->display($path $vehicle->getVehicleImages()->first()->getThumbName());
  167.             $logo $this->display($host '/images/logo-pdf.png');
  168.             $path $host '/images/icons/';
  169.             $icons = [];
  170.             for($i=1;$i<=6;$i++){
  171.                 $icons[$i] = $this->display($path 'icon-'$i .'.png');
  172.             }
  173.             // Configure Dompdf according to your needs
  174.             $pdfOptions = new Options();
  175.             $pdfOptions->set('isRemoteEnabled'true);
  176.             $pdfOptions->set('defaultFont''Arial');
  177.             // Instantiate Dompdf with our options
  178.             $dompdf = new Dompdf($pdfOptions);
  179.             $dompdf->setBasePath("/www/public/css/");
  180.             $html $this->renderView('frontend/vehicle-pdf.html.twig', [
  181.                 'vehicle' => $vehicle,
  182.                 'image' => $image,
  183.                 'auction' => $auction,
  184.                 'similar_vehicle' => $similar_vehicles,
  185.                 'features' => $features,
  186.                 'accessories' => $accessories,
  187.                 'css' => $css,
  188.                 'logo' => $logo,
  189.                 'icons' => $icons,
  190.                 'model' => substr($vehicle->getModel(),0,8),
  191.             ]);
  192.             // Load HTML to Dompdf
  193.             $dompdf->loadHtml($html);
  194.             // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
  195.             $dompdf->setPaper('A4''portrait');
  196.             // Render the HTML as PDF
  197.             $dompdf->render();
  198.             // Output the generated PDF to Browser (force download)
  199.             $dompdf->stream('maa-vehicle-#' $vehicle->getId() . '.pdf', [
  200.                 "Attachment" => true
  201.             ]);
  202.             return new Response(''200, [
  203.                 'Content-Type' => 'application/pdf',
  204.             ]);
  205.         }
  206.         return new Response('No vehicles found.');
  207.     }
  208.     public static function display($path)
  209.     {
  210.         $image base64_encode(file_get_contents($path));
  211.         return "data:image/png;base64,$image";
  212.     }
  213. }