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

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