custom/plugins/GbmedForm/src/Storefront/Page/CmsPage.php line 94

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedForm
  12.  * @copyright      Copyright (c) 2020, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\Form\Storefront\Page;
  18. use Gbmed\Form\Framework\Struct\GbmedFormStruct;
  19. use Shopware\Core\Content\Category\CategoryEntity;
  20. use Shopware\Core\Content\Category\Tree\Tree;
  21. use Shopware\Core\Content\Cms\CmsPageEntity;
  22. use Shopware\Core\Framework\Context;
  23. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  24. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  25. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  26. use Shopware\Core\Framework\Struct\Struct;
  27. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  28. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  29. use Shopware\Core\System\Salutation\SalutationCollection;
  30. use Shopware\Core\System\SystemConfig\SystemConfigService;
  31. use Shopware\Storefront\Page\Navigation\NavigationPage;
  32. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  33. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  34. class CmsPage implements EventSubscriberInterface
  35. {
  36.     /**
  37.      * @var EntityRepositoryInterface
  38.      */
  39.     private $categoryRepository;
  40.     /**
  41.      * @var SalesChannelRepositoryInterface
  42.      */
  43.     private $salutationRepository;
  44.     /**
  45.      * @var SystemConfigService
  46.      */
  47.     private $systemConfigService;
  48.     /**
  49.      * @var EntityRepositoryInterface
  50.      */
  51.     private $cmsPageRepository;
  52.     /**
  53.      * ProductPage constructor.
  54.      *
  55.      * @param EntityRepositoryInterface $categoryRepository
  56.      * @param SalesChannelRepositoryInterface $salutationRepository
  57.      * @param EntityRepositoryInterface $cmsPageRepository
  58.      * @param SystemConfigService $systemConfigService
  59.      */
  60.     public function __construct(
  61.         EntityRepositoryInterface $categoryRepository,
  62.         SalesChannelRepositoryInterface $salutationRepository,
  63.         EntityRepositoryInterface $cmsPageRepository,
  64.         SystemConfigService $systemConfigService
  65.     ) {
  66.         $this->categoryRepository $categoryRepository;
  67.         $this->salutationRepository $salutationRepository;
  68.         $this->cmsPageRepository $cmsPageRepository;
  69.         $this->systemConfigService $systemConfigService;
  70.     }
  71.     /**
  72.      * subscribed events
  73.      *
  74.      * @return array
  75.      */
  76.     public static function getSubscribedEvents(): array
  77.     {
  78.         return [
  79.             NavigationPageLoadedEvent::class => 'onNavigationPageLoadedEvent'
  80.         ];
  81.     }
  82.     /**
  83.      * add category into cmsPage
  84.      *
  85.      * @param NavigationPageLoadedEvent $event
  86.      * @throws \Exception
  87.      */
  88.     public function onNavigationPageLoadedEvent(NavigationPageLoadedEvent $event): void
  89.     {
  90.         if ($category $this->findCategory($event->getRequest()->get('navigationId'), $event->getContext())) {
  91.             /** @var NavigationPage $navigationPage */
  92.             $navigationPage $event->getPage();
  93.             /** @var CmsPageEntity $cmsPage */
  94.             $cmsPage $navigationPage->getCmsPage();
  95.             if (!($cmsPage instanceof CmsPageEntity)) {
  96.                 $cmsPage $this->basicCmsPage($event->getContext());
  97.                 $navigationPage->setCmsPage($cmsPage);
  98.             }
  99.             $cmsPage->addExtension('category'$category);
  100.             $cmsPage->addExtension('gbmedForm'$this->getFormData($event->getSalesChannelContext(), $category));
  101.         }
  102.     }
  103.     /**
  104.      * @param SalesChannelContext $salesChannelContext
  105.      * @param CategoryEntity $category
  106.      * @return Struct
  107.      */
  108.     private function getFormData(SalesChannelContext $salesChannelContextCategoryEntity $category): Struct
  109.     {
  110.         /** @var array $config */
  111.         $config $this->systemConfigService->get(
  112.             'GbmedForm.config',
  113.             $salesChannelContext->getSalesChannel()->getId()
  114.         );
  115.         /** @var string $formId */
  116.         $formId = (string)$this->systemConfigService->get(
  117.             'core.basicInformation.contactPage',
  118.             $salesChannelContext->getSalesChannel()->getId()
  119.         );
  120.         /** @var SalutationCollection $salutations */
  121.         $salutations $this->salutationRepository->search(new Criteria(), $salesChannelContext)->getEntities();
  122.         return new GbmedFormStruct([
  123.             'id' => null,
  124.             'formId' => $formId,
  125.             'data' => $salutations,
  126.             'type' => isset($config['contactCategory']) && $config['contactCategory'] === $category->getId() ? 'contact' false,
  127.             'isRecaptcha' => (!empty($config['sitekey']) && !empty($config['secret'])),
  128.             'config' => $config
  129.         ]);
  130.     }
  131.     /**
  132.      * return current category
  133.      *
  134.      * @param string|null $id
  135.      * @param Context $context
  136.      * @return CategoryEntity|null
  137.      */
  138.     private function findCategory($idContext $context): ?CategoryEntity
  139.     {
  140.         return $id === null null $this->categoryRepository->search(
  141.             new Criteria([$id]),
  142.             $context
  143.         )->first();
  144.     }
  145.     /**
  146.      * return basic contactPage
  147.      *
  148.      * @param Context $context
  149.      * @return CmsPageEntity
  150.      */
  151.     private function basicCmsPage(Context $context): CmsPageEntity
  152.     {
  153.         return $this->cmsPageRepository->search(
  154.             new Criteria([$this->systemConfigService->get('core.basicInformation.contactPage')]),
  155.             $context
  156.         )->first();
  157.     }
  158. }