<?php
namespace PortalBundle\Controller;
use CoreBundle\Model\Service\ServiceWork;
use Doctrine\ORM\EntityManagerInterface;
use CoreBundle\Entity\Vehicles\RecommendGroup;
use Exception;
use PortalBundle\Entity\Blog;
use PortalBundle\Enums\CatalogEnum;
use PortalBundle\Model\Catalog;
use PortalBundle\Model\SiteMap;
use PortalBundle\Services\ServiceService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Yaml\Yaml;
//TODO rewrite to observers/cron or other static content
class SiteMapController extends AbstractController
{
const FINANCE_CREDIT_DEALER = [9, 14, 15, 16, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44];
public function portal(Request $request, RouterInterface $router, EntityManagerInterface $em, Catalog $catalog,
ServiceWork $serviceWork, ServiceService $serviceService): Response
{
$root = $this->getParameter('kernel.project_dir');
$routingFile = $root . '/src/PortalBundle/Resources/config/routing.yml';
$routes = Yaml::parseFile($routingFile);
$result = [];
$companyGroup = [
'portal_about_founders',
'portal_about_founders_biography',
'portal_about_founders_photo',
'portal_about_founders_video',
'portal_blog',
'contacts',
'actions',
];
$allServices = [
'portal_finance_corporate',
'portal_finance_leasing',
'portal_finance_credit',
'portal_finance_tradein',
'portal_sale_vidi_select',
'portal_sale_buyback_car',
'portal_finance_insurance',
'portal_finance_extended_warranty',
'portal_service_test_drive'
];
$serviceTo = [
'portal_service_to',
'portal_service_body_repair',
'portal_car_repair'
];
foreach ($routes as $rName => $route) {
if (
!in_array(
$rName,
[
'portal_homepage',
'portal_in_stock_catalog',
]
) &&
!in_array($rName, $companyGroup)
&& !in_array($rName, $allServices)
&& !in_array($rName, $serviceTo)
) {
continue;
}
try {
$url = $router->generate($rName);
} catch (Exception $e) {
continue;
}
if ($rName == 'portal_homepage') {
$result[] = [
'url' => $url,
'priority' => '1',
'changefreq' => 'always',
];
} elseif ($rName == 'portal_blog') {
$result[] = [
'url' => $url,
'priority' => '0.5',
'changefreq' => 'daily',
];
} elseif (in_array($rName, $allServices)) {
$result[] = [
'url' => $url,
'priority' => '0.8',
'changefreq' => 'daily',
];
} elseif (in_array($rName, $companyGroup)) {
$result[] = [
'url' => $url,
'priority' => '0.4',
'changefreq' => 'daily',
];
} elseif (in_array($rName, $serviceTo)) {
$result[] = [
'url' => $url,
'priority' => '0.7',
'changefreq' => 'daily',
];
} else {
$result[] = [
'url' => $url,
'priority' => '0.8',
'changefreq' => 'daily',
];
}
}
$siteMap = new SiteMap($em, $catalog, $serviceWork, $serviceService);
$typesState = $siteMap->getActualTypesState();
foreach ($typesState as $item) {
if (in_array($item['state'], ['all'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' =>
$router->generate(
CatalogEnum::ROUTE_PORTAL_CATALOG,
['state' => $item['state'], 'type' => $item['type']['url']]
),
'priority' => '0.8',
'changefreq' => 'daily',
];
}
}
$creditTypesState = $siteMap->getActualTypesState(true);
foreach ($creditTypesState as $item) {
if (!in_array($item['state'], ['new', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG,
[
'state' => $item['state'],
'type' => $item['type']['url']
]
),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
}
$catalogCategoryGroups = $siteMap->getActualCatalogCategoryGroups();
/** @var RecommendGroup $group */
foreach ($catalogCategoryGroups as $group) {
if ($group->getUrl() === 'sportbikes') {
continue;
}
foreach (['new', 'used'] as $state) {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_CATEGORY,
[
'state' => $state,
'type' => 'car',
'group' => $group->getUrl(),
]
),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
}
$typesStateBrands = $siteMap->getActualTypesStateBrands();
foreach ($typesStateBrands as $item) {
if (in_array($item['state'], ['all', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CATALOG_B_NAME,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'brand' => $item['brand']['url']
]),
'priority' => '0.9',
'changefreq' => 'daily',
];
}
}
$creditTypesStateBrands = $siteMap->getActualTypesStateBrands(true);
foreach ($creditTypesStateBrands as $item) {
if (!in_array($item['state'], ['new', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_B_NAME,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'brand' => $item['brand']['url']
]
),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
}
$typesStateBrandsModels = $siteMap->getActualTypesStateBrandsModels();
foreach ($typesStateBrandsModels as $item) {
if ($item['model']['url'] == "") {
continue;
}
if (in_array($item['state'], ['all', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(CatalogEnum::ROUTE_PORTAL_CATALOG_BM_NAME,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'brand' => $item['brand']['url'],
'model' => $item['model']['url'],
]),
'priority' => '0.9',
'changefreq' => 'daily',
];
}
}
$creditTypesStateBrandsModels = $siteMap->getActualTypesStateBrandsModels(true);
foreach ($creditTypesStateBrandsModels as $item) {
if ($item['model']['url'] == "") {
continue;
}
if (!in_array($item['state'], ['new', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BM_NAME,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'brand' => $item['brand']['url'],
'model' => $item['model']['url'],
]
),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
}
$typeStateParamValueNU = $siteMap->getActualTypesStateParamValue();
foreach ($typeStateParamValueNU as $item) {
if (in_array($item['state'], ['all', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CATALOG_PARAM,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'param' => $item['param'],
'value' => $item['value'],
]
),
'priority' => '0.6',
'changefreq' => 'daily',
];
}
}
$creditTypesStateParamValueNU = $siteMap->getActualTypesStateParamValue(true);
foreach ($creditTypesStateParamValueNU as $item) {
if (!in_array($item['state'], ['new', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_PARAM,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'param' => $item['param'],
'value' => $item['value'],
]
),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
}
$typeStateBrandParamValue = $siteMap->getActualTypesStateBrandParamValue();
foreach ($typeStateBrandParamValue as $item) {
if (in_array($item['state'], ['all', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CATALOG_BRAMD_BODY_TYPE,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'param' => $item['param'],
'value' => $item['value'],
'brand' => $item['brand'],
]
),
'priority' => '0.7',
'changefreq' => 'daily',
];
}
}
$creditTypeStateBrandParamValue = $siteMap->getActualTypesStateBrandParamValue(true);
foreach ($creditTypeStateBrandParamValue as $item) {
if (!in_array($item['state'], ['new', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BRAMD_BODY_TYPE,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'param' => $item['param'],
'value' => $item['value'],
'brand' => $item['brand'],
]
),
'priority' => '0.5',
'changefreq' => 'daily',
];
}
}
$actualCreditTypesStateBrandsModels = $siteMap->getActualTypesStateBrandParamValuesModels(true);
foreach ($actualCreditTypesStateBrandsModels as $item) {
if ($item['model'] == "") {
continue;
}
if (!in_array($item['state'], ['new', 'used'])) {
continue;
}
if ($item['type']['url'] != 'special') {
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BRAND_BODY_TYPE_MODEL,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'param' => $item['param'],
'value' => $item['value'],
'brand' => $item['brand'],
'model' => $item['model']
]
),
'priority' => '0.5',
'changefreq' => 'daily'
];
$result[] = [
'url' => $router->generate(
CatalogEnum::ROUTE_PORTAL_CREDIT_CATALOG_BRAND_MODEL_BODY_TYPE,
[
'state' => $item['state'],
'type' => $item['type']['url'],
'param' => $item['param'],
'value' => $item['value'],
'brand' => $item['brand'],
'model' => $item['model']
]
),
'priority' => '0.5',
'changefreq' => 'daily'
];
}
}
//biography
$foundersBiography = $siteMap->getBiography($request->getLocale());
if (!empty($foundersBiography)) {
foreach ($foundersBiography as $biography) {
$result[] = [
'url' => $router->generate('portal_about_founders_biography_single', ['id' => $biography['id']]),
'priority' => '0.4',
'changefreq' => 'daily',
];
}
}
//contacts
$contacts = $siteMap->getContacts();
if (!empty($contacts)) {
foreach ($contacts as $url) {
$result[] = [
'url' => $router->generate('portal_contacts_single', ['seo_url' => $url]),
'priority' => '0.4',
'changefreq' => 'daily',
];
}
}
//actions
$actions = $siteMap->getActions();
if (!empty($actions)) {
foreach ($actions as $action) {
$result[] = [
'url' => $router->generate('actions_single', ['dealer' => $action['dealer'], 'url' => $action['url']]),
'priority' => '0.4',
'changefreq' => 'daily',
];
}
}
//bodyRepair
$repoCategories = $siteMap->getBodyRepair();
if (!empty($repoCategories['category'])) {
foreach ($repoCategories['category'] as $category) {
$result[] = [
'url' => $router->generate('portal_service_body_repair_category', ['category' => $category['category']]),
'priority' => '0.7',
'changefreq' => 'daily',
];
}
}
if (!empty($repoCategories['sub_category'])) {
foreach ($repoCategories['sub_category'] as $childCategory) {
$result[] = [
'url' => $router->generate('portal_service_body_repair_sub_category', ['category' => $childCategory['category'], 'subCategory' => $childCategory['subCategory']]),
'priority' => '0.7',
'changefreq' => 'daily',
];
}
}
//url detail page for new/used cars
$newVehicles = $siteMap->getDetailPageCar('new');
if (!empty($newVehicles)) {
foreach ($newVehicles as $vehicle) {
$result[] = [
'url' => $router->generate('portal_new_car', ['dealer' => $vehicle['dealer'], 'url' => $vehicle['url'], 'variation' => $vehicle['variation']]),
'priority' => '0.8',
'changefreq' => 'daily',
];
}
}
$usedVehicles = $siteMap->getDetailPageCar('used');
if (!empty($usedVehicles)) {
foreach ($usedVehicles as $vehicle) {
$result[] = [
'url' => $router->generate('portal_used_car', ['url' => $vehicle['url']]),
'priority' => '0.8',
'changefreq' => 'daily',
];
}
}
//serviceTo
$serviceTo = $siteMap->getServiceTo();
foreach ($serviceTo as $item) {
$result[] = [
'url' => $router->generate('portal_service_to_brand', ['brand' => $item['brand']]),
'priority' => '0.9',
'changefreq' => 'daily',
];
foreach ($item['models'] as $modelUrl) {
$result[] = [
'url' => $router->generate('portal_service_to_brand_model', ['brand' => $item['brand'], 'model' => $modelUrl]),
'priority' => '0.9',
'changefreq' => 'daily',
];
}
}
$blogs = $siteMap->getBlogs();
if (!empty($blogs)) {
/** @var Blog $blog */
foreach ($blogs as $blog) {
$result[] = [
'url' => $router->generate('portal_blog_single', ['url' => $blog->getUrl()]),
'priority' => '0.7',
'changefreq' => 'daily',
];
}
}
//carRepair
// $result = array_merge($result, $this->generateCarRepair());
$carRepair = $siteMap->getCarRepairData();
$urls = [];
foreach ($carRepair as $categoryData) {
$urls = array_merge($urls, $this->generateUrlsFromCategory($categoryData, $router));
}
$result = array_merge($result, $urls);
$content = $this->renderView(
'@Portal//sitemap.xml.twig',
['urls' => $result, 'route' => 'portal_sitemap']
);
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'application/xml']);
}
/**
* @param Request $request
* @return RedirectResponse
*/
public function sitemapRedirect(): RedirectResponse
{
return $this->redirectToRoute('portal_sitemap', ['_locale' => 'ua']);
}
/**
* @param Request $request
* @return Response
*/
public function robots()
{
$content = $this->renderView('@Portal//robots.txt.twig', []);
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
}
private function generateUrlsFromCategory(array $categoryData, RouterInterface $router): array
{
$urls = [];
if (!empty($categoryData['type']) && $categoryData['type'] === 'category') {
$urls[] = [
'url' => $router->generate(
'portal_service_page_work_group_service',
['work_group' => $categoryData['category']]
),
'priority' => '0.9',
'changefreq' => 'daily',
];
}
foreach ($categoryData['brands'] as $brand) {
$urls[] = [
'url' => $router->generate(
'portal_service_page_work_group_brand',
['work_group' => $categoryData['category'], 'brand' => $brand['brand']]
),
'priority' => '0.9',
'changefreq' => 'daily',
];
if (!empty($brand['models'])) {
foreach ($brand['models'] as $model) {
$urls[] = [
'url' => $router->generate(
'portal_service_page_work_group_model_сalc',
['work_group' => $categoryData['category'], 'brand' => $brand['brand'], 'model' => $model]
),
'priority' => '0.9',
'changefreq' => 'daily',
];
}
}
if (!empty($brand['modelWorks'])) {
foreach ($brand['modelWorks'] as $model) {
$urls[] = [
'url' => $router->generate(
'portal_service_page_work_group_model_сalc',
['work_group' => $categoryData['category'], 'brand' => $brand['brand'], 'model' => $model]
),
'priority' => '0.9',
'changefreq' => 'daily',
];
}
}
}
if (!empty($categoryData['brandSubGroups'])) {
foreach ($categoryData['brandSubGroups'] as $sg) {
$urls[] = [
'url' => $router->generate(
'portal_service_page_work_group_brand_calc',
['work_group' => $sg['subGroup'], 'brand' => $sg['brand'], 'sub_group' => $sg['subGroup']]
),
'priority' => '0.9',
'changefreq' => 'daily',
];
}
}
if (!empty($categoryData['children'])) {
foreach ($categoryData['children'] as $child) {
$urls[] = [
'url' => $router->generate(
'portal_service_page_work_group_content',
['work_group' => $categoryData['category'], 'sub_group' => $child['category']]
),
'priority' => '0.9',
'changefreq' => 'daily',
];
$urls = array_merge($urls, $this->generateUrlsFromCategory($child, $router));
}
}
return $urls;
}
}