vendor/pimcore/portal-engine/src/EventSubscriber/IndexUpdateSubscriber.php line 160

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\AssetMetadataClassDefinitionsBundle\Event\AssetMetadataConfigurationEvents;
  13. use Pimcore\AssetMetadataClassDefinitionsBundle\Event\Model\Asset\ConfigurationEvent;
  14. use Pimcore\Bundle\PortalEngineBundle\Enum\Index\DatabaseConfig;
  15. use Pimcore\Bundle\PortalEngineBundle\Enum\Index\Statistics\ElasticSearchAlias;
  16. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex\Asset\IndexService as AssetIndexService;
  17. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex\DataObject\IndexService as DataObjectIndexService;
  18. use Pimcore\Bundle\PortalEngineBundle\Service\SearchIndex\IndexQueueService;
  19. use Pimcore\Event\AssetEvents;
  20. use Pimcore\Event\DataObjectClassDefinitionEvents;
  21. use Pimcore\Event\DataObjectEvents;
  22. use Pimcore\Event\Model\AssetEvent;
  23. use Pimcore\Event\Model\DataObject\ClassDefinitionEvent;
  24. use Pimcore\Event\Model\DataObjectEvent;
  25. use Pimcore\Event\Model\TagEvent;
  26. use Pimcore\Event\TagEvents;
  27. use Pimcore\Logger;
  28. use Pimcore\Model\Asset;
  29. use Pimcore\Model\DataObject\AbstractObject;
  30. use Pimcore\Model\Element\Service;
  31. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  32. /**
  33.  * Class IndexUpdateSubscriber
  34.  *
  35.  * @package Pimcore\Bundle\PortalEngineBundle\EventSubsriber
  36.  */
  37. class IndexUpdateSubscriber implements EventSubscriberInterface
  38. {
  39.     /** @var IndexQueueService */
  40.     protected $indexQueueService;
  41.     /** @var DataObjectIndexService */
  42.     protected $dataObjectIndexService;
  43.     /** @var AssetIndexService */
  44.     protected $assetIndexService;
  45.     /**
  46.      * IndexUpdateSubscriber constructor.
  47.      *
  48.      * @param IndexQueueService $indexQueueService
  49.      * @param DataObjectIndexService $dataObjectIndexService
  50.      * @param AssetIndexService $assetIndexService
  51.      */
  52.     public function __construct(IndexQueueService $indexQueueServiceDataObjectIndexService $dataObjectIndexServiceAssetIndexService $assetIndexService)
  53.     {
  54.         $this->indexQueueService $indexQueueService;
  55.         $this->dataObjectIndexService $dataObjectIndexService;
  56.         $this->assetIndexService $assetIndexService;
  57.     }
  58.     /**
  59.      * @return array
  60.      */
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             DataObjectEvents::POST_UPDATE => 'updateDataObject',
  65.             DataObjectEvents::POST_ADD => 'updateDataObject',
  66.             DataObjectEvents::PRE_DELETE => 'deleteDataObject',
  67.             DataObjectClassDefinitionEvents::POST_UPDATE => 'updateDataObjectMapping',
  68.             DataObjectClassDefinitionEvents::POST_ADD => 'addDataObjectMapping',
  69.             DataObjectClassDefinitionEvents::POST_DELETE => 'deleteDataObjectIndex',
  70.             AssetEvents::POST_UPDATE => 'updateAsset',
  71.             AssetEvents::POST_ADD => 'updateAsset',
  72.             AssetEvents::POST_DELETE => 'deleteAsset',
  73.             AssetMetadataConfigurationEvents::POST_UPDATE => 'updateAssetMapping',
  74.             AssetMetadataConfigurationEvents::POST_DELETE => 'updateAssetMapping',
  75.             TagEvents::PRE_DELETE => 'deleteTag',
  76.             TagEvents::POST_ADD_TO_ELEMENT => 'updateTagAssignment',
  77.             TagEvents::POST_REMOVE_FROM_ELEMENT => 'updateTagAssignment',
  78.         ];
  79.     }
  80.     /**
  81.      * @param DataObjectEvent $event
  82.      */
  83.     public function updateDataObject(DataObjectEvent $event)
  84.     {
  85.         //do not update index when auto save or only saving version
  86.         if (
  87.             ($event->hasArgument('isAutoSave') && $event->getArgument('isAutoSave')) ||
  88.             ($event->hasArgument('saveVersionOnly') && $event->getArgument('saveVersionOnly'))
  89.         ) {
  90.             return;
  91.         }
  92.         $inheritanceBackup AbstractObject::getGetInheritedValues();
  93.         AbstractObject::setGetInheritedValues(true);
  94.         $dataObject $event->getObject();
  95.         if ($dataObject instanceof AbstractObject) {
  96.             $this->indexQueueService->updateIndexQueue($dataObjectDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  97.         }
  98.         AbstractObject::setGetInheritedValues($inheritanceBackup);
  99.     }
  100.     /**
  101.      * @param DataObjectEvent $event
  102.      */
  103.     public function deleteDataObject(DataObjectEvent $event)
  104.     {
  105.         $dataObject $event->getObject();
  106.         if ($dataObject instanceof AbstractObject) {
  107.             $this->indexQueueService->updateIndexQueue($dataObjectDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_DELETEtrue);
  108.         }
  109.     }
  110.     /**
  111.      * @param ClassDefinitionEvent $event
  112.      */
  113.     public function addDataObjectMapping(ClassDefinitionEvent $event)
  114.     {
  115.         $classDefinition $event->getClassDefinition();
  116.         $this->dataObjectIndexService->updateMapping($classDefinitiontrue);
  117.         $this->dataObjectIndexService->addClassDefinitionToAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  118.     }
  119.     /**
  120.      * @param ClassDefinitionEvent $event
  121.      */
  122.     public function updateDataObjectMapping(ClassDefinitionEvent $event)
  123.     {
  124.         $classDefinition $event->getClassDefinition();
  125.         $this->dataObjectIndexService->updateMapping($classDefinition);
  126.         $this->indexQueueService->updateDataObjects($classDefinition);
  127.         $this->dataObjectIndexService->addClassDefinitionToAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  128.     }
  129.     /**
  130.      * @param ClassDefinitionEvent $event
  131.      */
  132.     public function deleteDataObjectIndex(ClassDefinitionEvent $event)
  133.     {
  134.         $classDefinition $event->getClassDefinition();
  135.         try {
  136.             $this->dataObjectIndexService->deleteIndex($classDefinition);
  137.         } catch (\Exception $e) {
  138.             Logger::err($e);
  139.         }
  140.         $this->dataObjectIndexService->removeClassDefinitionFromAlias($classDefinitionElasticSearchAlias::CLASS_DEFINITIONS);
  141.     }
  142.     /**
  143.      * @param AssetEvent $event
  144.      */
  145.     public function updateAsset(AssetEvent $event)
  146.     {
  147.         $asset $event->getAsset();
  148.         if ($asset instanceof Asset) {
  149.             $this->indexQueueService->updateIndexQueue($assetDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  150.         }
  151.     }
  152.     /**
  153.      * @param AssetEvent $event
  154.      */
  155.     public function deleteAsset(AssetEvent $event)
  156.     {
  157.         $asset $event->getAsset();
  158.         if ($asset instanceof Asset) {
  159.             $this->indexQueueService->updateIndexQueue($assetDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_DELETEtrue);
  160.         }
  161.     }
  162.     /**
  163.      * @param ConfigurationEvent $event
  164.      */
  165.     public function updateAssetMapping(ConfigurationEvent $event)
  166.     {
  167.         $this->assetIndexService->updateMapping();
  168.         $this->indexQueueService->updateAssets();
  169.     }
  170.     public function deleteTag(TagEvent $event)
  171.     {
  172.         $this->indexQueueService->updateByTag($event->getTag());
  173.     }
  174.     public function updateTagAssignment(TagEvent $event)
  175.     {
  176.         $element Service::getElementById($event->getArgument('elementType'), $event->getArgument('elementId'));
  177.         //only update when element is object or asset
  178.         if ($element instanceof AbstractObject || $element instanceof Asset) {
  179.             $this->indexQueueService->updateIndexQueue($elementDatabaseConfig::QUEUE_TABLE_COLUMN_OPERATION_UPDATEtrue);
  180.         }
  181.     }
  182. }