vendor/pimcore/portal-engine/src/EventSubscriber/DocumentConfigSubscriber.php line 215

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\PortalEngineBundle\EventSubscriber;
  12. use Pimcore\Bundle\PortalEngineBundle\Enum\Document\Editables;
  13. use Pimcore\Bundle\PortalEngineBundle\Model\ElementDataAware;
  14. use Pimcore\Bundle\PortalEngineBundle\Service\DataPool\DataPoolConfigService;
  15. use Pimcore\Bundle\PortalEngineBundle\Service\DataPool\DataPoolService;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\DefaultValuesService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\FrontendBuildService;
  18. use Pimcore\Bundle\PortalEngineBundle\Service\PortalConfig\PortalConfigService;
  19. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex\Search\SearchServiceInterface;
  20. use Pimcore\Event\DocumentEvents;
  21. use Pimcore\Event\Model\DocumentEvent;
  22. use Pimcore\Model\Document\Editable;
  23. use Pimcore\Model\Document\Editable\Block\Item;
  24. use Pimcore\Model\Document\Editable\Input;
  25. use Pimcore\Model\Document\Page;
  26. use Symfony\Component\Console\ConsoleEvents;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\HttpKernel\KernelEvents;
  29. use Symfony\Contracts\EventDispatcher\Event;
  30. /**
  31.  * Class IndexUpdateListener
  32.  *
  33.  * @package Pimcore\Bundle\PortalEngineBundle\EventListener
  34.  */
  35. class DocumentConfigSubscriber implements EventSubscriberInterface
  36. {
  37.     use ElementDataAware;
  38.     /**
  39.      * @var DataPoolConfigService
  40.      */
  41.     protected $dataPoolConfigService;
  42.     /**
  43.      * @var DataPoolService
  44.      */
  45.     protected $dataPoolService;
  46.     /**
  47.      * @var PortalConfigService
  48.      */
  49.     protected $portalConfigService;
  50.     /**
  51.      * @var DefaultValuesService
  52.      */
  53.     protected $defaultValuesService;
  54.     /**
  55.      * @var FrontendBuildService
  56.      */
  57.     protected $frontendBuildService;
  58.     /**
  59.      * @var bool $updatePortalsJson
  60.      */
  61.     protected $updatePortalsJson false;
  62.     /**
  63.      * @var bool
  64.      */
  65.     protected $forceUpdatePortalsJson false;
  66.     /**
  67.      * @var []
  68.      */
  69.     protected $usedParamNames;
  70.     public function __construct(DataPoolConfigService $dataPoolConfigServiceDataPoolService $dataPoolServicePortalConfigService $portalConfigServiceDefaultValuesService $defaultValuesServiceFrontendBuildService $frontendBuildService)
  71.     {
  72.         $this->dataPoolConfigService $dataPoolConfigService;
  73.         $this->dataPoolService $dataPoolService;
  74.         $this->portalConfigService $portalConfigService;
  75.         $this->defaultValuesService $defaultValuesService;
  76.         $this->frontendBuildService $frontendBuildService;
  77.     }
  78.     public static function getSubscribedEvents()
  79.     {
  80.         return [
  81.             DocumentEvents::PRE_UPDATE => 'onDocumentSave',
  82.             DocumentEvents::PRE_ADD => 'onDocumentAdd',
  83.             DocumentEvents::POST_ADD => 'triggerUpdatePortalsJson',
  84.             DocumentEvents::POST_DELETE => 'triggerUpdatePortalsJson',
  85.             KernelEvents::TERMINATE => 'onTerminate',
  86.             ConsoleEvents::TERMINATE => [['onTerminate', -1000]]
  87.         ];
  88.     }
  89.     /**
  90.      * @param DocumentEvent $event
  91.      *
  92.      * @throws \Exception
  93.      */
  94.     public function onDocumentSave(DocumentEvent $event)
  95.     {
  96.         $document $event->getDocument();
  97.         if ($this->dataPoolConfigService->isDataPoolConfigDocument($document)) {
  98.             $this->dataPoolConfigService->setCurrentDataPoolConfigById($document->getId());
  99.             $dataPool $this->dataPoolService->getDataPoolByConfig($this->dataPoolConfigService->getCurrentDataPoolConfig());
  100.             $this->usedParamNames = [];
  101.             /**
  102.              * @var Page $document ;
  103.              */
  104.             if ($block $document->getEditable(Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_FILTERS)) {
  105.                 /**
  106.                  * @var Item $blockItem
  107.                  */
  108.                 $indices $block->getData();
  109.                 foreach ($block->getElements() as $i => $blockItem) {
  110.                     $index $indices[$i];
  111.                     $filterType $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\FilterDefinition::FILTER_TYPE);
  112.                     $filterAttribute $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\FilterDefinition::FILTER_ATTRIBUTE);
  113.                     if (empty($filterType) || empty($filterAttribute)) {
  114.                         continue;
  115.                     }
  116.                     $uniqueName $this->getUniqueFilterParamName($dataPool->getSearchService(), $filterAttribute);
  117.                     $parentBlockNames = [Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_FILTERS];
  118.                     $id Editable::buildChildEditableName(
  119.                         Editables\DataPool\DataPoolConfig\FilterDefinition::FILTER_PARAM_NAME'input'$parentBlockNames$index
  120.                     );
  121.                     $tag = new Input();
  122.                     $tag->setDataFromEditmode($uniqueName);
  123.                     $tag->setParentBlockNames($parentBlockNames);
  124.                     $tag->setName($id);
  125.                     $document->setEditable($tag);
  126.                 }
  127.             }
  128.             $this->usedParamNames = [];
  129.             /**
  130.              * @var Page $document ;
  131.              */
  132.             if ($block $document->getEditable(Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_SORT_OPTIONS)) {
  133.                 /**
  134.                  * @var Item $blockItem
  135.                  */
  136.                 $indices $block->getData();
  137.                 foreach ($block->getElements() as $i => $blockItem) {
  138.                     $index $indices[$i];
  139.                     $direction $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\SortOption::DIRECTION);
  140.                     $field $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\SortOption::FIELD);
  141.                     if (empty($direction) || empty($field)) {
  142.                         continue;
  143.                     }
  144.                     $uniqueName $this->getUniqueSortParamName($dataPool->getSearchService(), $field$direction);
  145.                     $parentBlockNames = [Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_SORT_OPTIONS];
  146.                     $id Editable::buildChildEditableName(Editables\DataPool\DataPoolConfig\SortOption::PARAM_NAME'input'$parentBlockNames$index);
  147.                     $tag = new Input();
  148.                     $tag->setDataFromEditmode($uniqueName);
  149.                     $tag->setParentBlockNames($parentBlockNames);
  150.                     $tag->setName($id);
  151.                     $document->setEditable($tag);
  152.                 }
  153.             }
  154.         } elseif ($document instanceof Page && $this->portalConfigService->isPortalEnginePortal($document)) {
  155.             $this->updatePortalsJson true;
  156.         }
  157.     }
  158.     /**
  159.      * @param DocumentEvent $event
  160.      *
  161.      * @throws \Exception
  162.      */
  163.     public function onDocumentAdd(DocumentEvent $event)
  164.     {
  165.         $document $event->getDocument();
  166.         if ($document instanceof Page && $this->portalConfigService->isPortalEnginePortal($document)) {
  167.             $this->defaultValuesService->setPortalPreCreateDefaultConfig($document);
  168.         }
  169.     }
  170.     /**
  171.      * @param DocumentEvent $event
  172.      *
  173.      * @throws \Exception
  174.      */
  175.     public function triggerUpdatePortalsJson(DocumentEvent $event)
  176.     {
  177.         $document $event->getDocument();
  178.         if ($document instanceof Page && $this->portalConfigService->isPortalEnginePortal($document)) {
  179.             $this->updatePortalsJson true;
  180.         }
  181.     }
  182.     public function onTerminate(/*Event*/ $terminateEvent)
  183.     {
  184.         if ($this->updatePortalsJson) {
  185.             $this->frontendBuildService->updatePortalsJson(true$this->forceUpdatePortalsJson);
  186.         }
  187.     }
  188.     /**
  189.      * @param bool $updatePortalsJson
  190.      */
  191.     public function setUpdatePortalsJson(bool $updatePortalsJsonbool $forceUpdatePortalsJson false)
  192.     {
  193.         $this->updatePortalsJson $updatePortalsJson;
  194.         $this->forceUpdatePortalsJson $forceUpdatePortalsJson;
  195.     }
  196.     /**
  197.      * @param string $name
  198.      * @param int $count
  199.      *
  200.      * @return mixed
  201.      *
  202.      * @throws \Exception
  203.      */
  204.     protected function getUniqueFilterParamName(SearchServiceInterface $searchService$name$count 1)
  205.     {
  206.         if ($count === 1) {
  207.             $filterableFields $searchService->getFilterableFieldsMapping();
  208.             $name = isset($filterableFields[$name]) ? $filterableFields[$name]->getName() : $name;
  209.         }
  210.         $nameWithoutCount $name;
  211.         if ($count 1) {
  212.             $name .= $count;
  213.         }
  214.         if (!in_array($name$this->usedParamNames)) {
  215.             $this->usedParamNames[] = $name;
  216.             return $name;
  217.         }
  218.         return $this->getUniqueFilterParamName($searchService$nameWithoutCount$count 1);
  219.     }
  220.     /**
  221.      * @param string $name
  222.      * @param string $direction
  223.      * @param int $count
  224.      *
  225.      * @return mixed
  226.      *
  227.      * @throws \Exception
  228.      */
  229.     protected function getUniqueSortParamName(SearchServiceInterface $searchService$name$direction$count 1)
  230.     {
  231.         if ($count === 1) {
  232.             $sortableFields $searchService->getSortableFieldsMapping();
  233.             $name = isset($sortableFields[$name]) ? $sortableFields[$name]->getName() : $name;
  234.             $name .= '#' $direction;
  235.         }
  236.         $nameWithoutCount $name;
  237.         if ($count 1) {
  238.             $name .= $count;
  239.         }
  240.         if (!in_array($name$this->usedParamNames)) {
  241.             $this->usedParamNames[] = $name;
  242.             return $name;
  243.         }
  244.         return $this->getUniqueSortParamName($searchService$nameWithoutCount$direction$count 1);
  245.     }
  246. }