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

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 array
  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.             $block $document->getEditable(Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_FILTERS);
  105.             if ($block instanceof Editable\Block) {
  106.                 $indices $block->getData();
  107.                 foreach ($block->getElements() as $i => $blockItem) {
  108.                     $index $indices[$i];
  109.                     $filterType $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\FilterDefinition::FILTER_TYPE);
  110.                     $filterAttribute $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\FilterDefinition::FILTER_ATTRIBUTE);
  111.                     if (empty($filterType) || empty($filterAttribute)) {
  112.                         continue;
  113.                     }
  114.                     $uniqueName $this->getUniqueFilterParamName($dataPool->getSearchService(), $filterAttribute);
  115.                     $parentBlockNames = [Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_FILTERS];
  116.                     $id Editable::buildChildEditableName(
  117.                         Editables\DataPool\DataPoolConfig\FilterDefinition::FILTER_PARAM_NAME'input'$parentBlockNames$index
  118.                     );
  119.                     $tag = new Input();
  120.                     $tag->setDataFromEditmode($uniqueName);
  121.                     $tag->setParentBlockNames($parentBlockNames);
  122.                     $tag->setName($id);
  123.                     $document->setEditable($tag);
  124.                 }
  125.             }
  126.             $this->usedParamNames = [];
  127.             /**
  128.              * @var Page $document ;
  129.              */
  130.             $block $document->getEditable(Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_SORT_OPTIONS);
  131.             if ($block instanceof Editable\Block) {
  132.                 /**
  133.                  * @var Item $blockItem
  134.                  */
  135.                 $indices $block->getData();
  136.                 foreach ($block->getElements() as $i => $blockItem) {
  137.                     $index $indices[$i];
  138.                     $direction $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\SortOption::DIRECTION);
  139.                     $field $this->getBlockItemElementData($blockItemEditables\DataPool\DataPoolConfig\SortOption::FIELD);
  140.                     if (empty($direction) || empty($field)) {
  141.                         continue;
  142.                     }
  143.                     $uniqueName $this->getUniqueSortParamName($dataPool->getSearchService(), $field$direction);
  144.                     $parentBlockNames = [Editables\DataPool\DataPoolConfig::GRID_CONFIGURATION_SORT_OPTIONS];
  145.                     $id Editable::buildChildEditableName(Editables\DataPool\DataPoolConfig\SortOption::PARAM_NAME'input'$parentBlockNames$index);
  146.                     $tag = new Input();
  147.                     $tag->setDataFromEditmode($uniqueName);
  148.                     $tag->setParentBlockNames($parentBlockNames);
  149.                     $tag->setName($id);
  150.                     $document->setEditable($tag);
  151.                 }
  152.             }
  153.         } elseif ($document instanceof Page && $this->portalConfigService->isPortalEnginePortal($document)) {
  154.             $this->updatePortalsJson true;
  155.         }
  156.     }
  157.     /**
  158.      * @param DocumentEvent $event
  159.      *
  160.      * @throws \Exception
  161.      */
  162.     public function onDocumentAdd(DocumentEvent $event)
  163.     {
  164.         $document $event->getDocument();
  165.         if ($document instanceof Page && $this->portalConfigService->isPortalEnginePortal($document)) {
  166.             $this->defaultValuesService->setPortalPreCreateDefaultConfig($document);
  167.         }
  168.     }
  169.     /**
  170.      * @param DocumentEvent $event
  171.      *
  172.      * @throws \Exception
  173.      */
  174.     public function triggerUpdatePortalsJson(DocumentEvent $event)
  175.     {
  176.         $document $event->getDocument();
  177.         if ($document instanceof Page && $this->portalConfigService->isPortalEnginePortal($document)) {
  178.             $this->updatePortalsJson true;
  179.         }
  180.     }
  181.     public function onTerminate(/*Event*/ $terminateEvent)
  182.     {
  183.         if ($this->updatePortalsJson) {
  184.             $this->frontendBuildService->updatePortalsJson(true$this->forceUpdatePortalsJson);
  185.         }
  186.     }
  187.     /**
  188.      * @param bool $updatePortalsJson
  189.      */
  190.     public function setUpdatePortalsJson(bool $updatePortalsJsonbool $forceUpdatePortalsJson false)
  191.     {
  192.         $this->updatePortalsJson $updatePortalsJson;
  193.         $this->forceUpdatePortalsJson $forceUpdatePortalsJson;
  194.     }
  195.     /**
  196.      * @param string $name
  197.      * @param int $count
  198.      *
  199.      * @return mixed
  200.      *
  201.      * @throws \Exception
  202.      */
  203.     protected function getUniqueFilterParamName(SearchServiceInterface $searchService$name$count 1)
  204.     {
  205.         if ($count === 1) {
  206.             $filterableFields $searchService->getFilterableFieldsMapping();
  207.             $name = isset($filterableFields[$name]) ? $filterableFields[$name]->getName() : $name;
  208.         }
  209.         $nameWithoutCount $name;
  210.         if ($count 1) {
  211.             $name .= $count;
  212.         }
  213.         if (!in_array($name$this->usedParamNames)) {
  214.             $this->usedParamNames[] = $name;
  215.             return $name;
  216.         }
  217.         return $this->getUniqueFilterParamName($searchService$nameWithoutCount$count 1);
  218.     }
  219.     /**
  220.      * @param string $name
  221.      * @param string $direction
  222.      * @param int $count
  223.      *
  224.      * @return mixed
  225.      *
  226.      * @throws \Exception
  227.      */
  228.     protected function getUniqueSortParamName(SearchServiceInterface $searchService$name$direction$count 1)
  229.     {
  230.         if ($count === 1) {
  231.             $sortableFields $searchService->getSortableFieldsMapping();
  232.             $name = isset($sortableFields[$name]) ? $sortableFields[$name]->getName() : $name;
  233.             $name .= '#' $direction;
  234.         }
  235.         $nameWithoutCount $name;
  236.         if ($count 1) {
  237.             $name .= $count;
  238.         }
  239.         if (!in_array($name$this->usedParamNames)) {
  240.             $this->usedParamNames[] = $name;
  241.             return $name;
  242.         }
  243.         return $this->getUniqueSortParamName($searchService$nameWithoutCount$direction$count 1);
  244.     }
  245. }