src/Elements/Bundle/DemiFrontendBundle/Service/DemiUrl.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elements DeMI
  4.  *
  5.  * This source file is available under the elements DeMI license version 1
  6.  *
  7.  *  @copyright  Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
  8.  *  @license    elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
  9.  */
  10. namespace Elements\Bundle\DemiFrontendBundle\Service;
  11. use Elements\Bundle\SeoHelperBundle\Templating\Helper\PrettyUrl;
  12. use Pimcore\Http\Request\Resolver\SiteResolver;
  13. use Pimcore\Http\RequestHelper;
  14. use Pimcore\Model\DataObject\AbstractObject;
  15. use Pimcore\Tool;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Elements\Demi\Frontend\Service\Configuration;
  19. class DemiUrl
  20. {
  21.     public function __construct(protected PrettyUrl $prettyUrl, protected RequestHelper $requestHelper, protected Configuration $configuration, protected SiteResolver $siteResolver, protected ContainerBagInterface $parameterBag)
  22.     {
  23.     }
  24.     public function getName(): string
  25.     {
  26.         return 'demiUrl';
  27.     }
  28.     public function getCheckoutUrl(string $controllerstring $action, ?array $params null, ?string $alternateActionLongCheckout null): string
  29.     {
  30.         $uri method_exists$this->requestHelper'getMainRequest' ) ? $this->requestHelper->getMainRequest()->getRequestUri() : '';
  31.         if (str_contains($uri'/affiliate-booking/')) {
  32.             $routeName 'demi_checkout_affiliate';
  33.         } else {
  34.             $routeName 'demi_checkout';
  35.         }
  36.         $params $params ?: [];
  37.         $params['controller'] = $controller;
  38.         $params['action'] = $action;
  39.         if (!empty($alternateActionLongCheckout) && $controller === 'checkout' && $action === 'step2'
  40.             && $this->parameterBag->has('elements_demi.checkout')
  41.             && $this->parameterBag->get('elements_demi.checkout') === 'long') {
  42.             $params['action'] = $alternateActionLongCheckout;
  43.         }
  44.         $params['path'] = $params['pathOverride'] ?? $this->configuration->getCheckoutPath()?->getFullPath();
  45.         unset($params['pathOverride']);
  46.         return $this->invoke($params$routeNametrue);
  47.     }
  48.     public function invoke(array $paramsstring $routeNamebool $reset falsebool $encode true$toLanguage null): string
  49.     {
  50.         try {
  51.             $toLanguage $locale $toLanguage ?: $this->requestHelper->getCurrentRequest()->getLocale();
  52.         } catch (\Throwable $exception) {
  53.             $toLanguage $locale 'en';
  54.         }
  55.         $pathPrepend '';
  56.         if ($routeName === 'demi_acco_detail_page'
  57.             || $routeName === 'demi_acco_ajax'
  58.             || $routeName === 'demi_acco_ajax_affiliate'
  59.             || $routeName === 'demi_acco_package_ajax'
  60.             || $routeName === 'demi_acco_package_ajax_affiliate'
  61.             || $routeName === 'demi_acco_detail_package_page'
  62.             || $routeName === 'demi_acco_affiliate_detail_page'
  63.             || $routeName === 'demi_acco_affiliate_detail_package_page'
  64.             || $routeName === 'demi_acco_detail_tvbpackage_page'
  65.             || $routeName === 'demi_acco_detail_tvbpackage_enquire_page'
  66.             || $routeName === 'demi_acco_detail_package_enquire_page'
  67.             || $routeName === 'demi_acco_detail_enquire_page') {
  68.             $accommodation $params['accommodation'];
  69.             unset($params['accommodation']);
  70.             $params['id'] = $accommodation $accommodation->getId() : $params['id'];
  71.             if (!$accommodation) {
  72.                 $accommodation AbstractObject::getById($params['id']);
  73.             }
  74.             $params['name'] = '';
  75.             if (!empty($accommodation)) {
  76.                 if ($this->configuration->getAccoDetailRouteIncludesName()) {
  77.                     $params['name'] .= $accommodation->getName($toLanguage);
  78.                 }
  79.                 if ($this->configuration->getAccoDetailRouteIncludesCategory()) {
  80.                     $params['name'] .= ' '.$accommodation->getCategoryNames(1''$toLanguage);
  81.                 }
  82.                 if ($this->configuration->getAccoDetailRouteIncludesTown() && $accommodation->getTown()) {
  83.                     $params['name'] .= ' '.$accommodation->getTown()->getName($toLanguage);
  84.                 }
  85.             }
  86.             $parent $this->configuration->getAccoParentDocument($toLanguage);
  87.             $matches = [];
  88.             if ($parent) {
  89.                 $path $parent->getFullPath();
  90.                 if (preg_match('/^(https:\/\/(([a-zA-ZÖÄÜöäüß-]+\.)+[\w|-]+))\/(.*)/i'$path$matches) || preg_match('/^(http:\/\/(([a-zA-ZÖÄÜöäüß-]+\.)+[\w|-]+))\/(.*)/i'$path$matches)) {
  91.                     $pathPrepend $matches[1];
  92.                     $params['path'] = str_replace($pathPrepend''$path);
  93.                 } else {
  94.                     $params['path'] = $path;
  95.                 }
  96.             } else {
  97.                 throw new NotFoundHttpException('Not found');
  98.             }
  99.             //in case the seo routing config is not set yet
  100.             if (empty($params['name'])) {
  101.                 $params['name'] = 'CHECK-CONFIG';
  102.             }
  103.             $package $params['package'] ?? null;
  104.             if ($package) {
  105.                 unset($params['package']);
  106.                 $params['pid'] = $package->getId();
  107.                 $params['pname'] = $package->getName($toLanguage);
  108.                 if (empty($params['pname'])) {
  109.                     //feratel bug - name should not be empty, there should be a fallback present for every language!
  110.                     //in case name is still empty - use "package" as fallback to avoid routing error
  111.                     $params['pname'] = 'package';
  112.                 }
  113.             }
  114.         } elseif (!isset($params['path'])) {
  115.             $params['path'] = $this->requestHelper->getMainRequest()->getPathInfo();
  116.         }
  117.         //prepend https + main domain / main site in checkout
  118.         if ((!array_key_exists('ignorePrependingDomain'$params) || !$params['ignorePrependingDomain']) && str_starts_with($routeName'demi_checkout')) {
  119.             if ($site $this->siteResolver->getSite()) {
  120.                 $pathPrepend 'https://'.$site->getMainDomain();
  121.             } else {
  122.                 $pathPrepend str_replace('http://''https://'Tool::getHostUrl());
  123.             }
  124.         }
  125.         //replace / in Names -> causes error in routing
  126.         if (isset($params['name'])) {
  127.             $params['name'] = str_replace(['/''_'], '-'$params['name']);
  128.         }
  129.         if (isset($params['pname'])) {
  130.             $params['pname'] = str_replace(['/''_'], '-'$params['pname']);
  131.         }
  132.         if ($locale && isset($params['path'])) {
  133.             $params['_locale'] = $toLanguage;
  134.             $pathPos strpos($params['path'], '/' $locale '/');
  135.             if ($pathPos === || $pathPos === false) {
  136.                 $matches = [];
  137.                 $tmpLocale $locale;
  138.                 if ($pathPos === false && preg_match('@^/([a-zA-Z]{...})/@'$params['path'], $matches) != false) {
  139.                     $params['_locale'] = $tmpLocale $matches[1];
  140.                 }
  141.                 $params['path'] = preg_replace('@^/' preg_quote($tmpLocale) . '/@'''$params['path']);
  142.             }
  143.             if ($toLanguage !== $locale) {
  144.                 $params['path'] = preg_replace('@^/' preg_quote($toLanguage) . '/@'''$params['path']);
  145.             }
  146.         }
  147.         return $pathPrepend.$this->prettyUrl->__invoke($params$routeName$reset$encode$toLanguage);
  148.     }
  149.     /**
  150.      * @param array $params
  151.      * @param string $routeName
  152.      * @param bool $reset
  153.      * @param bool $encode
  154.      * @param null $toLanguage
  155.      * @param string|null $paramsToUse
  156.      *
  157.      * @return string
  158.      *
  159.      * For Compatibility reasons
  160.      */
  161.     public function __invoke(array $paramsstring $routeNamebool $reset falsebool $encode true$toLanguage nullstring $paramsToUse null): string
  162.     {
  163.         return $this->invoke($params$routeName$reset$encode$toLanguage);
  164.     }
  165. }