src/DcSiteBundle/Controller/Nissan/CarController.php line 223

Open in your IDE?
  1. <?php
  2. namespace DcSiteBundle\Controller\Nissan;
  3. use CoreBundle\Component\CoreFormFactory;
  4. use CoreBundle\Component\FormManager;
  5. use CoreBundle\Entity\Forms;
  6. use CoreBundle\Entity\Vehicles\Category;
  7. use CoreBundle\Entity\Vehicles\Faq;
  8. use CoreBundle\Entity\Vehicles\Vehicle;
  9. use CoreBundle\Factory\Vehicle as VehicleFactory;
  10. use CoreBundle\Model\Api\OnlineService\ApiServer1C;
  11. use CoreBundle\Model\Vehicles\Repository;
  12. use CoreBundle\Services\MediaExtensionVidi;
  13. use DateTime;
  14. use DcSiteBundle\Enum\DealerPathEnum;
  15. use DcSiteBundle\Model\CreditModel;
  16. use DcSiteBundle\Services\CarInStockService;
  17. use DcSiteBundle\Services\HistoryManager;
  18. use DcSiteBundle\Services\ModelRange\Pages\PagesService;
  19. use DcSiteBundle\Services\UsedVehicleService;
  20. use DcSiteBundle\Services\VehicleService;
  21. use DcSiteBundle\Services\VehicleViewService;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use InsuranceBundle\Model\Program;
  24. use PortalBundle\Model\SeoMetaTag;
  25. use Symfony\Component\Filesystem\Filesystem;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\RequestStack;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  30. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  31. use Symfony\Component\Routing\RouterInterface;
  32. use Twig\Environment;
  33. class CarController extends BaseController
  34. {
  35.     private CarInStockService $carInStockService;
  36.     private UsedVehicleService $usedVehicleService;
  37.     public function __construct(CarInStockService $carInStockServiceUsedVehicleService $usedVehicleServiceCoreFormFactory $coreFormFactorySeoMetaTag $seoMetaTagRequestStack $requestStackRouterInterface $routerFormManager $formManagerEntityManagerInterface $emApiServer1C $apiServer1CSessionInterface $sessionFilesystem $filesystemMediaExtensionVidi $mediaExtensionVidiRepository $vehicleRepositoryVehicleFactory $vehicleFactoryEnvironment $twig)
  38.     {
  39.         $this->carInStockService $carInStockService;
  40.         $this->usedVehicleService $usedVehicleService;
  41.         parent::__construct($coreFormFactory$seoMetaTag$requestStack$router$formManager$em$apiServer1C$session$filesystem$mediaExtensionVidi$vehicleRepository$vehicleFactory$twig);
  42.     }
  43.     public function car(Request $requestVehicleService $vehicleServiceHistoryManager $historyManager$category$car)
  44.     {
  45.         $Dealer $this->getDealer();
  46.         $vehicle $this->vehicleFactory->createByUrl($car$Dealer);
  47.         if (!$vehicle || $vehicle->getCategory()->getUrl() !== $category) {
  48.             throw new NotFoundHttpException();
  49.         }
  50.         $VehicleEm $vehicle->getVehicleEntity();
  51.         $vehicleColor $vehicleService->getVehicleColor($VehicleEm);
  52.         $carInStock $this->carInStockService->getCarInStockByVehicle($VehicleEm->getId(), $request->getLocale(), 4);
  53.         $vehicleFaqs $vehicleService->getVehicleFaqs($VehicleEm);
  54.         $historyManager->setUserCarHistory($this->getUser(), $vehicle->getVehicleEntity());
  55.         $TdForm = new Forms();
  56.         $TdForm->setVehicle($vehicle->getVehicleEntity());
  57.         $faqs $this->em
  58.             ->getRepository(Faq::class)
  59.             ->getByVehicle($vehicle->getVehicleEntity());
  60.         $form $this->CoreFormFactory()
  61.             ->callMeForm(new Forms(), true);
  62.         $form->get('dealer')->setData($this->getDealer());
  63.         return $this->baseNissanRender('@DcSite/Nissan/Car/car-card.html.twig', [
  64.             'model' => $vehicle,
  65.             'vehicleColor' => $vehicleColor,
  66.             'carInStock' => $carInStock,
  67.             'faqs' => $vehicleFaqs,
  68.             'buyForm' => $this->CoreFormFactory()->buyForm(null$vehicle->getVehicleEntity(), nullnullfalsefalse$vehicle->getPrice())->createView(),
  69.             'buyInStockForm' => $this->CoreFormFactory()->buyInStockForm()->createView(),
  70.             'buyPartsForm' => $this->CoreFormFactory()->buyPartsForm($this->getDealer())->createView(),
  71.             'testDrivePageForm' => $this->CoreFormFactory()->testDriveForm($this->getDealer(), $TdForm)->createView(),
  72.             'structuredData' => $this->getCarPageStructuredData($request$vehicle),
  73.             'callMeForm' => $form->createView()
  74.         ]);
  75.     }
  76.     public function carConfiguraion(VehicleService $vehicleService$car): ?Response
  77.     {
  78.         $Dealer $this->getDealer();
  79.         $Vehicle $this->vehicleFactory->createByUrl($car$Dealer);
  80.         if (!$Vehicle) {
  81.             throw new NotFoundHttpException();
  82.         }
  83.         $VehicleEm $Vehicle->getVehicleEntity();
  84.         $vehiclePrice $vehicleService->getVehiclePrice($VehicleEm);
  85.         $vehicleEquipment $vehicleService->getVehicleEquipment($VehicleEm);
  86.         return $this->baseNissanRender('@DcSite/Nissan/Car/configuration.html.twig', [
  87.             'model' => $Vehicle,
  88.             'vehiclePrice' => $vehiclePrice,
  89.             'vehicleEquipment' => $vehicleEquipment,
  90.         ]);
  91.     }
  92.     public function catalog(Request $requestPagesService $pagesService): ?Response
  93.     {
  94.         $vehicles $this->vehicleRepository->getNewByDealer($this->getDealer(), [], true);
  95.         $result = [];
  96.         /** @var Vehicle $vehicle */
  97.         foreach ($vehicles as $vehicle) {
  98.             $vModel $this->vehicleFactory->createByEntity($vehicle);
  99.             if (!$vModel) {
  100.                 continue;
  101.             }
  102.             $categoryId $vModel->getCategory()->getId();
  103.             if (!isset($result[$categoryId])) {
  104.                 $result[$categoryId] = [
  105.                     'id' => $categoryId,
  106.                     'position' => $vehicle->getCategory()->getPosition(),
  107.                     'title' => $vehicle->getCategory()->getTitle($request->getLocale()),
  108.                     'url' => $vehicle->getCategory()->getUrl(),
  109.                     'cars' => [],
  110.                 ];
  111.             }
  112.             $result[$categoryId]['cars'][] = $vModel;
  113.         }
  114.         return $this->baseNissanRender('@DcSite/Nissan/Car/catalog.html.twig', [
  115.             'data' => $result,
  116.             'catalogModelRangePages' => $pagesService->setLang($request->getLocale())->getPages($this->getDealer())
  117.         ]);
  118.     }
  119.     public function catalogPage(Request $requestPagesService $pagesServicestring $url): ?Response
  120.     {
  121.         $page $pagesService->setLang($request->getLocale())->getPage($this->getDealer(), $url);
  122.         if (empty($page)) {
  123.             throw new NotFoundHttpException();
  124.         }
  125.         return $this->baseNissanRender('@DcSite/Nissan/Car/catalog-page.html.twig', ['page' => $page]);
  126.     }
  127.     public function category(Request $request): ?Response
  128.     {
  129.         $categoryUrl $request->get('category');
  130.         $Category $this->em->getRepository(Category::class)->findOneBy(['url' => $categoryUrl]);
  131.         if (!$Category) {
  132.             throw new NotFoundHttpException();
  133.         }
  134.         $vehicles $this->vehicleRepository->getByCategory($Category$this->getDealer());
  135.         $result = [];
  136.         foreach ($vehicles as $vehicle) {
  137.             $vehicleItem $this->vehicleFactory->createByEntity($vehicle);
  138.             if ($vehicleItem) {
  139.                 $result[] = $vehicleItem;
  140.             }
  141.         }
  142.         if (!$result) {
  143.             throw new NotFoundHttpException();
  144.         }
  145.         uasort($result, fn($a$b) => $a->getPosition() <=> $b->getPosition());
  146.         return $this->baseNissanRender('@DcSite/Nissan/Car/category.html.twig', [
  147.             'category' => $Category,
  148.             'cars' => $result
  149.         ]);
  150.     }
  151.     public function configurator(Request $request$url): ?Response
  152.     {
  153.         $Vehicle false;
  154.         if ($url != 'start') {
  155.             $Vehicle $this->vehicleFactory->createByUrl($url$this->getDealer());
  156.             if (!$Vehicle) {
  157.                 throw new NotFoundHttpException();
  158.             }
  159.         }
  160.         $hash $request->query->get('hash');
  161.         return $this->baseNissanRender('@DcSite/Nissan/Configurator/index.html.twig', [
  162.             'carId' => $Vehicle $Vehicle->getVehicleId() : 0,
  163.             'baseType' => Program::BASE_COEFFICIENT_TYPE,
  164.             'listType' => Program::LIST_COEFFICIENT_TYPE,
  165.             'optionType' => Program::OPTION_COEFFICIENT_TYPE,
  166.             'hash' => $hash,
  167.         ]);
  168.     }
  169.     public function testDrive(): ?Response
  170.     {
  171.         return $this->baseNissanRender('@DcSite/Nissan/Car/test-drive.html.twig', [
  172.             'testDrivePageForm' => $this->CoreFormFactory()->testDriveForm($this->getDealer(), null, [
  173.                 'child' => false,
  174.                 'widthTime' => true
  175.             ])->createView(),
  176.         ]);
  177.     }
  178.     public function leaf(): ?Response
  179.     {
  180.         return $this->baseNissanRender('@DcSite/Nissan/Car/leaf.html.twig');
  181.     }
  182.     public function archive(): ?Response
  183.     {
  184.         $vehicles $this->vehicleRepository->getDisabled($this->getDealer());
  185.         $result = [];
  186.         foreach ($vehicles as $vehicle) {
  187.             $model $this->vehicleFactory->createByEntity($vehicle);
  188.             if (!$model) {
  189.                 continue;
  190.             }
  191.             $result[] = $model;
  192.         }
  193.         return $this->baseNissanRender('@DcSite/Nissan/Car/archive.html.twig', ['cars' => $result]);
  194.     }
  195.     public function usedCarCatalog(Request $request)
  196.     {
  197.         $vehicles $this->usedVehicleService->getUsedVehicles(
  198.             $this->getDealer(),
  199.             DealerPathEnum::NISSAN,
  200.             $request->getLocale()
  201.         );
  202.         return $this->baseNissanRender('@DcSite/Nissan/Car/used-car-catalog.html.twig', ['vehicles' => $vehicles]);
  203.     }
  204.     public function secondhandSingle(Request $requestVehicleViewService $vehicleViewServiceCreditModel $creditModel$url): Response
  205.     {
  206.         if (!$url) {
  207.             throw new NotFoundHttpException();
  208.         }
  209.         $vehicle $this->vehicleFactory->createUsedByUrl($url);
  210.         if (!$vehicle) {
  211.             throw new NotFoundHttpException();
  212.         }
  213.         $vehicleViewService->saveViewVehicle($vehicle$request->getClientIp(), new DateTime());
  214.         $viewedCars = [];
  215.         if (isset($_COOKIE['viewedCars'])) {
  216.             $viewedCars json_decode($_COOKIE['viewedCars']);
  217.             if (!in_array($vehicle->getVehicleId(), $viewedCars)) {
  218.                 $viewedCars[] = $vehicle->getVehicleId();
  219.             }
  220.         } else {
  221.             setcookie('viewedCars'json_encode([$vehicle->getVehicleId()]));
  222.         }
  223.         $viewedCarEntities $this->em
  224.             ->getRepository(Vehicle::class)
  225.             ->findBy(['id' => array_slice(array_reverse(array_diff($viewedCars, [$vehicle->getVehicleId()])), 03)]);
  226.         $viewedCarFactories = [];
  227.         foreach ($viewedCarEntities as $viewedCarEntity) {
  228.             $viewedCarFactory $this->vehicleFactory->createByEntity($viewedCarEntity);
  229.             $galleryItems $viewedCarFactory->getGallery()
  230.                 ? $viewedCarFactory->getGallery()->getGalleryItems()
  231.                 : null;
  232.             $image $galleryItems && $galleryItems->count()
  233.                 ? $galleryItems->first()->getMedia()
  234.                 : $viewedCarFactory->getPreview();
  235.             $viewedCarFactories[] = [
  236.                 'vehicle' => $viewedCarFactory,
  237.                 'image' => $this->mediaExtensionVidi->getPath($image'reference'),
  238.                 'link' => $this->router->generate('nissan_used_car_single', ['url' => $viewedCarFactory->getUrl()]),
  239.             ];
  240.         }
  241.         $gallery $vehicle->getGallery();
  242.         $gallery_images = [];
  243.         if ($gallery && $gallery->getGalleryItems()) {
  244.             foreach ($gallery->getGalleryItems() as $galleryItem) {
  245.                 $gallery_images[] = $this->mediaExtensionVidi->getPath($galleryItem->getMedia(), 'reference');
  246.             }
  247.         }
  248.         $similarVehicles = [];
  249.         $similar $this->vehicleRepository->getSimilarVehicles($vehicletrue$this->getDealer());
  250.         foreach ($similar as $vehicleItem) {
  251.             $vehicleItemGallery $this->vehicleFactory->createByEntity($vehicleItem->getVariation()->getVehicle())->getGallery();
  252.             $vehicleItemGalleryImages = [];
  253.             if ($vehicleItemGallery && $vehicleItemGallery->getGalleryItems()) {
  254.                 foreach ($vehicleItemGallery->getGalleryItems() as $vehicleItemGallery_item) {
  255.                     $vehicleItemGalleryImages[] = $this->mediaExtensionVidi->getPath($vehicleItemGallery_item->getMedia(), 'reference');
  256.                 }
  257.             }
  258.             $simVehicle $this->vehicleFactory->createByEntity($vehicleItem->getVariation()->getVehicle());
  259.             $similarVehicles[] = [
  260.                 'vehicle' => $simVehicle,
  261.                 'creditPayment' => $creditModel->getMinPayment($this->vehicleFactory->createByEntity($vehicleItem->getVariation()->getVehicle())),
  262.                 'link' => $this->router->generate('nissan_used_car_single', ['url' => $simVehicle->getUrl()]),
  263.                 'gallery' => $vehicleItemGalleryImages
  264.             ];
  265.         }
  266.         $options $vehicle->getOptionsArray($request->getLocale());
  267.         return $this->baseNissanRender('@DcSite/Nissan/Car/used-car-single.html.twig', [
  268.             'secondhand' => $vehicle,
  269.             'options' => $options,
  270.             'buyForm' => $this->CoreFormFactory()->buyForm(null$vehicle->getVehicleEntity(), nullnulltruefalse$vehicle->getPrice())->createView(),
  271.             'consultForm' => $this->CoreFormFactory()->buyForm(null$vehicle->getVehicleEntity())->createView(),
  272.             'gallary' => $gallery_images,
  273.             'creditPayment' => $creditModel->getMinPayment($vehicle),
  274.             'similarCars' => [],
  275.             'viewedCars' => $viewedCarFactories,
  276.             'structuredCarData' => $this->getCarStructuredDataCarUsed($request$vehicle)
  277.         ]);
  278.     }
  279. }