<?php
namespace DcSiteBundle\Controller\Lexus;
use CoreBundle\Component\CoreFormFactory;
use CoreBundle\Component\FormManager;
use CoreBundle\Entity\Forms;
use CoreBundle\Entity\Vehicles\Category;
use CoreBundle\Entity\Vehicles\Vehicle;
use CoreBundle\Factory\InStockVehicle;
use CoreBundle\Factory\Vehicle as VehicleFactory;
use CoreBundle\Model\Api\OnlineService\ApiServer1C;
use CoreBundle\Model\Vehicles\Repository;
use CoreBundle\Model\Vehicles\UsedVehicle;
use CoreBundle\Services\MediaExtensionVidi;
use DateTime;
use DcSiteBundle\Enum\DealerPathEnum;
use DcSiteBundle\Services\CarInStockService;
use DcSiteBundle\Services\CatalogService;
use DcSiteBundle\Services\HistoryManager;
use DcSiteBundle\Services\ModelRange\Pages\PagesService;
use DcSiteBundle\Services\UsedVehicleService;
use DcSiteBundle\Services\VehicleService;
use DcSiteBundle\Services\VehicleViewService;
use Doctrine\ORM\EntityManagerInterface;
use InsuranceBundle\Model\Program;
use PortalBundle\Model\SeoMetaTag;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
use Twig_Error_Loader;
use Twig_Error_Runtime;
use Twig_Error_Syntax;
/**
* Class CarController
* @package DcSiteBundle\Controller\Lexus
*/
class CarController extends BaseController
{
private CarInStockService $carInStockService;
private UsedVehicleService $usedVehicleService;
private CatalogService $catalogService;
public function __construct(CoreFormFactory $coreFormFactory, SeoMetaTag $seoMetaTag, RequestStack $requestStack, RouterInterface $router, FormManager $formManager, EntityManagerInterface $em, ApiServer1C $apiServer1C, SessionInterface $session, Filesystem $filesystem, MediaExtensionVidi $mediaExtensionVidi, Repository $vehicleRepository, VehicleFactory $vehicleFactory, Environment $twig, CarInStockService $carInStockService, UsedVehicleService $usedVehicleService, CatalogService $catalogService)
{
$this->carInStockService = $carInStockService;
$this->usedVehicleService = $usedVehicleService;
$this->catalogService = $catalogService;
parent::__construct($coreFormFactory, $seoMetaTag, $requestStack, $router, $formManager, $em, $apiServer1C, $session, $filesystem, $mediaExtensionVidi, $vehicleRepository, $vehicleFactory, $twig);
}
public function car(Request $request, VehicleService $vehicleService, HistoryManager $historyManager, $url): ?Response
{
$Dealer = $this->getDealer();
$Vehicle = $this->vehicleFactory->createByUrl($url, $Dealer);
if (!$Vehicle) {
throw new NotFoundHttpException();
}
$VehicleEm = $Vehicle->getVehicleEntity();
$vehicleColor = $vehicleService->getVehicleColor($VehicleEm);
$carInStock = $this->carInStockService->getCarInStockByModel($this->getDealer(), $request->getLocale(), $VehicleEm->getModel(), 4);
$vehicleFaqs = $vehicleService->getVehicleFaqs($VehicleEm);
$historyManager->setUserCarHistory($this->getUser(), $Vehicle->getVehicleEntity());
$TdForm = new Forms();
$TdForm->setVehicle($Vehicle->getVehicleEntity());
$headerCars = [];
$vehicles = $this->vehicleRepository->getNewByDealer($this->getDealer());
/** @var Vehicle $vehicle */
foreach ($vehicles as $vehicle) {
if (!$vehicle->getGroup()) {
continue;
}
$carGroup = $vehicle->getGroup();
if (!isset($headerCars[$carGroup->getId()])) {
$headerCars[$carGroup->getId()] = [
'id' => $carGroup->getId(),
'title' => $carGroup->getTitle($request->getLocale()),
'cars' => []
];
}
$headerCars[$carGroup->getId()]['cars'][] = [
'title' => $vehicle->getModel()->getTitle(),
'url' => $vehicle->getUrl(),
'id' => $vehicle->getId(),
];
}
$accessoryUrl = explode(' ', $Vehicle->getGroup()->getTitle($request->getLocale()));
$form = $this->CoreFormFactory()
->callMeForm(new Forms(), true);
$form->get('dealer')->setData($this->getDealer());
return $this->baseLexusRender('@DcSite/Lexus/Car/car-card.html.twig', [
'model' => $Vehicle,
'vehicleColor' => $vehicleColor,
'carInStock' => $carInStock,
'faqs' => $vehicleFaqs,
'headerCars' => $headerCars,
'buyForm' => $this->CoreFormFactory()->buyForm(null, $Vehicle->getVehicleEntity(), null, null, false, false, $Vehicle->getPrice())->createView(),
'buyPartsForm' => $this->CoreFormFactory()->buyPartsForm($this->getDealer())->createView(),
'testDrivePageForm' => $this->CoreFormFactory()->testDriveForm($this->getDealer(), $TdForm)->createView(),
'structuredData' => $this->getCarPageStructuredData($request, $Vehicle),
'accessoryUrl' => $accessoryUrl['0'],
'callMeForm' => $form->createView()
]);
}
public function carConfiguraion(VehicleService $vehicleService, $url): ?Response
{
$Dealer = $this->getDealer();
$Vehicle = $this->vehicleFactory->createByUrl($url, $Dealer);
if (!$Vehicle) {
throw new NotFoundHttpException();
}
$VehicleEm = $Vehicle->getVehicleEntity();
$vehiclePrice = $vehicleService->getVehiclePrice($VehicleEm);
$vehicleEquipment = $vehicleService->getVehicleEquipment($VehicleEm);
return $this->baseLexusRender('@DcSite/Lexus/Car/configuration.html.twig', [
'model' => $Vehicle,
'vehiclePrice' => $vehiclePrice,
'vehicleEquipment' => $vehicleEquipment,
]);
}
public function loadCarByCategory(Request $request): JsonResponse
{
$id = $request->get('id');
$vehicles = $this->vehicleRepository->findByGroup($id, $this->getDealer());
$result = [];
/** @var Vehicle $vehicle */
foreach ($vehicles as $vehicle) {
$carModel = $this->vehicleFactory->createByEntity($vehicle);
if (!$carModel) {
continue;
}
$result[] = [
'id' => $carModel->getVehicleId(),
'title' => $carModel->getModelName(),
'price' => $carModel->price(),
'img' => $this->mediaExtensionVidi->getPath($carModel->getPreview(), 'small'),
];
}
return $this->json(['data' => $result]);
}
public function testDrive(): ?Response
{
return $this->baseLexusRender('@DcSite/Lexus/Car/test-drive.html.twig', [
'testDrivePageForm' => $this->CoreFormFactory()->testDriveForm($this->getDealer(), null, [
'child' => false,
'widthTime' => true,
'communication' => true,
])->createView(),
]);
}
public function carCatalog(Request $request, PagesService $pagesService, string $url = null): ?Response
{
if ($url) {
$vehicleCategory = $this->em->getRepository(Category::class)->findOneBy(['url' => $url]);
if (!$vehicleCategory) {
throw new NotFoundHttpException();
}
}
$categories = $this->catalogService->getCategoryVehicles($this->getDealer());
$catalogVehicleGroup = $this->catalogService->getCatalogVehicleGroupByCategory($this->getDealer(), $url);
$searchKeyCategory = array_search($url, array_column($categories, 'url'));
$categoryNow = ($searchKeyCategory !== false) ? $categories[$searchKeyCategory] : null;
return $this->baseLexusRender('@DcSite/Lexus/Car/car-catalog.html.twig', [
'buyForm' => $this->CoreFormFactory()->buyForm(null, null, null, $this->getDealer())->createView(),
'category' => $categoryNow,
'categories' => $categories,
'vehicles' => $catalogVehicleGroup,
'catalogModelRangePages' => $pagesService->setLang($request->getLocale())->getPages($this->getDealer())
]);
}
public function carCatalogPages(Request $request, PagesService $pagesService, string $url): ?Response
{
$page = $pagesService->setLang($request->getLocale())->getPage($this->getDealer(), $url);
if (empty($page)) {
throw new NotFoundHttpException();
}
return $this->baseLexusRender('@DcSite/Lexus/Car/car-catalog-page.html.twig', ['page' => $page]);
}
public function configurator(Request $request): ?Response
{
$url = $request->get('url');
$Vehicle = false;
if ($url != 'start') {
$Vehicle = $this->vehicleFactory->createByUrl($url, $this->getDealer());
if (!$Vehicle) {
throw new NotFoundHttpException();
}
}
$hash = $request->query->get('hash');
return $this->baseLexusRender('@DcSite/Lexus/Configurator/index.html.twig', [
'modelTitle' => $Vehicle ? $Vehicle->getModelName() : 0,
'carId' => $Vehicle ? $Vehicle->getVehicleId() : 0,
'baseType' => Program::BASE_COEFFICIENT_TYPE,
'listType' => Program::LIST_COEFFICIENT_TYPE,
'optionType' => Program::OPTION_COEFFICIENT_TYPE,
'hash' => $hash,
]);
}
public function carStock(Request $request): ?Response
{
$vehicles = $this->carInStockService->getCarInStockByDealer(
$this->getDealer(),
null,
$request->getLocale()
);
$this->carInStockService->sortVehicleByDealer($vehicles, $this->getDealer()->getUniqueId());
return $this->baseLexusRender('@DcSite/Lexus/Car/car-in-stock.html.twig', ['vehicles' => $vehicles]);
}
public function actionsCar(): ?Response
{
$vehicles = $this->vehicleRepository->getActionVehicleItems($this->getDealer());
$result = [];
foreach ($vehicles as $vehicle) {
$model = $this->vehicleFactory->createByVehicleItem($vehicle);
if (!$model) {
continue;
}
if (!isset($result[$model->getVehicleId()]) || $result[$model->getVehicleId()]->price() > $model->price()) {
$result[$model->getVehicleId()] = $model;
}
}
return $this->baseLexusRender('@DcSite/Lexus/Car/actions-car.html.twig', [
'vehicles' => $result,
]);
}
public function carStockSingle(Request $request, InStockVehicle $inStockVehicle, $url)
{
$car = $inStockVehicle->createByUrl($url, $this->getDealer());
if (!$car) {
$car = $inStockVehicle->createByUrl($url, $this->getDealer(), false);
if ($car) {
return $this->redirectToRoute('lexus_card_car', ['url' => $car->getVehicle()->getUrl()], 301);
} else {
return $this->redirectToRoute('lexus_car_stock', [], 301);
}
}
$options = $car->getOptions($request->getLocale());
$form = $this->CoreFormFactory()
->callMeForm(new Forms(), true);
$form->get('dealer')->setData($this->getDealer());
return $this->baseLexusRender('@DcSite/Lexus/Car/car-in-stock-single.html.twig', [
'model' => $car,
'buyInStockForm' => $this->CoreFormFactory()->buyInStockForm(null, $car->getInStockEntity())->createView(),
'options' => $options,
'structuredCarData' => $this->getCarStructuredDataCarInStock($request, $car),
'callMeForm' => $form->createView(),
]);
}
public function usedCarCatalog(Request $request): Response
{
$vehicles = $this->usedVehicleService->getUsedVehicles(
$this->getDealer(),
DealerPathEnum::Lexus,
$request->getLocale()
);
return $this->baseLexusRender('@DcSite/Lexus/Car/used-car-catalog.html.twig', ['vehicles' => $vehicles]);
}
public function secondhandSingle(Request $request, VehicleViewService $vehicleViewService, $url): Response
{
if (!$url) {
throw new NotFoundHttpException();
}
$vehicle = $this->usedVehicleService->getUsedVehicle($this->getDealer(), $this->getDealer()->getBrand(), $url);
if (!$vehicle) {
throw new NotFoundHttpException();
}
$vehicleViewService->saveViewVehicle($vehicle, $request->getClientIp(), new DateTime());
$options = $vehicle->getOptionsArray($request->getLocale());
return $this->baseLexusRender('@DcSite/Lexus/Car/used-car-single.html.twig', [
'secondhand' => $vehicle,
'options' => $options,
'buyForm' => $this->CoreFormFactory()->buyForm(null, $vehicle->getVehicleEntity(), null, null, true, false, $vehicle->getPrice())->createView(),
'structuredCarData' => $this->getCarStructuredDataCarUsed($request, $vehicle)
]);
}
public function archive(): ?Response
{
$vehicles = $this->vehicleRepository->getDisabled($this->getDealer());
$result = [];
foreach ($vehicles as $vehicle) {
$model = $this->vehicleFactory->createByEntity($vehicle);
if (!$model) {
continue;
}
$result[] = $model;
}
return $this->baseLexusRender('@DcSite/Lexus/Car/archive.html.twig', ['cars' => $result]);
}
public function usedCars(Request $request): JsonResponse
{
$vehicles = $this->vehicleRepository->getUsedByDealer($this->getDealer());
$usedCars = [];
/** @var UsedVehicle $limitedCar */
foreach ($vehicles as $vehicle) {
$vehicle = $this->vehicleFactory->createByEntity($vehicle);
if (!$vehicle || $vehicle->getModel()->getId() != $request->request->get('modelId') || $vehicle->hasReserved()) {
continue;
}
$galleryItems = $vehicle->getGallery() ? $vehicle->getGallery()->getGalleryItems() : null;
$image = $galleryItems && $galleryItems->count() ? $galleryItems->first()->getMedia() : $vehicle->getPreview();
$usedCars[] = [
"id" => $vehicle->getVehicleId(),
"name" => $vehicle->getModelName(),
"price" => $vehicle->price(),
"year" => $vehicle->getYear(),
'mileage' => $vehicle->getMileage(),
"url" => $this->router->generate('lexus_used_car_single', ['url' => $vehicle->getUrl()]),
"image" => $this->mediaExtensionVidi->getPath($image, 'reference'),
"body" => $vehicle->getBodyTypeName($request->getLocale()),
"engine" => $vehicle->getFuelTypeName($request->getLocale()),
"transmission" => $vehicle->getTransmissionTypeName($request->getLocale()),
"drive" => $vehicle->getDriveUnitTypeName($request->getLocale()),
"reserved" => $vehicle->hasReserved(),
];
}
return new JsonResponse(['success' => true, 'usedCars' => $usedCars]);
}
}