src/AutomarketBundle/Controller/CatalogController.php line 98

Open in your IDE?
  1. <?php
  2. namespace AutomarketBundle\Controller;
  3. use AutomarketBundle\Services\CatalogService;
  4. use AutomarketBundle\Services\SeoService;
  5. use CoreBundle\Component\CoreFormFactory;
  6. use CoreBundle\Model\Vehicles\VehicleType;
  7. use DcSiteBundle\Model\Pagination;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use PortalBundle\Model\SeoMetaTag;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. class CatalogController extends BaseController
  16. {
  17.     private CatalogService $catalogService;
  18.     private SeoService $seoService;
  19.     private Pagination $modelPagination;
  20.     public function __construct(CoreFormFactory $coreFormFactoryRequestStack $requestStackEntityManagerInterface $em,
  21.                                 SeoMetaTag      $seoMetaTagCatalogService $catalogServiceSeoService $seoService,
  22.                                 Pagination      $modelPagination)
  23.     {
  24.         parent::__construct($coreFormFactory$requestStack$em$seoMetaTag);
  25.         $this->catalogService $catalogService;
  26.         $this->seoService $seoService;
  27.         $this->modelPagination $modelPagination;
  28.     }
  29.     const PRE_PAGES 18;
  30.     const MAX_PAGINATION_BUTTON 5;
  31.     const TYPE_FILTER_BY_MODEL_UNIQUE = ['BT''FT''DT''TT''BC''SC'];
  32.     public function catalog(Request $request): Response
  33.     {
  34.         $lang $request->getLocale();
  35.         $routeParams $this->catalogService->getRouteFilterParams($this->getDealer());
  36.         $vehicleTypeData VehicleType::getTypeDataByUrl($request->get('type'));
  37.         if (!$vehicleTypeData) {
  38.             throw new NotFoundHttpException();
  39.         }
  40.         $findVehicles $this->catalogService->findVehicleByParams(self::PRE_PAGES);
  41.         if (!$findVehicles['data']) {
  42.             $findVehicles $this->catalogService->noFindVehicleByParams(self::PRE_PAGES);
  43.         }
  44.         $vehicles $this->catalogService->getVehicleByCatalog($this->getDealer(), $findVehicles['data'], $this->getUser(), $lang);
  45.         $vehicleIds = [];
  46.         foreach ($vehicles as $vehicle) {
  47.             $vehicleIds[] = $vehicle['id'];
  48.         }
  49.         $pagination $this->modelPagination->initPagination($findVehicles['count'], self::PRE_PAGESself::MAX_PAGINATION_BUTTON);
  50.         if (isset($routeParams['characteristic'])
  51.             && count($routeParams['characteristic']) == 1
  52.             && isset($routeParams['characteristic']['body'])) {
  53.             $seoLinks $this->seoService->buildSeoLinksByBodyType($vehicleIds);
  54.         } else {
  55.             $seoLinks $this->seoService->buildSeoLinks();
  56.         }
  57.         $modelRangePage $this->seoService->findModelRangePage();
  58.         $breadcrumbs $this->catalogService->getBreadcrumbs();
  59.         return $this->baseAutomarketRender('@Automarket/Catalog/catalog.html.twig', [
  60.             'vehicles' => $vehicles,
  61.             'seoLinks' => $seoLinks,
  62.             'modelRangePage' => $modelRangePage,
  63.             'countVehicle' => $findVehicles['count'],
  64.             'dataPriceVehicle' => $findVehicles['dataPrice'],
  65.             'routeParams' => $routeParams,
  66.             'pagination' => $pagination,
  67.             'breadcrumbs' => $breadcrumbs,
  68.             'pageReload' => true,
  69.         ]);
  70.     }
  71.     public function catalogSearch(Request $request): Response
  72.     {
  73.         $routeParams $request->query->all();
  74.         return $this->baseAutomarketRender('@Automarket/Catalog/catalog.html.twig', [
  75.             'routeParams' => $routeParams,
  76.             'pageReload' => false,
  77.         ]);
  78.     }
  79.     public function initFilter(Request $request): JsonResponse
  80.     {
  81.         $lang $request->getLocale();
  82.         $filter $this->catalogService->buildFilter(self::TYPE_FILTER_BY_MODEL_UNIQUE);
  83.         $findVehicles $this->catalogService->findVehicleByParams(self::PRE_PAGES);
  84.         if (!$findVehicles['data']) {
  85.             $findVehicles $this->catalogService->noFindVehicleByParams(self::PRE_PAGES);
  86.         }
  87.         $vehicle $this->catalogService->getVehicleByCatalog($this->getDealer(), $findVehicles['data'], $this->getUser(), $lang);
  88.         $pagination $this->modelPagination->initPagination($findVehicles['count'], self::PRE_PAGES5);
  89.         $showMore $this->catalogService->showMoreCatalog($findVehicles['count'], self::PRE_PAGES);
  90.         $filterHeader $this->catalogService->getFilterHeader();
  91.         return $this->json([
  92.             'filter' => $filter,
  93.             'countVehicle' => $findVehicles['count'],
  94.             'vehicle' => $vehicle,
  95.             'pagination' => $pagination,
  96.             'showMore' => $showMore,
  97.             'filterHeader' => $filterHeader
  98.         ]);
  99.     }
  100.     public function getCountVehicleByFilter(): JsonResponse
  101.     {
  102.         $findVehicles $this->catalogService->findVehicleByParams(self::PRE_PAGES);
  103.         $buildUrl $this->catalogService->buildSearchUrl();
  104.         return $this->json([
  105.             'countVehicle' => $findVehicles['count'],
  106.             'url' => $buildUrl,
  107.         ]);
  108.     }
  109.     public function getVehicleByFilter(Request $request): JsonResponse
  110.     {
  111.         $lang $request->getLocale();
  112.         $findVehicles $this->catalogService->findVehicleByParams(self::PRE_PAGES);
  113.         if (!$findVehicles['data']) {
  114.             $findVehicles $this->catalogService->noFindVehicleByParams(self::PRE_PAGES);
  115.         }
  116.         $vehicle $this->catalogService->getVehicleByCatalog($this->getDealer(), $findVehicles['data'], $this->getUser(), $lang);
  117.         $filterHeader $this->catalogService->getFilterHeader();
  118.         $pagination $this->modelPagination->initPagination($findVehicles['count'], self::PRE_PAGES5);
  119.         $showMore $this->catalogService->showMoreCatalog($findVehicles['count'], self::PRE_PAGES);
  120.         $buildUrl $this->catalogService->buildSearchUrl();
  121.         return $this->json([
  122.             'countVehicle' => $findVehicles['count'],
  123.             'vehicle' => $vehicle,
  124.             'url' => $buildUrl,
  125.             'pagination' => $pagination,
  126.             'showMore' => $showMore,
  127.             'filterHeader' => $filterHeader
  128.         ]);
  129.     }
  130. }