src/PortalBundle/Controller/StockController.php line 55

Open in your IDE?
  1. <?php
  2. namespace PortalBundle\Controller;
  3. use CoreBundle\Component\CoreFormFactory;
  4. use CoreBundle\Entity\Dealer;
  5. use CoreBundle\Entity\Vehicles\InStock;
  6. use CoreBundle\Entity\Vehicles\RecommendGroup;
  7. use CoreBundle\Factory\InStockVehicle;
  8. use CoreBundle\Model\Vehicles\Repository;
  9. use CoreBundle\Model\Vehicles\Vehicle;
  10. use CoreBundle\Model\Vehicles\VehicleType;
  11. use DcSiteBundle\Model\Pagination;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Exception;
  14. use PortalBundle\Model\Catalog;
  15. use PortalBundle\Model\SeoMetaTag;
  16. use PortalBundle\Services\CatalogService;
  17. use PortalBundle\Services\ServiceService;
  18. use PortalBundle\Services\VehicleFilterService;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Routing\RouterInterface;
  25. class StockController extends BaseController
  26. {
  27.     private Catalog $modelCatalog;
  28.     private CatalogService $catalogService;
  29.     private InStockVehicle $inStockVehicle;
  30.     private SeoMetaTag $seoMetaTag;
  31.     public function __construct(CoreFormFactory $coreFormFactoryRequestStack $requestStack,
  32.                                 RouterInterface $routerRepository $vehicleRepositoryServiceService $serviceService,
  33.                                 Catalog         $modelCatalogCatalogService $catalogServiceInStockVehicle $inStockVehicle,
  34.                                 SeoMetaTag      $seoMetaTag)
  35.     {
  36.         parent::__construct($coreFormFactory$requestStack$router$vehicleRepository$serviceService);
  37.         $this->modelCatalog $modelCatalog;
  38.         $this->catalogService $catalogService;
  39.         $this->inStockVehicle $inStockVehicle;
  40.         $this->seoMetaTag $seoMetaTag;
  41.     }
  42.     const PRE_PAGES 15;
  43.     const MAX_PAGINATION_BUTTON 5;
  44.     /**
  45.      * @param Request $request
  46.      * @return Response
  47.      * @throws Exception
  48.      */
  49.     public function inStock(Request $requestPagination $paginationModelEntityManagerInterface $em)
  50.     {
  51.         $routeParams $request->attributes->get('_route_params');
  52.         $routeParams['brand'] = $request->query->get('brand');
  53.         $routeParams['model'] = $request->query->get('model');
  54.         $params $request->query->all();
  55.         $param null;
  56.         $value null;
  57.         $model '';
  58.         $brand '';
  59.         $type $request->get('type');
  60.         $state $request->get('state');
  61.         if ($request->get('m-brand')) {
  62.             $brand array_key_first($request->get('m-brand'));
  63.         }
  64.         if ($request->get('m-model')) {
  65.             $model array_key_first($request->get('m-model'));
  66.         }
  67.         if (isset($params['dynamic'])) {
  68.             $param array_key_first($params['dynamic']);
  69.             if (isset($params['dynamic'][$param])) {
  70.                 $value array_key_first($params['dynamic'][$param]);
  71.             }
  72.         }
  73.         if ($brand && $brand === 'all') {
  74.             throw new NotFoundHttpException();
  75.         }
  76.         $vehicles $this->modelCatalog->findStockByParams($routeParams$paramsself::PRE_PAGES);
  77.         $allBrands $em->getRepository(InStock::class)->gatAllBrands();
  78.         if (!$vehicles['data']) {
  79.             $vehicles $this->modelCatalog->noFindStockVehicleByParams($routeParams$paramsself::PRE_PAGES);
  80.         }
  81.         /** @var InStock $vehicle */
  82.         $result = [];
  83.         foreach ($vehicles['data'] as $vehicle) {
  84.             $inStockModel $this->inStockVehicle->createByEntity($vehicle[0]);
  85.             $vehicle $this->modelCatalog->getInStockVehicleByCatalog($this->getUser(), $inStockModel);
  86.             $result[] = $vehicle;
  87.         }
  88.         $typeData VehicleType::getTypeDataByUrl($type);
  89.         if (!$typeData) {
  90.             throw new NotFoundHttpException();
  91.         }
  92.         $vehicleTypeId $typeData['id'];
  93.         $catalogNav $this->modelCatalog->getStockCatalogNav($type$state$brand$params$value);
  94.         $brandEm $em->getRepository(\CoreBundle\Entity\Brand::class)->getBrandByUrl($brand);
  95.         $pagination $paginationModel->initPagination($vehicles['count'], self::PRE_PAGES5);
  96.         $filters $this->catalogService->getFilters($params$request->getLocale(), $state$vehicleTypeId$brand$model$param$value$routeParams);
  97.         $catalogCharacteristic $this->catalogService->catalogCharacteristic($vehicleTypeIdfalse);
  98.         $dealers $em->getRepository(Dealer::class)->getDealerByBrandUrl($brandEm);
  99.         $dealers array_filter($dealers, fn($dealer) => $dealer->getId() !== 39 && $dealer->getId() !== 16);
  100.         return $this->basePortalRender('@Portal/Car/new-in-stock-catalog.html.twig', [
  101.             'count' => $vehicles['count'],
  102.             'result' => $result,
  103.             'allBrands' => $allBrands,
  104.             'filters' => $filters,
  105.             'perPage' => self::PRE_PAGES,
  106.             'type' => $type,
  107.             'state' => $state,
  108.             'baseParams' => $routeParams,
  109.             'searchParams' => $params,
  110.             'catalogNav' => $catalogNav,
  111.             'pagination' => $pagination,
  112.             'page' => $request->query->get('page') ?: 1,
  113.             'dealers' => $dealers,
  114.             'locale' => $request->getLocale(),
  115.             'seoMeta' => $this->seoMetaTag->buildSeoMetaGetParams($request$routeParams$params$param$value),
  116.             'brand' => $brand,
  117.             'model' => $model,
  118.             'param' => $param,
  119.             'value' => $value,
  120.             'year' => ($param === 1000) ? $param $value,
  121.             'seo_year' => date('Y'),
  122.             'valueParam' => $catalogCharacteristic[$param][$value] ?? null,
  123.             'userId' => $this->getUser()
  124.                 ? $this->getUser()->getId()
  125.                 : null,
  126.         ]);
  127.     }
  128.     public function inStockApi(Request $requestPagination $paginationModelInStockVehicle $inStockVehicle)
  129.     {
  130.         $routeParams $request->attributes->get('_route_params');
  131.         $routeParams['brand'] = $request->query->get('brand');
  132.         $routeParams['model'] = $request->query->get('model');
  133.         $params $request->query->all();
  134.         $param null;
  135.         $value null;
  136.         $model '';
  137.         $brand '';
  138.         $type $request->get('type');
  139.         $state $request->get('state');
  140.         if ($request->get('m-brand')) {
  141.             $brand array_key_first($request->get('m-brand'));
  142.         }
  143.         if ($request->get('m-model')) {
  144.             $model array_key_first($request->get('m-model'));
  145.         }
  146.         if (isset($params['dynamic'])) {
  147.             $param array_key_first($params['dynamic']);
  148.             if (isset($params['dynamic'][$param])) {
  149.                 $value array_key_first($params['dynamic'][$param]);
  150.             }
  151.         }
  152.         if ($brand && $brand === 'all') {
  153.             throw new NotFoundHttpException();
  154.         }
  155.         $vehicles $this->modelCatalog->findStockByParams($routeParams$paramsself::PRE_PAGES);
  156.         if (!$vehicles['data']) {
  157.             $vehicles $this->modelCatalog->noFindStockVehicleByParams($routeParams$paramsself::PRE_PAGES);
  158.         }
  159.         /** @var InStock $vehicle */
  160.         $result = [];
  161.         foreach ($vehicles['data'] as $vehicle) {
  162.             $inStockModel $inStockVehicle->createByEntity($vehicle[0]);
  163.             $vehicle $this->modelCatalog->getInStockVehicleByCatalog($this->getUser(), $inStockModel);
  164.             $result[] = $vehicle;
  165.         }
  166.         $typeData VehicleType::getTypeDataByUrl($type);
  167.         if (!$typeData) {
  168.             throw new NotFoundHttpException();
  169.         }
  170.         $vehicleTypeId $typeData['id'];
  171.         $pagination $paginationModel->initPagination($vehicles['count'], self::PRE_PAGES5);
  172.         $filters $this->catalogService->getFilters($params$request->getLocale(), $state$vehicleTypeId$brand$model$param$value$routeParams);
  173.         if ($request->isXmlHttpRequest()) {
  174.             return $this->json([
  175.                 'vehicles' => $this->catalogService->getStockAjaxResult($result$request->getLocale()),
  176.                 'pagination' => $pagination,
  177.                 'count' => $vehicles['count'],
  178.                 'searchFilters' => $filters
  179.             ]);
  180.         }
  181.         return false;
  182.     }
  183.     /**
  184.      * @param Request $request
  185.      * @return JsonResponse
  186.      */
  187.     public function buildStockFilters(Request $request): JsonResponse
  188.     {
  189.         $filters $this->modelCatalog->buildStockFilters($request->request->all(), $request->getLocale());
  190.         return $this->json($filters);
  191.     }
  192.     /**
  193.      * @param Request $request
  194.      * @return JsonResponse
  195.      */
  196.     public function buildUrl(Request $request)
  197.     {
  198.         $params $request->request->all();
  199.         $type $params['type'];
  200.         $state $params['state'];
  201.         $brand $params['brand'] ?? null;
  202.         $model $params['model'] ?? null;
  203.         $isAjax $params['isAjax'] ?? null;
  204.         if (is_array($brand) && count($brand) == 1) {
  205.             $brand array_keys($brand)[0];
  206.         }
  207.         if (is_array($model) && count($model) == 1) {
  208.             $model array_keys($model)[0];
  209.         }
  210.         $baseRouteParams = [
  211.             'type' => $type,
  212.             'state' => $state,
  213.         ];
  214.         $getParams = [];
  215.         foreach ($params as $key => $row) {
  216.             if (!in_array($key, ['type''state''brand''model''dynamic']) && $row) {
  217.                 $getParams[$key] = $row;
  218.             }
  219.         }
  220.         $getParams['hasNDS'] = true;
  221.         $getParams['isSelect'] = true;
  222.         $getParams['inStock'] = true;
  223.         if (is_array($brand)) {
  224.             $getParams['m-brand'] = $brand;
  225.             $brand null;
  226.         } else if ($brand) {
  227.             $getParams['m-brand'] = [$brand => true];
  228.         }
  229.         if (is_array($model)) {
  230.             $getParams['m-model'] = $model;
  231.             $model null;
  232.         } else if ($model) {
  233.             $getParams['m-model'] = [$model => true];
  234.             $model null;
  235.         }
  236.         $dynamic = [];
  237.         if (isset($params['dynamic'])) {
  238.             foreach ($params['dynamic'] as $id => $values) {
  239.                 if (array_key_exists('from'$values) || array_key_exists('to'$values)) {
  240.                     $dynamic[$id] = $values;
  241.                 } else {
  242.                     foreach ($values as $url => $v) {
  243.                         if ($v != 'false') {
  244.                             $dynamic[$id][$url] = 1;
  245.                         }
  246.                     }
  247.                 }
  248.             }
  249.         }
  250.         $catalogCharacteristic array_flip($this->modelCatalog->getCatalogCharacteristicsId());
  251.         if (!$brand && !$model && (!count($dynamic) || empty(array_intersect_key($dynamic$catalogCharacteristic)))) {
  252.             $url $this->router->generate(
  253.                     'portal_in_stock_filter_catalog',
  254.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  255.                 );
  256.             $apiUrl $this->router->generate(
  257.                     'portal_in_stock_filter_catalog_api',
  258.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  259.                 );
  260.             return $this->json(['url' => $url'apiUrl' => $apiUrl]);
  261.         }
  262.         if (!$brand && !$model && count($dynamic)) {
  263.             $dId key($dynamic);
  264.             $value key($dynamic[$dId]);
  265.             if (!count($dynamic[$dId])) {
  266.                 unset($dynamic[$dId]);
  267.             }
  268.             $url $this->router->generate(
  269.                     'portal_in_stock_filter_catalog',
  270.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  271.                 );
  272.             $apiUrl $this->router->generate(
  273.                     'portal_in_stock_filter_catalog_api',
  274.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  275.                 );
  276.             return $this->json(['url' => $url'apiUrl' => $apiUrl]);
  277.         }
  278.         if ($model) {
  279.             $baseRouteParams['model'] = $model;
  280.             $baseRouteParams['brand'] = $brand;
  281.             $url $this->router->generate(
  282.                     'portal_in_stock_filter_catalog',
  283.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  284.                 );
  285.             $apiUrl $this->router->generate(
  286.                     'portal_in_stock_filter_catalog_api',
  287.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  288.                 );
  289.             return $this->json(['url' => $url'apiUrl' => $apiUrl]);
  290.         }
  291.         if ($brand) {
  292.             $baseRouteParams['brand'] = $brand;
  293.             $url $this->router->generate(
  294.                     'portal_in_stock_filter_catalog',
  295.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  296.                 );
  297.             $apiUrl $this->router->generate(
  298.                     'portal_in_stock_filter_catalog_api',
  299.                     array_merge($baseRouteParams$getParams, ['dynamic' => $dynamic])
  300.                 );
  301.             return $this->json(['url' => $url'apiUrl' => $apiUrl]);
  302.         }
  303.     }
  304.     public function catalogCategory(Request $request$urlEntityManagerInterface $em\CoreBundle\Factory\Vehicle $vehicleFactory): Response
  305.     {
  306.         $dealers $em->getRepository(Dealer::class)->getCarDealer();
  307.         $recommends $em->getRepository(RecommendGroup::class)->findBy(['state' => 1]);
  308.         $recommendGroup $em->getRepository(RecommendGroup::class)->findOneBy(['url' => $url]);
  309.         $recommendGroupVehicles $em->getRepository(\CoreBundle\Entity\Vehicles\Vehicle::class)->getVehicleByRecommendGroupUrl(['url' => $url]);
  310.         $vehicles $vehicleFactory->createByEntities($recommendGroupVehicles);
  311.         $sort $request->get('sort');
  312.         usort($vehicles, function (Vehicle $aVehicle $b) use ($sort) {
  313.             switch ($sort) {
  314.                 case 'price-DESC':
  315.                     return $a->fullPrice() < $b->fullPrice();
  316.                 default:
  317.                     return $a->fullPrice() > $b->fullPrice();
  318.             }
  319.         });
  320.         $seoMeta = [];
  321.         $urn preg_replace('/^(\/?ua\/|\/?ru\/)/''/'$request->getRequestUri());
  322.         $seo $em->getRepository(\CoreBundle\Entity\SeoMetaTag::class)->findOneBy(['domain' => $request->getHost(), 'urn' => $urn]);
  323.         if ($seo && $seo->getState()) {
  324.             $seoMeta = [
  325.                 'title' => $seo->getTitle($request->getLocale()),
  326.                 'description' => $seo->getDescription($request->getLocale()),
  327.                 'h1' => $seo->getH1($request->getLocale()),
  328.                 'robots' => $seo->getRobots(),
  329.             ];
  330.         }
  331.         return $this->basePortalRender('@Portal/Car/category.html.twig', [
  332.             'dealers' => $dealers,
  333.             'recommends' => $recommends,
  334.             'recommendGroup' => $recommendGroup,
  335.             'vehicles' => $vehicles,
  336.             'url' => $url,
  337.             'seoMeta' => $seoMeta,
  338.         ]);
  339.     }
  340.     //TODO remove this magic numbers
  341.     private function createPaginator($data$page 1$limit 21): array
  342.     {
  343.         $maxPages 10;
  344.         $paginator = [
  345.             'total' => ceil(count($data) / $limit),
  346.             'current' => $page,
  347.         ];
  348.         if ($paginator['total'] > $maxPages && $page floor($maxPages 2)) {
  349.             $paginator['start'] = $page floor($maxPages 2);
  350.             $paginator['end'] = $paginator['start'] + $maxPages 1;
  351.         } else {
  352.             $paginator['start'] = 1;
  353.             $paginator['end'] = min($paginator['total'], $maxPages);
  354.         }
  355.         return $paginator;
  356.     }
  357.     public function getChangeFilter(VehicleFilterService $vehicleFilterService): JsonResponse
  358.     {
  359.         return $this->json(['success' => true'filter' => $vehicleFilterService->initFilter()]);
  360.     }
  361.     public function initVehicleFilterUrl(VehicleFilterService $vehicleFilterService): JsonResponse
  362.     {
  363.         return $this->json(['success' => true'filterUrl' => $vehicleFilterService->getUrl()]);
  364.     }
  365.     public function vehicleColor(): JsonResponse
  366.     {
  367.         return $this->json([
  368.             'success' => true,
  369.             'image' => $this->modelCatalog->getVehicleImageColor(),
  370.         ]);
  371.     }
  372. }