src/AtechNet/CoreBundle/Controller/SeoUrlController.php line 134

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Controller;
  3. use App\AtechNet\CoreBundle\Entity\Domain;
  4. use App\AtechNet\CoreBundle\Entity\Language;
  5. use App\AtechNet\CoreBundle\Entity\SeoUrl;
  6. use App\AtechNet\CoreBundle\Helper\Furbify\MultiStore;
  7. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class SeoUrlController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/{slug}", name="seo", requirements={"slug"=".+"}, defaults={"_locale":""}, priority=-1)
  14.      * 
  15.      * @param string $slug
  16.      * @param Request $request
  17.      * @return mixed
  18.      */
  19.     public function indexAction(string $slug, Request $request)
  20.     {
  21.         // Entity Manager
  22.         $entityManager = $this->getEntityManager();
  23.         // Multi-Store
  24.         $multiStore = $this->getMultiStoreHelper($request);
  25.         mb_internal_encoding('UTF-8');
  26.         // Default Controller
  27.         $controller = 'App\AtechNet\CoreBundle\Controller\DefaultController::index';
  28.         // Parts of URL
  29.         $parts = explode('/', $slug);
  30.         // Remove any empty arrays from trailing
  31.         if (mb_strlen(end($parts)) == 0) {
  32.             array_pop($parts);
  33.         }
  34.         // Default Language
  35.         $defaultLanguageId = $multiStore->getDefaultLanguageId();
  36.         // Language
  37.         $languageId = $multiStore->getLanguageId();
  38.         if (isset($parts[0])) {
  39.             $language = $entityManager->getRepository(Language::class)->findOneByCode($parts[0]);
  40.             if ($language) {
  41.                 $request->getSession()->set('languageId', $language->getId());
  42.                 $request->getSession()->set('_locale', $language->getCode());
  43.                 $request->setLocale($language->getCode());
  44.                 MultiStore::setLocalDomain($request);
  45.                 $languageId = $language->getId();
  46.                 $multiStore->setLanguageSwitched(true);
  47.                 array_shift($parts);
  48.             } else {
  49.                 $language = $multiStore->getDefaultLanguage();
  50.                 if ($language) {
  51.                     $request->getSession()->set('languageId', $language->getId());
  52.                     $request->getSession()->set('_locale', $language->getCode());
  53.                     $request->setLocale($language->getCode());
  54.                     MultiStore::setLocalDomain($request);
  55.                 }
  56.             }
  57.         }
  58.         // Domain
  59.         $domainId = $multiStore->getDomainId();
  60.         if (in_array($domainId, MultiStore::getWholesaleDomainIDs())) {
  61.             switch ($domainId) {
  62.                 case Domain::DOMAIN_ID_WS_SK:
  63.                     $domainId = Domain::DOMAIN_ID_RT_SK;
  64.                     break;
  65.                 case Domain::DOMAIN_ID_WS_HU:
  66.                     $domainId = Domain::DOMAIN_ID_RT_HU;
  67.                     break;
  68.                 case Domain::DOMAIN_ID_WS_CZ:
  69.                     $domainId = Domain::DOMAIN_ID_RT_CZ;
  70.                     break;
  71.                 case Domain::DOMAIN_ID_WS_EU:
  72.                     $domainId = Domain::DOMAIN_ID_RT_EU;
  73.                     break;
  74.             }
  75.         }
  76.         // Update Default Controller
  77.         if (!$multiStore->isLanguageSwitched()) {
  78.             $controller = 'App\AtechNet\CoreBundle\Controller\DefaultController::notFoundAction';
  79.         }
  80.         // Redirect Before Processing
  81.         $redirectTo = $this->redirectToStaticPage($slug, $domainId);
  82.         if ( $redirectTo > 0 ) {
  83.             $redirect = $this->url($request)->link('page', [
  84.                 'static_id' => $redirectTo
  85.             ]);
  86.             return $this->redirect($redirect);
  87.         }
  88.         // Reset Bazaar Flag
  89.         $request->getSession()->set('isBazaarPage', false);
  90.         // Handle Bazaar Folder
  91.         if ( isset($parts[0]) && in_array($parts[0], [ 'bazar', 'bazaar' ]) ) {
  92.             if ( count($parts) > 1 ) {
  93.                 array_shift($parts);
  94.             }
  95.             // Set Bazaar Flag
  96.             $request->getSession()->set('isBazaarPage', true);
  97.         }
  98.         // Controller Request
  99.         $params = [
  100.             'request' => $request,
  101.         ];
  102.         foreach ($parts as $part) {
  103.             $seo_url = $entityManager->getRepository(SeoUrl::class)->findOneBy([
  104.                 'keyword'   => $part, 
  105.                 'language'  => $defaultLanguageId,
  106.                 'archived'  => false,
  107.                 'domain'    => [$domainId, null],
  108.             ]);
  109.             if ($seo_url) {
  110.                 $url = explode('=', $seo_url->getQuery());
  111.                 if ($url[0] == 'product_id') {
  112.                     $controller = 'App\AtechNet\CoreBundle\Controller\ProductController::showAction';
  113.                     $params['productId'] = (int)$url[1];
  114.                     break;
  115.                 } elseif ($url[0] == 'category_id') {
  116.                     $controller = 'App\AtechNet\CoreBundle\Controller\ProductController::listAction';
  117.                     $params['categoryId'] = (int)$url[1];
  118.                 } elseif ($url[0] == 'static_id') {
  119.                     $controller = 'App\AtechNet\CoreBundle\Controller\PageController::showAction';
  120.                     $params['pageId'] = (int)$url[1];
  121.                     break;
  122.                 } elseif ($url[0] == 'blog_id') {
  123.                     $controller = 'App\AtechNet\CoreBundle\Controller\PageController::showBlogAction';
  124.                     $params['blogId'] = (int)$url[1];
  125.                     break;
  126.                 } elseif ($url[0] == 'blog_category_id') {
  127.                     $controller = 'App\AtechNet\CoreBundle\Controller\ProductController::listBlogAction';
  128.                     $params['blogCategoryId'] = (int)$url[1];
  129.                     break;
  130.                 } else {
  131.                     $controller = 'App\AtechNet\CoreBundle\Controller\DefaultController::notFoundAction';
  132.                 }
  133.             } // If at least one SEO keyword does not exist in the URL to be loaded, it is treated as a 404 error page
  134.             else {
  135.                 $controller = 'App\AtechNet\CoreBundle\Controller\DefaultController::notFoundAction';
  136.                 break;
  137.             }
  138.         }
  139.         if ($controller == 'App\AtechNet\CoreBundle\Controller\DefaultController::notFoundAction') {
  140.             if (isset($parts[0]) && isset($parts[1])) {
  141.                 if ($parts[0] == 'product') {
  142.                     if ($parts[1] == 'search') {
  143.                         $controller = 'App\AtechNet\CoreBundle\Controller\ProductController::searchAction';
  144.                     } else {
  145.                         $controller = 'App\AtechNet\CoreBundle\Controller\ProductController::showAction';
  146.                         $params['productId'] = (int)$parts[1];
  147.                     }
  148.                 } elseif ($parts[0] == 'category') {
  149.                     $controller = 'App\AtechNet\CoreBundle\Controller\ProductController::listAction';
  150.                     $params['categoryId'] = (int)$parts[1];
  151.                 } elseif ($parts[0] == 'page') {
  152.                     $controller = 'App\AtechNet\CoreBundle\Controller\PageController:::showAction';
  153.                     $params['pageId'] = (int)$parts[1];
  154.                 } elseif ($parts[0] == 'blog') {
  155.                     if (isset($parts[2]) && $parts[2] == 'category') {
  156.                         $controller = 'App\AtechNet\CoreBundle\Controller\PageController::listBlogAction';
  157.                         $params['blogCategoryId'] = (int)$parts[1];
  158.                     } else {
  159.                         $controller = 'App\AtechNet\CoreBundle\Controller\PageController::showBlogAction';
  160.                         $params['blogId'] = (int)$parts[1];
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.         if ($controller == 'App\AtechNet\CoreBundle\Controller\DefaultController::notFoundAction') {
  166.             $target = '';
  167.             $query = [];
  168.             foreach ($parts as $part) {
  169.                 $archivedSeoUrl = $entityManager->getRepository(SeoUrl::class)->findOneBy([
  170.                     'keyword' => $part,
  171.                     'language' => $defaultLanguageId,
  172.                     'archived' => true,
  173.                     'domain' => [$domainId, null],
  174.                 ]);
  175.                 if ($archivedSeoUrl) {
  176.                     $url = explode('=', $archivedSeoUrl->getQuery());
  177.                     if ($url[0] == 'product_id') {
  178.                         $target = 'product';
  179.                         $query = [
  180.                             'product_id' => $url[1],
  181.                         ];
  182.                     } elseif ($url[0] == 'category_id') {
  183.                         $target = 'category';
  184.                         if (isset($query['path'])) {
  185.                             $query['path'] .= '_' . $url[1];
  186.                         } else {
  187.                             $query = [
  188.                                 'path' => $url[1],
  189.                             ];
  190.                         }
  191.                     } elseif ($url[0] == 'blog_id') {
  192.                         $target = 'blog';
  193.                         $query = [
  194.                             'blog_id' => $url[1],
  195.                         ];
  196.                     } elseif ($url[0] == 'static_id') {
  197.                         $target = 'page';
  198.                         $query = [
  199.                             'static_id' => $url[1],
  200.                         ];
  201.                     }
  202.                 } // If the URL consists of several parts, we assume that one of the parts is not archived
  203.                 else {
  204.                     $validSeoUrl = $this->getEntityManager()->getRepository(SeoUrl::class)->findOneBy([
  205.                         'keyword' => $part,
  206.                         'language' => $languageId,
  207.                         'archived' => false,
  208.                         'domain' => [$domainId, null],
  209.                     ]);
  210.                     if ($validSeoUrl) {
  211.                         $url = explode('=', $validSeoUrl->getQuery());
  212.                         if ($url[0] == 'category_id') {
  213.                             $target = 'category';
  214.                             if (isset($query['path'])) {
  215.                                 $query['path'] .= '_' . $url[1];
  216.                             } else {
  217.                                 $query = [
  218.                                     'path' => $url[1],
  219.                                 ];
  220.                             }
  221.                         }
  222.                     }
  223.                 }
  224.             }
  225.             if (!empty($target)) {
  226.                 return $this->redirect($this->url($request)->link($target, $query), 301);
  227.             }
  228.             if (
  229.                 !empty($request->get('fs')) &&
  230.                 strpos($request->getPathInfo(), 'product') !== false
  231.             ) {
  232.                 $request->getSession()->set('blankPage', true);
  233.             }
  234.         }
  235.         // Use locale based on user settings
  236.         if ($multiStore->isLanguageSwitched()) {
  237.             $params['_locale'] = $request->getLocale();
  238.         }
  239.         return $this->forward($controller, $params);
  240.     }
  241.     /**
  242.      * @param string $slug
  243.      * @param int $domainId
  244.      */
  245.     private function redirectToStaticPage(string $slug, int $domainId)
  246.     {
  247.         if ( $slug == 'vratenie-tovaru' && $domainId == Domain::DOMAIN_ID_RT_SK ) {
  248.             return 36;
  249.         }
  250.         if ( $slug == 'termek-visszakuldese' && $domainId == Domain::DOMAIN_ID_RT_HU ) {
  251.             return 31;
  252.         }
  253.         if ( $slug == 'vraceni-zbozi' && $domainId == Domain::DOMAIN_ID_RT_CZ ) {
  254.             // return 67;
  255.         }
  256.         return 0;
  257.     }
  258. }