src/Document/Areabrick/Abstracts/AbstractAreabrick.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Document\Areabrick\Abstracts;
  3. use App\Controller\DefaultFrontendController;
  4. use App\Document\Areabrick\BrickTraits\BackgroundColorTrait;
  5. use Pimcore\Extension\Document\Areabrick\AbstractTemplateAreabrick;
  6. use Pimcore\Extension\Document\Areabrick\EditableDialogBoxConfiguration;
  7. use Pimcore\Model\Document\Editable\Area\Info;
  8. use Pimcore\Model\Document;
  9. use Pimcore\Model\Document\Page;
  10. use Pimcore\Model\Document\Snippet;
  11. use Pimcore\Targeting\VisitorInfoStorageInterface;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. abstract class AbstractAreabrick extends AbstractTemplateAreabrick
  16. {
  17.     // Allows all blocks to use the Background config and Background Panel.
  18.     use BackgroundColorTrait;
  19.     public Info $info;
  20.     protected ?\Pimcore\Model\User $user null;
  21.     protected ?string $userLocale null;
  22.     public function __construct(
  23.         private VisitorInfoStorageInterface $visitorInfoStorage,
  24.         protected TranslatorInterface $translator
  25.     ) {
  26.         /** @var \Pimcore\Model\User $user */
  27.         $this->user \Pimcore\Tool\Session::getReadonly()->get('user');
  28.         $this->userLocale $this->user?->getLanguage();
  29.     }
  30.     public function action(Info $info)
  31.     {
  32.         $this->info $info;
  33.         /** @var Request $request */
  34.         $request $info->getRequest();
  35.         /** Also @see DefaultFrontendController::data() */
  36.         $info->setParam('data', [
  37.             'request' => [
  38.                'locale' => $request->getLocale(),
  39.                 'url' => rawurldecode($request->getUri()),
  40.            ],
  41.             'visitorId' => $this->getVisitorId(),
  42.             'gclid' => $this->getParameterOrCookieValue($request'gclid'),
  43.             'msclkid' => $this->getParameterOrCookieValue($request'msclkid'),
  44.             'li_fat_id' => $this->getParameterOrCookieValue($request'li_fat_id'),
  45.             'page' => [
  46.                 'lang' => $this->getLang($info),
  47.                 'country' => $this->getCountry($info),
  48.                 'title' => $this->getTitle($info),
  49.             ],
  50.         ]);
  51.     }
  52.     /**
  53.      * Also @see DefaultFrontendController::getLang()
  54.      * @param Info $info
  55.      * @return string
  56.      */
  57.     private function getLang(Info $info): string
  58.     {
  59.         preg_match('/^\/([a-z]{2}\-[a-z]{2})(\/|$)/i'$info->getDocument()->getRealFullPath(), $matches);
  60.         // The en-gb language is the default language and does not have /en-gb/ in the path.
  61.         return $matches[1] ?? 'en-gb';
  62.     }
  63.     /**
  64.      * Also @see DefaultFrontendController::getCountry()
  65.      * @param Info $info
  66.      * @return string
  67.      */
  68.     private function getCountry(Info $info): string
  69.     {
  70.         $lang $this->getLang($info);
  71.         $split explode('-'$lang);
  72.         return $split[1] ?? $split[0];
  73.     }
  74.     /**
  75.      * Also @see DefaultFrontendController::getTitle()
  76.      * @param Info $info
  77.      * @return string|null
  78.      */
  79.     private function getTitle(Info $info): ?string
  80.     {
  81.         $document $info->getDocument();
  82.         return match (true) {
  83.             $document instanceof Page => $document->getTitle() ?: $document->getKey(),
  84.             /** @see DefaultController::snippetAction() */
  85.             $document instanceof Snippet => $document->getKey(),
  86.             default => null,
  87.         };
  88.     }
  89.     /**
  90.      * @inheritDoc
  91.      */
  92.     public function getTemplateLocation(): string
  93.     {
  94.         return static::TEMPLATE_LOCATION_GLOBAL;
  95.     }
  96.     /**
  97.      * @inheritDoc
  98.      */
  99.     public function getTemplateSuffix(): string
  100.     {
  101.         return static::TEMPLATE_SUFFIX_TWIG;
  102.     }
  103.     public function editableWidth(): int
  104.     {
  105.         return 800;
  106.     }
  107.     public function editableHeight(): int
  108.     {
  109.         return 650;
  110.     }
  111.     public function editableReloadOnClose(): bool
  112.     {
  113.         return true;
  114.     }
  115.     /**
  116.      * Overwrite this to add fields to the Inhoud panel.
  117.      *
  118.      * @param Info|null $info
  119.      * @return array|null
  120.      */
  121.     public function editableContentFields(?Info $info): ?array
  122.     {
  123.         return null;
  124.     }
  125.     public function areaBrickEditableFieldItems(?Info $info): ?array
  126.     {
  127.         $items = [
  128.             'type' => 'tabpanel',
  129.             'items' => [],
  130.         ];
  131.         if ($contentFields $this->editableContentFields($info)) {
  132.             $items['items'][] = [
  133.                 'type' => 'panel',
  134.                 'title' => $this->translator->trans('Inhoud'locale$this->userLocale),
  135.                 'items' => $contentFields,
  136.             ];
  137.         }
  138.         if ($bgPanel $this->getBackgroundColorPanel($info)) {
  139.             $items['items'][] = $bgPanel;
  140.         }
  141.         return $items['items'] ? $items : [];
  142.     }
  143.     /**
  144.      * @param Document\Editable $area
  145.      * @param Info|null $info
  146.      * @return EditableDialogBoxConfiguration
  147.      */
  148.     public function getEditableDialogBoxConfiguration(Document\Editable $area, ?Info $info): EditableDialogBoxConfiguration
  149.     {
  150.         $config = new EditableDialogBoxConfiguration();
  151.         $config->setWidth($this->editableWidth());
  152.         $config->setHeight($this->editableHeight());
  153.         $config->setReloadOnClose($this->editableReloadOnClose());
  154.         $config->setItems($this->areaBrickEditableFieldItems($info));
  155.         return $config;
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public function getHtmlTagOpen(Info $info): string
  161.     {
  162.        return "";
  163.     }
  164.     /**
  165.      * {@inheritdoc}
  166.      */
  167.     public function getHtmlTagClose(Info $info): string
  168.     {
  169.         return "";
  170.     }
  171.     private function getVisitorId(): ?string
  172.     {
  173.         // always check if there is a visitor info before trying to fetch it
  174.         if (!$this->visitorInfoStorage->hasVisitorInfo() ) {
  175.             return $this->generateVisitorId();
  176.         }
  177.         $visitorInfo $this->visitorInfoStorage->getVisitorInfo();
  178.         /*
  179.          when using cookie blockers, the visitor ID is sometimes not available and will return null.
  180.          So make sure you properly handle null in the template.
  181.         */
  182.         return $visitorInfo->getVisitorId() ?? $this->generateVisitorId();
  183.     }
  184.     private function generateVisitorId(): string {
  185.         $result '';
  186.         $chars '0123456789abcdef';
  187.         for ($i 16$i 0; --$i) {
  188.             try {
  189.                 $result .= $chars[random_int(0strlen($chars) - 1)];
  190.             } catch (\Exception $e) {
  191.                 $result .= 'a';
  192.             }
  193.         }
  194.         return $result;
  195.     }
  196.     private function getParameterOrCookieValue(Request $requeststring $parameterName): ?string
  197.     {
  198.         $cookieValue $request->cookies->get($parameterName);
  199.         if ($cookieValue !== null && $cookieValue !== '') {
  200.             return $cookieValue;
  201.         }
  202.         return $request->query->get($parameterName);
  203.     }
  204. }