src/PortalBundle/Controller/SiteMapController.php line 551

Open in your IDE?
  1. <?php
  2. namespace PortalBundle\Controller;
  3. use CoreBundle\Model\Service\ServiceWork;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use CoreBundle\Entity\Vehicles\RecommendGroup;
  6. use Exception;
  7. use PortalBundle\Entity\Blog;
  8. use PortalBundle\Enums\CatalogEnum;
  9. use PortalBundle\Model\Catalog;
  10. use PortalBundle\Model\SiteMap;
  11. use PortalBundle\Services\ServiceService;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\RouterInterface;
  17. use Symfony\Component\Yaml\Yaml;
  18. //TODO rewrite to observers/cron or other static content
  19. class SiteMapController extends AbstractController
  20. {
  21.     const FINANCE_CREDIT_DEALER = [9141516272829303132333435363738394041424344];
  22.     public function portal(Request $requestRouterInterface $routerEntityManagerInterface $emCatalog $catalog,
  23.                            ServiceWork $serviceWorkServiceService $serviceService): Response
  24.     {
  25.         $root $this->getParameter('kernel.project_dir');
  26.         $routingFile $root '/src/PortalBundle/Resources/config/routing.yml';
  27.         $routes Yaml::parseFile($routingFile);
  28.         $result = [];
  29.         $companyGroup = [
  30.             'portal_about_founders',
  31.             'portal_about_founders_biography',
  32.             'portal_about_founders_photo',
  33.             'portal_about_founders_video',
  34.             'portal_blog',
  35.             'contacts',
  36.             'actions',
  37.         ];
  38.         $allServices = [
  39.             'portal_finance_corporate',
  40.             'portal_finance_leasing',
  41.             'portal_finance_credit',
  42.             'portal_finance_tradein',
  43.             'portal_sale_vidi_select',
  44.             'portal_sale_buyback_car',
  45.             'portal_finance_insurance',
  46.             'portal_finance_extended_warranty',
  47.             'portal_service_test_drive'
  48.         ];
  49.         $serviceTo = [
  50.             'portal_service_to',
  51.             'portal_service_body_repair',
  52.             'portal_car_repair'
  53.         ];
  54.         foreach ($routes as $rName => $route) {
  55.             if (
  56.                 !in_array(
  57.                     $rName,
  58.                     [
  59.                         'portal_homepage',
  60.                         'portal_in_stock_catalog',
  61.                     ]
  62.                 ) &&
  63.                 !in_array($rName$companyGroup)
  64.                 && !in_array($rName$allServices)
  65.                 && !in_array($rName$serviceTo)
  66.             ) {
  67.                 continue;
  68.             }
  69.             try {
  70.                 $url $router->generate($rName);
  71.             } catch (Exception $e) {
  72.                 continue;
  73.             }
  74.             if ($rName == 'portal_homepage') {
  75.                 $result[] = [
  76.                     'url' => $url,
  77.                     'priority' => '1',
  78.                     'changefreq' => 'always',
  79.                 ];
  80.             } elseif ($rName == 'portal_blog') {
  81.                 $result[] = [
  82.                     'url' => $url,
  83.                     'priority' => '0.5',
  84.                     'changefreq' => 'daily',
  85.                 ];
  86.             } elseif (in_array($rName$allServices)) {
  87.                 $result[] = [
  88.                     'url' => $url,
  89.                     'priority' => '0.8',
  90.                     'changefreq' => 'daily',
  91.                 ];
  92.             } elseif (in_array($rName$companyGroup)) {
  93.                 $result[] = [
  94.                     'url' => $url,
  95.                     'priority' => '0.4',
  96.                     'changefreq' => 'daily',
  97.                 ];
  98.             } elseif (in_array($rName$serviceTo)) {
  99.                 $result[] = [
  100.                     'url' => $url,
  101.                     'priority' => '0.7',
  102.                     'changefreq' => 'daily',
  103.                 ];
  104.             } else {
  105.                 $result[] = [
  106.                     'url' => $url,
  107.                     'priority' => '0.8',
  108.                     'changefreq' => 'daily',
  109.                 ];
  110.             }
  111.         }
  112.         $siteMap = new SiteMap($em$catalog,  $serviceWork$serviceService);
  113.         $typesState $siteMap->getActualTypesState();
  114.         foreach ($typesState as $item) {
  115.             if (in_array($item['state'], ['all'])) {
  116.                 continue;
  117.             }
  118.             if ($item['type']['url'] != 'special') {
  119.                 $result[] = [
  120.                     'url' =>
  121.                         $router->generate(
  122.                             CatalogEnum::ROUTE_PORTAL_CATALOG,
  123.                             ['state' => $item['state'], 'type' => $item['type']['url']]
  124.                         ),
  125.                     'priority' => '0.8',
  126.                     'changefreq' => 'daily',
  127.                 ];
  128.             }
  129.         }
  130.         $creditTypesState $siteMap->getActualTypesState(true);
  131.         foreach ($creditTypesState as $item) {
  132.             if (!in_array($item['state'], ['new''used'])) {
  133.                 continue;
  134.             }
  135.             if ($item['type']['url'] != 'special') {
  136.                 $result[] = [
  137.                     'url' => $router->generate(
  138.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG,
  139.                         [
  140.                             'state' => $item['state'],
  141.                             'type' => $item['type']['url']
  142.                         ]
  143.                     ),
  144.                     'priority' => '0.5',
  145.                     'changefreq' => 'daily',
  146.                 ];
  147.             }
  148.         }
  149.         $catalogCategoryGroups $siteMap->getActualCatalogCategoryGroups();
  150.         /** @var RecommendGroup $group */
  151.         foreach ($catalogCategoryGroups as $group) {
  152.             if ($group->getUrl() === 'sportbikes') {
  153.                 continue;
  154.             }
  155.             foreach (['new''used'] as $state) {
  156.                 $result[] = [
  157.                     'url' => $router->generate(
  158.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_CATEGORY,
  159.                         [
  160.                             'state' => $state,
  161.                             'type' => 'car',
  162.                             'group' => $group->getUrl(),
  163.                         ]
  164.                     ),
  165.                     'priority' => '0.5',
  166.                     'changefreq' => 'daily',
  167.                 ];
  168.             }
  169.         }
  170.         $typesStateBrands $siteMap->getActualTypesStateBrands();
  171.         foreach ($typesStateBrands as $item) {
  172.             if (in_array($item['state'], ['all''used'])) {
  173.                 continue;
  174.             }
  175.             if ($item['type']['url'] != 'special') {
  176.                 $result[] = [
  177.                     'url' => $router->generate(
  178.                         CatalogEnum::ROUTE_PORTAL_CATALOG_B_NAME,
  179.                         [
  180.                             'state' => $item['state'],
  181.                             'type' => $item['type']['url'],
  182.                             'brand' => $item['brand']['url']
  183.                         ]),
  184.                     'priority' => '0.9',
  185.                     'changefreq' => 'daily',
  186.                 ];
  187.             }
  188.         }
  189.         $creditTypesStateBrands $siteMap->getActualTypesStateBrands(true);
  190.         foreach ($creditTypesStateBrands as $item) {
  191.             if (!in_array($item['state'], ['new''used'])) {
  192.                 continue;
  193.             }
  194.             if ($item['type']['url'] != 'special') {
  195.                 $result[] = [
  196.                     'url' => $router->generate(
  197.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_B_NAME,
  198.                         [
  199.                             'state' => $item['state'],
  200.                             'type' => $item['type']['url'],
  201.                             'brand' => $item['brand']['url']
  202.                         ]
  203.                     ),
  204.                     'priority' => '0.5',
  205.                     'changefreq' => 'daily',
  206.                 ];
  207.             }
  208.         }
  209.         $typesStateBrandsModels $siteMap->getActualTypesStateBrandsModels();
  210.         foreach ($typesStateBrandsModels as $item) {
  211.             if ($item['model']['url'] == "") {
  212.                 continue;
  213.             }
  214.             if (in_array($item['state'], ['all''used'])) {
  215.                 continue;
  216.             }
  217.             if ($item['type']['url'] != 'special') {
  218.                 $result[] = [
  219.                     'url' => $router->generate(CatalogEnum::ROUTE_PORTAL_CATALOG_BM_NAME,
  220.                         [
  221.                             'state' => $item['state'],
  222.                             'type' => $item['type']['url'],
  223.                             'brand' => $item['brand']['url'],
  224.                             'model' => $item['model']['url'],
  225.                         ]),
  226.                     'priority' => '0.9',
  227.                     'changefreq' => 'daily',
  228.                 ];
  229.             }
  230.         }
  231.         $creditTypesStateBrandsModels $siteMap->getActualTypesStateBrandsModels(true);
  232.         foreach ($creditTypesStateBrandsModels as $item) {
  233.             if ($item['model']['url'] == "") {
  234.                 continue;
  235.             }
  236.             if (!in_array($item['state'], ['new''used'])) {
  237.                 continue;
  238.             }
  239.             if ($item['type']['url'] != 'special') {
  240.                 $result[] = [
  241.                     'url' => $router->generate(
  242.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BM_NAME,
  243.                         [
  244.                             'state' => $item['state'],
  245.                             'type' => $item['type']['url'],
  246.                             'brand' => $item['brand']['url'],
  247.                             'model' => $item['model']['url'],
  248.                         ]
  249.                      ),
  250.                     'priority' => '0.5',
  251.                     'changefreq' => 'daily',
  252.                 ];
  253.             }
  254.         }
  255.         $typeStateParamValueNU $siteMap->getActualTypesStateParamValue();
  256.         foreach ($typeStateParamValueNU as $item) {
  257.             if (in_array($item['state'], ['all''used'])) {
  258.                 continue;
  259.             }
  260.             if ($item['type']['url'] != 'special') {
  261.                 $result[] = [
  262.                     'url' => $router->generate(
  263.                         CatalogEnum::ROUTE_PORTAL_CATALOG_PARAM,
  264.                         [
  265.                             'state' => $item['state'],
  266.                             'type' => $item['type']['url'],
  267.                             'param' => $item['param'],
  268.                             'value' => $item['value'],
  269.                         ]
  270.                     ),
  271.                     'priority' => '0.6',
  272.                     'changefreq' => 'daily',
  273.                 ];
  274.             }
  275.         }
  276.         $creditTypesStateParamValueNU $siteMap->getActualTypesStateParamValue(true);
  277.         foreach ($creditTypesStateParamValueNU as $item) {
  278.             if (!in_array($item['state'], ['new''used'])) {
  279.                 continue;
  280.             }
  281.             if ($item['type']['url'] != 'special') {
  282.                 $result[] = [
  283.                     'url' => $router->generate(
  284.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_PARAM,
  285.                         [
  286.                             'state' => $item['state'],
  287.                             'type' => $item['type']['url'],
  288.                             'param' => $item['param'],
  289.                             'value' => $item['value'],
  290.                         ]
  291.                     ),
  292.                     'priority' => '0.5',
  293.                     'changefreq' => 'daily',
  294.                 ];
  295.             }
  296.         }
  297.         $typeStateBrandParamValue $siteMap->getActualTypesStateBrandParamValue();
  298.         foreach ($typeStateBrandParamValue as $item) {
  299.             if (in_array($item['state'], ['all''used'])) {
  300.                 continue;
  301.             }
  302.             if ($item['type']['url'] != 'special') {
  303.                 $result[] = [
  304.                     'url' => $router->generate(
  305.                         CatalogEnum::ROUTE_PORTAL_CATALOG_BRAMD_BODY_TYPE,
  306.                         [
  307.                             'state' => $item['state'],
  308.                             'type' => $item['type']['url'],
  309.                             'param' => $item['param'],
  310.                             'value' => $item['value'],
  311.                             'brand' => $item['brand'],
  312.                         ]
  313.                     ),
  314.                     'priority' => '0.7',
  315.                     'changefreq' => 'daily',
  316.                 ];
  317.             }
  318.         }
  319.         $creditTypeStateBrandParamValue $siteMap->getActualTypesStateBrandParamValue(true);
  320.         foreach ($creditTypeStateBrandParamValue as $item) {
  321.             if (!in_array($item['state'], ['new''used'])) {
  322.                 continue;
  323.             }
  324.             if ($item['type']['url'] != 'special') {
  325.                 $result[] = [
  326.                     'url' => $router->generate(
  327.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BRAMD_BODY_TYPE,
  328.                         [
  329.                             'state' => $item['state'],
  330.                             'type' => $item['type']['url'],
  331.                             'param' => $item['param'],
  332.                             'value' => $item['value'],
  333.                             'brand' => $item['brand'],
  334.                         ]
  335.                     ),
  336.                     'priority' => '0.5',
  337.                     'changefreq' => 'daily',
  338.                 ];
  339.             }
  340.         }
  341.         $actualCreditTypesStateBrandsModels $siteMap->getActualTypesStateBrandParamValuesModels(true);
  342.         foreach ($actualCreditTypesStateBrandsModels as $item) {
  343.             if ($item['model'] == "") {
  344.                 continue;
  345.             }
  346.             if (!in_array($item['state'], ['new''used'])) {
  347.                 continue;
  348.             }
  349.             if ($item['type']['url'] != 'special') {
  350.                 $result[] = [
  351.                     'url' => $router->generate(
  352.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BRAND_BODY_TYPE_MODEL,
  353.                         [
  354.                             'state' => $item['state'],
  355.                             'type' => $item['type']['url'],
  356.                             'param' => $item['param'],
  357.                             'value' => $item['value'],
  358.                             'brand' => $item['brand'],
  359.                             'model' => $item['model']
  360.                         ]
  361.                     ),
  362.                     'priority' => '0.5',
  363.                     'changefreq' => 'daily'
  364.                 ];
  365.                 $result[] = [
  366.                     'url' => $router->generate(
  367.                         CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BRAND_MODEL_BODY_TYPE,
  368.                         [
  369.                             'state' => $item['state'],
  370.                             'type' => $item['type']['url'],
  371.                             'param' => $item['param'],
  372.                             'value' => $item['value'],
  373.                             'brand' => $item['brand'],
  374.                             'model' => $item['model']
  375.                         ]
  376.                     ),
  377.                     'priority' => '0.5',
  378.                     'changefreq' => 'daily'
  379.                 ];
  380.             }
  381.         }
  382.         //biography
  383.         $foundersBiography $siteMap->getBiography($request->getLocale());
  384.         if (!empty($foundersBiography)) {
  385.             foreach ($foundersBiography as $biography) {
  386.                 $result[] = [
  387.                     'url' => $router->generate('portal_about_founders_biography_single', ['id' => $biography['id']]),
  388.                     'priority' => '0.4',
  389.                     'changefreq' => 'daily',
  390.                 ];
  391.             }
  392.         }
  393.         //contacts
  394.         $contacts $siteMap->getContacts();
  395.         if (!empty($contacts)) {
  396.             foreach ($contacts as $url) {
  397.                 $result[] = [
  398.                     'url' => $router->generate('portal_contacts_single', ['seo_url' => $url]),
  399.                     'priority' => '0.4',
  400.                     'changefreq' => 'daily',
  401.                 ];
  402.             }
  403.         }
  404.         //actions
  405.         $actions $siteMap->getActions();
  406.         if (!empty($actions)) {
  407.             foreach ($actions as $action) {
  408.                 $result[] = [
  409.                     'url' => $router->generate('actions_single', ['dealer' => $action['dealer'], 'url' => $action['url']]),
  410.                     'priority' => '0.4',
  411.                     'changefreq' => 'daily',
  412.                 ];
  413.             }
  414.         }
  415.         //bodyRepair
  416.         $repoCategories $siteMap->getBodyRepair();
  417.         if (!empty($repoCategories['category'])) {
  418.             foreach ($repoCategories['category'] as $category) {
  419.                 $result[] = [
  420.                     'url' => $router->generate('portal_service_body_repair_category', ['category' => $category['category']]),
  421.                     'priority' => '0.7',
  422.                     'changefreq' => 'daily',
  423.                 ];
  424.             }
  425.         }
  426.         if (!empty($repoCategories['sub_category'])) {
  427.             foreach ($repoCategories['sub_category'] as $childCategory) {
  428.                 $result[] = [
  429.                     'url' => $router->generate('portal_service_body_repair_sub_category', ['category' => $childCategory['category'], 'subCategory' => $childCategory['subCategory']]),
  430.                     'priority' => '0.7',
  431.                     'changefreq' => 'daily',
  432.                 ];
  433.             }
  434.         }
  435.         //url detail page for new/used cars
  436.         $newVehicles $siteMap->getDetailPageCar('new');
  437.         if (!empty($newVehicles)) {
  438.             foreach ($newVehicles as $vehicle) {
  439.                 $result[] = [
  440.                     'url' => $router->generate('portal_new_car', ['dealer' => $vehicle['dealer'], 'url' => $vehicle['url'], 'variation' => $vehicle['variation']]),
  441.                     'priority' => '0.8',
  442.                     'changefreq' => 'daily',
  443.                 ];
  444.             }
  445.         }
  446.         $usedVehicles $siteMap->getDetailPageCar('used');
  447.         if (!empty($usedVehicles)) {
  448.             foreach ($usedVehicles as $vehicle) {
  449.                 $result[] = [
  450.                     'url' => $router->generate('portal_used_car', ['url' => $vehicle['url']]),
  451.                     'priority' => '0.8',
  452.                     'changefreq' => 'daily',
  453.                 ];
  454.             }
  455.         }
  456.         //serviceTo
  457.         $serviceTo $siteMap->getServiceTo();
  458.         foreach ($serviceTo as $item) {
  459.             $result[] = [
  460.                 'url' => $router->generate('portal_service_to_brand', ['brand' => $item['brand']]),
  461.                 'priority' => '0.9',
  462.                 'changefreq' => 'daily',
  463.             ];
  464.             foreach ($item['models'] as $modelUrl) {
  465.                     $result[] = [
  466.                         'url' => $router->generate('portal_service_to_brand_model', ['brand' =>  $item['brand'], 'model' => $modelUrl]),
  467.                         'priority' => '0.9',
  468.                         'changefreq' => 'daily',
  469.                     ];
  470.             }
  471.         }
  472.         $blogs $siteMap->getBlogs();
  473.         if (!empty($blogs)) {
  474.             /** @var Blog $blog */
  475.             foreach ($blogs as $blog) {
  476.                 $result[] = [
  477.                     'url' => $router->generate('portal_blog_single', ['url' => $blog->getUrl()]),
  478.                     'priority' => '0.7',
  479.                     'changefreq' => 'daily',
  480.                 ];
  481.             }
  482.         }
  483.         //carRepair
  484. //        $result = array_merge($result, $this->generateCarRepair());
  485.         $carRepair $siteMap->getCarRepairData();
  486.         $urls = [];
  487.         foreach ($carRepair as $categoryData) {
  488.             $urls array_merge($urls$this->generateUrlsFromCategory($categoryData$router));
  489.         }
  490.         $result array_merge($result$urls);
  491.         $content $this->renderView(
  492.             '@Portal//sitemap.xml.twig',
  493.             ['urls' => $result'route' => 'portal_sitemap']
  494.         );
  495.         return new Response($contentResponse::HTTP_OK, ['Content-Type' => 'application/xml']);
  496.     }
  497.     /**
  498.      * @param Request $request
  499.      * @return RedirectResponse
  500.      */
  501.     public function sitemapRedirect(): RedirectResponse
  502.     {
  503.         return $this->redirectToRoute('portal_sitemap', ['_locale' => 'ua']);
  504.     }
  505.     /**
  506.      * @param Request $request
  507.      * @return Response
  508.      */
  509.     public function robots()
  510.     {
  511.         $content $this->renderView('@Portal//robots.txt.twig', []);
  512.         return new Response($contentResponse::HTTP_OK, ['Content-Type' => 'text/plain']);
  513.     }
  514.     private function generateUrlsFromCategory(array $categoryDataRouterInterface $router): array
  515.     {
  516.         $urls = [];
  517.         if (!empty($categoryData['type']) && $categoryData['type'] === 'category') {
  518.             $urls[] = [
  519.                 'url'        => $router->generate(
  520.                     'portal_service_page_work_group_service',
  521.                     ['work_group' => $categoryData['category']]
  522.                 ),
  523.                 'priority'   => '0.9',
  524.                 'changefreq' => 'daily',
  525.             ];
  526.         }
  527.         foreach ($categoryData['brands'] as $brand) {
  528.             $urls[] = [
  529.                 'url'        => $router->generate(
  530.                     'portal_service_page_work_group_brand',
  531.                     ['work_group' => $categoryData['category'], 'brand' => $brand['brand']]
  532.                 ),
  533.                 'priority'   => '0.9',
  534.                 'changefreq' => 'daily',
  535.             ];
  536.             if (!empty($brand['models'])) {
  537.                 foreach ($brand['models'] as $model) {
  538.                     $urls[] = [
  539.                         'url' => $router->generate(
  540.                             'portal_service_page_work_group_model_сalc',
  541.                             ['work_group' => $categoryData['category'], 'brand' => $brand['brand'], 'model' => $model]
  542.                         ),
  543.                         'priority' => '0.9',
  544.                         'changefreq' => 'daily',
  545.                     ];
  546.                 }
  547.             }
  548.             if (!empty($brand['modelWorks'])) {
  549.                 foreach ($brand['modelWorks'] as $model) {
  550.                     $urls[] = [
  551.                         'url' => $router->generate(
  552.                             'portal_service_page_work_group_model_сalc',
  553.                             ['work_group' => $categoryData['category'], 'brand' => $brand['brand'], 'model' => $model]
  554.                         ),
  555.                         'priority' => '0.9',
  556.                         'changefreq' => 'daily',
  557.                     ];
  558.                 }
  559.             }
  560.         }
  561.         if (!empty($categoryData['brandSubGroups'])) {
  562.             foreach ($categoryData['brandSubGroups'] as $sg) {
  563.                 $urls[] = [
  564.                     'url' => $router->generate(
  565.                         'portal_service_page_work_group_brand_calc',
  566.                         ['work_group' => $sg['subGroup'], 'brand' => $sg['brand'], 'sub_group' => $sg['subGroup']]
  567.                     ),
  568.                     'priority' => '0.9',
  569.                     'changefreq' => 'daily',
  570.                 ];
  571.             }
  572.         }
  573.         if (!empty($categoryData['children'])) {
  574.             foreach ($categoryData['children'] as $child) {
  575.                 $urls[] = [
  576.                     'url' => $router->generate(
  577.                         'portal_service_page_work_group_content',
  578.                         ['work_group' => $categoryData['category'], 'sub_group' => $child['category']]
  579.                     ),
  580.                     'priority' => '0.9',
  581.                     'changefreq' => 'daily',
  582.                 ];
  583.                 $urls array_merge($urls$this->generateUrlsFromCategory($child$router));
  584.             }
  585.         }
  586.         return $urls;
  587.     }
  588. }