<?php
namespace App\Controller;
use App\Entity\Auctions;
use App\Entity\AuctionVehicles;
use App\Entity\MetaData;
use App\Entity\PageViews;
use App\Entity\Vehicle;
use App\Entity\VehicleOption;
use App\Services\PaginationManager;
use Doctrine\ORM\EntityManagerInterface;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
class AuctionsController extends AbstractController
{
const ITEMS_PER_PAGE = 12;
private $session;
private $params;
public function __construct(PaginationManager $pageManager, ParameterBagInterface $params) {
$this->pageManager = $pageManager;
$this->session = new Session();
//$this->session->start();
$this->params = $params;
}
/**
* @Route("/auctions", name="auctions")
*/
public function index(Request $request): Response
{
$repo_meta = $this->getDoctrine()->getRepository(MetaData::class)
->findOneBy(['id' => 4]);
$auction_vehicles = $this->getDoctrine()->getRepository(AuctionVehicles::class)
->findByAuction();
$auction = $this->getDoctrine()->getRepository(Auctions::class)
->findOneBy(['status' => 1]);
$results = $this->pageManager->paginate($auction_vehicles, $request, self::ITEMS_PER_PAGE);
return $this->render('frontend/auctions.html.twig',
[
'meta' => $repo_meta,
'auctions' => $auction,
'auction_vehicles' => $results,
'lastPage' => $this->pageManager->lastPage($results),
]
);
}
/**
* @Route("/auction-vehicle/{year}/{make}/{model}/{id}", name="vehicle")
*/
public function getVehicleAction(Request $request): Response
{
$auction = $this->getDoctrine()->getRepository(Auctions::class)->findOneBy(['status' => 2]);
$vehicle = $this->getDoctrine()->getRepository(Vehicle::class)->findOneBy(['id' => $request->get('id')]);
$features = $this->getDoctrine()->getRepository(VehicleOption::class)
->findByCategory($request->get('id'),'Feature');
$accessories = $this->getDoctrine()->getRepository(VehicleOption::class)
->findByCategory($request->get('id'),'Accessory');
$min = $vehicle->getVehicleMotusData()->getMgv() - '25000';
$max = $vehicle->getVehicleMotusData()->getMgv() + '25000';
$similar_vehicles = $this->getDoctrine()->getRepository(AuctionVehicles::class)
->findBySimilarPrice($auction->getId(),$min,$max);
return $this->render('frontend/vehicle.html.twig',
[
'vehicle' => $vehicle,
'auction' => $auction,
'similar_vehicle' => $similar_vehicles,
'features' => $features,
'accessories' => $accessories
]
);
}
/**
* @Route("/page-views", name="page_views")
*/
public function pageViewsAction(Request $request, EntityManagerInterface $entityManager): Response
{
$page_view = new PageViews();
$page_view->setPage($request->get('page'));
$page_view->setUserIp($request->get('remote_ip'));
$entityManager->persist($page_view);
$entityManager->flush();
$page_count = $this->getDoctrine()->getRepository(PageViews::class)->findByPage($request->get('page'));
return new Response($page_count[0][1]);
}
/**
* @Route("/auction-pdf", name="auction_pdf")
*/
public function generateAuctionPdf(Request $request)
{
$vehicles = $this->getDoctrine()->getRepository(AuctionVehicles::class)->findAll();
$auction = $this->getDoctrine()->getRepository(Auctions::class)->findOneBy(['status' => 1]);
if($vehicles != null) {
$vehicles_arr = [];
$host = $request->getSchemeAndHttpHost();
$path = $host . '/images/vehicles/';
foreach($vehicles as $vehicle){
$vehicles_arr[] = [
'id' => $vehicle->getVehicle()->getId(),
'year' => $vehicle->getVehicle()->getYear(),
'make' => $vehicle->getVehicle()->getMake(),
'model' => $vehicle->getVehicle()->getModel(),
'reg_no' => $vehicle->getVehicle()->getRegNo(),
'mileage' => $vehicle->getVehicle()->getMileage(),
'price' => $vehicle->getVehicle()->getVehicleMotusData()->getMgv(),
'lot_no' => $vehicle->getVehicle()->getVehicleMotusData()->getLotNo(),
'image' => $this->display($path . $vehicle->getVehicle()->getVehicleImages()->first()->getThumbName()),
];
}
$css = file_get_contents(__DIR__ . '/../../public/css/bootstrap3.min.css');
$image = $this->display($host . '/images/img-5.jpeg');
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('isRemoteEnabled', true);
$pdfOptions->set('defaultFont', 'Arial');
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
$dompdf->setBasePath("/www/public/css/");
$html = $this->renderView('frontend/auctions-pdf.html.twig', [
'title' => "Welcome to our PDF Test",
'auction' => $auction,
'vehicles' => $vehicles_arr,
'css' => $css,
'image' => $image,
'auction_date' => date('D d M Y', strtotime('next monday'))
]);
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (force download)
$dompdf->stream('maa-auction-#' . $auction->getId() . '.pdf', [
"Attachment" => true
]);
return new Response('', 200, [
'Content-Type' => 'application/pdf',
]);
}
return new Response('No vehicles found.');
}
/**
* @Route("/vehicle-pdf/{year}/{make}/{model}/{id}", name="vehicle_pdf")
*/
public function generateVehiclePdf(Request $request)
{
$auction = $this->getDoctrine()->getRepository(Auctions::class)->findOneBy(['status' => 2]);
$vehicle = $this->getDoctrine()->getRepository(Vehicle::class)->findOneBy(['id' => $request->get('id')]);
$features = $this->getDoctrine()->getRepository(VehicleOption::class)
->findByCategory($request->get('id'),'Feature');
$accessories = $this->getDoctrine()->getRepository(VehicleOption::class)
->findByCategory($request->get('id'),'Accessory');
$min = $vehicle->getVehicleMotusData()->getMgv() - '25000';
$max = $vehicle->getVehicleMotusData()->getMgv() + '25000';
$similar_vehicles = $this->getDoctrine()->getRepository(AuctionVehicles::class)
->findBySimilarPrice($auction->getId(),$min,$max);
if($vehicle != null) {
$vehicles_arr = [];
$host = $request->getSchemeAndHttpHost();
$path = $host . '/images/vehicles/';
$css = file_get_contents(__DIR__ . '/../../public/css/bootstrap3.min.css');
$image = $this->display($path . $vehicle->getVehicleImages()->first()->getThumbName());
$logo = $this->display($host . '/images/logo-pdf.png');
$path = $host . '/images/icons/';
$icons = [];
for($i=1;$i<=6;$i++){
$icons[$i] = $this->display($path . 'icon-'. $i .'.png');
}
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('isRemoteEnabled', true);
$pdfOptions->set('defaultFont', 'Arial');
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
$dompdf->setBasePath("/www/public/css/");
$html = $this->renderView('frontend/vehicle-pdf.html.twig', [
'vehicle' => $vehicle,
'image' => $image,
'auction' => $auction,
'similar_vehicle' => $similar_vehicles,
'features' => $features,
'accessories' => $accessories,
'css' => $css,
'logo' => $logo,
'icons' => $icons,
'model' => substr($vehicle->getModel(),0,8),
]);
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (force download)
$dompdf->stream('maa-vehicle-#' . $vehicle->getId() . '.pdf', [
"Attachment" => true
]);
return new Response('', 200, [
'Content-Type' => 'application/pdf',
]);
}
return new Response('No vehicles found.');
}
public static function display($path)
{
$image = base64_encode(file_get_contents($path));
return "data:image/png;base64,$image";
}
}